Module: Chef::Knife::OciHelper

Overview

OCI helper module

Instance Method Summary collapse

Instance Method Details

#_non_summary_list(list) ⇒ Object

Return data in non-summary mode format.



116
117
118
119
120
121
122
123
# File 'lib/chef/knife/oci_helper.rb', line 116

def _non_summary_list(list)
  list_for_display = []
  list.each do |item|
    list_for_display += [item.to_hash]
  end

  list_for_display
end

#_summary_list(list) ⇒ Object

Return data in summary mode format



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/chef/knife/oci_helper.rb', line 102

def _summary_list(list)
  list_for_display = []

  if list
    list.each do |item|
      display_item = yield(item, list_for_display)
      list_for_display += display_item if display_item
    end
  end

  list_for_display
end

#bold(list) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/chef/knife/oci_helper.rb', line 93

def bold(list)
  bolded_list = []
  list.each do |column|
    bolded_list += [ui.color(column, :bold)]
  end
  bolded_list.flatten.compact
end

#check_can_access_instance(instance_id) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/chef/knife/oci_helper.rb', line 167

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 == OCI::Core::Models::Instance::LIFECYCLE_STATE_TERMINATED
rescue OCI::Errors::ServiceError => service_error
  raise unless service_error.service_code == 'NotAuthorizedOrNotFound'
  error_and_exit 'Instance not authorized or not found'
else
  return response
end

#compartment_idObject

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.



48
49
50
# File 'lib/chef/knife/oci_helper.rb', line 48

def compartment_id
  @compartment_id ||= config[:compartment_id] || Chef::Config[:knife][:compartment_id] || oci_config.tenancy
end

#compute_clientObject



34
35
36
# File 'lib/chef/knife/oci_helper.rb', line 34

def compute_client
  @compute_client ||= OCI::Core::ComputeClient.new(config: oci_config)
end

#config_file_locationObject



22
23
24
25
26
# File 'lib/chef/knife/oci_helper.rb', line 22

def config_file_location
  # Load first from command line args if available, then from knife.rb, then use the default.
  # For backwards compatibility with knife-bmcs, if oci version is not found in knife.rb, then check for bmcs version.
  config[:oci_config_file] || Chef::Config[:knife][:oci_config_file] || Chef::Config[:knife][:bmcs_config_file] || OCI::ConfigFileLoader::DEFAULT_CONFIG_FILE
end

#config_file_profileObject



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

def config_file_profile
  # Load first from command line args if available, then from knife.rb, then use the default.
  # For backwards compatibility with knife-bmcs, if oci version is not found in knife.rb, then check for bmcs version.
  config[:oci_profile] || Chef::Config[:knife][:oci_profile] || Chef::Config[:knife][:bmcs_profile] || OCI::ConfigFileLoader::DEFAULT_PROFILE
end

#confirm(prompt) ⇒ Object

Return a true or false with the confirmation result. Note: user prompt is bypassed with –yes to confirm automatically.



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/chef/knife/oci_helper.rb', line 155

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_from_array(list_for_display, num_columns) ⇒ Object

Display a list using a one dimensional array as input

Example output in summary mode: display_list_from_array([‘a’,‘b’, ‘c’, ‘d’], 2) a b c d



144
145
146
147
148
149
150
151
# File 'lib/chef/knife/oci_helper.rb', line 144

def display_list_from_array(list_for_display, num_columns)
  if config[:format] == 'summary'
    num_columns = 1 if num_columns < 1
    puts ui.list(list_for_display, :uneven_columns_across, num_columns)
  else
    ui.output(list_for_display)
  end
end

#error_and_exit(message) ⇒ Object



52
53
54
55
# File 'lib/chef/knife/oci_helper.rb', line 52

def error_and_exit(message)
  ui.error message
  exit(1)
end

#get_display_results(options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/knife/oci_helper.rb', line 74

def get_display_results(options)
  max_results = config[:limit] ? Integer(config[:limit]) : nil

  num_fetched_results = 0
  list_for_display = []
  response = nil
  loop do
    response, new_items = yield(options)

    list_for_display += new_items
    num_fetched_results += response.data.length if response.data
    break if next_page_token(response).nil?
    break if max_results && num_fetched_results >= max_results
    options[:page] = next_page_token(response)
    options[:limit] = (max_results - num_fetched_results).to_s if max_results
  end
  [list_for_display, response]
end

#identity_clientObject



42
43
44
# File 'lib/chef/knife/oci_helper.rb', line 42

def identity_client
  @identity_client ||= OCI::Identity::IdentityClient.new(config: oci_config)
end

#network_clientObject



38
39
40
# File 'lib/chef/knife/oci_helper.rb', line 38

def network_client
  @network_client ||= OCI::Core::VirtualNetworkClient.new(config: oci_config)
end

#next_page_token(response) ⇒ Object



69
70
71
72
# File 'lib/chef/knife/oci_helper.rb', line 69

def next_page_token(response)
  return response.headers['opc-next-page'] if response.headers.include? 'opc-next-page'
  nil
end

#oci_configObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/chef/knife/oci_helper.rb', line 11

def oci_config
  unless @oci_config
    @oci_config = OCI::ConfigFileLoader.load_config(config_file_location: config_file_location, profile_name: config_file_profile)
    @oci_config.region = config[:region] if config[:region]

    @oci_config.additional_user_agent = "Oracle-ChefKnifeOCI/#{::Knife::OCI::VERSION}"
  end

  @oci_config
end

#response_to_list(response, &block) ⇒ Object

Return a one dimensional array of data based on API response. Result is compatible with display_list_from_array.



127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/knife/oci_helper.rb', line 127

def response_to_list(response, &block)
  list = if response.data.nil?
           []
         else
           response.data.is_a?(Array) ? response.data : [response.data]
         end

  return _summary_list(list, &block) if config[:format] == 'summary'
  _non_summary_list(list)
end

#validate_required_params(required_params, params) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/chef/knife/oci_helper.rb', line 57

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



65
66
67
# File 'lib/chef/knife/oci_helper.rb', line 65

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