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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



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
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fog/identity/openstack/v2.rb', line 102

def initialize(options = {})
  @openstack_username = options[:openstack_username] || 'admin'
  @openstack_tenant = options[:openstack_tenant] || 'admin'
  @openstack_auth_uri = URI.parse(options[:openstack_auth_url])
  @openstack_management_url = @openstack_auth_uri.to_s

  @auth_token = Fog::Mock.random_base64(64)
  @auth_token_expiration = (Time.now.utc + 86400).iso8601

  @admin_tenant = data[:tenants].values.find do |u|
    u['name'] == 'admin'
  end

  if @openstack_tenant
    @current_tenant = data[:tenants].values.find do |u|
      u['name'] == @openstack_tenant
    end

    if @current_tenant
      @current_tenant_id = @current_tenant['id']
    else
      @current_tenant_id = Fog::Mock.random_hex(32)
      @current_tenant = data[:tenants][@current_tenant_id] = {
        'id'   => @current_tenant_id,
        'name' => @openstack_tenant
      }
    end
  else
    @current_tenant = @admin_tenant
  end

  @current_user = data[:users].values.find do |u|
    u['name'] == @openstack_username
  end
  @current_tenant_id = Fog::Mock.random_hex(32)

  if @current_user
    @current_user_id = @current_user['id']
  else
    @current_user_id = Fog::Mock.random_hex(32)
    @current_user = data[:users][@current_user_id] = {
      'id'       => @current_user_id,
      'name'     => @openstack_username,
      'email'    => "#{@openstack_username}@mock.com",
      'tenantId' => Fog::Mock.random_numbers(6).to_s,
      'enabled'  => true
    }
  end
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



70
71
72
# File 'lib/fog/identity/openstack/v2.rb', line 70

def auth_token
  @auth_token
end

#auth_token_expirationObject (readonly)

Returns the value of attribute auth_token_expiration.



71
72
73
# File 'lib/fog/identity/openstack/v2.rb', line 71

def auth_token_expiration
  @auth_token_expiration
end

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



73
74
75
# File 'lib/fog/identity/openstack/v2.rb', line 73

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



72
73
74
# File 'lib/fog/identity/openstack/v2.rb', line 72

def current_user
  @current_user
end

#unscoped_tokenObject (readonly)

Returns the value of attribute unscoped_token.



74
75
76
# File 'lib/fog/identity/openstack/v2.rb', line 74

def unscoped_token
  @unscoped_token
end

Class Method Details

.dataObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fog/identity/openstack/v2.rb', line 76

def self.data
  @users ||= {}
  @roles ||= {}
  @tenants ||= {}
  @ec2_credentials ||= Hash.new { |hash, key| hash[key] = {} }
  @user_tenant_membership ||= {}

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

.reset!Object



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

def self.reset!
  @data = nil
  @users = nil
  @roles = nil
  @tenants = nil
  @ec2_credentials = nil
end

Instance Method Details

#add_user_to_tenant(tenant_id, user_id, role_id) ⇒ Object



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

def add_user_to_tenant(tenant_id, user_id, role_id)
  role = data[:roles][role_id]
  data[:user_tenant_membership][tenant_id] ||= {}
  data[:user_tenant_membership][tenant_id][user_id] ||= []
  data[:user_tenant_membership][tenant_id][user_id].push(role['id']).uniq!

  response = Excon::Response.new
  response.status = 200
  response.body = {
    'role' => {
      'id'   => role['id'],
      'name' => role['name']
    }
  }
  response
end

#check_token(token_id, tenant_id = nil) ⇒ Object



16
17
# File 'lib/fog/identity/openstack/v2/requests/check_token.rb', line 16

def check_token(token_id, tenant_id = nil)
end

#create_ec2_credential(user_id, tenant_id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/identity/openstack/v2/requests/create_ec2_credential.rb', line 38

def create_ec2_credential(user_id, tenant_id)
  response = Excon::Response.new
  response.status = 200

  data = {
    'access'    => Fog::Mock.random_hex(32),
    'secret'    => Fog::Mock.random_hex(32),
    'tenant_id' => tenant_id,
    'user_id'   => user_id,
  }

  self.data[:ec2_credentials][user_id][data['access']] = data

  response.body = {'credential' => data}

  response
end

#create_role(name) ⇒ Object



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

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

#create_tenant(attributes) ⇒ Object



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

def create_tenant(attributes)
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response.body = {
    'tenant' => {
      'id'          => "df9a815161eba9b76cc748fd5c5af73e",
      'description' => attributes[:description] || 'normal tenant',
      'enabled'     => true,
      'name'        => attributes[:name] || 'default'
    }
  }
  response
end

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



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

