Class: OneviewSDK::Cli
- Inherits:
-
Thor
- Object
- Thor
- OneviewSDK::Cli
- Defined in:
- lib/oneview-sdk/cli.rb
Overview
command-line-interface for oneview-sdk When you install this gem, this cli should be available to you by running: ‘$ oneview-sdk-ruby`
Defined Under Namespace
Classes: Runner
Instance Method Summary collapse
-
#cert(type, url = ) ⇒ Object
Check, import, or list OneView certs.
-
#console ⇒ Object
Open a Ruby console with a connection to OneView.
-
#create_from_file(file_path) ⇒ Object
Create/Update resource defined in file.
-
#delete(type, name) ⇒ Object
Delete resource by name.
-
#delete_from_file(file_path) ⇒ Object
Delete resource defined in file.
-
#env ⇒ Object
Show environment variables for oneview-sdk-ruby.
-
#list(type) ⇒ Object
List names of resources.
-
#login ⇒ Object
Attempt authentication and return token.
-
#rest(method, uri) ⇒ Object
Make REST call to the OneView API.
-
#search(type) ⇒ Object
Search for resource by key/value pair(s).
-
#show(type, name) ⇒ Object
Show resource details.
-
#to_file(type, name) ⇒ Object
Save resource details to file.
-
#update(type, name) ⇒ Object
Update resource by name.
-
#version ⇒ Object
Print gem and OneView appliance versions.
Instance Method Details
#cert(type, url = ) ⇒ Object
Check, import, or list OneView certs
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/oneview-sdk/cli.rb', line 399 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. end |
#console ⇒ Object
Open a Ruby console with a connection to OneView
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/oneview-sdk/cli.rb', line 78 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
Create/Update resource defined in file
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/oneview-sdk/cli.rb', line 338 def create_from_file(file_path) client_setup resource = OneviewSDK::Resource.from_file(@client, file_path) fail_nice 'Failed to determine resource type!' if resource.class == OneviewSDK::Resource existing_resource = resource.class.new(@client, resource.data) resource.data.delete('uri') if existing_resource.retrieve! if ['if_missing'] puts "Skipped: #{resource.class.name.split('::').last} '#{resource[:name]}' already exists.\n#{existing_resource[:uri]}" return end if existing_resource.like?(resource.data) puts "Skipped: #{resource.class.name.split('::').last} '#{resource[:name]}' is up to date.\n#{existing_resource[:uri]}" return end begin existing_resource.update(resource.data) puts "Updated Successfully!\n#{existing_resource[:uri]}" rescue StandardError => e fail_nice "Failed to update #{resource.class.name.split('::').last} '#{resource[:name]}': #{e}" end else begin resource.create puts "Created Successfully!\n#{resource[:uri]}" rescue StandardError => e fail_nice "Failed to create #{resource.class.name.split('::').last} '#{resource[:name]}': #{e}" end end rescue IncompleteResource => e fail_nice "Failed to create #{resource.class.name.split('::').last} '#{resource[:name]}': #{e}" rescue SystemCallError => e # File open errors fail_nice e end |
#delete(type, name) ⇒ Object
Delete resource by name
294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/oneview-sdk/cli.rb', line 294 def delete(type, name) resource_class = parse_type(type) client_setup matches = resource_class.find_by(@client, name: name) fail_nice('Not Found', 2) if matches.empty? resource = matches.first return unless ['force'] || agree("Delete '#{name}'? [Y/N] ") begin resource.delete puts '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
Delete resource defined in file
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/oneview-sdk/cli.rb', line 315 def delete_from_file(file_path) client_setup resource = OneviewSDK::Resource.from_file(@client, file_path) fail_nice("#{resource.class.name.split('::').last} '#{resource[:name] || resource[:uri]}' Not Found", 2) unless resource.retrieve! return unless ['force'] || agree("Delete '#{resource[:name]}'? [Y/N] ") begin resource.delete puts 'Deleted Successfully!' rescue StandardError => e fail_nice "Failed to delete #{resource.class.name.split('::').last} '#{resource[:name]}': #{e}" end rescue IncompleteResource => e fail_nice "Failed to delete #{resource.class.name.split('::').last} '#{resource[:name]}': #{e}" rescue SystemCallError => e # File open errors fail_nice e end |
#env ⇒ Object
Show environment variables for oneview-sdk-ruby
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/oneview-sdk/cli.rb', line 109 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
List names of resources
136 137 138 139 140 141 142 |
# File 'lib/oneview-sdk/cli.rb', line 136 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 |
#login ⇒ Object
Attempt authentication and return token
124 125 126 127 |
# File 'lib/oneview-sdk/cli.rb', line 124 def login client_setup puts "Login Successful! Token = #{@client.token}" end |
#rest(method, uri) ⇒ Object
Make REST call to the OneView API
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/oneview-sdk/cli.rb', line 225 def rest(method, uri) log_level = @options['log_level'] == :warn ? :error : @options['log_level'].to_sym # Default to :error client_setup('log_level' => log_level) uri_copy = uri.dup uri_copy.prepend('/') unless uri_copy.start_with?('/') if @options['data'] begin data = { body: JSON.parse(@options['data']) } rescue JSON::ParserError => e fail_nice("Failed to parse data as JSON\n#{e.}") end end data ||= {} response = @client.rest_api(method, uri_copy, data) if response.code.to_i.between?(200, 299) case @options['format'] when 'yaml' puts JSON.parse(response.body).to_yaml when 'json' puts JSON.pretty_generate(JSON.parse(response.body)) else # raw puts response.body end else body = JSON.pretty_generate(JSON.parse(response.body)) rescue response.body fail_nice("Request failed: #{response.inspect}\nHeaders: #{response.to_hash}\nBody: #{body}") end rescue OneviewSDK::InvalidRequest => e fail_nice(e.) end |
#search(type) ⇒ Object
Search for resource by key/value pair(s)
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/oneview-sdk/cli.rb', line 186 def search(type) resource_class = parse_type(type) client_setup filter = parse_hash(['filter']) matches = resource_class.find_by(@client, filter) if matches.empty? # Search with integers & booleans converted filter = parse_hash(['filter'], true) matches = resource_class.find_by(@client, filter) unless filter == ['filter'] end if ['attribute'] data = [] matches.each do |d| temp = {} ['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
Show resource details
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/oneview-sdk/cli.rb', line 155 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 ['attribute'] new_data = {} ['attribute'].split(',').each do |attr| new_data[attr] = data[attr] end data = new_data end output data end |
#to_file(type, name) ⇒ Object
Save resource details to file
385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/oneview-sdk/cli.rb', line 385 def to_file(type, name) file = File.(['path']) resource_class = parse_type(type) client_setup resource = resource_class.find_by(@client, name: name).first fail_nice "#{resource_class.name.split('::').last} '#{name}' not found" unless resource resource.to_file(file, ['format']) puts "Output to #{file}" rescue SystemCallError => e fail_nice "Failed to create file! (You may need to create the necessary directories). Message: #{e}" end |
#update(type, name) ⇒ Object
Update resource by name
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/oneview-sdk/cli.rb', line 267 def update(type, name) resource_class = parse_type(type) client_setup fail_nice 'Must set the hash or json option' unless @options['hash'] || @options['json'] fail_nice 'Must set the hash OR json option. Not both' if @options['hash'] && @options['json'] begin data = @options['hash'] || JSON.parse(@options['json']) rescue JSON::ParserError => e fail_nice("Failed to parse json\n#{e.}") end matches = resource_class.find_by(@client, name: name) fail_nice 'Not Found' if matches.empty? resource = matches.first begin resource.update(data) puts 'Updated Successfully!' rescue StandardError => e fail_nice "Failed to update #{resource.class.name.split('::').last} '#{name}': #{e}" end end |
#version ⇒ Object
Print gem and OneView appliance versions
94 95 96 97 98 99 100 |
# File 'lib/oneview-sdk/cli.rb', line 94 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 |