Class: TopologicalInventory::Providers::Common::SourcesApiClient

Inherits:
SourcesApiClient::ApiClient
  • Object
show all
Defined in:
lib/topological_inventory/providers/common/sources_api_client.rb

Constant Summary collapse

INTERNAL_API_PATH =
'//internal/v1.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(identity = nil) ⇒ SourcesApiClient

Returns a new instance of SourcesApiClient.



11
12
13
14
15
# File 'lib/topological_inventory/providers/common/sources_api_client.rb', line 11

def initialize(identity = nil)
  super(::SourcesApiClient::Configuration.default)
  self.identity = identity
  self.api      = init_default_api
end

Instance Method Details

#fetch_application(source_id) ⇒ Object



44
45
46
47
# File 'lib/topological_inventory/providers/common/sources_api_client.rb', line 44

def fetch_application(source_id)
  applications = api.list_source_applications(source_id)&.data || []
  applications.first
end

#fetch_authentication(source_id, default_endpoint = nil, authtype = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/topological_inventory/providers/common/sources_api_client.rb', line 49

def fetch_authentication(source_id, default_endpoint = nil, authtype = nil)
  endpoint = default_endpoint || fetch_default_endpoint(source_id)
  return if endpoint.nil?

  endpoint_authentications = api.list_endpoint_authentications(endpoint.id.to_s).data || []
  return if endpoint_authentications.empty?

  auth_id = if authtype.nil?
              endpoint_authentications.first&.id
            else
              endpoint_authentications.detect { |a| a.authtype = authtype }&.id
            end
  return if auth_id.nil?

  fetch_authentication_with_password(auth_id)
end

#fetch_default_endpoint(source_id) ⇒ Object



39
40
41
42
# File 'lib/topological_inventory/providers/common/sources_api_client.rb', line 39

def fetch_default_endpoint(source_id)
  endpoints = api.list_source_endpoints(source_id)&.data || []
  endpoints.find(&:default)
end

#init_default_apiObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/topological_inventory/providers/common/sources_api_client.rb', line 17

def init_default_api
  # TODO: remove this once PSK is set up everywhere.
  if identity.present?
    if psk
      parsed_identity = JSON.parse(Base64.decode64(identity.fetch('x-rh-identity')))

      default_headers.merge!(
        "x-rh-sources-psk"            => psk,
        "x-rh-sources-account-number" => parsed_identity['identity']['account_number']
      )
    else
      default_headers.merge!(identity)
    end
  end

  ::SourcesApiClient::DefaultApi.new(self)
end

#pskObject



35
36
37
# File 'lib/topological_inventory/providers/common/sources_api_client.rb', line 35

def psk
  @psk ||= ENV.fetch("SOURCES_PSK", nil)
end