Class: OneviewSDK::Client

Inherits:
Object
  • Object
show all
Includes:
Rest
Defined in:
lib/oneview-sdk/client.rb

Overview

The client defines the connection to the OneView server and handles communication with it.

Direct Known Subclasses

ImageStreamer::Client

Constant Summary

Constants included from Rest

Rest::READ_TIMEOUT, 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

Instance Method Summary collapse

Methods included from Rest

#download_file, #response_handler, #rest_api, #rest_delete, #rest_get, #rest_patch, #rest_post, #rest_put, #upload_file

Constructor Details

#initialize(options = {}) ⇒ Client

Creates client object, establish connection, and set up logging and api version.

Parameters:

  • (defaults to: {})

    the options to configure the client

Options Hash (options):

  • :logger (Logger) — default: Logger.new(STDOUT)

    Logger object to use. Must implement debug(String), info(String), warn(String), error(String), & level=

  • :log_level (Symbol) — default: :info

    Log level. Logger must define a constant with this name. ie Logger::INFO

  • :print_wait_dots (Boolean) — default: false

    When true, prints status dots while waiting on the tasks to complete.

  • :url (String)

    URL of OneView appliance

  • :user (String) — default: 'Administrator'

    The username to use for authentication with the OneView appliance

  • :password (String) — default: ENV['ONEVIEWSDK_PASSWORD']

    The password to use for authentication with OneView appliance

  • :domain (String) — default: 'LOCAL'

    The name of the domain directory used for authentication

  • :token (String) — default: ENV['ONEVIEWSDK_TOKEN']

    The token to use for authentication with OneView appliance Use the token or the username and password (not both). The token has precedence.

  • :api_version (Integer) — default: 200

    This is the API version to use by default for requests

  • :ssl_enabled (Boolean) — default: true

    Use ssl for requests? Respects ENV

  • :timeout (Integer) — default: nil

    Override the default request timeout value

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/oneview-sdk/client.rb', line 41

def initialize(options = {})
  options = Hash[options.map { |k, v| [k.to_sym, v] }] # Convert string hash keys to symbols
  STDOUT.sync = true
  @logger = options[:logger] || Logger.new(STDOUT)
  [:debug, :info, :warn, :error, :level=].each { |m| raise InvalidClient, "Logger must respond to #{m} method " unless @logger.respond_to?(m) }
  self.log_level = options[:log_level] || :info
  @print_wait_dots = options.fetch(:print_wait_dots, false)
  @url = options[:url] || ENV['ONEVIEWSDK_URL']
  raise InvalidClient, 'Must set the url option' unless @url
  @max_api_version = appliance_api_version
  if options[:api_version] && options[:api_version].to_i > @max_api_version
    logger.warn "API version #{options[:api_version]} is greater than the appliance API version (#{@max_api_version})"
  end
  @api_version = options[:api_version] || [OneviewSDK::DEFAULT_API_VERSION, @max_api_version].min
  # Set the default OneviewSDK module API version
  OneviewSDK.api_version = @api_version unless OneviewSDK.api_version_updated? || !OneviewSDK::SUPPORTED_API_VERSIONS.include?(@api_version)
  @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 = options[:ssl_enabled] unless options[:ssl_enabled].nil?
  @timeout = options[:timeout] unless options[:timeout].nil?
  @cert_store = OneviewSDK::SSLHelper.load_trusted_certs if @ssl_enabled
  @token = options[:token] || ENV['ONEVIEWSDK_TOKEN']
  @logger.warn 'User option not set. Using default (Administrator)' unless @token || options[:user] || ENV['ONEVIEWSDK_USER']
  @user = options[:user] || ENV['ONEVIEWSDK_USER'] || 'Administrator'
  @password = options[:password] || ENV['ONEVIEWSDK_PASSWORD']
  raise InvalidClient, 'Must set user & password options or token option' unless @token || @password
  @domain = options[:domain] || ENV['ONEVIEWSDK_DOMAIN'] || 'LOCAL'
  @token ||= 
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def api_version
  @api_version
end

#cert_storeObject

Returns the value of attribute cert_store.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def cert_store
  @cert_store
end

#domainObject

Returns the value of attribute domain.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def domain
  @domain
end

#log_levelObject

Returns the value of attribute log_level.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def logger
  @logger
end

#max_api_versionObject (readonly)

Returns the value of attribute max_api_version.



20
21
22
# File 'lib/oneview-sdk/client.rb', line 20

def max_api_version
  @max_api_version
end

#passwordObject

Returns the value of attribute password.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def password
  @password
end

Returns the value of attribute print_wait_dots.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def print_wait_dots
  @print_wait_dots
