topological_inventory-providers-common 1.0.8 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -2
- data/lib/topological_inventory/providers/common.rb +1 -2
- data/lib/topological_inventory/providers/common/collector.rb +12 -4
- data/lib/topological_inventory/providers/common/collectors_pool.rb +2 -1
- data/lib/topological_inventory/providers/common/logging.rb +10 -4
- data/lib/topological_inventory/providers/common/mixins/sources_api.rb +58 -0
- data/lib/topological_inventory/providers/common/mixins/topology_api.rb +26 -0
- data/lib/topological_inventory/providers/common/mixins/x_rh_headers.rb +24 -0
- data/lib/topological_inventory/providers/common/operations/async_worker.rb +56 -0
- data/lib/topological_inventory/providers/common/operations/health_check.rb +15 -0
- data/lib/topological_inventory/providers/common/operations/source.rb +119 -144
- data/lib/topological_inventory/providers/common/sources_api_client.rb +92 -0
- data/lib/topological_inventory/providers/common/topology_api_client.rb +43 -0
- data/lib/topological_inventory/providers/common/version.rb +1 -1
- data/spec/support/shared/availability_check.rb +1 -1
- data/spec/topological_inventory/providers/common/operations/async_worker_spec.rb +36 -0
- data/topological_inventory-providers-common.gemspec +1 -1
- metadata +16 -7
- data/lib/topological_inventory/providers/common/operations/endpoint_client.rb +0 -65
- data/lib/topological_inventory/providers/common/operations/processor.rb +0 -135
- data/lib/topological_inventory/providers/common/operations/sources_api_client.rb +0 -94
- data/lib/topological_inventory/providers/common/operations/topology_api_client.rb +0 -28
- data/spec/topological_inventory/providers/common/operations/processor_spec.rb +0 -102
@@ -1,94 +0,0 @@
|
|
1
|
-
require "sources-api-client"
|
2
|
-
|
3
|
-
module TopologicalInventory
|
4
|
-
module Providers
|
5
|
-
module Common
|
6
|
-
module Operations
|
7
|
-
class SourcesApiClient < ::SourcesApiClient::ApiClient
|
8
|
-
delegate :update_source, :update_endpoint, :update_application, :to => :api
|
9
|
-
|
10
|
-
INTERNAL_API_PATH = '//internal/v1.0'.freeze
|
11
|
-
|
12
|
-
def initialize(identity = nil)
|
13
|
-
super(::SourcesApiClient::Configuration.default)
|
14
|
-
self.identity = identity
|
15
|
-
self.api = init_default_api
|
16
|
-
end
|
17
|
-
|
18
|
-
def init_default_api
|
19
|
-
default_headers.merge!(identity) if identity.present?
|
20
|
-
::SourcesApiClient::DefaultApi.new(self)
|
21
|
-
end
|
22
|
-
|
23
|
-
def fetch_default_endpoint(source_id)
|
24
|
-
endpoints = api.list_source_endpoints(source_id)&.data || []
|
25
|
-
endpoints.find(&:default)
|
26
|
-
end
|
27
|
-
|
28
|
-
def fetch_application(source_id)
|
29
|
-
applications = api.list_source_applications(source_id)&.data || []
|
30
|
-
applications.first
|
31
|
-
end
|
32
|
-
|
33
|
-
def fetch_authentication(source_id, default_endpoint = nil, authtype = nil)
|
34
|
-
endpoint = default_endpoint || fetch_default_endpoint(source_id)
|
35
|
-
return if endpoint.nil?
|
36
|
-
|
37
|
-
endpoint_authentications = api.list_endpoint_authentications(endpoint.id.to_s).data || []
|
38
|
-
return if endpoint_authentications.empty?
|
39
|
-
|
40
|
-
auth_id = if authtype.nil?
|
41
|
-
endpoint_authentications.first&.id
|
42
|
-
else
|
43
|
-
endpoint_authentications.detect { |a| a.authtype = authtype }&.id
|
44
|
-
end
|
45
|
-
return if auth_id.nil?
|
46
|
-
|
47
|
-
fetch_authentication_with_password(auth_id)
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
attr_accessor :identity, :api, :custom_base_path
|
53
|
-
|
54
|
-
def fetch_authentication_with_password(auth_id)
|
55
|
-
on_internal_api do
|
56
|
-
local_var_path = "/authentications/#{auth_id}"
|
57
|
-
|
58
|
-
query_params = "expose_encrypted_attribute[]=password"
|
59
|
-
|
60
|
-
header_params = { 'Accept' => select_header_accept(['application/json']) }
|
61
|
-
return_type = 'Authentication'
|
62
|
-
data, _, _ = call_api(:GET, local_var_path,
|
63
|
-
:header_params => header_params,
|
64
|
-
:query_params => query_params,
|
65
|
-
:auth_names => ['UserSecurity'],
|
66
|
-
:return_type => return_type)
|
67
|
-
data
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def build_request_url(path)
|
72
|
-
# Add leading and trailing slashes to path
|
73
|
-
path = "/#{path}".gsub(/\/+/, '/')
|
74
|
-
URI.encode((custom_base_url || @config.base_url) + path)
|
75
|
-
end
|
76
|
-
|
77
|
-
def custom_base_url
|
78
|
-
return nil if custom_base_path.nil?
|
79
|
-
|
80
|
-
url = "#{@config.scheme}://#{[@config.host, custom_base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
|
81
|
-
URI.encode(url)
|
82
|
-
end
|
83
|
-
|
84
|
-
def on_internal_api
|
85
|
-
self.custom_base_path = INTERNAL_API_PATH
|
86
|
-
yield
|
87
|
-
ensure
|
88
|
-
self.custom_base_path = nil
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module TopologicalInventory
|
2
|
-
module Providers
|
3
|
-
module Common
|
4
|
-
module Operations
|
5
|
-
module TopologyApiClient
|
6
|
-
def topology_api_client
|
7
|
-
@topology_api_client ||=
|
8
|
-
begin
|
9
|
-
api_client = TopologicalInventoryApiClient::ApiClient.new
|
10
|
-
api_client.default_headers.merge!(identity) if identity.present?
|
11
|
-
TopologicalInventoryApiClient::DefaultApi.new(api_client)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def update_task(task_id, state:, status:, context:)
|
16
|
-
task = TopologicalInventoryApiClient::Task.new("state" => state, "status" => status, "context" => context)
|
17
|
-
topology_api_client.update_task(task_id, task)
|
18
|
-
end
|
19
|
-
|
20
|
-
def svc_instance_url(service_instance)
|
21
|
-
rest_api_path = '/service_instances/{id}'.sub('{' + 'id' + '}', service_instance&.id.to_s)
|
22
|
-
topology_api_client.api_client.build_request(:GET, rest_api_path).url
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,102 +0,0 @@
|
|
1
|
-
require "topological_inventory/providers/common/operations/processor"
|
2
|
-
|
3
|
-
RSpec.describe TopologicalInventory::Providers::Common::Operations::Processor do
|
4
|
-
let(:topology_api_client) { double }
|
5
|
-
let(:source_id) { 1 }
|
6
|
-
let(:source_ref) { 1000 }
|
7
|
-
let(:service_plan) { double("TopologicalInventoryApiClient::ServicePlan") }
|
8
|
-
let(:service_offering) { double("TopologicalInventoryApiClient::ServiceOffering") }
|
9
|
-
|
10
|
-
# Overriden in contexts
|
11
|
-
let(:payload) { {} }
|
12
|
-
|
13
|
-
before do
|
14
|
-
@processor = described_class.new(nil, nil, payload)
|
15
|
-
allow(@processor).to receive(:logger).and_return(double('null_object').as_null_object)
|
16
|
-
|
17
|
-
allow(service_plan).to receive(:service_offering_id).and_return(1)
|
18
|
-
allow(service_plan).to receive(:name).and_return(double)
|
19
|
-
|
20
|
-
allow(service_offering).to receive(:name).and_return(double)
|
21
|
-
allow(service_offering).to receive(:source_ref).and_return(source_ref)
|
22
|
-
allow(service_offering).to receive(:extra).and_return({:type => 'job_template'})
|
23
|
-
allow(service_offering).to receive(:source_id).and_return(source_id)
|
24
|
-
|
25
|
-
@endpoint_client = double
|
26
|
-
allow(@endpoint_client).to receive(:order_service)
|
27
|
-
|
28
|
-
allow(@processor).to receive(:endpoint_client).and_return(@endpoint_client)
|
29
|
-
allow(@processor).to receive(:topology_api_client).and_return(topology_api_client)
|
30
|
-
allow(topology_api_client).to receive(:update_task)
|
31
|
-
allow(topology_api_client).to receive(:show_service_plan).and_return(service_plan)
|
32
|
-
allow(topology_api_client).to receive(:show_service_offering).and_return(service_offering)
|
33
|
-
end
|
34
|
-
|
35
|
-
context "Order by ServicePlan" do
|
36
|
-
let(:payload) do
|
37
|
-
{
|
38
|
-
'request_context' => {"x-rh-identity" => 'abcd'},
|
39
|
-
'params' => {
|
40
|
-
'order_params' => {
|
41
|
-
'service_plan_id' => 1,
|
42
|
-
'service_parameters' => { :name => "Job 1",
|
43
|
-
:param1 => "Test Topology",
|
44
|
-
:param2 => 50 },
|
45
|
-
'provider_control_parameters' => {}
|
46
|
-
},
|
47
|
-
'service_plan_id' => 1,
|
48
|
-
'task_id' => 1 # in tp-inv api (Task)
|
49
|
-
}
|
50
|
-
}
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "#order_service" do
|
54
|
-
it "orders job" do
|
55
|
-
allow(@processor).to receive(:poll_order_complete_thread).and_return(double)
|
56
|
-
|
57
|
-
expect(@endpoint_client).to receive(:order_service).with(service_offering, service_plan, payload['params']['order_params'])
|
58
|
-
@processor.send(:order_service, payload['params'])
|
59
|
-
end
|
60
|
-
|
61
|
-
it "updates task on error" do
|
62
|
-
err_message = "Sample error"
|
63
|
-
|
64
|
-
allow(@processor).to receive(:poll_order_complete_thread).and_return(double)
|
65
|
-
allow(@processor).to receive(:update_task).and_return(double)
|
66
|
-
allow(@endpoint_client).to receive(:order_service).and_raise(err_message)
|
67
|
-
|
68
|
-
expect(@processor).to receive(:update_task).with(payload['params']['task_id'], :state => "completed", :status => "error", :context => { :error => err_message })
|
69
|
-
|
70
|
-
@processor.send(:order_service, payload['params'])
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context "Order by ServiceOffering" do
|
76
|
-
let(:payload) do
|
77
|
-
{
|
78
|
-
'request_context' => {"x-rh-identity" => 'abcd'},
|
79
|
-
'params' => {
|
80
|
-
'order_params' => {
|
81
|
-
'service_offering_id' => 1,
|
82
|
-
'service_parameters' => { :name => "Job 1",
|
83
|
-
:param1 => "Test Topology",
|
84
|
-
:param2 => 50 },
|
85
|
-
'provider_control_parameters' => {}
|
86
|
-
},
|
87
|
-
'service_offering_id' => 1,
|
88
|
-
'task_id' => 1 # in tp-inv api (Task)
|
89
|
-
}
|
90
|
-
}
|
91
|
-
end
|
92
|
-
|
93
|
-
describe "#order_service" do
|
94
|
-
it "orders job" do
|
95
|
-
allow(@processor).to receive(:poll_order_complete_thread).and_return(double)
|
96
|
-
|
97
|
-
expect(@endpoint_client).to receive(:order_service).with(service_offering, nil, payload['params']['order_params'])
|
98
|
-
@processor.send(:order_service, payload['params'])
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|