Class: Deltacloud::Drivers::Rackspace::RackspaceClient

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud/drivers/rackspace/rackspace_client.rb

Constant Summary collapse

@@AUTH_API =
URI.parse('https://auth.api.rackspacecloud.com/v1.0')

Instance Method Summary collapse

Constructor Details

#initialize(username, auth_key) ⇒ RackspaceClient

Returns a new instance of RackspaceClient.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 36

def initialize(username, auth_key)
  http = Net::HTTP.new(@@AUTH_API.host,@@AUTH_API.port)
  http.use_ssl = true
  authed = http.get(@@AUTH_API.path, {'X-Auth-User' => username, 'X-Auth-Key' => auth_key})
  if authed.is_a?(Net::HTTPUnauthorized)
    raise Deltacloud::AuthException, "Failed to authenticate to Rackspace"
  elsif !authed.is_a?(Net::HTTPSuccess)
    backend_error!(resp)
  end
  @auth_token  = authed.header['X-Auth-Token']
  @service_uri = URI.parse(authed.header['X-Server-Management-Url'])
  @service = Net::HTTP.new(@service_uri.host, @service_uri.port)
  @service.use_ssl = true
end

Instance Method Details

#delete_server(server_id) ⇒ Object



79
80
81
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 79

def delete_server(server_id)
  delete("/servers/#{server_id}", headers)
end

#headersObject



89
90
91
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 89

def headers
  {"Accept" => "application/json", "X-Auth-Token" => @auth_token, "Content-Type" => "application/json"}
end

#list_flavorsObject



51
52
53
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 51

def list_flavors
  JSON.parse(get('/flavors/detail'))['flavors']
end

#list_imagesObject



55
56
57
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 55

def list_images
  JSON.parse(get('/images/detail'))['images']
end

#list_serversObject



59
60
61
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 59

def list_servers
    JSON.parse(get('/servers/detail'))['servers']
end

#load_server_details(server_id) ⇒ Object



64
65
66
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 64

def load_server_details( server_id )
  JSON.parse(get("/servers/#{server_id}"))['server']
end

#reboot_server(server_id) ⇒ Object



83
84
85
86
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 83

def reboot_server(server_id)
  json = { :reboot => { :type => :SOFT }}.to_json
  post("/servers/#{server_id}/action", json, headers)
end

#start_server(image_id, flavor_id, name) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/deltacloud/drivers/rackspace/rackspace_client.rb', line 69

def start_server(image_id, flavor_id, name)
  json = { :server => { :name => name,
                        :imageId => image_id.to_i,
                        :flavorId => flavor_id.to_i }}.to_json
  # FIXME: The response has the root password in 'adminPass'; we somehow
  # need to communicate this back since it's the only place where we can
  # get it from
  JSON.parse(post("/servers", json, headers).body)["server"]
end