Class: Fog::Rackspace::Identity::Mock

Inherits:
Service
  • Object
show all
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

#auth_token, #service_catalog

Instance Method Summary collapse

Methods included from Common

#apply_options, #authenticate

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(options={})
  apply_options(options)

  authenticate
end

Instance Method Details

#build_service_catalog(compute_tenant, object_tenant) ⇒ Hash

Construct a full, fake service catalog.

Parameters:

  • compute_tenant (String)

    Tenant ID to be used in entries for compute-based services (most of them).

  • object_tenant (String)

    Tenant ID to be used in object-store related entries.

Returns:

  • (Hash)

    A fully-populated, valid 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.

Parameters:

  • tenant_id (String)

    The tenant ID used for this endpoint.

  • region (String, nil)

    The region to include in this endpoint, if any.

  • options (Hash)

    Options inherited from #service_catalog_entry.

Returns:

  • (Hash)

    A well-formed endpoint hash.



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, options)
  endpoint = { "tenantId" => tenant_id }
  endpoint["region"] = region if region
  r = region.downcase if region
  endpoint["publicURL"] = options[:public_url].call(r) if options[:public_url]

  if options[:internal_url_snet]
    endpoint["internalURL"] = endpoint["publicURL"].gsub(%r{^https://}, "https://snet-")
  end

  endpoint["internalURL"] = options[:internal_url].call(r) if options[:internal_url]
  if options[:version_base_url] && options[:version_id]
    base = options[:version_base_url].call(r)
    version = options[: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_tenantObject

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_tenantsObject



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.

Parameters:

  • name (String)

    The required “name” attribute of the service catalog entry.

  • type (String)

    The required “type” attribute.

  • tenant_id (String)

    Tenant ID to be used for this service.

  • options (Hash)

    Control the contents of the generated entry.

Options Hash (options):

  • :public_url (Proc)

    Callable invoked with each region (or ‘nil`) to generate a `publicURL` for that region.

  • :single_endpoint (Boolean)

    If ‘true`, only a single endpoint entry will be generated, rather than an endpoint for each region.

  • :internal_url_snet (Boolean)

    If ‘true`, an internalURL entry will be generated by prepending “snet-” to the publicURL.

  • :rackspace_api_name (String)

    If specified, will generate publicURL as a Rackspace API URL.

  • :rackspace_api_version (String) — default: `"1.0"`

    Specify the version of the Rackspace API URL.

Returns:

  • (Hash)

    A valid service catalog entry.



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, options)
  if options[:rackspace_api_name]
    api_name = options[:rackspace_api_name]
    api_version = options[:rackspace_api_version] || "1.0"
    options[: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 options[:single_endpoint]
    entry["endpoints"] = [endpoint_entry(tenant_id, nil, options)]
  else
    entry["endpoints"] = %w{ORD DFW SYD IAD HKG}.map do |region|
      endpoint_entry(tenant_id, region, options)
    end
  end
  entry
end