module Chef::Knife::BmcsHelper
BMCS helper module
Public Instance Methods
bmcs_config()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
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.
# 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()
click to toggle source
# File lib/chef/knife/bmcs_helper.rb, line 25 def compute_client @compute_client ||= OracleBMC::Core::ComputeClient.new(config: bmcs_config) end
confirm(prompt)
click to toggle source
Return a true or false with the confirmation result. Note: user prompt is bypassed with –yes to confirm automatically.
# 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) { |item, list_for_display| ... }
click to toggle source
TODO: Method should be refactored to reduce complexity. rubocop:disable Metrics/PerceivedComplexity
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/chef/knife/bmcs_helper.rb, line 43 def error_and_exit(message) ui.error message exit(1) end
identity_client()
click to toggle source
# File lib/chef/knife/bmcs_helper.rb, line 33 def identity_client @identity_client ||= OracleBMC::Identity::IdentityClient.new(config: bmcs_config) end
network_client()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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