Class: OneviewSDK::Resource

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

Overview

Resource base class that defines all common resource functionality.

Constant Summary collapse

BASE_URI =
'/rest'.freeze
UNIQUE_IDENTIFIERS =

Ordered list of unique attributes to search by

%w[name uri].freeze
DEFAULT_REQUEST_HEADER =
{}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, params = {}, api_ver = nil) ⇒ Resource

Create a resource object, associate it with a client, and set its properties.

Raises:

Parameters:

  • The client object for the OneView appliance

  • (defaults to: {})

    The options for this resource (key-value pairs)

  • (defaults to: nil)

    The api version to use when interracting with this resource. Defaults to the client.api_version if it exists, or Appliance’s max API version to be used by default for requests



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/oneview-sdk/resource.rb', line 34

def initialize(client, params = {}, api_ver = nil)
  raise InvalidClient, 'Must specify a valid client'\
    unless client.is_a?(OneviewSDK::Client) || client.is_a?(OneviewSDK::ImageStreamer::Client)
  @client = client
  @logger = @client.logger
  @api_version = api_ver || @client.api_version
  if @api_version > @client.max_api_version
    raise UnsupportedVersion,
          "#{self.class.name} api_version '#{@api_version}' is greater than the client's max_api_version '#{@client.max_api_version}'"
  end
  @data ||= {}
  set_all(params)
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



23
24
25
# File 'lib/oneview-sdk/resource.rb', line 23

def api_version
  @api_version
end

#clientObject

Returns the value of attribute client.



23
24
25
# File 'lib/oneview-sdk/resource.rb', line 23

def client
  @client
end

#dataObject

Returns the value of attribute data.



23
24
25
# File 'lib/oneview-sdk/resource.rb', line 23

def data
  @data
end

#loggerObject

Returns the value of attribute logger.



23
24
25
# File 'lib/oneview-sdk/resource.rb', line 23

def logger
  @logger
end

Class Method Details

.build_query(query_options) ⇒ Object

Builds a Query string corresponding to the parameters passed

Options Hash (query_options):

  • String (String)

    Values that are Strings can be associated as usual

  • Resources (String)

    Values that are Resources can be associated as usual, with keys representing only the resource names (like ‘ethernet_network’). This method translates the SDK and Ruby standards to OneView request standard.

Parameters:

  • Query parameters and values to be applied to the query url. All key values should be Strings in snake case, the values could be Strings or Resources.



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/oneview-sdk/resource.rb', line 336

def self.build_query(query_options)
  return '' if !query_options || query_options.empty?
  query_path = '?'
  query_options.each do |k, v|
    words = k.to_s.split('_')
    words.map!(&:capitalize!)
    words[0] = words.first.downcase
    new_key = words.join
    v = "'" + v.join(',') + "'" if v.is_a?(Array) && v.any?
    v.retrieve! if v.respond_to?(:retrieve!) && !v['uri']
    if v.class <= OneviewSDK::Resource
      new_key = new_key.concat('Uri')
      v = v['uri']
    end
    query_path.concat("&#{new_key}=#{v}")
  end
  query_path.sub('?&', '?')
end

.find_by(client, attributes, uri = self::BASE_URI, header = self::DEFAULT_REQUEST_HEADER) ⇒ Array<Resource>

Make a GET request to the resource uri, and returns an array with results matching the search

