Class: DeltacloudVM::Client::Base

Inherits:
Object
  • Object
show all
Extended by:
Helpers::XmlHelper
Includes:
Helpers::Model, Methods::Api
Defined in:
lib/deltacloud_vm/client/models/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::XmlHelper

extract_xml_body

Methods included from Methods::Api

#api_uri, #current_driver, #current_provider, #feature?, #features, #must_support!, #path, #support?, #supported_collections, #version

Methods included from Helpers::Model

#error, #from_collection, #from_resource, #model

Constructor Details

#initialize(opts = {}) ⇒ Base

The Base class that other models should inherit from To initialize, you need to supply these mandatory params:

  • :_client -> Reference to Client instance

  • :_id -> The ‘id’ of resource. The ‘_’ is there to avoid conflicts



40
41
42
43
44
45
46
47
48
# File 'lib/deltacloud_vm/client/models/base.rb', line 40

def initialize(opts={})
  @options = opts
  @obj_id = @options.delete(:_id)
  # Do not allow to modify the object#base_id
  @obj_id.freeze
  @client = @options.delete(:_client)
  @original_body = @options.delete(:original_body)
  update_instance_variables!(@options)
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



32
33
34
# File 'lib/deltacloud_vm/client/models/base.rb', line 32

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/deltacloud_vm/client/models/base.rb', line 31

def name
  @name
end

#obj_idObject (readonly) Also known as: _id

These attributes are common for all models

  • obj_id -> The :id of DeltacloudVM API model (eg. instance ID)



29
30
31
# File 'lib/deltacloud_vm/client/models/base.rb', line 29

def obj_id
  @obj_id
end

#urlObject (readonly)

Returns the value of attribute url.



30
31
32
# File 'lib/deltacloud_vm/client/models/base.rb', line 30

def url
  @url
end

Class Method Details

.convert(client_ref, obj) ⇒ Object

Convert the parsed Hash from parse method to instance of DeltacloudVM model

  • client_ref -> Reference to the Client instance

  • obj -> Might be a Nokogiri::Element or Response



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/deltacloud_vm/client/models/base.rb', line 121

def convert(client_ref, obj)
  body = extract_xml_body(obj).to_xml.root
  attrs = parse(body)
  attrs.merge!({
    :_id => body['id'],
    :url => body['href'],
    :_client => client_ref,
    :name => body.text_at(:name),
    :description => body.text_at(:description)
  })
  validate_attrs!(attrs)
  new(attrs.merge(:original_body => obj))
end

.from_collection(client_ref, response) ⇒ Object

Convert response for the collection responses.



137
138
139
140
141
# File 'lib/deltacloud_vm/client/models/base.rb', line 137

def from_collection(client_ref, response)
  response.body.to_xml.xpath('/*/*').map do |entity|
    convert(client_ref, entity)
  end
end

.parse(client_ref, inst) ⇒ Object

Parse the XML response body from DeltacloudVM API to Hash. Result is then used to create an instance of DeltacloudVM model

NOTE: Children classes must implement this class method



111
112
113
114
# File 'lib/deltacloud_vm/client/models/base.rb', line 111

def parse(client_ref, inst)
  warn "The self#parse method **must** be defined in #{self.class.name}"
  {}
end

.validate_attrs!(attrs) ⇒ Object

The :_id and :_client attributes are mandotory to construct a Base model object.

Raises:



146
147
148
149
# File 'lib/deltacloud_vm/client/models/base.rb', line 146

def validate_attrs!(attrs)
  raise error.new('The :_id must not be nil.') if attrs[:_id].nil?
  raise error.new('The :_client reference is missing.') if attrs[:_client].nil?
end

Instance Method Details

#clientObject

An internal reference to the current DeltacloudVM::Client::Connection instance. Used for implementing the model methods



71
72
73
# File 'lib/deltacloud_vm/client/models/base.rb', line 71

def client
  @client
end

#connectionObject

Shorthand for client.connection

Return Faraday connection object.



79
80
81
# File 'lib/deltacloud_vm/client/models/base.rb', line 79

def connection
  client.connection
end

#entrypointObject

Return the cached version of DeltacloudVM API entrypoint



85
86
87
# File 'lib/deltacloud_vm/client/models/base.rb', line 85

def entrypoint
  client.entrypoint
end

#idObject

The model#id is the old way for getting the DeltacloudVM API resource ‘id’. However this collide with the Ruby Object#id.



99
100
101
102
# File 'lib/deltacloud_vm/client/models/base.rb', line 99

def id
  warn '[DEPRECATION] `id` is deprecated because of a possible conflict with Object#id. Use `_id` instead.'
  _id
end

#original_bodyObject

Return the original XML body model was constructed from This might help debugging broken XML



92
93
94
# File 'lib/deltacloud_vm/client/models/base.rb', line 92

def original_body
  @original_body
end

#to_sObject

Eye-candy representation of model, without ugly @client representation



64
65
66
# File 'lib/deltacloud_vm/client/models/base.rb', line 64

def to_s
  "#<#{self.class.name}> #{@options.merge(:_id => @obj_id).inspect}"
end

#update_instance_variables!(opts = {}) ⇒ Object

Populate instance variables in model This method could also be used to update the variables for already initialized models. Look at Instance#reload! method.



56
57
58
59
60
# File 'lib/deltacloud_vm/client/models/base.rb', line 56

def update_instance_variables!(opts={})
  @options.merge!(opts)
  @options.each { |key, val| self.instance_variable_set("@#{key}", val) unless val.nil? }
  self
end