Class: OneviewSDK::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/oneview-sdk/cli.rb

Overview

cli for oneview-sdk

Defined Under Namespace

Classes: Runner

Instance Method Summary collapse

Instance Method Details

#cert(type, url = ) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/oneview-sdk/cli.rb', line 288

def cert(type, url = ENV['ONEVIEWSDK_URL'])
  case type.downcase
  when 'check'
    fail_nice 'Must specify a url' unless url
    puts "Checking certificate for '#{url}' ..."
    if OneviewSDK::SSLHelper.check_cert(url)
      puts 'Certificate is valid!'
    else
      fail_nice 'Certificate Validation Failed!'
    end
  when 'import'
    fail_nice 'Must specify a url' unless url
    puts "Importing certificate for '#{url}' into '#{OneviewSDK::SSLHelper::CERT_STORE}'..."
    OneviewSDK::SSLHelper.install_cert(url)
  when 'list'
    if File.file?(OneviewSDK::SSLHelper::CERT_STORE)
      puts File.read(OneviewSDK::SSLHelper::CERT_STORE)
    else
      puts 'No certs imported!'
    end
  else fail_nice "Invalid action '#{type}'. Valid actions are [check, import, list]"
  end
rescue StandardError => e
  fail_nice e.message
end

#consoleObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/oneview-sdk/cli.rb', line 76

def console
  client_setup({}, true, true)
  puts "Console Connected to #{@client.url}"
  puts "HINT: The @client object is available to you\n\n"
rescue
  puts "WARNING: Couldn't connect to #{@options['url'] || ENV['ONEVIEWSDK_URL']}\n\n"
ensure
  require 'pry'
  Pry.config.prompt = proc { '> ' }
  Pry.plugins['stack_explorer'] && Pry.plugins['stack_explorer'].disable!
  Pry.plugins['byebug'] && Pry.plugins['byebug'].disable!
  Pry.start(OneviewSDK::Console.new(@client))
end

#create_from_file(file_path) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/oneview-sdk/cli.rb', line 257

def create_from_file(file_path)
  fail_nice "Can't use the 'force' and 'if_missing' flags at the same time." if options['force'] && options['if_missing']
  client_setup
  resource = OneviewSDK::Resource.from_file(@client, file_path)
  resource[:uri] = nil
  fail_nice 'File must specify a resource name' unless resource[:name]
  existing_resource = resource.class.find_by(@client, name: resource[:name]).first
  if existing_resource
    if options['if_missing']
      puts "Skipped: '#{resource[:name]}': #{resource.class.name.split('::').last} already exists."
      return
    end
    fail_nice "#{resource.class.name.split('::').last} '#{resource[:name]}' already exists." unless options['force']
    begin
      resource.data.delete('uri')
      existing_resource.update(resource.data)
      output "Updated Successfully!\n#{resource[:uri]}"
    rescue StandardError => e
      fail_nice "Failed to update #{resource.class.name.split('::').last} '#{resource[:name]}': #{e}"
    end
  else
    begin
      resource.create
      output "Created Successfully!\n#{resource[:uri]}"
    rescue StandardError => e
      fail_nice "Failed to create #{resource.class.name.split('::').last} '#{resource[:name]}': #{e}"
    end
  end
end

#delete(type, name) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/oneview-sdk/cli.rb', line 209

def delete(type, name)
  resource_class = parse_type(type)
  client_setup
  matches = resource_class.find_by(@client, name: name)
  fail_nice 'Not Found' if matches.empty?
  resource = matches.first
  return unless options['force'] || agree("Delete '#{name}'? [Y/N] ")
  begin
    resource.delete
    output 'Deleted Successfully!'
  rescue StandardError => e
    fail_nice "Failed to delete #{resource.class.name.split('::').last} '#{name}': #{e}"
  end
end

#delete_from_file(file_path) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/oneview-sdk/cli.rb', line 229

def delete_from_file(file_path)
  client_setup
  resource = OneviewSDK::Resource.from_file(@client, file_path)
  fail_nice 'File must define name or uri' unless resource[:name] || resource[:uri]
  found = resource.retrieve! rescue false
  found ||= resource.refresh rescue false
  fail_nice "#{resource.class.name.split('::').last} '#{resource[:name] || resource[:uri]}' Not Found" unless found
  unless options['force'] || agree("Delete '#{resource[:name]}'? [Y/N] ")
    puts 'OK, exiting.'
    return
  end
  begin
    resource.delete
    output 'Deleted Successfully!'
  rescue StandardError => e
    fail_nice "Failed to delete #{resource.class.name.split('::').last} '#{resource[:name]}': #{e}"
  end
end

#envObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/oneview-sdk/cli.rb', line 105

def env
  data = {}
  OneviewSDK::ENV_VARS.each { |k| data[k] = ENV[k] }
  if @options['format'] == 'human'
    data.each do |key, value|
      value = "'#{value}'" if value && ! %w(true false).include?(value)
      printf "%-#{data.keys.max_by(&:length).length}s = %s\n", key, value || 'nil'
    end
  else
    output(parse_hash(data, true))
  end
end

#list(type) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/oneview-sdk/cli.rb', line 130

def list(type)
  resource_class = parse_type(type)
  client_setup
  data = []
  resource_class.get_all(@client).each { |r| data.push(r[:name]) }
  output data
end

#loginObject



119
120
121
122
# File 'lib/oneview-sdk/cli.rb', line 119

def 
  client_setup
  puts "Login Successful! Token = #{@client.token}"
end

#search(type) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/oneview-sdk/cli.rb', line 178

def search(type)
  resource_class = parse_type(type)
  client_setup
  filter = parse_hash(options['filter'])
  matches = resource_class.find_by(@client, filter)
  if matches.empty? # Search with integers & booleans converted
    filter = parse_hash(options['filter'], true)
    matches = resource_class.find_by(@client, filter) unless filter == options['filter']
  end
  if options['attribute']
    data = []
    matches.each do |d|
      temp = {}
      options['attribute'].split(',').each do |attr|
        temp[attr] = d[attr]
      end
      data.push(d['name'] => temp)
    end
    output data, -2 # Shift left by 2 so things look right
  else # List names only by default
    names = []
    matches.each { |m| names.push(m['name']) }
    output names
  end
end

#show(type, name) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/oneview-sdk/cli.rb', line 148

def show(type, name)
  resource_class = parse_type(type)
  client_setup
  matches = resource_class.find_by(@client, name: name)
  fail_nice 'Not Found' if matches.empty?
  data = matches.first.data
  if options['attribute']
    new_data = {}
    options['attribute'].split(',').each do |attr|
      new_data[attr] = data[attr]
    end
    data = new_data
  end
  output data
end

#versionObject



91
92
93
94
95
96
97
# File 'lib/oneview-sdk/cli.rb', line 91

def version
  puts "Gem Version: #{OneviewSDK::VERSION}"
  client_setup({ 'log_level' => :error }, true)
  puts "OneView appliance API version at '#{@client.url}' = #{@client.max_api_version}"
rescue StandardError, SystemExit
  puts 'OneView appliance API version unknown'
end