Module: Util::OCIClients

Extended by:
Logging
Included in:
Discover::Infrastructure
Defined in:
lib/util/oci_clients.rb

Constant Summary

Constants included from Logging

Logging::SEV_LABEL, Logging::TRACE

Class Method Summary collapse

Methods included from Logging

logger, logger=

Class Method Details

.create_clients(auth_object, options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/util/oci_clients.rb', line 19

def create_clients(auth_object, options)
  begin
    @la_client = initialize_la_client(auth_object, nil)

    if options[:mode] == 'object'
      set_clients
      return
    end

    @ce_client = initialize_ce_client(auth_object)
    @id_client = initialize_id_client(auth_object)
    @lb_client = initialize_lb_client(auth_object)
    @rs_client = initialize_rs_client(auth_object)
    @vnc_client = initialize_vnc_client(auth_object)
  rescue StandardError => e
    logger.error("Error while creating OCI clients. Error: #{e}")
    raise e
  end
  set_clients
  nil
end

.get_auth_config_objectObject



215
216
217
# File 'lib/util/oci_clients.rb', line 215

def get_auth_config_object
  @auth_object
end

.get_clientsObject



200
201
202
# File 'lib/util/oci_clients.rb', line 200

def get_clients
  @oci_clients
end

.initialize_auth_config(auth_config_hash) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/util/oci_clients.rb', line 177

def initialize_auth_config(auth_config_hash)
  @endpoint = auth_config_hash[:endpoint]

  begin
    if !auth_config_hash[:config_file_location].nil? && !auth_config_hash[:profile_name].nil?
      @oci_config = OCI::ConfigFileLoader.load_config(config_file_location: auth_config_hash[:config_file_location],
                                                      profile_name: auth_config_hash[:profile_name])
    end

    if !@oci_config.nil?
      @auth_type = Enum::AuthTypeEnum::CONFIG
    else
      @oci_config = OCI::Config.new
      @instance_principals_signer = instance_principals_signer = OCI::Auth::Signers::InstancePrincipalsSecurityTokenSigner.new
      @auth_type = Enum::AuthTypeEnum::INSTANCE_PRINCIPAL
    end
  rescue StandardError => e
    logger.error("Error occurred while initializing OCI authentication configuration. Error: #{e}")
    raise e
  end
  set_auth_config_object
end

.initialize_ce_client(auth_object) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/util/oci_clients.rb', line 41

def initialize_ce_client(auth_object)
  client = nil
  logger.debug("Creating container engine client with auth_type: #{auth_object[:auth_type]}")
  begin
    case auth_object[:auth_type]
    when Enum::AuthTypeEnum::CONFIG
      client = OCI::ContainerEngine::ContainerEngineClient.new(config: auth_object[:oci_config])
    when Enum::AuthTypeEnum::INSTANCE_PRINCIPAL
      client = OCI::ContainerEngine::ContainerEngineClient.new(config: auth_object[:oci_config], signer: auth_object[:instance_principals_signer])
    else
      logger.warn("Unknown auth_type '#{auth_object[:auth_type]}' provided for container engine client.")
    end
    @ce_client = client
  rescue StandardError => e
    logger.error("Error while creating container engine client: #{e}")
    raise e
  end
  client
end

.initialize_id_client(auth_object) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/util/oci_clients.rb', line 61

def initialize_id_client(auth_object)
  client = nil
  logger.debug("Creating identity client with auth_type: #{auth_object[:auth_type]}")
  begin
    case auth_object[:auth_type]
    when Enum::AuthTypeEnum::CONFIG
      client = OCI::Identity::IdentityClient.new(config: auth_object[:oci_config])
    when Enum::AuthTypeEnum::ENDPOINT
      client = OCI::Identity::IdentityClient.new(config: auth_object[:oci_config], endpoint: auth_object[:endpoint])
    when Enum::AuthTypeEnum::PRINCIPAL
      client = OCI::Identity::IdentityClient.new(config: auth_object[:oci_config], signer: auth_object[:instance_principals_signer])
    else
      logger.warn("Unknown auth_type '#{auth_object[:auth_type]}' provided for identity client.")
    end
    @id_client = client
  rescue StandardError => e
    logger.error("Error while creating identity client: #{e}")
    raise e
  end
  @id_client
end

.initialize_la_client(auth_object, custom_retry_config) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/util/oci_clients.rb', line 105

def initialize_la_client(auth_object, custom_retry_config)
  client = nil
  logger.debug("Creating log analytics client with auth_type: #{auth_object[:auth_type]}")
  Config::OCIClientRetryConfig.set_custom_retry_config(custom_retry_config) unless custom_retry_config.nil?

  begin
    case auth_object[:auth_type]
    when Enum::AuthTypeEnum::CONFIG
      client = OCI::LogAnalytics::LogAnalyticsClient.new(config: auth_object[:oci_config],
                                                         endpoint: auth_object[:endpoint],
                                                         retry_config: Config::OCIClientRetryConfig.get_retry_config)
    when Enum::AuthTypeEnum::INSTANCE_PRINCIPAL
      client = OCI::LogAnalytics::LogAnalyticsClient.new(config: auth_object[:oci_config],
                                                         endpoint: auth_object[:endpoint],
                                                         signer: auth_object[:instance_principals_signer],
                                                         retry_config: Config::OCIClientRetryConfig.get_retry_config)
    else
      logger.warn("Unknown auth_type while creating log analytics client: #{auth_object[:auth_type]}")
      raise StandardError, 'Unknown auth_type for log analytics client.'
    end
    @la_client = client
  rescue StandardError => e
    logger.error("Error while creating log analytics client: #{e}")
    raise e
  end
  @la_client
end

.initialize_lb_client(auth_object) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/util/oci_clients.rb', line 83

def initialize_lb_client(auth_object)
  client = nil
  logger.debug("Creating load balancer client with auth_type: #{auth_object[:auth_type]}")
  begin
    case auth_object[:auth_type]
    when Enum::AuthTypeEnum::CONFIG
      client = OCI::LoadBalancer::LoadBalancerClient.new(config: auth_object[:oci_config])
    when Enum::AuthTypeEnum::ENDPOINT
      client = OCI::LoadBalancer::LoadBalancerClient.new(config: auth_object[:oci_config], endpoint: auth_object[:endpoint])
    when Enum::AuthTypeEnum::PRINCIPAL
      client = OCI::LoadBalancer::LoadBalancerClient.new(config: auth_object[:oci_config], signer: auth_object[:instance_principals_signer])
    else
      logger.warn("Unknown auth_type '#{auth_object[:auth_type]}' provided for load balancer client.")
    end
    @lb_client = client
  rescue StandardError => e
    logger.error("Error while creating load balancer client: #{e}")
    raise e
  end
  @lb_client
end

.initialize_rs_client(auth_object) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/util/oci_clients.rb', line 133

def initialize_rs_client(auth_object)
  client = nil
  logger.debug("Creating resource search client with auth_type: #{auth_object[:auth_type]}")
  begin
    case auth_object[:auth_type]
    when Enum::AuthTypeEnum::CONFIG
      client = OCI::ResourceSearch::ResourceSearchClient.new(config: auth_object[:oci_config])
    when Enum::AuthTypeEnum::ENDPOINT
      client = OCI::ResourceSearch::ResourceSearchClient.new(config: auth_object[:oci_config], endpoint: auth_object[:endpoint])
    when Enum::AuthTypeEnum::PRINCIPAL
      client = OCI::ResourceSearch::ResourceSearchClient.new(config: auth_object[:oci_config], signer: auth_object[:instance_principals_signer])
    else
      logger.warn("Unknown auth_type '#{auth_object[:auth_type]}' provided for resource search client.")
    end
    @rs_client = client
  rescue StandardError => e
    logger.error("Error while creating resource search client: #{e}")
    raise e
  end
  @rs_client
end

.initialize_vnc_client(auth_object) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/util/oci_clients.rb', line 155

def initialize_vnc_client(auth_object)
  client = nil
  logger.debug("Creating virtual network client with auth_type: #{auth_object[:auth_type]}")
  begin
    case auth_object[:auth_type]
    when Enum::AuthTypeEnum::CONFIG
      client = OCI::Core::VirtualNetworkClient.new(config: auth_object[:oci_config])
    when Enum::AuthTypeEnum::ENDPOINT
      client = OCI::Core::VirtualNetworkClient.new(config: auth_object[:oci_config], endpoint: auth_object[:endpoint])
    when Enum::AuthTypeEnum::PRINCIPAL
      client = OCI::Core::VirtualNetworkClient.new(config: auth_object[:oci_config], signer: auth_object[:instance_principals_signer])
    else
      logger.warn("Unknown auth_type '#{auth_object[:auth_type]}' provided for virtual network client.")
    end
    @vnc_client = client
  rescue StandardError => e
    logger.error("Error while creating virtual network client: #{e}")
    raise e
  end
  @vnc_client
end

.set_auth_config_objectObject



219
220
221
222
223
224
225
226
# File 'lib/util/oci_clients.rb', line 219

def set_auth_config_object
  @auth_object = {
    oci_config: @oci_config,
    endpoint: @endpoint,
    instance_principals_signer: @instance_principals_signer,
    auth_type: @auth_type
  }.compact
end

.set_clientsObject



204
205
206
207
208
209
210
211
212
213
# File 'lib/util/oci_clients.rb', line 204

def set_clients
  @oci_clients = {
    ce_client: @ce_client,
    id_client: @id_client,
    lb_client: @lb_client,
    la_client: @la_client,
    rs_client: @rs_client,
    vnc_client: @vnc_client
  }
end