Class: RackspaceCloud::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
# File 'lib/rackspace_cloud/base.rb', line 5

def initialize(config={})
  validate_config(config)
  @user = config[:user]
  @access_key = config[:access_key]
  @authorized = false
end

Instance Attribute Details

#access_keyObject (readonly)

Returns the value of attribute access_key.



3
4
5
# File 'lib/rackspace_cloud/base.rb', line 3

def access_key
  @access_key
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



3
4
5
# File 'lib/rackspace_cloud/base.rb', line 3

def auth_token
  @auth_token
end

#flavorsObject (readonly)

storage for the lists of flavors and images we request at auth time



26
27
28
# File 'lib/rackspace_cloud/base.rb', line 26

def flavors
  @flavors
end

#imagesObject (readonly)

Returns the value of attribute images.



35
36
37
# File 'lib/rackspace_cloud/base.rb', line 35

def images
  @images
end

#limitsObject (readonly)

Returns the value of attribute limits.



44
45
46
# File 'lib/rackspace_cloud/base.rb', line 44

def limits
  @limits
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/rackspace_cloud/base.rb', line 3

def user
  @user
end

Instance Method Details

#api_versionObject



115
116
117
# File 'lib/rackspace_cloud/base.rb', line 115

def api_version
  RackspaceCloud::API_VERSION
end

#authorized?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rackspace_cloud/base.rb', line 21

def authorized?
  @authorized
end

#connectObject



12
13
14
15
16
17
18
19
# File 'lib/rackspace_cloud/base.rb', line 12

def connect
  configure_service_urls(RackspaceCloud.request_authorization(@user, @access_key))
  RackspaceCloud.check_version_compatibility
  @authorized = true
  populate_flavors
  populate_images
  get_limits
end

#create_server(name, flavor, image, metadata = {}, personality = []) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rackspace_cloud/base.rb', line 57

def create_server(name, flavor, image, ={}, personality=[])
  new_server_data = {'server' => {
    'name' => name,
    'flavorId' => flavor.to_i,
    'imageId'  => image.to_i,
    'metadata' => ,
    'personality' => personality
  }}
  
  RackspaceCloud::Server.new(self, request("/servers", :method => :post, :data => new_server_data)['server'])
end

#create_shared_ip_group(name, server = nil) ⇒ Object

Yes, you can only specify at most a single server to initially populate a shared IP group. Odd, that.



76
77
78
79
80
# File 'lib/rackspace_cloud/base.rb', line 76

def create_shared_ip_group(name, server=nil)
  new_group_data = {'sharedIpGroup' => {'name' => name, 'server' => server}}
  new_group_data['sharedIpGroup']['server'] = server.to_i unless server.nil?
  RackspaceCloud::SharedIPGroup.new(self, request("/shared_ip_groups", :method => :post, :data => new_group_data))
end

#get_limitsObject



45
46
47
48
49
# File 'lib/rackspace_cloud/base.rb', line 45

def get_limits
  @limits ||= {}
  @limits.merge!(request("/limits")['limits'])
  nil
end

#populate_flavorsObject



27
28
29
30
31
32
33
# File 'lib/rackspace_cloud/base.rb', line 27

def populate_flavors
  @flavors ||= {}
  request("/flavors/detail")['flavors'].each do |flavor|
    @flavors[flavor['id']] = RackspaceCloud::Flavor.new(flavor)
  end
  nil
end

#populate_imagesObject



36
37
38
39
40
41
42
# File 'lib/rackspace_cloud/base.rb', line 36

def populate_images
  @images ||= {}
  request("/images/detail")['images'].each do |image|
    @images[image['id']] = RackspaceCloud::Image.new(self, image)
  end
  nil
end

#request(path, options = {}) ⇒ Object

Raises:

  • (RuntimeError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rackspace_cloud/base.rb', line 82

def request(path, options={})
  raise RuntimeError, "Please authorize before using by calling connect()" unless authorized?
  @session ||= begin
    s = Patron::Session.new
    s.base_url = @server_management_url
    s.headers['X-Auth-Token'] = @auth_token
    s.headers["User-Agent"] = "rackspacecloud_ruby_gem"
    s.timeout = 10
    s
  end
  response = case options[:method]
  when :post
    @session.headers['Accept'] = "application/json"
    @session.headers['Content-Type'] = "application/json"
    @session.post("#{path}", options[:data].to_json)
  when :put
    @session.headers['Content-Type'] = "application/json"
    @session.put("#{path}", options[:data].to_json)      
  when :delete
    @session.delete("#{path}")
  else      
    @session.get("#{path}.json")
  end

  case response.status
  when 200, 201, 202, 204
    JSON.parse(response.body) unless response.body.empty?
  else
    puts response.body
    raise RuntimeError, "Error fetching #{path}: #{response.status}"
  end
end

#serversObject



51
52
53
54
55
# File 'lib/rackspace_cloud/base.rb', line 51

def servers
  request("/servers/detail")["servers"].collect {|server_json|
    RackspaceCloud::Server.new(self, server_json)
  }
end

#shared_ip_groupsObject



69
70
71
72
73
# File 'lib/rackspace_cloud/base.rb', line 69

def shared_ip_groups
  request("/shared_ip_groups/detail")['sharedIpGroups'].collect {|group_json|
    RackspaceCloud::SharedIPGroup.new(self, group_json)
  }
end