Class: Fog::Identity::OpenStack::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/requests/identity/get_tenant.rb,
lib/fog/openstack/identity.rb,
lib/fog/openstack/requests/identity/get_role.rb,
lib/fog/openstack/requests/identity/list_roles.rb,
lib/fog/openstack/requests/identity/list_users.rb,
lib/fog/openstack/requests/identity/check_token.rb,
lib/fog/openstack/requests/identity/create_role.rb,
lib/fog/openstack/requests/identity/create_user.rb,
lib/fog/openstack/requests/identity/delete_role.rb,
lib/fog/openstack/requests/identity/delete_user.rb,
lib/fog/openstack/requests/identity/update_user.rb,
lib/fog/openstack/requests/identity/list_tenants.rb,
lib/fog/openstack/requests/identity/create_tenant.rb,
lib/fog/openstack/requests/identity/delete_tenant.rb,
lib/fog/openstack/requests/identity/update_tenant.rb,
lib/fog/openstack/requests/identity/get_user_by_id.rb,
lib/fog/openstack/requests/identity/validate_token.rb,
lib/fog/openstack/requests/identity/create_user_role.rb,
lib/fog/openstack/requests/identity/delete_user_role.rb,
lib/fog/openstack/requests/identity/get_user_by_name.rb,
lib/fog/openstack/requests/identity/get_tenants_by_id.rb,
lib/fog/openstack/requests/identity/add_user_to_tenant.rb,
lib/fog/openstack/requests/identity/get_tenants_by_name.rb,
lib/fog/openstack/requests/identity/list_user_global_roles.rb,
lib/fog/openstack/requests/identity/remove_user_from_tenant.rb,
lib/fog/openstack/requests/identity/list_endpoints_for_token.rb,
lib/fog/openstack/requests/identity/list_roles_for_user_on_tenant.rb

Overview

class Real

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fog/openstack/identity.rb', line 70

def initialize(options={})
  @openstack_username = options[:openstack_username]

  unless self.data[:users].values.detect {|user| user['name'] == @openstack_username}
    id = Fog::Mock.random_numbers(6).to_s
    self.data[:users][id] = {
      'id'       => id,
      'name'     => options[:openstack_username],
      'email'    => "#{options[:openstack_username]}@mock.com",
      'tenantId' => Fog::Mock.random_numbers(6).to_s,
      'enabled'  => true
    }
  end
end

Class Method Details

.dataObject



56
57
58
59
60
61
62
63
64
# File 'lib/fog/openstack/identity.rb', line 56

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :users => {},
      :roles => {},
      :tenants => {}
    }
  end
end

.resetObject



66
67
68
# File 'lib/fog/openstack/identity.rb', line 66

def self.reset
  @data = nil
end

Instance Method Details

#add_user_to_tenant(tenant_id, user_id, role_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/identity/add_user_to_tenant.rb', line 15

def add_user_to_tenant(tenant_id, user_id, role_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'role' => {
      'id' => '503df61a99d6461fb247cdb6a3f3a4dd',
      'name' => 'admin'
    }
  }
  response
end

#create_role(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/openstack/requests/identity/create_role.rb', line 22

def create_role(name)
  data = {
    'id'   => Fog::Mock.random_numbers(6).to_s,
    'name' => name
  }
  self.data[:roles][data['id']] = data
  Excon::Response.new(
    :body   => { 'role' => data },
    :status => 202
  )
end

#create_tenant(attributes) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/openstack/requests/identity/create_tenant.rb', line 18

def create_tenant(attributes)
  response = Excon::Response.new
  response.status = [200, 204][rand(1)]
  response.body = {
    'tenant' => {
      'id' => '1',
      'description' => 'Has access to everything',
      'enabled' => true,
      'name' => 'admin'
    }
  }
  response
end

#create_user(name, password, email, tenantId = nil, enabled = true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/openstack/requests/identity/create_user.rb', line 29

def create_user(name, password, email, tenantId=nil, enabled=true)
  response = Excon::Response.new
  response.status = 200
  data = {
    'id'       => Fog::Mock.random_numbers(6).to_s,
    'name'     => name,
    'email'    => email,
    'tenantId' => tenantId,
    'enabled'  => enabled
  }
  self.data[:users][data['id']] = data
  response.body = { 'user' => data }
  response
end

#create_user_role(tenant_id, user_id, role_id) ⇒ Object



17
18
19
20
21
22
# File 'lib/fog/openstack/requests/identity/create_user_role.rb', line 17

def create_user_role(tenant_id, user_id, role_id)
  Excon::Response.new(
    :body   => { 'role' => self.data[:roles][role_id] },
    :status => 200
  )
end

#credentialsObject



93
94
95
96
97
98
99
# File 'lib/fog/openstack/identity.rb', line 93

def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url,
    :openstack_current_user_id => @openstack_current_user_id}
end

#dataObject



85
86
87
# File 'lib/fog/openstack/identity.rb', line 85

def data
  self.class.data[@openstack_username]
end

#delete_role(role_id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/identity/delete_role.rb', line 18

