class Bcome::Driver::Gcp

Constants

APPLICATION_NAME

Public Class Methods

new(*params) click to toggle source
Calls superclass method Bcome::Driver::Base::new
# File lib/objects/driver/gcp.rb, line 9
def initialize(*params)
  super
  validate_service_scopes
  validate_authentication_scheme
end

Public Instance Methods

do_fetch_server_list(_filters) click to toggle source
# File lib/objects/driver/gcp.rb, line 41
def do_fetch_server_list(_filters)
  # Network filter key now called :filter. retained :list_filter for backwards compatibility.
  # Fallback is ""
  filters = (
    @params[:filters] || (
      @params[:list_filter] || ''
    )
  )

  gcp_service.list_instances(@params[:project], @params[:zone], filter: filters)
rescue Google::Apis::AuthorizationError => e
  raise ::Bcome::Exception::CannotAuthenticateToGcp
rescue Google::Apis::ClientError => e
  raise ::Bcome::Exception::Generic, "Namespace #{@node.namespace} / #{e.message}"
rescue Google::Apis::TransmissionError => e
  raise ::Bcome::Exception::Generic, 'Cannot reach GCP - do you have an internet connection?'
end
fetch_server_list(_filters) click to toggle source
# File lib/objects/driver/gcp.rb, line 23
def fetch_server_list(_filters)
  unless authentication_scheme.authorized?
    get_authenticated_gcp_service
    raise ::Bcome::Exception::Generic, 'GCP authentication process failed' unless authentication_scheme.authorized?
  end

  wrap_indicator type: :basic, title: loader_title, completed_title: loader_completed_title do
    begin
      @instances = do_fetch_server_list(_filters)
      signal_success
    rescue Exception => e
      signal_failure
      raise e
    end
  end
  @instances.items
end
has_network_credentials?() click to toggle source
# File lib/objects/driver/gcp.rb, line 59
def has_network_credentials?
  true
end
network_credentials() click to toggle source
# File lib/objects/driver/gcp.rb, line 63
def network_credentials
  {
    access_token: access_token,
    project_name: @params[:project]
  }
end
pretty_provider_name() click to toggle source
# File lib/objects/driver/gcp.rb, line 15
def pretty_provider_name
  'GCP'
end
pretty_resource_location() click to toggle source
# File lib/objects/driver/gcp.rb, line 19
def pretty_resource_location
  "#{@params[:project]}/#{@params[:zone]}"
end

Protected Instance Methods

access_token() click to toggle source
# File lib/objects/driver/gcp.rb, line 148
def access_token
  gcp_service.authorization.access_token
end
auth_scheme() click to toggle source
# File lib/objects/driver/gcp.rb, line 81
def auth_scheme
  auth_schemes[@params[:authentication_scheme].to_sym]
end
auth_schemes() click to toggle source
# File lib/objects/driver/gcp.rb, line 85
def auth_schemes
  {
    oauth: ::Bcome::Driver::Gcp::Authentication::Oauth,
    service_account: ::Bcome::Driver::Gcp::Authentication::ServiceAccount
    # api_key: ::Bcome::Driver::Gcp::Authentication::ApiKey
  }
end
authentication_scheme() click to toggle source
# File lib/objects/driver/gcp.rb, line 102
def authentication_scheme
  # Service scopes are specified directly from the network config
  # A minumum scope of https://www.googleapis.com/auth/compute.readonly is required in order to list resources.

  auth_scheme_key = @params[:authentication_scheme].to_sym
  auth_scheme = auth_schemes[auth_scheme_key]
  raise ::Bcome::Exception::InvalidGcpAuthenticationScheme, "Invalid GCP authentication scheme '#{auth_scheme_key}' for node #{@node.namespace}" unless auth_scheme

  case auth_scheme_key
  when :oauth

    client_config = ::Bcome::Driver::Gcp::Authentication::OauthClientConfig.new(service_scopes, oauth_filename)

    # Prevent second oauth flow during same session with same credentials, different inventory.
    # If we already have an outh authentication scheme for the same scopes & oauth credentials, then we'll return that one

    # If the scheme is set, return it
    return @authentication_scheme if @authentication_scheme

    # Look to see if we have an existing oauth scheme setup for the same scopes & credentials file
    if @authentication_scheme = ::Bcome::Driver::Gcp::Authentication::OauthSessionStore.instance.in_memory_session_for(client_config)
      @compute_service = @authentication_scheme.service

      return @authentication_scheme
    end

    # Otherwise, we'll create a new outh scheme and register it with the session store
    @authentication_scheme = auth_scheme.new(self, compute_service, client_config, @node)
    ::Bcome::Driver::Gcp::Authentication::OauthSessionStore.instance << @authentication_scheme
    @authentication_scheme

  when :service_account
    @authentication_scheme ||= auth_scheme.new(compute_service, service_scopes, @node, @params[:service_account_credentials], self)
  else
    raise ::Bcome::Exception::InvalidGcpAuthenticationScheme, "Invalid GCP authentication scheme '#{auth_scheme_key}' for node #{@node.namespace}"
  end
end
authorization() click to toggle source
# File lib/objects/driver/gcp.rb, line 152
def authorization
  gcp_service.authorization
end
compute_service() click to toggle source
# File lib/objects/driver/gcp.rb, line 93
def compute_service
  @compute_service ||= ::Google::Apis::ComputeBeta::ComputeService.new
end
gcp_service() click to toggle source
# File lib/objects/driver/gcp.rb, line 144
def gcp_service
  @gcp_service ||= get_authenticated_gcp_service
end
get_authenticated_gcp_service() click to toggle source
# File lib/objects/driver/gcp.rb, line 97
def get_authenticated_gcp_service
  authentication_scheme.do!
  compute_service
end
has_service_scopes_defined?() click to toggle source
# File lib/objects/driver/gcp.rb, line 164
def has_service_scopes_defined?
  service_scopes&.any?
end
invalid_auth_scheme?() click to toggle source
# File lib/objects/driver/gcp.rb, line 77
def invalid_auth_scheme?
  !auth_schemes.keys.include?(@params[:authentication_scheme].to_sym)
end
oauth_filename() click to toggle source
# File lib/objects/driver/gcp.rb, line 140
def oauth_filename
  @params[:secrets_path] || @params[:secrets_filename]
end
service_scopes() click to toggle source
# File lib/objects/driver/gcp.rb, line 156
def service_scopes
  @params[:service_scopes]
end
validate_authentication_scheme() click to toggle source
# File lib/objects/driver/gcp.rb, line 72
def validate_authentication_scheme
  raise ::Bcome::Exception::MissingGcpAuthenticationScheme, "node #{@node.namespace}" if @params[:authentication_scheme].nil? || @params[:authentication_scheme].empty?
  raise ::Bcome::Exception::InvalidGcpAuthenticationScheme, "Invalid GCP authentication scheme '#{@params[:authentication_scheme]}' for node #{@node.namespace}" unless auth_scheme
end
validate_service_scopes() click to toggle source
# File lib/objects/driver/gcp.rb, line 160
def validate_service_scopes
  raise ::Bcome::Exception::MissingGcpServiceScopes, 'Please define as minimum https://www.googleapis.com/auth/compute.readonly' unless has_service_scopes_defined?
end