Module: Chef::Knife::BmcsHelper
- Included in:
- BmcsAdList, BmcsCompartmentList, BmcsImageList, BmcsServerCreate, BmcsServerDelete, BmcsServerList, BmcsServerShow, BmcsShapeList, BmcsSubnetList, BmcsVcnList
- Defined in:
- lib/chef/knife/bmcs_helper.rb
Overview
BMCS helper module
Instance Method Summary collapse
- #bmcs_config ⇒ Object
- #check_can_access_instance(instance_id) ⇒ Object
-
#compartment_id ⇒ Object
Get the compartment ID first from the command line args if available, then from the knife.rb file, and if neither of those is specified then use the tenancy.
- #compute_client ⇒ Object
-
#confirm(prompt) ⇒ Object
Return a true or false with the confirmation result.
-
#display_list(response, columns, warn_on_truncated: true) ⇒ Object
TODO: Method should be refactored to reduce complexity.
- #display_server_info(config, instance, vnics) ⇒ Object
- #error_and_exit(message) ⇒ Object
- #identity_client ⇒ Object
- #network_client ⇒ Object
- #show_value(key, value, color = :cyan) ⇒ Object
- #validate_required_params(required_params, params) ⇒ Object
- #warn_if_page_is_truncated(response) ⇒ Object
Instance Method Details
#bmcs_config ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/chef/knife/bmcs_helper.rb', line 11 def bmcs_config unless @bmcs_config # Load config and profile first from command line args if available, then from knife.rb, then use the default. config_file = config[:bmcs_config_file] || Chef::Config[:knife][:bmcs_config_file] || OracleBMC::ConfigFileLoader::DEFAULT_CONFIG_FILE profile = config[:bmcs_profile] || Chef::Config[:knife][:bmcs_profile] || OracleBMC::ConfigFileLoader::DEFAULT_PROFILE @bmcs_config = OracleBMC::ConfigFileLoader.load_config(config_file_location: config_file, profile_name: profile) @bmcs_config.region = config[:region] if config[:region] @bmcs_config.additional_user_agent = "Oracle-ChefKnifeBMCS/#{::Knife::BMCS::VERSION}" end @bmcs_config end |
#check_can_access_instance(instance_id) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/chef/knife/bmcs_helper.rb', line 115 def check_can_access_instance(instance_id) response = compute_client.get_instance(instance_id) error_and_exit 'Instance is already in terminated state' if response && response.data && response.data.lifecycle_state == OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_TERMINATED rescue OracleBMC::Errors::ServiceError => service_error raise unless service_error.serviceCode == 'NotAuthorizedOrNotFound' error_and_exit 'Instance not authorized or not found' else return response end |
#compartment_id ⇒ Object
Get the compartment ID first from the command line args if available, then from the knife.rb file, and if neither of those is specified then use the tenancy.
39 40 41 |
# File 'lib/chef/knife/bmcs_helper.rb', line 39 def compartment_id @compartment_id ||= config[:compartment_id] || Chef::Config[:knife][:compartment_id] || bmcs_config.tenancy end |
#compute_client ⇒ Object
25 26 27 |
# File 'lib/chef/knife/bmcs_helper.rb', line 25 def compute_client @compute_client ||= OracleBMC::Core::ComputeClient.new(config: bmcs_config) end |
#confirm(prompt) ⇒ Object
Return a true or false with the confirmation result. Note: user prompt is bypassed with –yes to confirm automatically.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/chef/knife/bmcs_helper.rb', line 103 def confirm(prompt) return true if config[:yes] valid_responses = %w[yes no y n] response = nil 3.times do response = ui.ask(prompt).downcase break if valid_responses.include? response ui.warn "Valid responses are #{valid_responses}" end response.match(/^y/) end |
#display_list(response, columns, warn_on_truncated: true) ⇒ Object
TODO: Method should be refactored to reduce complexity. rubocop:disable Metrics/PerceivedComplexity
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/chef/knife/bmcs_helper.rb', line 62 def display_list(response, columns, warn_on_truncated: true) list = if response.data.nil? [] else response.data.is_a?(Array) ? response.data : [response.data] end list_for_display = [] if config[:format] == 'summary' width = 1 unless columns.empty? columns.each do |column| list_for_display += [ui.color(column, :bold)] end list_for_display = list_for_display.flatten.compact width = columns.length end if list list.each do |item| display_item = yield(item, list_for_display) list_for_display += display_item if display_item end end puts ui.list(list_for_display, :uneven_columns_across, width) else list.each do |item| list_for_display += [item.to_hash] end ui.output(list_for_display) end warn_if_page_is_truncated(response) if warn_on_truncated end |
#display_server_info(config, instance, vnics) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/chef/knife/bmcs_helper.rb', line 129 def display_server_info(config, instance, vnics) show_value('Display Name', instance.display_name) show_value('Instance ID', instance.id) show_value('Availability Domain', instance.availability_domain) show_value('Compartment ID', instance.compartment_id) show_value('Region', instance.region) show_value('Image ID', instance.image_id) show_value('Shape', instance.shape) 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) end show_value('Node Name', config[:chef_node_name]) end |
#error_and_exit(message) ⇒ Object
43 44 45 46 |
# File 'lib/chef/knife/bmcs_helper.rb', line 43 def error_and_exit() ui.error exit(1) end |
#identity_client ⇒ Object
33 34 35 |
# File 'lib/chef/knife/bmcs_helper.rb', line 33 def identity_client @identity_client ||= OracleBMC::Identity::IdentityClient.new(config: bmcs_config) end |
#network_client ⇒ Object
29 30 31 |
# File 'lib/chef/knife/bmcs_helper.rb', line 29 def network_client @network_client ||= OracleBMC::Core::VirtualNetworkClient.new(config: bmcs_config) end |
#show_value(key, value, color = :cyan) ⇒ Object
125 126 127 |
# File 'lib/chef/knife/bmcs_helper.rb', line 125 def show_value(key, value, color = :cyan) ui.msg "#{ui.color(key, color)}: #{value}" if value && !value.to_s.empty? end |
#validate_required_params(required_params, params) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/chef/knife/bmcs_helper.rb', line 48 def validate_required_params(required_params, params) missing_params = required_params.select do |param| params[param].nil? end error_and_exit("Missing the following required parameters: #{missing_params.join(', ').tr('_', '-')}") unless missing_params.empty? end |
#warn_if_page_is_truncated(response) ⇒ Object
56 57 58 |
# File 'lib/chef/knife/bmcs_helper.rb', line 56 def warn_if_page_is_truncated(response) ui.warn('This list has been truncated. To view more items, increase the limit.') if response.headers.include? 'opc-next-page' end |