Class: VagrantPlugins::OCI::Action::ConnectOCI

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-oci/action/connect_oci.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ConnectOCI

Returns a new instance of ConnectOCI.



8
9
10
11
# File 'lib/vagrant-oci/action/connect_oci.rb', line 8

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_oci::action::connect_oci")
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vagrant-oci/action/connect_oci.rb', line 13

def call(env)
  provider_config = env[:machine].provider_config

  region = provider_config.region
  config_file = provider_config.config_file || ::OCI::ConfigFileLoader::DEFAULT_CONFIG_FILE
  profile_name = provider_config.profile || ::OCI::ConfigFileLoader::DEFAULT_PROFILE

  oci_config = ::OCI::ConfigFileLoader.load_config(config_file_location: config_file,
                                                   profile_name: profile_name)
  env[:oci_config] = oci_config

  @logger.info("Creating OCI API compute client and adding to Vagrant environment")
  env[:oci_compute] = ::OCI::Core::ComputeClient.new(config: oci_config, region: region)
  env[:oci_network] = ::OCI::Core::VirtualNetworkClient.new(config: oci_config, region: region)
  env[:oci_identity] = ::OCI::Identity::IdentityClient.new(config: oci_config, region: region)

  @@ad_list ||= {}
  @@ad_list[oci_config.tenancy] ||= {}
  @@ad_list[oci_config.tenancy][region] ||= env[:oci_identity].list_availability_domains(oci_config.tenancy).data.map(&:to_hash).sort_by {|i| i[:name]}
  env[:ad_name] = @@ad_list[oci_config.tenancy][region][provider_config.ad.to_i - 1][:name]

  @@compartment_list ||= env[:oci_identity].list_compartments(oci_config.tenancy, compartment_id_in_subtree: true).flat_map {|i| i}
  compartment_map = @@compartment_list.flat_map {|i| i.data}.inject({}) {|a, v| a[v.name] = v.id; a}
  if provider_config.server_compartment_id.nil? and provider_config.server_compartment_name
    env[:machine].provider_config.server_compartment_id = compartment_map[provider_config.server_compartment_name]
  end
  if provider_config.image_id.nil? and provider_config.image_compartment_name
    env[:machine].provider_config.image_compartment_id = compartment_map[provider_config.image_compartment_name]
  end
  if provider_config.subnet_vcn_compartment_id.nil? and provider_config.subnet_vcn_compartment_name
    env[:machine].provider_config.subnet_vcn_compartment_id = compartment_map[provider_config.subnet_vcn_compartment_name]
  end
  if provider_config.subnet_compartment_id.nil? and provider_config.subnet_compartment_name
    env[:machine].provider_config.subnet_compartment_id = compartment_map[provider_config.subnet_compartment_name]
  end

  if provider_config.image_regex
    @@image_list ||= {}
    @@image_list[oci_config.tenancy] ||= {}
    @@image_list[oci_config.tenancy][region] ||= env[:oci_compute].list_images(provider_config.image_compartment_id).data.map(&:to_hash).sort_by {|i| i[:name]}.reverse
  end

  @app.call(env)
end