def delete_role(role_id)
  response = Excon::Response.new
  if role = list_roles.body['roles'][role_id]
    self.data[:roles].delete(role_id)
    response.status = 204
    response
  else
    raise Fog::Identity::OpenStack::NotFound
  end
end

#delete_tenant(attributes) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/identity/delete_tenant.rb', line 15

def delete_tenant(attributes)
  response = Excon::Response.new
  response.status = [200, 204][rand(1)]
  response.body = {
    'tenant' => {
      'id' => '1',
      'description' => 'Has access to everything',
      'enabled' => true,
      'name' => 'admin'
    }
  }
  response
end

#delete_user(user_id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/identity/delete_user.rb', line 18

def delete_user(user_id)
  self.data[:users].delete(
    list_users.body['users'].find {|x| x['id'] == user_id }['id'])

  response = Excon::Response.new
  response.status = 204
  response
rescue
  raise Fog::Identity::OpenStack::NotFound
end

#delete_user_role(tenant_id, user_id, role_id) ⇒ Object



17
18
19
20
21
# File 'lib/fog/openstack/requests/identity/delete_user_role.rb', line 17

def delete_user_role(tenant_id, user_id, role_id)
  response = Excon::Response.new
  response.status = 200
  response
end

#get_role(id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/identity/get_role.rb', line 15

def get_role(id)
  response = Excon::Response.new
  if data = self.data[:roles][id]
    response.status = 200
    response.body = { 'role' => data }
    response
  else
    raise Fog::Identity::OpenStack::NotFound
  end
end

#get_tenant(id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/identity/get_tenant.rb', line 15

def get_tenant(id)
  response = Excon::Response.new
  response.status = [200, 204][rand(1)]
  response.body = {
    'tenant' => {
      'id' => '1',
      'description' => 'Has access to everything',
      'enabled' => true,
      'name' => 'admin'
    }
  }
  response
end

#get_user_by_id(user_id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/openstack/requests/identity/get_user_by_id.rb', line 17

def get_user_by_id(user_id)
  response = Excon::Response.new
  response.status = 200

  existing_user = self.data[:users].find do |u|
      u[0] == user_id || u[1]['name'] == 'mock'
    end
  existing_user = existing_user[1] if existing_user

  response.body = {
    'user' => existing_user || create_user('mock', 'mock', '[email protected]').body['user']
  }
  response
end

#list_rolesObject



18
19
20
21
22
23
# File 'lib/fog/openstack/requests/identity/list_roles.rb', line 18

def list_roles
  Excon::Response.new(
    :body   => { 'roles' => self.data[:roles] },
    :status => 200
  )
end

#list_roles_for_user_on_tenant(tenant_id, user_id) ⇒ Object



15
16
17
18
19
20
# File 'lib/fog/openstack/requests/identity/list_roles_for_user_on_tenant.rb', line 15

def list_roles_for_user_on_tenant(tenant_id, user_id)
  Excon::Response.new(
    :body   => { 'roles' => self.data[:roles] },
    :status => 200
  )
end

#list_tenantsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/openstack/requests/identity/list_tenants.rb', line 20

def list_tenants
  Excon::Response.new(
    :body => {
      'tenants' => [
        {'id' => '1',
         'description' => 'Has access to everything',
         'enabled' => true,
         'name' => 'admin'},
        {'id' => '2',
         'description' => 'Normal tenant',
         'enabled' => true,
         'name' => 'default'},
        {'id' => '3',
         'description' => 'Disabled tenant',
         'enabled' => false,
         'name' => 'disabled'}
      ]
    },
    :status => [200, 204][rand(1)]
  )
end

#list_users(tenant_id = nil) ⇒ Object



16
17
18
19
20
21
# File 'lib/fog/openstack/requests/identity/list_users.rb', line 16

def list_users(tenant_id = nil)
  Excon::Response.new(
    :body   => { 'users' => self.data[:users].values },
    :status => 200
  )
end

#remove_user_from_tenant(tenant_id, user_id, role_id) ⇒ Object



15
16
# File 'lib/fog/openstack/requests/identity/remove_user_from_tenant.rb', line 15

def remove_user_from_tenant(tenant_id, user_id, role_id)
end

#reset_dataObject



89
90
91
# File 'lib/fog/openstack/identity.rb', line 89

def reset_data
  self.class.data.delete(@openstack_username)
end

#update_tenant(id, attributes) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/fog/openstack/requests/identity/update_tenant.rb', line 18

def update_tenant(id, attributes)
  response = Excon::Response.new
  response.status = [200, 204][rand(1)]
  attributes = {'enabled' => true, 'id' => '1'}.merge(attributes)
  response.body = {
    'tenant' => attributes
  }
  response
end

#update_user(user_id, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/openstack/requests/identity/update_user.rb', line 20

def update_user(user_id, options)
  response = Excon::Response.new
  if user = self.data[:users][user_id]
    if options['name']
      user['name'] = options['name']
    end
    response.status = 200
    response
  else
    raise Fog::Identity::OpenStack::NotFound
  end
end