Class: OneviewSDK::Resource
- Inherits:
-
Object
- Object
- OneviewSDK::Resource
- Defined in:
- lib/oneview-sdk/resource.rb
Overview
Resource base class that defines all common resource functionality.
Direct Known Subclasses
API200::Fabric, API200::Rack, API200::Resource, API200::Switch, API200::User, API200::Volume, API300::C7000::Resource, API300::C7000::Scope, API300::Synergy::Resource, ImageStreamer::API300::Resource
Constant Summary collapse
- BASE_URI =
'/rest'.freeze
- UNIQUE_IDENTIFIERS =
Ordered list of unique attributes to search by
%w(name uri).freeze
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#client ⇒ Object
Returns the value of attribute client.
-
#data ⇒ Object
Returns the value of attribute data.
-
#logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
-
.build_query(query_options) ⇒ Object
Builds a Query string corresponding to the parameters passed.
-
.find_by(client, attributes, uri = self::BASE_URI) ⇒ Array<Resource>
Make a GET request to the resource uri, and returns an array with results matching the search.
-
.find_with_pagination(client, uri) ⇒ Array<Hash>
Make a GET request to the uri, and returns an array with all results (search using resource pagination).
-
.from_file(client, file_path) ⇒ Resource
Load resource from a .json or .yaml file.
-
.get_all(client) ⇒ Array<Resource>
Make a GET request to the resource base uri, and returns an array with all objects of this type.
-
.schema(client) ⇒ Hash
Get resource schema.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check equality of 2 resources.
-
#[](key) ⇒ Object
Access data using hash syntax.
-
#[]=(key, value) ⇒ Object
Set data using hash syntax.
-
#create ⇒ Resource
Create the resource on OneView using the current data.
-
#create! ⇒ Resource
Delete the resource from OneView if it exists, then create it using the current data.
-
#delete ⇒ true
Delete resource from OneView.
-
#each(&block) ⇒ Object
Run block once for each data key-value pair.
-
#eql?(other) ⇒ Boolean
Check equality of 2 resources.
-
#exists? ⇒ Boolean
Check if a resource exists.
-
#initialize(client, params = {}, api_ver = nil) ⇒ Resource
constructor
Create a resource object, associate it with a client, and set its properties.
-
#like?(other) ⇒ Boolean
Check the equality of the data for the other resource with this resource.
-
#refresh ⇒ Resource
Updates this object using the data that exists on OneView.
-
#retrieve! ⇒ Boolean
Retrieve resource details based on this resource’s name or URI.
-
#schema ⇒ Hash
Get resource schema.
-
#set(key, value) ⇒ Object
Set a resource attribute with the given value and call any validation method if necessary.
-
#set_all(params = {}) ⇒ Resource
Set the given hash of key-value pairs as resource data attributes.
-
#to_file(file_path, format = :json) ⇒ True
Save resource to json or yaml file.
-
#update(attributes = {}) ⇒ Resource
Set data and save to OneView.
Constructor Details
#initialize(client, params = {}, api_ver = nil) ⇒ Resource
Create a resource object, associate it with a client, and set its properties.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/oneview-sdk/resource.rb', line 33 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_version ⇒ Object
Returns the value of attribute api_version.
22 23 24 |
# File 'lib/oneview-sdk/resource.rb', line 22 def api_version @api_version end |
#client ⇒ Object
Returns the value of attribute client.
22 23 24 |
# File 'lib/oneview-sdk/resource.rb', line 22 def client @client end |
#data ⇒ Object
Returns the value of attribute data.
22 23 24 |
# File 'lib/oneview-sdk/resource.rb', line 22 def data @data end |
#logger ⇒ Object
Returns the value of attribute logger.
22 23 24 |
# File 'lib/oneview-sdk/resource.rb', line 22 def logger @logger end |
Class Method Details
.build_query(query_options) ⇒ Object
Builds a Query string corresponding to the parameters passed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/oneview-sdk/resource.rb', line 306 def self.build_query() return '' if ! || .empty? query_path = '?' .each do |k, v| words = k.to_s.split('_') words.map!(&:capitalize!) words[0] = words.first.downcase new_key = words.join 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) ⇒ Array<Resource>
Make a GET request to the resource uri, and returns an array with results matching the search
264 265 266 267 268 269 270 271 272 |
# File 'lib/oneview-sdk/resource.rb', line 264 def self.find_by(client, attributes, uri = self::BASE_URI) all = find_with_pagination(client, uri) results = [] all.each do |member| temp = new(client, member) results.push(temp) if temp.like?(attributes) end results end |
.find_with_pagination(client, uri) ⇒ Array<Hash>
Make a GET request to the uri, and returns an array with all results (search using resource pagination)
278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/oneview-sdk/resource.rb', line 278 def self.find_with_pagination(client, uri) all = [] loop do response = client.rest_get(uri) 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
245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/oneview-sdk/resource.rb', line 245 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) ⇒ Array<Resource>
Make a GET request to the resource base uri, and returns an array with all objects of this type
294 295 296 |
# File 'lib/oneview-sdk/resource.rb', line 294 def self.get_all(client) find_by(client, {}) end |
.schema(client) ⇒ Hash
Get resource schema
233 234 235 236 237 238 239 |
# File 'lib/oneview-sdk/resource.rb', line 233 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. =~ /404 NOT FOUND/ raise e end |
Instance Method Details
#==(other) ⇒ Boolean
Check equality of 2 resources. Same as eql?(other)
122 123 124 125 126 |
# File 'lib/oneview-sdk/resource.rb', line 122 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
The key will be converted to a string
Access data using hash syntax
105 106 107 |
# File 'lib/oneview-sdk/resource.rb', line 105 def [](key) @data[key.to_s] end |
#[]=(key, value) ⇒ Object
The key will be converted to a string
Set data using hash syntax
114 115 116 117 |
# File 'lib/oneview-sdk/resource.rb', line 114 def []=(key, value) set(key, value) value end |
#create ⇒ Resource
Calls the refresh method to set additional data
Create the resource on OneView using the current data
152 153 154 155 156 157 158 |
# File 'lib/oneview-sdk/resource.rb', line 152 def create ensure_client response = @client.rest_post(self.class::BASE_URI, { 'body' => @data }, @api_version) body = @client.response_handler(response) set_all(body) self end |
#create! ⇒ Resource
Calls refresh method to set additional data
Delete the resource from OneView if it exists, then create it using the current data
165 166 167 168 169 |
# File 'lib/oneview-sdk/resource.rb', line 165 def create! temp = self.class.new(@client, @data) temp.delete if temp.retrieve! create end |
#delete ⇒ true
Delete resource from OneView
197 198 199 200 201 202 |
# File 'lib/oneview-sdk/resource.rb', line 197 def delete ensure_client && ensure_uri response = @client.rest_delete(@data['uri'], {}, @api_version) @client.response_handler(response) true end |
#each(&block) ⇒ Object
Run block once for each data key-value pair
97 98 99 |
# File 'lib/oneview-sdk/resource.rb', line 97 def each(&block) @data.each(&block) end |
#eql?(other) ⇒ Boolean
Check equality of 2 resources. Same as ==(other)
131 132 133 |
# File 'lib/oneview-sdk/resource.rb', line 131 def eql?(other) self == other end |
#exists? ⇒ Boolean
one of the UNIQUE_IDENTIFIERS, e.g. name or uri, must be specified in the resource
Check if a resource exists
65 66 67 68 69 70 71 72 73 |
# File 'lib/oneview-sdk/resource.rb', line 65 def exists? retrieval_keys = self.class::UNIQUE_IDENTIFIERS.select { |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]) return true if results.size == 1 end false end |
#like?(other) ⇒ Boolean
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.
143 144 145 |
# File 'lib/oneview-sdk/resource.rb', line 143 def like?(other) recursive_like?(other, @data) end |
#refresh ⇒ Resource
Will overwrite any data that differs from OneView
Updates this object using the data that exists on OneView
174 175 176 177 178 179 180 |
# File 'lib/oneview-sdk/resource.rb', line 174 def refresh ensure_client && ensure_uri response = @client.rest_get(@data['uri'], @api_version) body = @client.response_handler(response) set_all(body) self end |
#retrieve! ⇒ Boolean
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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/oneview-sdk/resource.rb', line 50 def retrieve! retrieval_keys = self.class::UNIQUE_IDENTIFIERS.select { |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]) next if results.size != 1 set_all(results[0].data) return true end false end |
#schema ⇒ Hash
This may not be implemented in the API for every resource. Check the API docs
Get resource schema
226 227 228 |
# File 'lib/oneview-sdk/resource.rb', line 226 def schema self.class.schema(@client) end |
#set(key, value) ⇒ Object
Keys will be converted to strings
Set a resource attribute with the given value and call any validation method if necessary
90 91 92 93 94 |
# File 'lib/oneview-sdk/resource.rb', line 90 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 = {}) ⇒ Resource
All top-level keys will be converted to strings
Set the given hash of key-value pairs as resource data attributes
79 80 81 82 83 84 |
# File 'lib/oneview-sdk/resource.rb', line 79 def set_all(params = {}) 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
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
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/oneview-sdk/resource.rb', line 209 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 = {}) ⇒ Resource
Set data and save to OneView
187 188 189 190 191 192 193 |
# File 'lib/oneview-sdk/resource.rb', line 187 def update(attributes = {}) set_all(attributes) ensure_client && ensure_uri response = @client.rest_put(@data['uri'], { 'body' => @data }, @api_version) @client.response_handler(response) self end |