Parameters:

  • The client object for the OneView appliance

  • Hash containing the attributes name and value

  • (defaults to: self::BASE_URI)

    URI of the endpoint

  • (defaults to: self::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • Results matching the search



291
292
293
294
295
296
297
298
299
# File 'lib/oneview-sdk/resource.rb', line 291

def self.find_by(client, attributes, uri = self::BASE_URI, header = self::DEFAULT_REQUEST_HEADER)
  all = find_with_pagination(client, uri, header)
  results = []
  all.each do |member|
    temp = new(client, member)
    results.push(temp) if temp.like?(attributes)
  end
  results
end

.find_with_pagination(client, uri, header = self::DEFAULT_REQUEST_HEADER) ⇒ Array<Hash>

Make a GET request to the uri, and returns an array with all results (search using resource pagination)

Parameters:

  • The client object for the OneView appliance

  • URI of the endpoint

  • (defaults to: self::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • Results



306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/oneview-sdk/resource.rb', line 306

def self.find_with_pagination(client, uri, header = self::DEFAULT_REQUEST_HEADER)
  all = []
  loop do
    response = client.rest_get(uri, header)
    body = client.response_handler(response)
    members = body['members']
    break unless members
    all.concat(members)
    break unless body['nextPageUri'] && (body['nextPageUri'] != body['uri'])
    uri = body['nextPageUri']
  end
  all
end

.from_file(client, file_path) ⇒ Resource

Load resource from a .json or .yaml file

Parameters:

  • The client object for the OneView appliance

  • The full path to the file

Returns:

  • New resource created from the file contents



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/oneview-sdk/resource.rb', line 271

def self.from_file(client, file_path)
  resource = OneviewSDK::Config.load(file_path)
  klass = self
  if klass == OneviewSDK::Resource && resource['type'] # Use correct resource class by parsing type
    klass = OneviewSDK # Secondary/temp class/module reference
    resource['type'].split('::').each do |id|
      c = klass.const_get(id) rescue nil
      klass = c if c.is_a?(Class) || c.is_a?(Module)
    end
    klass = OneviewSDK::Resource unless klass <= OneviewSDK::Resource
  end
  klass.new(client, resource['data'], resource['api_version'])
end

.get_all(client, header = self::DEFAULT_REQUEST_HEADER) ⇒ Array<Resource>

Make a GET request to the resource base uri, and returns an array with all objects of this type

Parameters:

  • The client object for the OneView appliance

  • (defaults to: self::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • Results



324
325
326
# File 'lib/oneview-sdk/resource.rb', line 324

def self.get_all(client, header = self::DEFAULT_REQUEST_HEADER)
  find_by(client, {}, self::BASE_URI, header)
end

.get_all_with_query(client, query = nil) ⇒ Array<Resource>

Make a GET request to the resource base uri, query parameters and returns an array with all objects of this type

Parameters:

  • The client object for the OneView appliance

  • (defaults to: nil)

    The query options for the request (key-value pairs)

  • The header options for the request (key-value pairs)

Returns:

  • Results



360
361
362
363
# File 'lib/oneview-sdk/resource.rb', line 360

def self.get_all_with_query(client, query = nil)
  query_uri = build_query(query) if query
  find_with_pagination(client, "#{self::BASE_URI}/#{query_uri}")
end

.schema(client) ⇒ Hash

Get resource schema

Parameters:

  • The client object for the OneView appliance

Returns:

  • Schema



259
260
261
262
263
264
265
# File 'lib/oneview-sdk/resource.rb', line 259

def self.schema(client)
  response = client.rest_get("#{self::BASE_URI}/schema", {}, client.api_version)
  client.response_handler(response)
rescue StandardError => e
  client.logger.error('This resource does not implement the schema endpoint!') if e.message =~ /404 NOT FOUND/
  raise e
end

Instance Method Details

#==(other) ⇒ Boolean

Check equality of 2 resources. Same as eql?(other)

Parameters:

  • The other resource to check equality for

Returns:

  • Whether or not the two objects are equal



141
142
143
144
145
# File 'lib/oneview-sdk/resource.rb', line 141

def ==(other)
  self_state  = instance_variables.sort.map { |v| instance_variable_get(v) }
  other_state = other.instance_variables.sort.map { |v| other.instance_variable_get(v) }
  other.class == self.class && other_state == self_state
end

#[](key) ⇒ Object

Note:

The key will be converted to a string

Access data using hash syntax

Parameters:

  • Name of key to get value for

Returns:

  • The value of the given key. If not found, returns nil



125
126
127
# File 'lib/oneview-sdk/resource.rb', line 125

def [](key)
  @data[key.to_s]
end

#[]=(key, value) ⇒ Object

Note:

The key will be converted to a string

Set data using hash syntax

Parameters:

  • Name of key to set the value for

  • to set for the given key

Returns:

  • The value set for the given key



134
135
136
# File 'lib/oneview-sdk/resource.rb', line 134

def []=(key, value)
  set(key, value)
end

#create(header = self.class::DEFAULT_REQUEST_HEADER) ⇒ Resource

Note:

Calls the refresh method to set additional data

Create the resource on OneView using the current data

Raises:

  • if the client is not set

  • if the resource creation fails

Parameters:

  • (defaults to: self.class::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • self



172
173
174
175
176
177
178
179
# File 'lib/oneview-sdk/resource.rb', line 172

def create(header = self.class::DEFAULT_REQUEST_HEADER)
  ensure_client
  options = {}.merge(header).merge('body' => @data)
  response = @client.rest_post(self.class::BASE_URI, options, @api_version)
  body = @client.response_handler(response)
  set_all(body)
  self
end

#create!(header = self.class::DEFAULT_REQUEST_HEADER) ⇒ Resource

Note:

Calls refresh method to set additional data

Delete the resource from OneView if it exists, then create it using the current data

Raises:

  • if the client is not set

  • if the resource creation fails

Parameters:

  • (defaults to: self.class::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • self



187
188
189
190
191
# File 'lib/oneview-sdk/resource.rb', line 187

def create!(header = self.class::DEFAULT_REQUEST_HEADER)
  temp = self.class.new(@client, @data)
  temp.delete(header) if temp.retrieve!(header)
  create(header)
end

#deep_merge!(other_data, target_data = @data) ⇒ Object

Note:

both arguments should be a Ruby Hash object. The second hash should have strings as a keys. This method will change the second argument.

Merges the first hash data structure with the second

Raises:

  • if the arguments, or one them, is not a Hash object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/oneview-sdk/resource.rb', line 83

def deep_merge!(other_data, target_data = @data)
  raise 'Both arguments should be a object Hash' unless other_data.is_a?(Hash) && target_data.is_a?(Hash)
  other_data.each do |key, value|
    value_target = target_data[key.to_s]
    if value_target.is_a?(Hash) && value.is_a?(Hash)
      deep_merge!(value, value_target)
    else
      target_data[key.to_s] = value
    end
  end
end

#delete(header = self.class::DEFAULT_REQUEST_HEADER) ⇒ true

Delete resource from OneView

Parameters:

  • (defaults to: self.class::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • if resource was deleted successfully



223
224
225
226
227
228
# File 'lib/oneview-sdk/resource.rb', line 223

def delete(header = self.class::DEFAULT_REQUEST_HEADER)
  ensure_client && ensure_uri
  response = @client.rest_delete(@data['uri'], header, @api_version)
  @client.response_handler(response)
  true
end

#each(&block) ⇒ Object

Run block once for each data key-value pair



117
118
119
# File 'lib/oneview-sdk/resource.rb', line 117

def each(&block)
  @data.each(&block)
end

#eql?(other) ⇒ Boolean

Check equality of 2 resources. Same as ==(other)

Parameters:

  • The other resource to check for equality

Returns:

  • Whether or not the two objects are equal



150
151
152
# File 'lib/oneview-sdk/resource.rb', line 150

def eql?(other)
  self == other
end

#exists?(header = self.class::DEFAULT_REQUEST_HEADER) ⇒ Boolean

Note:

one of the UNIQUE_IDENTIFIERS, e.g. name or uri, must be specified in the resource

Check if a resource exists

Raises:

Parameters:

  • (defaults to: self.class::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • Whether or not resource exists



68
69
70
71
72
73
74
75
76
# File 'lib/oneview-sdk/resource.rb', line 68

def exists?(header = self.class::DEFAULT_REQUEST_HEADER)
  retrieval_keys = self.class::UNIQUE_IDENTIFIERS.reject { |k| @data[k].nil? }
  raise IncompleteResource, "Must set resource #{self.class::UNIQUE_IDENTIFIERS.join(' or ')} before trying to retrieve!" if retrieval_keys.empty?
  retrieval_keys.each do |k|
    results = self.class.find_by(@client, { k => @data[k] }, self.class::BASE_URI, header)
    return true if results.size == 1
  end
  false
end

#like?(other) ⇒ Boolean

Note:

Does not check the client, logger, or api_version if another resource is passed in

Check the equality of the data for the other resource with this resource.

Examples:

Compare to hash

myResource = OneviewSDK::Resource.new(client, { name: 'res1', description: 'example'}, 200)
myResource.like?(description: '') # returns false
myResource.like?(name: 'res1') # returns true

Parameters:

  • resource or hash to compare the key-value pairs with

Returns:

  • Whether or not the two objects are alike



162
163
164
# File 'lib/oneview-sdk/resource.rb', line 162

def like?(other)
  recursive_like?(other, @data)
end

#refresh(header = self.class::DEFAULT_REQUEST_HEADER) ⇒ Resource

Note:

Will overwrite any data that differs from OneView

Updates this object using the data that exists on OneView

Parameters:

  • (defaults to: self.class::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • self



197
198
199
200
201
202
203
# File 'lib/oneview-sdk/resource.rb', line 197

def refresh(header = self.class::DEFAULT_REQUEST_HEADER)
  ensure_client && ensure_uri
  response = @client.rest_get(@data['uri'], header, @api_version)
  body = @client.response_handler(response)
  set_all(body)
  self
end

#retrieve!(header = self.class::DEFAULT_REQUEST_HEADER) ⇒ Boolean

Note:

one of the UNIQUE_IDENTIFIERS, e.g. name or uri, must be specified in the resource

Retrieve resource details based on this resource’s name or URI.

Raises:

Parameters:

  • (defaults to: self.class::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • Whether or not retrieve was successful



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oneview-sdk/resource.rb', line 52

def retrieve!(header = self.class::DEFAULT_REQUEST_HEADER)
  retrieval_keys = self.class::UNIQUE_IDENTIFIERS.reject { |k| @data[k].nil? }
  raise IncompleteResource, "Must set resource #{self.class::UNIQUE_IDENTIFIERS.join(' or ')} before trying to retrieve!" if retrieval_keys.empty?
  retrieval_keys.each do |k|
    results = self.class.find_by(@client, { k => @data[k] }, self.class::BASE_URI, header)
    next if results.size != 1
    set_all(results[0].data)
    return true
  end
  false
end

#schemaHash

Note:

This may not be implemented in the API for every resource. Check the API docs

Get resource schema

Returns:

  • Schema



252
253
254
# File 'lib/oneview-sdk/resource.rb', line 252

def schema
  self.class.schema(@client)
end

#set(key, value) ⇒ Object

Note:

Keys will be converted to strings

Set a resource attribute with the given value and call any validation method if necessary

Parameters:

  • attribute name

  • value to assign to the given attribute



110
111
112
113
114
# File 'lib/oneview-sdk/resource.rb', line 110

def set(key, value)
  method_name = "validate_#{key}"
  send(method_name.to_sym, value) if respond_to?(method_name.to_sym)
  @data[key.to_s] = value
end

#set_all(params = self.class::DEFAULT_REQUEST_HEADER) ⇒ Resource

Note:

All top-level keys will be converted to strings

Set the given hash of key-value pairs as resource data attributes

Parameters:

  • (defaults to: self.class::DEFAULT_REQUEST_HEADER)

    The options for this resource (key-value pairs or resource object)

Returns:

  • self



99
100
101
102
103
104
# File 'lib/oneview-sdk/resource.rb', line 99

def set_all(params = self.class::DEFAULT_REQUEST_HEADER)
  params = params.data if params.class <= Resource
  params = Hash[params.map { |(k, v)| [k.to_s, v] }]
  params.each { |key, value| set(key.to_s, value) }
  self
end

#to_file(file_path, format = :json) ⇒ True

Note:

If a .yml or .yaml file extension is given in the file_path, the format will be set automatically

Save resource to json or yaml file

Parameters:

  • The full path to the file

  • (defaults to: :json)

    The format. Options: [:json, :yml, :yaml]. Defaults to .json

Returns:

  • The Resource was saved successfully



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/oneview-sdk/resource.rb', line 235

def to_file(file_path, format = :json)
  format = :yml if %w[.yml .yaml].include? File.extname(file_path)
  temp_data = { type: self.class.name, api_version: @api_version, data: @data }
  case format.to_sym
  when :json
    File.open(file_path, 'w') { |f| f.write(JSON.pretty_generate(temp_data)) }
  when :yml, :yaml
    File.open(file_path, 'w') { |f| f.write(temp_data.to_yaml) }
  else
    raise InvalidFormat, "Invalid format: #{format}"
  end
  true
end

#update(attributes = {}, header = self.class::DEFAULT_REQUEST_HEADER) ⇒ Resource

Set data and save to OneView

Raises:

  • if the client or uri is not set

  • if the resource save fails

Parameters:

  • (defaults to: {})

    The attributes to add/change for this resource (key-value pairs)

  • (defaults to: self.class::DEFAULT_REQUEST_HEADER)

    The header options for the request (key-value pairs)

Returns:

  • self



211
212
213
214
215
216
217
218
# File 'lib/oneview-sdk/resource.rb', line 211

def update(attributes = {}, header = self.class::DEFAULT_REQUEST_HEADER)
  set_all(attributes)
  ensure_client && ensure_uri
  options = {}.merge(header).merge('body' => @data)
  response = @client.rest_put(@data['uri'], options, @api_version)
  @client.response_handler(response)
  self
end