Class: Fog::Scaleway::Account::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/scaleway/account.rb,
lib/fog/scaleway/requests/account/get_user.rb,
lib/fog/scaleway/requests/account/get_token.rb,
lib/fog/scaleway/requests/account/list_tokens.rb,
lib/fog/scaleway/requests/account/update_user.rb,
lib/fog/scaleway/requests/account/create_token.rb,
lib/fog/scaleway/requests/account/delete_token.rb,
lib/fog/scaleway/requests/account/update_token.rb,
lib/fog/scaleway/requests/account/get_organization.rb,
lib/fog/scaleway/requests/account/list_organizations.rb,
lib/fog/scaleway/requests/account/get_token_permissions.rb,
lib/fog/scaleway/requests/account/get_organization_quotas.rb

Constant Summary collapse

TIME_FORMAT =
'%Y-%m-%dT%H:%M:%S.%6N%:z'.freeze
ORGANIZATION =
{
  'address_line2' => 'ADDRESS LINE2',
  'address_country_code' => 'ADDRESS COUNTRY CODE',
  'address_line1' => 'ADDRESS LINE1',
  'support_level' => 'SUPPORT LEVEL',
  'name' => 'ORGANIZATION',
  'modification_date' => Time.now.utc.strftime(TIME_FORMAT),
  'currency' => 'CURRENCY',
  'locale' => 'LOCALE',
  'customer_class' => 'CUSTOMER CLASS',
  'support_id' => 'SUPPORT ID',
  'creation_date' => Time.now.utc.strftime(TIME_FORMAT),
  'address_postal_code' => 'ADDRESS POSTAL CODE',
  'address_city_name' => 'ADDRESS CITY NAME',
  'address_subdivision_code' => 'ADDRESS SUBDIVISION CODE',
  'timezone' => 'TIMEZONE',
  'vat_number' => nil,
  'support_pin' => 'SUPPORT PIN',
  'id' => '7c91ef18-eea8-4f4f-bfca-deaea872338c',
  'warnings' => [],
  'users' => [{
    'phone_number' => nil,
    'firstname' => 'FIRSTNAME',
    'lastname' => 'LASTNAME',
    'creation_date' => Time.now.utc.strftime(TIME_FORMAT),
    'ssh_public_keys' => [],
    'id' => 'e9459194-0f66-4958-b3c3-01e623d21566',
    'organizations' => [{
      'id' => '7c91ef18-eea8-4f4f-bfca-deaea872338c',
      'name' => 'ORGANIZATION'
    }],
    'modification_date' => Time.now.utc.strftime(TIME_FORMAT),
    'roles' => [{
      'organization' => {
        'id' => '7c91ef18-eea8-4f4f-bfca-deaea872338c',
        'name' => 'ORGANIZATION'
      },
      'role' => 'ROLE'
    }],
    'fullname' => 'FULLNAME',
    'email' => '[email protected]'
  }]
}.freeze
ORGANIZATION_QUOTAS =
{
  'sis' => 100,
  'servers_type_VC1M' => 25,
  'servers_type_VC1L' => 10,
  'servers_type_C1' => 2,
  'servers_type_C2M' => 5,
  'servers_type_C2L' => 4,
  'ips' => 10,
  'snapshots' => 25,
  'servers' => 10,
  'security_rules' => 30,
  'servers_type_C2S' => 10,
  'trusted' => 1,
  'volumes' => 20,
  'images' => 100,
  'servers_type_VC1S' => 50,
  'invites' => 100,
  'security_groups' => 100
}.freeze
TOKEN_PERMISSIONS =
{
  'account' => {
    'token:*' => [ORGANIZATION['users'][0]['id']],
    'organization:*' => [ORGANIZATION['id']],
    'user:*' => [ORGANIZATION['users'][0]['id']]
  },
  'task' => { 'tasks:*' => ["#{ORGANIZATION['id']}:*"] },
  'compute' => {
    'security_groups:*' => ["#{ORGANIZATION['id']}:*"],
    'images:*' => ["#{ORGANIZATION['id']}:*"],
    'servers:*' => ["#{ORGANIZATION['id']}:*"],
    'ips:*' => ["#{ORGANIZATION['id']}:*"],
    'snapshots:*' => ["#{ORGANIZATION['id']}:*"],
    'volumes:*' => ["#{ORGANIZATION['id']}:*"]
  },
  'billing' => {
    'usages:*' => ["#{ORGANIZATION['id']}:*"],
    'invoices:*' => ["#{ORGANIZATION['id']}:*"]
  },
  'storage' => {
    'storage:*' => ["#{ORGANIZATION['id']}:*"]
  },
  'dns' => {
    'dns:*' => ["#{ORGANIZATION['id']}:*"]
  },
  'ticket' => {
    'tickets:*' => [ORGANIZATION['users'][0]['id']]
  },
  'payment' => {
    'cards:*' => ["#{ORGANIZATION['id']}:*"]
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mock

Returns a new instance of Mock.



197
198
199
200
# File 'lib/fog/scaleway/account.rb', line 197

def initialize(options)
  @token = options[:scaleway_token]
  @email = options[:scaleway_email]
end

Class Method Details

.dataObject



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/fog/scaleway/account.rb', line 185

def self.data
  @data ||= Hash.new do |hash, token|
    hash[token] = {
      organizations: { ORGANIZATION['id'] => ORGANIZATION },
      organization_quotas: Hash.new(ORGANIZATION_QUOTAS),
      tokens: {},
      token_permissions: Hash.new(TOKEN_PERMISSIONS),
      users: { ORGANIZATION['users'][0]['id'] => ORGANIZATION['users'][0] }
    }
  end
end

.resetObject



202
203
204
# File 'lib/fog/scaleway/account.rb', line 202

def self.reset
  @data = nil
end

Instance Method Details

#create_token(options = {}) ⇒ Object



21
22
23
24
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
# File 'lib/fog/scaleway/requests/account/create_token.rb', line 21

def create_token(options = {})
  if @email.nil?
    raise ArgumentError, 'email is required to create a token'
  end

  body = {
    email: @email
  }

  body.merge!(options)

  body = jsonify(body)

  user = data[:users].values.find do |u|
    u['email'] == body['email']
  end

  raise_invalid_auth('Invalid credentials') unless user

  expires = nil
  if body['expires'] != false
    expires = (Time.now + 30 * 60).utc.strftime(TIME_FORMAT)
  end

  token = {
    'user_id' => user['id'],
    'description' => body['description'] || '',
    'roles' => { 'organization' => nil, 'role' => nil },
    'expires' => expires,
    'creation_date' => now,
    'inherits_user_perms' => true,
    'id' => Fog::UUID.uuid
  }

  data[:tokens][token['id']] = token

  response(status: 201, body: { 'token' => token })
end

#delete_token(token_id) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/fog/scaleway/requests/account/delete_token.rb', line 11

def delete_token(token_id)
  token = lookup(:tokens, token_id)

  data[:tokens].delete(token['id'])

  response(status: 204)
end

#get_organization(organization_id) ⇒ Object



11
12
13
14
15
# File 'lib/fog/scaleway/requests/account/get_organization.rb', line 11

def get_organization(organization_id)
  organization = lookup(:organizations, organization_id)

  response(status: 200, body: { 'organization' => organization })
end

#get_organization_quotas(organization_id) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/fog/scaleway/requests/account/get_organization_quotas.rb', line 11

def get_organization_quotas(organization_id)
  organization = lookup(:organizations, organization_id)

  quotas = lookup(:organization_quotas, organization['id'])

  response(status: 200, body: { 'quotas' => quotas })
end

#get_token(token_id) ⇒ Object



11
12
13
14
15
# File 'lib/fog/scaleway/requests/account/get_token.rb', line 11

def get_token(token_id)
  token = lookup(:tokens, token_id)

  response(status: 200, body: { 'token' => token })
end

#get_token_permission(token_id) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/fog/scaleway/requests/account/get_token_permissions.rb', line 11

def get_token_permission(token_id)
  token = lookup(:tokens, token_id)

  permissions = lookup(:token_permissions, token['id'])

  response(status: 200, body: { 'permissions' => permissions })
end

#get_user(user_id) ⇒ Object



11
12
13
14
15
# File 'lib/fog/scaleway/requests/account/get_user.rb', line 11

def get_user(user_id)
  user = lookup(:users, user_id)

  response(status: 200, body: { 'user' => user })
end

#list_organizationsObject



11
12
13
14
15
# File 'lib/fog/scaleway/requests/account/list_organizations.rb', line 11

def list_organizations
  organizations = data[:organizations].values

  response(status: 200, body: { 'organizations' => organizations })
end

#list_tokensObject



11
12
13
14
15
# File 'lib/fog/scaleway/requests/account/list_tokens.rb', line 11

def list_tokens
  tokens = data[:tokens].values

  response(status: 200, body: { 'tokens' => tokens })
end

#update_token(token_id, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/scaleway/requests/account/update_token.rb', line 14

def update_token(token_id, options = {})
  options = jsonify(options)

  token = lookup(:tokens, token_id)

  token['description'] = options['description'] unless options['description'].nil?
  token['expires'] = (Time.now + 30 * 60).utc.strftime(TIME_FORMAT)

  response(status: 200, body: { 'token' => token })
end

#update_user(user_id, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/scaleway/requests/account/update_user.rb', line 14

def update_user(user_id, options = {})
  options = jsonify(options)

  user = lookup(:users, user_id)

  %w[firstname lastname].each do |attr|
    user[attr] = options[attr] if options.key?(attr)
  end

  user['fullname'] = [user['firstname'], user['lastname']].compact.join(' ')

  if (ssh_public_keys = options['ssh_public_keys'])
    user['ssh_public_keys'] = ssh_public_keys.map do |key|
      {
        'key' => key['key'],
        'fingerprint' => generate_fingerprint(key['key'])
      }
    end
  end

  response(status: 200, body: { 'user' => user })
end