end

#ssl_enabledObject

Returns the value of attribute ssl_enabled.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def ssl_enabled
  @ssl_enabled
end

#timeoutObject

Returns the value of attribute timeout.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def token
  @token
end

#urlObject

Returns the value of attribute url.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def url
  @url
end

#userObject

Returns the value of attribute user.



21
22
23
# File 'lib/oneview-sdk/client.rb', line 21

def user
  @user
end

Instance Method Details

#create(resource) ⇒ Object

Tells OneView to create the resource using the current attribute data

Parameters:

  • the object to create



84
85
86
87
# File 'lib/oneview-sdk/client.rb', line 84

def create(resource)
  resource.client = self
  resource.create
end

#delete(resource) ⇒ Object

Deletes this object from OneView

Parameters:

  • the object to delete



105
106
107
108
# File 'lib/oneview-sdk/client.rb', line 105

def delete(resource)
  resource.client = self
  resource.delete
end

#destroy_sessionOneviewSDK::Client

Delete the session on the appliance, invalidating the client’s token. To generate a new token after calling this method, use the refresh_login method. Call this after a token expires or the user and/or password is updated on the client object.

Returns:

  • self



165
166
167
168
# File 'lib/oneview-sdk/client.rb', line 165

def destroy_session
  response_handler(rest_delete('/rest/login-sessions'))
  self
end

#get_all(type, api_ver = @api_version, variant = nil) ⇒ Array<Resource>

Get array of all resources of a specified type

Examples:

Get all Ethernet Networks

networks = @client.get_all('EthernetNetworks')
synergy_networks = @client.get_all('EthernetNetworks', 300, 'Synergy')

Parameters:

  • Resource type

  • (defaults to: @api_version)

    API module version to fetch resources from

  • (defaults to: nil)

    API module variant to fetch resource from

Returns:

  • Results

Raises:

  • if the type is invalid



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

def get_all(type, api_ver = @api_version, variant = nil)
  klass = OneviewSDK.resource_named(type, api_ver, variant)
  raise TypeError, "Invalid resource type '#{type}'. OneviewSDK::API#{api_ver} does not contain a class like it." unless klass
  klass.get_all(self)
end

#new_i3s_client(options = {}) ⇒ OneviewSDK::ImageStreamer::Client

Creates the image streamer client object.

Parameters:

  • (defaults to: {})

    the options to configure the client

Options Hash (options):

  • :logger (Logger) — default: Logger.new(STDOUT)

    Logger object to use. Must implement debug(String), info(String), warn(String), error(String), & level=

  • :log_level (Symbol) — default: :info

    Log level. Logger must define a constant with this name. ie Logger::INFO

  • :print_wait_dots (Boolean) — default: false

    When true, prints status dots while waiting on the tasks to complete.

  • :url (String)

    URL of Image Streamer

  • :api_version (Integer) — default: 300

    This is the API version to use by default for requests

  • :ssl_enabled (Boolean) — default: true

    Use ssl for requests? Respects ENV

  • :timeout (Integer) — default: nil

    Override the default request timeout value

Returns:

  • New instance of image streamer client



181
182
183
# File 'lib/oneview-sdk/client.rb', line 181

def new_i3s_client(options = {})
  OneviewSDK::ImageStreamer::Client.new(options.merge(token: @token))
end

#refresh(resource) ⇒ Object

Updates this object using the data that exists on OneView

Parameters:

  • the object to refresh



98
99
100
101
# File 'lib/oneview-sdk/client.rb', line 98

def refresh(resource)
  resource.client = self
  resource.refresh
end

#refresh_loginOneviewSDK::Client

Refresh the client’s session token & max_api_version. Call this after a token expires or the user and/or password is updated on the client object.

Returns:

  • self



155
156
157
158
159
# File 'lib/oneview-sdk/client.rb', line 155

def 
  @max_api_version = appliance_api_version
  @token = 
  self
end

#update(resource, attributes = {}) ⇒ Object

Sets the attribute data, and then saves to OneView

Parameters:

  • the object to update



91
92
93
94
# File 'lib/oneview-sdk/client.rb', line 91

def update(resource, attributes = {})
  resource.client = self
  resource.update(attributes)
end

#wait_for(task_uri) ⇒ Hash

Wait for a task to complete

Parameters:

Returns:

  • if the task completed successfully, return the task details

Raises:

  • if the task resulted in an error or early termination.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/oneview-sdk/client.rb', line 129

def wait_for(task_uri)
  raise ArgumentError, '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)
      raise TaskError, msg
    else
      print '.' if @print_wait_dots
      sleep 10
    end
  end
end