Class: Rackspace::CloudServers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rackspace/cloud_servers/base.rb

Direct Known Subclasses

Flavor, Image, Server

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribs = {}) ⇒ Base

The resource can be established with a hash of parameters



5
6
7
# File 'lib/rackspace/cloud_servers/base.rb', line 5

def initialize(attribs = {})
  set_attributes(attribs)
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/rackspace/cloud_servers/base.rb', line 2

def id
  @id
end

Class Method Details

.allObject

This returns all records for the resource



80
81
82
# File 'lib/rackspace/cloud_servers/base.rb', line 80

def all
  self.find
end

.countObject

This returns the amount of records for this resource



121
122
123
124
# File 'lib/rackspace/cloud_servers/base.rb', line 121

def count
  records = self.all
  records.nil? ? 0 : records.length
end

.create(attribs = {}) ⇒ Object

This creates and saves a resource with the specified attributes in one call



127
128
129
130
131
# File 'lib/rackspace/cloud_servers/base.rb', line 127

def create(attribs = {})
  o = self.new(attribs)
  o.save
  o
end

.find(action = :all) ⇒ Object

This finds all records for the resource, or a specific resource by ID



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rackspace/cloud_servers/base.rb', line 95

def find(action = :all)
  result = case action
  when :all
    Rackspace::Connection.get "#{self.resource_url}/detail"
  when :first
    Rackspace::Connection.get "#{self.resource_url}/detail"
  when :last
    Rackspace::Connection.get "#{self.resource_url}/detail"
  else
    Rackspace::Connection.get self.resource_url(action)
  end
  return nil if result.to_s.blank?
  json = JSON.parse(result.to_s)
  case action
  when :all
    json[self.resource].collect { |h| self.new(h) }
  when :first
    json[self.resource].collect { |h| self.new(h) }.first
  when :last
    json[self.resource].collect { |h| self.new(h) }.last
  else
    self.new(json[self.resource.singularize])
  end
end

.firstObject

This returns the first record for the resource



85
86
87
# File 'lib/rackspace/cloud_servers/base.rb', line 85

def first
  self.find(:first)
end

.lastObject

This returns the last record for the resource



90
91
92
# File 'lib/rackspace/cloud_servers/base.rb', line 90

def last
  self.find(:last)
end

.resourceObject

This returns the name of the resource, used for the API URLs



69
70
71
# File 'lib/rackspace/cloud_servers/base.rb', line 69

def resource
  self.name.split("::").last.tableize
end

.resource_url(id = nil) ⇒ Object

This returns the resource URL



74
75
76
77
# File 'lib/rackspace/cloud_servers/base.rb', line 74

def resource_url(id = nil)
  root = "#{Rackspace::Connection.server_management_url}/#{self.resource}"
  id.nil? ? root : "#{root}/#{id}"
end

Instance Method Details

#attributes_for_updateObject

These are the attributes used for the update operations Empty array means no properties will be updated, but this can be overridden with nil (all properties are updated), or an explicit array of properties that can be updated



53
54
55
# File 'lib/rackspace/cloud_servers/base.rb', line 53

def attributes_for_update
  []
end

#createObject

This creates the new record using the API



27
28
29
30
# File 'lib/rackspace/cloud_servers/base.rb', line 27

def create
  set_attributes(JSON.parse(Rackspace::Connection.post(self.class.resource_url, {self.class.resource.singularize => JSON.parse(self.to_json)}))[self.class.resource.singularize])
  true
end

#destroyObject

This deletes the record using the API



45
46
47
48
# File 'lib/rackspace/cloud_servers/base.rb', line 45

def destroy
  Rackspace::Connection.delete(self.class.resource_url(self.id))
  true
end

#new_record?Boolean

This returns true if the record hasn’t yet been persisted, false otherwise

Returns:

  • (Boolean)


17
18
19
# File 'lib/rackspace/cloud_servers/base.rb', line 17

def new_record?
  self.id.nil?
end

#reloadObject

This reloads the current object with the latest persisted data



58
59
60
61
62
63
64
65
# File 'lib/rackspace/cloud_servers/base.rb', line 58

def reload
  return false if self.new_record?
  result = Rackspace::Connection.get(self.class.resource_url(self.id))
  return nil if result.to_s.blank?
  json = JSON.parse(result.to_s)
  self.set_attributes(json[self.class.resource.singularize])
  self
end

#saveObject

This creates the record if it is new, otherwise it attempts to update the record



22
23
24
# File 'lib/rackspace/cloud_servers/base.rb', line 22

def save
  self.new_record? ? create : update
end

#set_attributes(attribs) ⇒ Object

This sets the relevant accessors using the specified attributes



10
11
12
13
14
# File 'lib/rackspace/cloud_servers/base.rb', line 10

def set_attributes(attribs)
  attribs.each_pair do |key, value|
    self.send("#{key}=", value) if self.respond_to?("#{key}=")
  end
end

#updateObject

This updates the existing record using the API



33
34
35
36
37
38
39
40
41
42
# File 'lib/rackspace/cloud_servers/base.rb', line 33

def update
  attribs = JSON.parse(self.to_json)
  unless self.attributes_for_update.nil?
    attribs.keys.each do |key|
      attribs.delete(key) unless self.attributes_for_update.include?(key.to_sym)
    end
  end
  Rackspace::Connection.put(self.class.resource_url(self.id), {self.class.resource.singularize => attribs})
  true
end