Class: OneviewSDK::Client
- Inherits:
-
Object
- Object
- OneviewSDK::Client
- Includes:
- Rest
- Defined in:
- lib/oneview-sdk/client.rb
Overview
The client defines the connection to the OneView server and handles communication with it.
Constant Summary collapse
- DEFAULT_API_VERSION =
200
Constants included from Rest
Rest::RESPONSE_CODE_ACCEPTED, Rest::RESPONSE_CODE_BAD_REQUEST, Rest::RESPONSE_CODE_CREATED, Rest::RESPONSE_CODE_NOT_FOUND, Rest::RESPONSE_CODE_NO_CONTENT, Rest::RESPONSE_CODE_OK, Rest::RESPONSE_CODE_UNAUTHORIZED
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#cert_store ⇒ Object
Returns the value of attribute cert_store.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#max_api_version ⇒ Object
readonly
Returns the value of attribute max_api_version.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#print_wait_dots ⇒ Object
Returns the value of attribute print_wait_dots.
-
#ssl_enabled ⇒ Object
Returns the value of attribute ssl_enabled.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#create(resource) ⇒ Object
Tell OneView to create the resource using the current attribute data.
-
#delete(resource) ⇒ Object
Deletes this object from OneView.
-
#get_all(type) ⇒ Array<Resource>
Get array of all resources of a specified type.
-
#initialize(options = {}) ⇒ Client
constructor
Create client object, establish connection, and set up logging and api version.
-
#refresh(resource) ⇒ Object
Updates this object using the data that exists on OneView.
-
#update(resource, attributes = {}) ⇒ Object
Set attribute data and save to OneView.
-
#wait_for(task_uri) ⇒ Hash
Wait for a task to complete.
Methods included from Rest
#response_handler, #rest_api, #rest_delete, #rest_get, #rest_patch, #rest_post, #rest_put
Constructor Details
#initialize(options = {}) ⇒ Client
Create client object, establish connection, and set up logging and api version.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/oneview-sdk/client.rb', line 29 def initialize( = {}) = Hash[.map { |k, v| [k.to_sym, v] }] # Convert string hash keys to symbols @logger = [:logger] || Logger.new(STDOUT) [:debug, :info, :warn, :error, :level=].each { |m| fail "Logger must respond to #{m} method " unless @logger.respond_to?(m) } @log_level = [:log_level] || :info @logger.level = @logger.class.const_get(@log_level.upcase) rescue @log_level @print_wait_dots = .fetch(:print_wait_dots, false) @url = [:url] || ENV['ONEVIEWSDK_URL'] fail 'Must set the url option' unless @url @max_api_version = appliance_api_version if [:api_version] && [:api_version].to_i > @max_api_version logger.warn "API version #{[:api_version]} is greater than the appliance API version (#{@max_api_version})" end @api_version = [:api_version] || [DEFAULT_API_VERSION, @max_api_version].min @ssl_enabled = true if ENV.key?('ONEVIEWSDK_SSL_ENABLED') if %w(true false 1 0).include?(ENV['ONEVIEWSDK_SSL_ENABLED']) @ssl_enabled = ! %w(false 0).include?(ENV['ONEVIEWSDK_SSL_ENABLED']) else @logger.warn "Unrecognized ssl_enabled value '#{ENV['ONEVIEWSDK_SSL_ENABLED']}'. Valid options are 'true' & 'false'" end end @ssl_enabled = [:ssl_enabled] unless [:ssl_enabled].nil? @cert_store = OneviewSDK::SSLHelper.load_trusted_certs if @ssl_enabled @token = [:token] || ENV['ONEVIEWSDK_TOKEN'] return if @token @logger.warn 'User option not set. Using default (Administrator)' unless [:user] || ENV['ONEVIEWSDK_USER'] @user = [:user] || ENV['ONEVIEWSDK_USER'] || 'Administrator' @password = [:password] || ENV['ONEVIEWSDK_PASSWORD'] fail 'Must set user & password options or token option' unless @password @token = login end |
Instance Attribute Details
#api_version ⇒ Object
Returns the value of attribute api_version.
12 13 14 |
# File 'lib/oneview-sdk/client.rb', line 12 def api_version @api_version end |
#cert_store ⇒ Object
Returns the value of attribute cert_store.
12 13 14 |
# File 'lib/oneview-sdk/client.rb', line 12 def cert_store @cert_store end |
#log_level ⇒ Object
Returns the value of attribute log_level.
12 13 14 |
# File 'lib/oneview-sdk/client.rb', line 12 def log_level @log_level end |
#logger ⇒ Object
Returns the value of attribute logger.
12 13 14 |
# File 'lib/oneview-sdk/client.rb', line 12 def logger @logger end |
#max_api_version ⇒ Object (readonly)
Returns the value of attribute max_api_version.
11 12 13 |
# File 'lib/oneview-sdk/client.rb', line 11 def max_api_version @max_api_version end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
11 12 13 |
# File 'lib/oneview-sdk/client.rb', line 11 def password @password end |
#print_wait_dots ⇒ Object
Returns the value of attribute print_wait_dots.
12 13 14 |
# File 'lib/oneview-sdk/client.rb', line 12 def print_wait_dots @print_wait_dots end |
#ssl_enabled ⇒ Object
Returns the value of attribute ssl_enabled.
12 13 14 |
# File 'lib/oneview-sdk/client.rb', line 12 def ssl_enabled @ssl_enabled end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
11 12 13 |
# File 'lib/oneview-sdk/client.rb', line 11 def token @token end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
11 12 13 |
# File 'lib/oneview-sdk/client.rb', line 11 def url @url end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
11 12 13 |
# File 'lib/oneview-sdk/client.rb', line 11 def user @user end |
Instance Method Details
#create(resource) ⇒ Object
Tell OneView to create the resource using the current attribute data
64 65 66 67 |
# File 'lib/oneview-sdk/client.rb', line 64 def create(resource) resource.client = self resource.create end |
#delete(resource) ⇒ Object
Deletes this object from OneView
85 86 87 88 |
# File 'lib/oneview-sdk/client.rb', line 85 def delete(resource) resource.client = self resource.delete end |
#get_all(type) ⇒ Array<Resource>
Get array of all resources of a specified type
95 96 97 98 99 |
# File 'lib/oneview-sdk/client.rb', line 95 def get_all(type) OneviewSDK.resource_named(type).get_all(self) rescue StandardError raise "Invalid resource type '#{type}'" end |
#refresh(resource) ⇒ Object
Updates this object using the data that exists on OneView
78 79 80 81 |
# File 'lib/oneview-sdk/client.rb', line 78 def refresh(resource) resource.client = self resource.refresh end |
#update(resource, attributes = {}) ⇒ Object
Set attribute data and save to OneView
71 72 73 74 |
# File 'lib/oneview-sdk/client.rb', line 71 def update(resource, attributes = {}) resource.client = self resource.update(attributes) end |
#wait_for(task_uri) ⇒ Hash
Wait for a task to complete
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/oneview-sdk/client.rb', line 105 def wait_for(task_uri) fail 'Must specify a task_uri!' if task_uri.nil? || task_uri.empty? loop do task_uri.gsub!(%r{/https:(.*)\/rest/}, '/rest') task = rest_get(task_uri) body = JSON.parse(task.body) case body['taskState'].downcase when 'completed' return body when 'warning' @logger.warn "Task ended with warning status. Details: #{JSON.pretty_generate(body['taskErrors']) rescue body}" return body when 'error', 'killed', 'terminated' msg = "Task ended with bad state: '#{body['taskState']}'.\nResponse: " msg += body['taskErrors'] ? JSON.pretty_generate(body['taskErrors']) : JSON.pretty_generate(body) fail(msg) else print '.' if @print_wait_dots sleep 10 end end end |