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



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/oneview-sdk/cli.rb', line 277

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



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/oneview-sdk/cli.rb', line 65

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



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/oneview-sdk/cli.rb', line 246

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/oneview-sdk/cli.rb', line 198

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/oneview-sdk/cli.rb', line 218

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



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/oneview-sdk/cli.rb', line 94

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



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

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



108
109
110
111
# File 'lib/oneview-sdk/cli.rb', line 108

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

#search(type) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/oneview-sdk/cli.rb', line 167

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 temp
    end
    output data
  else # List names only by default
    names = []
    matches.each { |m| names.push(m['name']) }
    output names
  end
end

#show(type, name) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/oneview-sdk/cli.rb', line 137

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



80
81
82
83
84
85
86
# File 'lib/oneview-sdk/cli.rb', line 80

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