Class: Raca::Account
- Inherits:
-
Object
- Object
- Raca::Account
- 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
-
#auth_token ⇒ Object
Return the temporary token that should be used when making further API requests.
-
#containers(region) ⇒ Object
Return a Raca::Containers object for a region.
-
#http_client(hostname) ⇒ Object
Return a Raca::HttpClient suitable for making requests to hostname.
-
#initialize(username, key, cache = nil) ⇒ Account
constructor
A new instance of Account.
- #inspect ⇒ Object
-
#public_endpoint(service_name, region = nil) ⇒ Object
Return the public API URL for a particular rackspace service.
-
#refresh_cache ⇒ Object
Raca classes use this method to occasionally re-authenticate with the rackspace servers.
-
#servers(region) ⇒ Object
Return a Raca::Servers object for a region.
-
#service_names ⇒ Object
Return the names of the available services.
-
#users ⇒ Object
Return a Raca::Users object.
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_token ⇒ Object
28 29 30 |
# File 'lib/raca/account.rb', line 28 def auth_token extract_value(identity_data, "access", "token", "id") end |
#containers(region) ⇒ Object
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 |
#inspect ⇒ Object
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.
account = Raca::Account.new("username", "secret")
puts account.public_endpoint("cloudServers", :syd)
Some service APIs are not regioned. In those cases, the region code can be left off:
account = Raca::Account.new("username", "secret")
puts account.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_cache ⇒ Object
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
97 98 99 |
# File 'lib/raca/account.rb', line 97 def servers(region) Raca::Servers.new(self, region) end |
#service_names ⇒ Object
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 |