Module: Chef::Knife::OciHelperShow

Included in:
OciServerCreate, OciServerShow
Defined in:
lib/chef/knife/oci_helper_show.rb

Overview

Utility routines to fill out data for ‘server show’ functionality

Instance Method Summary collapse

Instance Method Details

#add_server_details(server, vcn_id) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/chef/knife/oci_helper_show.rb', line 50

def add_server_details(server, vcn_id)
  server.extend ServerDetails

  server.launchtime = server.time_created.strftime('%a, %e %b %Y %T %Z')
  server.compartment_name = lookup_compartment_name(server.compartment_id)
  server.image_name = lookup_image_name(server.image_id)
  server.vcn_id = vcn_id
  server.vcn_name = lookup_vcn_name(vcn_id)
end

#add_vnic_details(vnic) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef/knife/oci_helper_show.rb', line 60

def add_vnic_details(vnic)
  vnic.extend VnicDetails

  begin
    subnet = network_client.get_subnet(vnic.subnet_id, {})
  rescue OCI::Errors::ServiceError => service_error
    raise unless service_error.service_code == 'NotAuthorizedOrNotFound'
  else
    vnic.fqdn = vnic.hostname_label + '.' + subnet.data.subnet_domain_name if
      subnet.data && subnet.data.subnet_domain_name && vnic.hostname_label
    vnic.subnet_name = subnet.data.display_name if
      subnet.data && subnet.data.display_name
    # piggyback the vcn_id from here, so we can avoid a few network calls
    vnic.vcn_id = subnet.data.vcn_id
  end
end

#display_server_info(config, instance, vnics) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/oci_helper_show.rb', line 82

def display_server_info(config, instance, vnics)
  show_value('Display Name', instance.display_name)
  show_value('Instance ID', instance.id)
  show_value('Lifecycle State', instance.lifecycle_state)
  show_value('Availability Domain', instance.availability_domain)
  show_value('Compartment Name', instance.compartment_name) if instance.respond_to? :compartment_name
  show_value('Compartment ID', instance.compartment_id)
  show_value('Region', instance.region)
  show_value('Image Name', instance.image_name) if instance.respond_to? :image_name
  show_value('Image ID', instance.image_id)
  show_value('Shape', instance.shape)
  show_value('VCN Name', instance.vcn_name) if instance.respond_to? :vcn_name
  show_value('VCN ID', instance.vcn_id) if instance.respond_to? :vcn_id
  show_value('Launched', instance.launchtime) if instance.respond_to? :launchtime
  vnics.each_index do |index|
    prefix = vnics[index].is_primary ? 'Primary' : 'Secondary'
    show_value("#{prefix} Public IP Address", vnics[index].public_ip)
    show_value("#{prefix} Private IP Address", vnics[index].private_ip)
    show_value("#{prefix} Hostname", vnics[index].hostname_label)
    show_value("#{prefix} FQDN", vnics[index].fqdn) if vnics[index].respond_to? :fqdn
    show_value("#{prefix} Subnet Name", vnics[index].subnet_name) if vnics[index].respond_to? :subnet_name
  end
  show_value('Node Name', config[:chef_node_name])
end

#lookup_compartment_name(compartment_id) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/chef/knife/oci_helper_show.rb', line 26

def lookup_compartment_name(compartment_id)
  compartment = identity_client.get_compartment(compartment_id, {})
rescue OCI::Errors::ServiceError => service_error
  raise unless service_error.service_code == 'NotAuthorizedOrNotFound'
else
  compartment.data.name
end

#lookup_image_name(image_id) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/chef/knife/oci_helper_show.rb', line 34

def lookup_image_name(image_id)
  image = compute_client.get_image(image_id, {})
rescue OCI::Errors::ServiceError => service_error
  raise unless service_error.service_code == 'NotAuthorizedOrNotFound'
else
  image.data.display_name
end

#lookup_vcn_name(vcn_id) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/chef/knife/oci_helper_show.rb', line 42

def lookup_vcn_name(vcn_id)
  vcn = network_client.get_vcn(vcn_id, {})
rescue OCI::Errors::ServiceError => service_error
  raise unless service_error.service_code == 'NotAuthorizedOrNotFound'
else
  vcn.data.display_name
end

#show_value(key, value, color = :cyan) ⇒ Object



77
78
79
# File 'lib/chef/knife/oci_helper_show.rb', line 77

def show_value(key, value, color = :cyan)
  ui.msg "#{ui.color(key, color)}: #{value}" if value && !value.to_s.empty?
end