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
Enclosure, EnclosureGroup, EthernetNetwork, FCNetwork, FCoENetwork, FirmwareDriver, Interconnect, LIGUplinkSet, LogicalEnclosure, LogicalInterconnect, LogicalInterconnectGroup, ServerHardware, ServerHardwareType, ServerProfile, ServerProfileTemplate, StoragePool, StorageSystem, UplinkSet, Volume, VolumeSnapshot, VolumeTemplate
Constant Summary collapse
- BASE_URI =
'/rest'.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
-
.find_by(client, attributes, uri = self::BASE_URI) ⇒ Array<Resource>
Make a GET request to the resource uri and return an array with results matching the search.
-
.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 return 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 equality of data on other resource with that of 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.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/oneview-sdk/resource.rb', line 20 def initialize(client, params = {}, api_ver = nil) fail 'Must specify a valid client' unless client.is_a?(OneviewSDK::Client) @client = client @logger = @client.logger @api_version = api_ver || @client.api_version if @api_version > @client.max_api_version fail "#{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.
9 10 11 |
# File 'lib/oneview-sdk/resource.rb', line 9 def api_version @api_version end |
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/oneview-sdk/resource.rb', line 9 def client @client end |
#data ⇒ Object
Returns the value of attribute data.
9 10 11 |
# File 'lib/oneview-sdk/resource.rb', line 9 def data @data end |
#logger ⇒ Object
Returns the value of attribute logger.
9 10 11 |
# File 'lib/oneview-sdk/resource.rb', line 9 def logger @logger end |
Class Method Details
.find_by(client, attributes, uri = self::BASE_URI) ⇒ Array<Resource>
Make a GET request to the resource uri and return an array with results matching the search
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/oneview-sdk/resource.rb', line 234 def self.find_by(client, attributes, uri = self::BASE_URI) results = [] loop do response = client.rest_get(uri) body = client.response_handler(response) members = body['members'] break unless members members.each do |member| temp = new(client, member) results.push(temp) if temp.like?(attributes) end break unless body['nextPageUri'] uri = body['nextPageUri'] end results end |
.from_file(client, file_path) ⇒ Resource
Load resource from a .json or .yaml file
224 225 226 227 |
# File 'lib/oneview-sdk/resource.rb', line 224 def self.from_file(client, file_path) resource = OneviewSDK::Config.load(file_path) new(client, resource['data'], resource['api_version']) end |
.get_all(client) ⇒ Array<Resource>
Make a GET request to the resource base uri and return an array with all objects of this type
253 254 255 |
# File 'lib/oneview-sdk/resource.rb', line 253 def self.get_all(client) find_by(client, {}) end |
.schema(client) ⇒ Hash
Get resource schema
212 213 214 215 216 217 218 |
# File 'lib/oneview-sdk/resource.rb', line 212 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)
101 102 103 104 105 |
# File 'lib/oneview-sdk/resource.rb', line 101 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
84 85 86 |
# File 'lib/oneview-sdk/resource.rb', line 84 def [](key) @data[key.to_s] end |
#[]=(key, value) ⇒ Object
The key will be converted to a string
Set data using hash syntax
93 94 95 96 |
# File 'lib/oneview-sdk/resource.rb', line 93 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
131 132 133 134 135 136 137 |
# File 'lib/oneview-sdk/resource.rb', line 131 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
144 145 146 147 148 |
# File 'lib/oneview-sdk/resource.rb', line 144 def create! temp = self.class.new(@client, @data) temp.delete if temp.retrieve! create end |
#delete ⇒ true
Delete resource from OneView
176 177 178 179 180 181 |
# File 'lib/oneview-sdk/resource.rb', line 176 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
76 77 78 |
# File 'lib/oneview-sdk/resource.rb', line 76 def each(&block) @data.each(&block) end |
#eql?(other) ⇒ Boolean
Check equality of 2 resources. Same as ==(other)
110 111 112 |
# File 'lib/oneview-sdk/resource.rb', line 110 def eql?(other) self == other end |
#exists? ⇒ Boolean
name or uri must be specified inside resource
Check if a resource exists
47 48 49 50 51 52 |
# File 'lib/oneview-sdk/resource.rb', line 47 def exists? fail 'Must set resource name or uri before trying to retrieve!' unless @data['name'] || @data['uri'] return true if @data['name'] && self.class.find_by(@client, name: @data['name']).size == 1 return true if @data['uri'] && self.class.find_by(@client, uri: @data['uri']).size == 1 false end |
#like?(other) ⇒ Boolean
Doesn’t check the client, logger, or api_version if another Resource is passed in
Check equality of data on other resource with that of this resource.
122 123 124 |
# File 'lib/oneview-sdk/resource.rb', line 122 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
153 154 155 156 157 158 159 |
# File 'lib/oneview-sdk/resource.rb', line 153 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
Name or URI must be specified inside resource
Retrieve resource details based on this resource’s name or URI.
35 36 37 38 39 40 41 42 |
# File 'lib/oneview-sdk/resource.rb', line 35 def retrieve! fail 'Must set resource name or uri before trying to retrieve!' unless @data['name'] || @data['uri'] results = self.class.find_by(@client, name: @data['name']) if @data['name'] results = self.class.find_by(@client, uri: @data['uri']) if @data['uri'] && (!results || results.empty?) return false unless results.size == 1 set_all(results[0].data) true end |
#schema ⇒ Hash
This may not be implemented in the API for every resource. Check the API docs
Get resource schema
205 206 207 |
# File 'lib/oneview-sdk/resource.rb', line 205 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
69 70 71 72 73 |
# File 'lib/oneview-sdk/resource.rb', line 69 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
58 59 60 61 62 63 |
# File 'lib/oneview-sdk/resource.rb', line 58 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
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/oneview-sdk/resource.rb', line 188 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 fail "Invalid format: #{format}" end true end |
#update(attributes = {}) ⇒ Resource
Set data and save to OneView
166 167 168 169 170 171 172 |
# File 'lib/oneview-sdk/resource.rb', line 166 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 |