def create_user(name, _password, email, tenantId = nil, enabled = true)
  response = Excon::Response.new
  response.status = 200
  data = {
    'id'       => Fog::Mock.random_hex(32),
    '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



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

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

#credentialsObject



160
161
162
163
164
165
166
167
168
# File 'lib/fog/identity/openstack/v2.rb', line 160

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,
   :current_user              => @current_user,
   :current_tenant            => @current_tenant}
end

#dataObject



152
153
154
# File 'lib/fog/identity/openstack/v2.rb', line 152

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

#delete_ec2_credential(user_id, access) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/identity/openstack/v2/requests/delete_ec2_credential.rb', line 29

def delete_ec2_credential(user_id, access)
  raise Fog::Identity::OpenStack::NotFound unless data[:ec2_credentials][user_id][access]

  data[:ec2_credentials][user_id].delete access

  response = Excon::Response.new
  response.status = 204
  response
rescue
end

#delete_role(role_id) ⇒ Object



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

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

#delete_tenant(_attributes) ⇒ Object



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

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

#delete_user(user_id) ⇒ Object



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

def delete_user(user_id)
  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



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

def delete_user_role(_tenant_id, _user_id, _role_id)
  response = Excon::Response.new
  response.status = 204
  response
end

#get_ec2_credential(user_id, access) ⇒ Object

Raises:

  • (Fog::OpenStack::Identity::NotFound)


36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/identity/openstack/v2/requests/get_ec2_credential.rb', line 36

def get_ec2_credential(user_id, access)
  ec2_credential = data[:ec2_credentials][user_id][access]

  raise Fog::OpenStack::Identity::NotFound unless ec2_credential

  response = Excon::Response.new
  response.status = 200
  response.body = {'credential' => ec2_credential}
  response
end

#get_role(id) ⇒ Object



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

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



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

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

#get_user_by_id(user_id) ⇒ Object



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

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

  existing_user = 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

#get_user_by_name(name) ⇒ Object



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

def get_user_by_name(name)
  response = Excon::Response.new
  response.status = 200
  user = data[:users].values.select { |u| u['name'] == name }[0]
  response.body = {
    'user' => user
  }
  response
end

#list_ec2_credentials(options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/identity/openstack/v2/requests/list_ec2_credentials.rb', line 43

def list_ec2_credentials(options = {})
  user_id = if options.kind_of?(Hash)
              options.delete(:user_id)
            else
              options
            end

  ec2_credentials = data[:ec2_credentials][user_id].values

  response = Excon::Response.new
  response.status = 200
  response.body = {'credentials' => ec2_credentials}
  response
end

#list_roles(_options = {}) ⇒ Object



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

def list_roles(_options = {})
  if data[:roles].empty?
    ['admin', 'Member'].each do |name|
      id = Fog::Mock.random_hex(32)
      data[:roles][id] = {'id' => id, 'name' => name}
    end
  end

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

#list_roles_for_user_on_tenant(tenant_id, user_id) ⇒ Object



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

def list_roles_for_user_on_tenant(tenant_id, user_id)
  data[:user_tenant_membership][tenant_id] ||= {}
  data[:user_tenant_membership][tenant_id][user_id] ||= []
  roles = data[:user_tenant_membership][tenant_id][user_id].map do |role_id|
    data[:roles][role_id]
  end

  Excon::Response.new(
    :body   => {'roles' => roles},
    :status => 200
  )
end

#list_tenants(_options = nil, _marker = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/identity/openstack/v2/requests/list_tenants.rb', line 27

def list_tenants(_options = nil, _marker = nil)
  Excon::Response.new(
    :body   => {
      'tenants_links' => [],
      '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(2)]
  )
end

#list_users(options = {}) ⇒ Object



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

def list_users(options = {})
  tenant_id = options[:tenant_id]

  users = data[:users].values

  if tenant_id
    users = users.select do |user|
      user['tenantId'] == tenant_id
    end
  end

  Excon::Response.new(
    :body   => {'users' => users},
    :status => 200
  )
end

#remove_user_from_tenant(tenant_id, user_id, role_id) ⇒ Object



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

def remove_user_from_tenant(tenant_id, user_id, role_id)
end

#reset_dataObject



156
157
158
# File 'lib/fog/identity/openstack/v2.rb', line 156

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

#set_tenant(_tenant) ⇒ Object



14
15
16
# File 'lib/fog/identity/openstack/v2/requests/set_tenant.rb', line 14

def set_tenant(_tenant)
  true
end

#update_tenant(_id, attributes) ⇒ Object



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

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

#update_user(user_id, options) ⇒ Object



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

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

#validate_token(token_id, tenant_id = nil) ⇒ Object



16
17
# File 'lib/fog/identity/openstack/v2/requests/validate_token.rb', line 16

def validate_token(token_id, tenant_id = nil)
end