Class: Raca::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/raca/account.rb

Overview

This is your entrypoint to the rackspace API. Start by creating a Raca::Account object and then use the instance method to access each of the supported rackspace APIs.

Constant Summary collapse

IDENTITY_URL =
"https://identity.api.rackspacecloud.com/v2.0/"

Instance Method Summary collapse

Constructor Details

#initialize(username, key, cache = nil) ⇒ Account

Returns a new instance of Account.



13
14
15
16
17
18
19
20
# File 'lib/raca/account.rb', line 13

def initialize(username, key, cache = nil)
  @username, @key, @cache = username, key, cache
  @cache ||= if defined?(Rails)
    Rails.cache
  else
    {}
  end
end

Instance Method Details

#auth_tokenObject

Return the temporary token that should be used when making further API requests.

 = Raca::Account.new("username", "secret")
puts .auth_token


28
29
30
# File 'lib/raca/account.rb', line 28

def auth_token
  extract_value(identity_data, "access", "token", "id")
end

#containers(region) ⇒ Object

Return a Raca::Containers object for a region. Use this to interact with the cloud files service.

 = Raca::Account.new("username", "secret")
puts .containers(:ord)


87
88
89
# File 'lib/raca/account.rb', line 87

def containers(region)
  Raca::Containers.new(self, region)
end

#http_client(hostname) ⇒ Object

Return a Raca::HttpClient suitable for making requests to hostname.



143
144
145
# File 'lib/raca/account.rb', line 143

def http_client(hostname)
  Raca::HttpClient.new(self, hostname)
end

#inspectObject



147
148
149
# File 'lib/raca/account.rb', line 147

def inspect
  "#<Raca::Account:#{__id__} username=#{@username}>"
end

#public_endpoint(service_name, region = nil) ⇒ Object

Return the public API URL for a particular rackspace service.

Use Account#service_names to see a list of valid service_name’s for this.

Check the project README for an updated list of the available regions.

 = Raca::Account.new("username", "secret")
puts .public_endpoint("cloudServers", :syd)

Some service APIs are not regioned. In those cases, the region code can be left off:

 = Raca::Account.new("username", "secret")
puts .public_endpoint("cloudDNS")


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/raca/account.rb', line 47

def public_endpoint(service_name, region = nil)
  return IDENTITY_URL if service_name == "identity"

  endpoints = service_endpoints(service_name)
  if endpoints.size > 1 && region
    region = region.to_s.upcase
    endpoints = endpoints.select { |e| e["region"] == region } || {}
  elsif endpoints.size > 1 && region.nil?
    raise ArgumentError, "The requested service exists in multiple regions, please specify a region code"
  end

  if endpoints.size == 0
    raise ArgumentError, "No matching services found"
  else
    endpoints.first["publicURL"]
  end
end

#refresh_cacheObject

Raca classes use this method to occasionally re-authenticate with the rackspace servers. You can probably ignore it.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/raca/account.rb', line 114

def refresh_cache
  # Raca::HttpClient depends on Raca::Account, so we intentionally don't use it here
  # to avoid a circular dependency
  Net::HTTP.new(identity_host, 443).tap {|http|
    http.use_ssl = true
  }.start {|http|
    payload = {
      auth: {
        'RAX-KSKEY:apiKeyCredentials' => {
          username: @username,
          apiKey: @key
        }
      }
    }
    response = http.post(
      tokens_path,
      JSON.dump(payload),
      {'Content-Type' => 'application/json'},
    )
    if response.is_a?(Net::HTTPSuccess)
      cache_write(cache_key, JSON.load(response.body))
    else
      raise_on_error(response)
    end
  }
end

#servers(region) ⇒ Object

Return a Raca::Servers object for a region. Use this to interact with the next gen cloud servers service.

 = Raca::Account.new("username", "secret")
puts .servers(:ord)


97
98
99
# File 'lib/raca/account.rb', line 97

def servers(region)
  Raca::Servers.new(self, region)
end

#service_namesObject

Return the names of the available services. As rackspace add new services and APIs they should appear here.

Any name returned from here can be passe to #public_endpoint to get the API endpoint for that service

 = Raca::Account.new("username", "secret")
puts .service_names


74
75
76
77
78
79
# File 'lib/raca/account.rb', line 74

def service_names
  catalog = extract_value(identity_data, "access", "serviceCatalog") || {}
  catalog.map { |service|
    service["name"]
  }
end

#usersObject

Return a Raca::Users object. Use this to query and manage the users associated with the current account.

 = Raca::Account.new("username", "secret")
puts .users


107
108
109
# File 'lib/raca/account.rb', line 107

def users
  Raca::Users.new(self)
end