Class: Fog::Rackspace::Identity::Mock
- Includes:
- Common
- Defined in:
- lib/fog/rackspace/identity.rb,
lib/fog/rackspace/requests/identity/create_token.rb,
lib/fog/rackspace/requests/identity/list_tenants.rb
Instance Attribute Summary
Attributes included from Common
Instance Method Summary collapse
-
#build_service_catalog(compute_tenant, object_tenant) ⇒ Hash
Construct a full, fake service catalog.
- #create_token(username, api_key) ⇒ Object
-
#endpoint_entry(tenant_id, region, options) ⇒ Hash
Helper method that generates a single endpoint hash within a service catalog entry.
-
#generate_object_tenant ⇒ Object
Generate a realistic-looking object tenant ID.
-
#initialize(options = {}) ⇒ Mock
constructor
A new instance of Mock.
- #list_tenants ⇒ Object
-
#service_catalog_entry(name, type, tenant_id, options) ⇒ Hash
Generate an individual service catalog entry for a fake service catalog.
Methods included from Common
Methods inherited from Service
#authenticate, #endpoint_uri, #region, #request, #request_without_retry, #service_name, #service_net?
Constructor Details
#initialize(options = {}) ⇒ Mock
Returns a new instance of Mock.
64 65 66 67 68 |
# File 'lib/fog/rackspace/identity.rb', line 64 def initialize(={}) () authenticate end |
Instance Method Details
#build_service_catalog(compute_tenant, object_tenant) ⇒ Hash
Construct a full, fake service catalog.
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 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/fog/rackspace/requests/identity/create_token.rb', line 86 def build_service_catalog(compute_tenant, object_tenant) [ service_catalog_entry("cloudFilesCDN", "rax:object-cdn", object_tenant, :public_url => lambda do |r| "https://cdn#{Fog::Mock.random_numbers(1)}.clouddrive.com/v1/#{object_tenant}" end), service_catalog_entry("cloudFiles", "object-store", object_tenant, :internal_url_snet => true, :public_url => lambda do |r| "https://storage101.#{r}#{Fog::Mock.random_numbers(1)}.clouddrive.com/v1/#{object_tenant}" end), service_catalog_entry("cloudMonitoring", "rax:monitor", compute_tenant, :single_endpoint => true, :rackspace_api_name => 'monitoring'), service_catalog_entry("cloudServersOpenStack", "compute", compute_tenant, :version_base_url => lambda { |r| "https://#{r}.servers.api.rackspacecloud.com" }, :version_id => "2"), service_catalog_entry("cloudBlockStorage", "volume", compute_tenant, :rackspace_api_name => 'blockstorage', :rackspace_api_version => '1'), service_catalog_entry("cloudDatabases", "rax:database", compute_tenant, :rackspace_api_name => 'databases'), service_catalog_entry("cloudLoadBalancers", "rax:load-balander", compute_tenant, :rackspace_api_name => 'loadbalancers'), service_catalog_entry("cloudDNS", "rax:dns", compute_tenant, :single_endpoint => true, :rackspace_api_name => 'dns'), service_catalog_entry("cloudOrchestration", "orchestration", compute_tenant, :rackspace_api_name => 'orchestration', :rackspace_api_version => '1'), service_catalog_entry("cloudQueues", "rax:queues", compute_tenant, :internal_url_snet => true, :rackspace_api_name => 'queues', :rackspace_api_version => '1'), service_catalog_entry("cloudBackup", "rax:backup", compute_tenant, :rackspace_api_name => 'backup'), service_catalog_entry("cloudImages", "image", compute_tenant, :rackspace_api_name => 'images', :rackspace_api_version => '2'), service_catalog_entry("autoscale", "rax:autoscale", compute_tenant, :rackspace_api_name => 'autoscale'), service_catalog_entry("cloudServers", "compute", compute_tenant, :single_endpoint => true, :version_base_url => lambda { |r| "https://servers.api.rackspacecloud.com" }, :version_id => '1.0') ] end |
#create_token(username, api_key) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fog/rackspace/requests/identity/create_token.rb', line 25 def create_token(username, api_key) unless username == 'baduser' || api_key == 'bad_key' compute_tenant = Fog::Mock.random_numbers(6) object_tenant = generate_object_tenant response = Excon::Response.new response.status = 200 response.body = { "access" => { "token"=> { "id" => Fog::Mock.random_hex(32), "expires" => (Time.now.utc + 86400).strftime("%Y-%m-%dT%H:%M:%S.%LZ"), "tenant" => { "id" => compute_tenant, "name" => compute_tenant } }, "user" => { "id" => Fog::Mock.random_numbers(6), "name" => username, "roles" => [ { "id" => Fog::Mock.random_numbers(1), "description" => "Fake Role for an object store", "name" => "object-store:default" }, { "id" => Fog::Mock.random_numbers(1), "description" => "Fake Role for a compute cluster", "name" => "compute:default" } ] }, "serviceCatalog" => build_service_catalog(compute_tenant, object_tenant), } } response else response = Excon::Response.new response.status = 401 response.body = { "unauthorized" => { "code" => 401, "message" => "Username or API key is invalid." } } raise Excon::Errors::Unauthorized.new('Unauthorized', nil, response) end end |
#endpoint_entry(tenant_id, region, options) ⇒ Hash
Helper method that generates a single endpoint hash within a service catalog entry.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/fog/rackspace/requests/identity/create_token.rb', line 193 def endpoint_entry(tenant_id, region, ) endpoint = { "tenantId" => tenant_id } endpoint["region"] = region if region r = region.downcase if region endpoint["publicURL"] = [:public_url].call(r) if [:public_url] if [:internal_url_snet] endpoint["internalURL"] = endpoint["publicURL"].gsub(%r{^https://}, "https://snet-") end endpoint["internalURL"] = [:internal_url].call(r) if [:internal_url] if [:version_base_url] && [:version_id] base = [:version_base_url].call(r) version = [:version_id] endpoint["publicURL"] = "#{base}/v#{version}/#{tenant_id}" endpoint["versionInfo"] = "#{base}/v#{version}" endpoint["versionList"] = base endpoint["versionId"] = version end endpoint end |
#generate_object_tenant ⇒ Object
Generate a realistic-looking object tenant ID.
73 74 75 76 |
# File 'lib/fog/rackspace/requests/identity/create_token.rb', line 73 def generate_object_tenant uuid = [8, 4, 4, 4, 12].map { |n| Fog::Mock.random_hex(n) }.join('_') "FogMockFS_#{uuid}" end |
#list_tenants ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/fog/rackspace/requests/identity/list_tenants.rb', line 22 def list_tenants response = Excon::Response.new response.status = [200, 203][rand(1)] response.body = { "tenants" => [ { "id" => Fog::Mock.random_numbers(6), "name" => "Enabled tenant", "enabled" => true }, { "id" => Fog::Mock.random_numbers(6), "name" => "Disabled tenant", "enabled" => false }, ], "tenants_links" => [] } response end |
#service_catalog_entry(name, type, tenant_id, options) ⇒ Hash
Generate an individual service catalog entry for a fake service catalog. Understands common patterns used within Rackspace service catalogs.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/fog/rackspace/requests/identity/create_token.rb', line 164 def service_catalog_entry(name, type, tenant_id, ) if [:rackspace_api_name] api_name = [:rackspace_api_name] api_version = [:rackspace_api_version] || "1.0" [:public_url] = lambda do |r| prefix = r ? "#{r}." : "" "https://#{prefix}#{api_name}.api.rackspacecloud.com/v#{api_version}/#{tenant_id}" end end entry = { "name" => name, "type" => type } if [:single_endpoint] entry["endpoints"] = [endpoint_entry(tenant_id, nil, )] else entry["endpoints"] = %w{ORD DFW SYD IAD HKG}.map do |region| endpoint_entry(tenant_id, region, ) end end entry end |