Class: Fog::Scaleway::Compute::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/scaleway/compute.rb,
lib/fog/scaleway/requests/compute/get_ip.rb,
lib/fog/scaleway/requests/compute/get_task.rb,
lib/fog/scaleway/requests/compute/list_ips.rb,
lib/fog/scaleway/requests/compute/create_ip.rb,
lib/fog/scaleway/requests/compute/delete_ip.rb,
lib/fog/scaleway/requests/compute/get_image.rb,
lib/fog/scaleway/requests/compute/update_ip.rb,
lib/fog/scaleway/requests/compute/get_server.rb,
lib/fog/scaleway/requests/compute/get_volume.rb,
lib/fog/scaleway/requests/compute/list_tasks.rb,
lib/fog/scaleway/requests/compute/delete_task.rb,
lib/fog/scaleway/requests/compute/list_images.rb,
lib/fog/scaleway/requests/compute/create_image.rb,
lib/fog/scaleway/requests/compute/delete_image.rb,
lib/fog/scaleway/requests/compute/get_snapshot.rb,
lib/fog/scaleway/requests/compute/list_servers.rb,
lib/fog/scaleway/requests/compute/list_volumes.rb,
lib/fog/scaleway/requests/compute/update_image.rb,
lib/fog/scaleway/requests/compute/create_server.rb,
lib/fog/scaleway/requests/compute/create_volume.rb,
lib/fog/scaleway/requests/compute/delete_server.rb,
lib/fog/scaleway/requests/compute/delete_volume.rb,
lib/fog/scaleway/requests/compute/get_container.rb,
lib/fog/scaleway/requests/compute/get_dashboard.rb,
lib/fog/scaleway/requests/compute/get_user_data.rb,
lib/fog/scaleway/requests/compute/update_server.rb,
lib/fog/scaleway/requests/compute/update_volume.rb,
lib/fog/scaleway/requests/compute/get_bootscript.rb,
lib/fog/scaleway/requests/compute/list_snapshots.rb,
lib/fog/scaleway/requests/compute/list_user_data.rb,
lib/fog/scaleway/requests/compute/create_snapshot.rb,
lib/fog/scaleway/requests/compute/delete_snapshot.rb,
lib/fog/scaleway/requests/compute/list_containers.rb,
lib/fog/scaleway/requests/compute/update_snapshot.rb,
lib/fog/scaleway/requests/compute/delete_user_data.rb,
lib/fog/scaleway/requests/compute/list_bootscripts.rb,
lib/fog/scaleway/requests/compute/update_user_data.rb,
lib/fog/scaleway/requests/compute/get_security_group.rb,
lib/fog/scaleway/requests/compute/list_server_actions.rb,
lib/fog/scaleway/requests/compute/list_security_groups.rb,
lib/fog/scaleway/requests/compute/create_security_group.rb,
lib/fog/scaleway/requests/compute/delete_security_group.rb,
lib/fog/scaleway/requests/compute/execute_server_action.rb,
lib/fog/scaleway/requests/compute/update_security_group.rb,
lib/fog/scaleway/requests/compute/get_security_group_rule.rb,
lib/fog/scaleway/requests/compute/list_security_group_rules.rb,
lib/fog/scaleway/requests/compute/create_security_group_rule.rb,
lib/fog/scaleway/requests/compute/delete_security_group_rule.rb,
lib/fog/scaleway/requests/compute/update_security_group_rule.rb

Constant Summary collapse

INITIAL_IMAGES =
[{
  'default_bootscript' => {
    'kernel' => 'http://169.254.42.24/kernel/x86_64-4.5.7-std-3/vmlinuz-4.5.7-std-3',
    'initrd' => 'http://169.254.42.24/initrd/initrd-Linux-x86_64-v3.11.1.gz',
    'default' => true,
    'bootcmdargs' => 'LINUX_COMMON ip=:::::eth0: boot=local',
    'architecture' => 'x86_64',
    'title' => 'x86_64 4.5.7 std #3 (latest/stable)',
    'dtb' => '',
    'organization' => '11111111-1111-4111-8111-111111111111',
    'id' => '3b522e7a-8468-4577-ab3e-2b9535384bb8',
    'public' => true
  },
  'creation_date' => '2016-05-20T09:35:48.735687+00:00',
  'name' => 'Ubuntu Xenial (16.04 latest)',
  'modification_date' => '2016-07-12T15:19:54.680577+00:00',
  'organization' => 'abaeb1aa-760b-4391-aeab-c0622be90abf',
  'extra_volumes' => '[]',
  'arch' => 'x86_64',
  'id' => '75c28f52-6c64-40fc-bb31-f53ca9d02de9',
  'root_volume' => {
    'size' => 50_000_000_000,
    'id' => '714abe6f-5721-4222-ae69-15b4a6488f22',
    'volume_type' => 'l_ssd',
    'name' => 'x86_64-ubuntu-xenial-2016-05-20_09:25'
  },
  'public' => true
}, {
  'default_bootscript' => {
    'kernel' => 'kernel/armv7l-4.5.7-std-4',
    'initrd' => 'initrd/uInitrd-Linux-armv7l-v3.11.1',
    'default' => true,
    'bootcmdargs' => 'LINUX_COMMON ip=:::::eth0: boot=local',
    'architecture' => 'arm',
    'title' => 'armv7l 4.5.7 std #4 (latest)',
    'dtb' => 'dtb/c1-armv7l-4.5.7-std-4',
    'organization' => '11111111-1111-4111-8111-111111111111',
    'id' => '599b736c-48b5-4530-9764-f04d06ecadc7',
    'public' => true
  },
  'creation_date' => '2016-05-20T09:00:08.222054+00:00',
  'name' => 'Ubuntu Xenial (16.04 latest)',
  'modification_date' => '2016-07-12T15:21:13.269384+00:00',
  'organization' => 'abaeb1aa-760b-4391-aeab-c0622be90abf',
  'extra_volumes' => '[]',
  'arch' => 'arm',
  'id' => 'eeb73cbf-78a9-4481-9e38-9aaadaf8e0c9',
  'root_volume' => {
    'size' => 50_000_000_000,
    'id' => 'ea3aaf5a-c91b-463d-9093-00fae8632cfd',
    'volume_type' => 'l_ssd',
    'name' => 'armv7l-ubuntu-xenial-2016-05-20_08:51'
  },
  'public' => true
}].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mock

Returns a new instance of Mock.



233
234
235
# File 'lib/fog/scaleway/compute.rb', line 233

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

Class Method Details

.dataObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/fog/scaleway/compute.rb', line 212

def self.data
  @data ||= Hash.new do |hash, token|
    hash[token] = {
      servers: {},
      user_data: Hash.new({}),
      volumes: {},
      snapshots: {},
      images: Hash[INITIAL_IMAGES.map { |i| [i['id'], i] }],
      ips: {},
      security_groups: {},
      security_group_rules: Hash.new({}),
      bootscripts: Hash[INITIAL_IMAGES.map do |i|
        [i['default_bootscript']['id'], i['default_bootscript']]
      end],
      tasks: {},
      containers: {},
      server_actions: Hash.new(%w(poweron poweroff reboot terminate))
    }
  end
end

.resetObject



237
238
239
# File 'lib/fog/scaleway/compute.rb', line 237

def self.reset
  @data = nil
end

Instance Method Details

#create_image(name, arch, root_volume, options = {}) ⇒ Object



20
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
# File 'lib/fog/scaleway/requests/compute/create_image.rb', line 20

def create_image(name, arch, root_volume, options = {})
  body = {
    organization: @organization,
    name: name,
    arch: arch,
    root_volume: root_volume
  }

  body.merge!(options)

  body = jsonify(body)

  creation_date = now

  root_volume = lookup(:snapshots, body['root_volume'])

  image = {
    'default_bootscript' => body['default_bootscript'],
    'creation_date' => creation_date,
    'name' => body['name'],
    'modification_date' => creation_date,
    'organization' => body['organization'],
    'extra_volumes' => '[]',
    'arch' => body['arch'],
    'id' => Fog::UUID.uuid,
    'root_volume' => {
      'size' => root_volume['size'],
      'id' => root_volume['id'],
      'volume_type' => root_volume['volume_type'],
      'name' => root_volume['name']
    },
    'public' => false
  }

  data[:images][image['id']] = image

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

#create_ip(options = {}) ⇒ Object



17
18
19
20
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
59
60
61
62
63
64
# File 'lib/fog/scaleway/requests/compute/create_ip.rb', line 17

def create_ip(options = {})
  body = {
    organization: @organization
  }

  body.merge!(options)

  body = jsonify(body)

  server = lookup(:servers, body['server']) if body['server']

  ip = {
    'organization' => body['organization'],
    'reverse' => nil,
    'id' => Fog::UUID.uuid,
    'server' => nil,
    'address' => Fog::Mock.random_ip
  }

  if server
    ip['server'] = {
      'id' => server['id'],
      'name' => server['name']
    }
  end

  if server && (public_ip = server['public_ip'])
    if public_ip['dynamic']
      ip['id'] = public_ip['id']
      ip['address'] = public_ip['address']
    else
      message = "Server '#{server['id']}' is already attached to an IP address"
      raise_invalid_request_error(message)
    end
  end

  data[:ips][ip['id']] = ip

  if server
    server['public_ip'] = {
      'dynamic' => false,
      'id' => ip['id'],
      'address' => ip['address']
    }
  end

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

#create_security_group(name, options = {}) ⇒ Object



18
19
20
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
# File 'lib/fog/scaleway/requests/compute/create_security_group.rb', line 18

def create_security_group(name, options = {})
  body = {
    organization: @organization,
    name: name
  }

  body.merge!(options)

  body = jsonify(body)

  security_group = {
    'description' => body['description'],
    'enable_default_security' => body.fetch('enable_default_security', true),
    'servers' => [],
    'organization' => body['organization'],
    'organization_default' => body.fetch('organization_default', false),
    'id' => Fog::UUID.uuid,
    'name' => body['name']
  }

  if security_group['organization_default'] && default_security_group
    raise_conflict('Cannot have more than one organization default group')
  end

  data[:security_groups][security_group['id']] = security_group

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

#create_security_group_rule(security_group_id, action, direction, ip_range, protocol, dest_port_from = nil) ⇒ Object



15
16
17
18
19
20
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
# File 'lib/fog/scaleway/requests/compute/create_security_group_rule.rb', line 15

def create_security_group_rule(security_group_id, action, direction, ip_range, protocol, dest_port_from = nil)
  body = {
    action: action,
    direction: direction,
    ip_range: ip_range,
    protocol: protocol,
    dest_port_from: dest_port_from
  }

  body = jsonify(body)

  rules = data[:security_group_rules][security_group_id].values

  position = (rules.map { |r| r[:position] }.max || 0) + 1

  rule = {
    'direction' => body['direction'],
    'protocol' => body['protocol'],
    'ip_range' => body['ip_range'],
    'dest_port_from' => body['dest_port_from'],
    'action' => body['action'],
    'position' => position,
    'dest_port_to' => nil,
    'editable' => nil,
    'id' => Fog::UUID.uuid
  }

  data[:security_group_rules][security_group_id][rule['id']] = rule

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

#create_server(name, image, volumes, options = {}) ⇒ Object



20
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
# File 'lib/fog/scaleway/requests/compute/create_server.rb', line 20

def create_server(name, image, volumes, options = {})
  body = {
    organization: @organization,
    name: name,
    image: image,
    volumes: volumes
  }

  body.merge!(options)

  body = jsonify(body)

  image = lookup(:images, body['image'])

  creation_date = now

  volumes = {}
  body['volumes'].each do |index, volume|
    volume = lookup(:volumes, volume['id'])

    if volume['server']
      message = "volume #{volume['id']} is already attached to a server"
      raise_invalid_request_error(message)
    end

    volumes[index] = volume
  end

  root_volume = image['root_volume']
  volumes['0'] = {
    'size' => root_volume['size'],
    'name' => root_volume['name'],
    'modification_date' => creation_date,
    'organization' => body['organization'],
    'export_uri' => nil,
    'creation_date' => creation_date,
    'id' => Fog::UUID.uuid,
    'volume_type' => root_volume['volume_type'],
    'server' => nil
  }

  public_ip = nil
  if body['public_ip']
    public_ip = lookup(:ips, body['public_ip'])

    public_ip = {
      'dynamic' => false,
      'id' => public_ip['id'],
      'address' => public_ip['address']
    }
  end

  dynamic_ip_required = !public_ip && body.fetch('dynamic_ip_required', true)

  default_bootscript_id = image['default_bootscript']['id']
  bootscript_id = body.fetch('bootscript', default_bootscript_id)
  bootscript = lookup(:bootscripts, bootscript_id)

  if body['security_group']
    security_group = lookup(:security_groups, body['security_group'])
  else
    security_group = default_security_group
    security_group ||= create_default_security_group
  end

  server = {
    'arch' => image['arch'],
    'bootscript' => bootscript,
    'commercial_type' => body.fetch('commercial_type', 'C1'),
    'creation_date' => creation_date,
    'dynamic_ip_required' => dynamic_ip_required,
    'enable_ipv6' => body.fetch('enable_ipv6', false),
    'extra_networks' => [],
    'hostname' => body['name'],
    'id' => Fog::UUID.uuid,
    'image' => image,
    'ipv6' => nil,
    'location' => nil,
    'modification_date' => creation_date,
    'name' => body['name'],
    'organization' => body['organization'],
    'private_ip' => nil,
    'public_ip' => public_ip,
    'security_group' => {
      'id' => security_group['id'],
      'name' => security_group['name']
    },
    'state' => 'stopped',
    'state_detail' => '',
    'tags' => body.fetch('tags', []),
    'volumes' => volumes
  }

  data[:servers][server['id']] = server

  data[:volumes][server['volumes']['0']['id']] = server['volumes']['0']

  server['volumes'].each do |_index, volume|
    volume['server'] = {
      'id' => server['id'],
      'name' => server['name']
    }
  end

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

#create_snapshot(name, volume_id) ⇒ Object



13
14
15
16
17
18
19
20
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
# File 'lib/fog/scaleway/requests/compute/create_snapshot.rb', line 13

def create_snapshot(name, volume_id)
  body = {
    organization: @organization,
    name: name,
    volume_id: volume_id
  }

  body = jsonify(body)

  base_volume = lookup(:volumes, body['volume_id'])

  server_id = base_volume['server'] && base_volume['server']['id']
  server = lookup(:servers, server_id) if server_id
  in_use = server && server['state'] != 'stopped'

  raise_invalid_request_error('server must be stopped to snapshot') if in_use

  creation_date = now

  snapshot = {
    'state' => 'snapshotting',
    'base_volume' => {
      'id' => base_volume['id'],
      'name' => base_volume['name']
    },
    'name' => body['name'],
    'modification_date' => creation_date,
    'organization' => body['organization'],
    'size' => base_volume['size'],
    'id' => Fog::UUID.uuid,
    'volume_type' => base_volume['volume_type'],
    'creation_date' => creation_date
  }

  data[:snapshots][snapshot['id']] = snapshot

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

#create_volume(name, volume_type, options) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fog/scaleway/requests/compute/create_volume.rb', line 29

def create_volume(name, volume_type, options)
  if options[:size].nil? && options[:base_volume].nil? && options[:base_snapshot].nil?
    raise ArgumentError, 'size, base_volume or base_snapshot are required to create a volume'
  end

  body = {
    organization: @organization,
    name: name,
    volume_type: volume_type
  }

  if !options[:size].nil?
    body[:size] = options[:size]
  elsif !options[:base_volume].nil?
    body[:base_volume] = options[:base_volume]
  else
    body[:base_snapshot] = options[:base_snapshot]
  end

  body = jsonify(body)

  creation_date = now

  if body['size']
    size = body['size']
  elsif body['base_volume']
    base_volume = lookup(:volumes, body['base_volume'])

    server_id = base_volume['server'] && base_volume['server']['id']
    server = lookup(:servers, server_id) if server_id
    in_use = server && server['state'] != 'stopped'

    raise_invalid_request_error('Base volume is in use') if in_use

    size = base_volume['size']
  elsif body['base_snapshot']
    base_snapshot = lookup(:snapshots, body['base_snapshot'])
    size = base_snapshot['size']
  end

  volume = {
    'size' => size,
    'name' => body['name'],
    'modification_date' => creation_date,
    'organization' => body['organization'],
    'export_uri' => nil,
    'creation_date' => creation_date,
    'id' => Fog::UUID.uuid,
    'volume_type' => body['volume_type'],
    'server' => nil
  }

  data[:volumes][volume['id']] = volume

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

#delete_image(image_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/scaleway/requests/compute/delete_image.rb', line 11

def delete_image(image_id)
  image = lookup(:images, image_id)

  data[:images].delete(image['id'])

  data[:servers].each do |_id, server|
    if server['image'] && server['image']['id'] == image['id']
      server['image'] = nil
    end
  end

  response(status: 204)
end

#delete_ip(ip_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/scaleway/requests/compute/delete_ip.rb', line 11

def delete_ip(ip_id)
  ip = lookup(:ips, ip_id)

  data[:ips].delete(ip['id'])

  if ip['server']
    server = lookup(:servers, ip['server']['id'])

    server['public_ip'] = if server['dynamic_ip_required']
                            create_dynamic_ip
                          end
  end

  response(status: 204)
end

#delete_security_group(security_group_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/scaleway/requests/compute/delete_security_group.rb', line 11

def delete_security_group(security_group_id)
  security_group = lookup(:security_groups, security_group_id)

  unless security_group['servers'].empty?
    raise_conflict('Group is in use. You cannot delete it.')
  end

  if security_group['organization_default']
    raise_conflict('Group is default group. You cannot delete it.')
  end

  data[:security_groups].delete(security_group['id'])

  data[:security_group_rules].delete(security_group['id'])

  response(status: 204)
end

#delete_security_group_rule(security_group_id, rule_id) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/fog/scaleway/requests/compute/delete_security_group_rule.rb', line 11

def delete_security_group_rule(security_group_id, rule_id)
  security_group = lookup(:security_groups, security_group_id)

  unless data[:security_group_rules][security_group['id']].delete(rule_id)
    raise_unknown_resource(rule_id)
  end

  response(status: 204)
end

#delete_server(server_id) ⇒ Object



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

def delete_server(server_id)
  server = lookup(:servers, server_id)

  if server['state'] != 'stopped'
    raise_invalid_request_error('server should be stopped')
  end

  data[:servers].delete(server['id'])

  data[:user_data].delete(server['id'])

  data[:server_actions].delete(server['id'])

  if server['public_ip'] && !server['public_ip']['dynamic']
    ip = lookup(:ips, server['public_ip']['id'])
    ip['server'] = nil
  end

  security_group = lookup(:security_groups, server['security_group']['id'])
  security_group['servers'].reject! { |s| s['id'] == server['id'] }

  volumes = server['volumes'].values
  volumes.each { |volume| volume['server'] = nil }

  response(status: 204)
end

#delete_snapshot(snapshot_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/scaleway/requests/compute/delete_snapshot.rb', line 11

def delete_snapshot(snapshot_id)
  snapshot = lookup(:snapshots, snapshot_id)

  in_use = data[:images].any? do |_id, image|
    image['root_volume']['id'] == snapshot['id']
  end

  if in_use
    raise_invalid_request_error('one or more images are attached to this snapshot')
  end

  data[:snapshots].delete(snapshot['id'])

  response(status: 204)
end

#delete_task(task_id) ⇒ Object



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

def delete_task(task_id)
  task = lookup(:tasks, task_id)

  data[:tasks].delete(task['id'])

  response(status: 204)
end

#delete_user_data(server_id, key) ⇒ Object



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

def delete_user_data(server_id, key)
  server = lookup(:servers, server_id)

  data[:user_data][server['id']].delete(key)

  response(status: 204)
end

#delete_volume(volume_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/scaleway/requests/compute/delete_volume.rb', line 11

def delete_volume(volume_id)
  volume = lookup(:volumes, volume_id)

  if volume['server']
    raise_invalid_request_error('a server is attached to this volume')
  end

  data[:volumes].delete(volume_id)

  data[:snapshots].each do |_id, snapshot|
    if snapshot['base_volume'] && snapshot['base_volume']['id'] == volume_id
      snapshot['base_volume'] = nil
    end
  end

  response(status: 204)
end

#execute_server_action(server_id, action) ⇒ Object



16
17
18
19
20
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fog/scaleway/requests/compute/execute_server_action.rb', line 16

def execute_server_action(server_id, action)
  server = lookup(:servers, server_id)

  started_at = now

  task = {
    'status' => 'pending',
    'terminated_at' => nil,
    'href_from' => "/servers/#{server_id}/action",
    'progress' => 0,
    'started_at' => started_at,
    'id' => Fog::UUID.uuid
  }

  case action
  when 'poweron'
    unless server['state'] == 'stopped'
      raise_invalid_request_error('server should be stopped')
    end

    task['description'] = 'server_batch_poweron'

    server['ipv6'] = create_ipv6 if server['enable_ipv6']

    server['location'] = {
      'platform_id' => Fog::Mock.random_numbers(2),
      'node_id' => Fog::Mock.random_numbers(2),
      'cluster_id' => Fog::Mock.random_numbers(2),
      'zone_id' => @region,
      'chassis_id' => Fog::Mock.random_numbers(2)
    }

    server['private_ip'] = Fog::Mock.random_ip

    if server['dynamic_ip_required'] && server['public_ip'].nil?
      server['public_ip'] = create_dynamic_ip
    end

    server['state'] = 'starting'
    server['state_detail'] = 'provisioning node'
    server['modification_date'] = started_at
  when 'poweroff'
    if server['state'] == 'stopping'
      raise_invalid_request_error('server is being stopped or rebooted')
    elsif server['state'] != 'running'
      raise_invalid_request_error('server should be running')
    end

    task['description'] = 'server_poweroff'

    server['state'] = 'stopping'
    server['state_detail'] = 'stopping'
    server['modification_date'] = started_at
  when 'reboot'
    if server['state'] == 'stopping'
      raise_invalid_request_error('server is being stopped or rebooted')
    elsif server['state'] != 'running'
      raise_invalid_request_error('server should be running')
    end

    task['description'] = 'server_reboot'

    server['state'] = 'stopping'
    server['state_detail'] = 'rebooting'
    server['modification_date'] = started_at
  when 'terminate'
    if server['state'] == 'stopping'
      raise_invalid_request_error('server is being stopped or rebooted')
    elsif server['state'] != 'running'
      raise_invalid_request_error('server should be running')
    end

    task['description'] = 'server_terminate'

    server['state'] = 'stopping'
    server['state_detail'] = 'terminating'
    server['modification_date'] = started_at
  end

  data[:tasks][task['id']] = task

  response(status: 202, body: { 'task' => task })
end

#get_bootscript(bootscript_id) ⇒ Object



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

def get_bootscript(bootscript_id)
  bootscript = lookup(:bootscripts, bootscript_id)

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

#get_container(container_id) ⇒ Object



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

def get_container(container_id)
  container = lookup(:containers, container_id)

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

#get_dashboardObject

rubocop:disable Style/AccessorMethodName



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/scaleway/requests/compute/get_dashboard.rb', line 11

def get_dashboard # rubocop:disable Style/AccessorMethodName
  running_servers = data[:servers].select do |_id, s|
    s['state'] == 'running'
  end

  servers_by_types = data[:servers].group_by { |s| s['commercial_type'] }
  servers_count_by_types = Hash[servers_by_types.map { |t, s| [t, s.size] }]

  ips_unused = data[:ips].reject { |ip| ip['server'] }.size

  dashboard = {
    'snapshots_count' => data[:snapshots].size,
    'servers_count' => data[:servers].size,
    'volumes_count' => data[:volumes].size,
    'images_count' => data[:images].size,
    'ips_count' => data[:ips].size,
    'running_servers_count' => running_servers.size,
    'servers_by_types' => servers_count_by_types,
    'ips_unused' => ips_unused
  }

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

#get_image(image_id) ⇒ Object



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

def get_image(image_id)
  image = lookup(:images, image_id)

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

#get_ip(ip_id) ⇒ Object



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

def get_ip(ip_id)
  ip = lookup(:ips, ip_id)

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

#get_security_group(security_group_id) ⇒ Object



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

def get_security_group(security_group_id)
  security_group = lookup(:security_groups, security_group_id)

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

#get_security_group_rule(security_group_id, rule_id) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/fog/scaleway/requests/compute/get_security_group_rule.rb', line 11

def get_security_group_rule(security_group_id, rule_id)
  security_group = lookup(:security_groups, security_group_id)

  rule = data[:security_group_rules][security_group['id']][rule_id]

  raise_unknown_resource(rule_id) unless rule

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

#get_server(server_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/scaleway/requests/compute/get_server.rb', line 11

def get_server(server_id)
  server = lookup(:servers, server_id)

  elapsed_time = Time.now - Time.parse(server['modification_date'])

  if server['state'] == 'starting' && elapsed_time >= Fog::Mock.delay
    server['state'] = 'running'
    server['state_detail'] = 'booted'
    server['modification_date'] = now
  elsif server['state'] == 'stopping' && elapsed_time >= Fog::Mock.delay
    case server['state_detail']
    when 'stopping'
      server['ipv6'] = nil
      server['location'] = nil
      server['private_ip'] = nil

      if server['public_ip'] && server['public_ip']['dynamic']
        server['public_ip'] = nil
      end

      server['state'] = 'stopped'
      server['state_detail'] = ''
      server['modification_date'] = now
    when 'rebooting'
      server['state'] = 'running'
      server['state_detail'] = 'booted'
      server['modification_date'] = now
    when 'terminating'
      terminate_server(server)
    end
  end

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

#get_snapshot(snapshot_id) ⇒ Object



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

def get_snapshot(snapshot_id)
  snapshot = lookup(:snapshots, snapshot_id)

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

#get_task(task_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/scaleway/requests/compute/get_task.rb', line 11

def get_task(task_id)
  task = lookup(:tasks, task_id)

  if Time.now - Time.parse(task['started_at']) >= Fog::Mock.delay
    task['status'] = 'success'
    task['terminated_at'] = now
    task['progress'] = 100
  end

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

#get_user_data(server_id, key) ⇒ Object

Raises:

  • (Excon::Error::NotFound)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/scaleway/requests/compute/get_user_data.rb', line 11

def get_user_data(server_id, key)
  server = lookup(:servers, server_id)

  user_data = data[:user_data][server['id']][key]

  raise Excon::Error::NotFound unless user_data

  response(status: 200,
           headers: { 'Content-Type' => 'text/plain' },
           body: user_data)
end

#get_volume(volume_id) ⇒ Object



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

def get_volume(volume_id)
  volume = lookup(:volumes, volume_id)

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

#list_bootscripts(filters = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/fog/scaleway/requests/compute/list_bootscripts.rb', line 11

def list_bootscripts(filters = {})
  bootscripts = data[:bootscripts].values

  if (arch = filters[:arch])
    bootscripts.select! { |b| b['architecture'] == arch }
  end

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

#list_containersObject



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

def list_containers
  containers = data[:containers].values

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

#list_images(filters = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/scaleway/requests/compute/list_images.rb', line 11

def list_images(filters = {})
  images = data[:images].values

  if (organization = filters[:organization])
    images.select! { |i| i['organization'] == organization }
  end

  if (arch = filters[:arch])
    images.select! { |b| b['arch'] == arch }
  end

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

#list_ipsObject



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

def list_ips
  ips = data[:ips].values

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

#list_security_group_rules(security_group_id) ⇒ Object



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

def list_security_group_rules(security_group_id)
  security_group = lookup(:security_groups, security_group_id)

  rules = data[:security_group_rules][security_group['id']].values

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

#list_security_groupsObject



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

def list_security_groups
  security_groups = data[:security_groups].values

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

#list_server_actions(server_id) ⇒ Object



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

def list_server_actions(server_id)
  server = lookup(:servers, server_id)

  actions = data[:server_actions][server['id']]

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

#list_serversObject



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

def list_servers
  servers = data[:servers].values

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

#list_snapshotsObject



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

def list_snapshots
  snapshots = data[:snapshots].values

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

#list_tasksObject



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

def list_tasks
  tasks = data[:tasks].values

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

#list_user_data(server_id) ⇒ Object



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

def list_user_data(server_id)
  server = lookup(:servers, server_id)

  user_data = data[:user_data][server['id']].keys

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

#list_volumesObject



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

def list_volumes
  volumes = data[:volumes].values

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

#update_image(image_id, body) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/scaleway/requests/compute/update_image.rb', line 11

def update_image(image_id, body)
  body = jsonify(body)

  image = lookup(:images, image_id)

  default_bootscript = if body['default_bootscript'].is_a?(Hash)
                         lookup(:bootscripts, body['default_bootscript']['id'])
                       elsif body['default_bootscript'].is_a?(String)
                         lookup(:bootscripts, body['default_bootscript'])
                       end

  image['default_bootscript'] = default_bootscript
  image['name'] = body['name']
  image['arch'] = body['arch']

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

#update_ip(ip_id, body) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/scaleway/requests/compute/update_ip.rb', line 11

def update_ip(ip_id, body)
  body = jsonify(body)

  ip = lookup(:ips, ip_id)

  server = nil
  if (server_id = body['server'])
    server = lookup(:servers, server_id)

    if server['public_ip'] && !server['public_ip']['dynamic']
      message = "Server '#{server['id']}' is already attached to an IP address"
      raise_invalid_request_error(message)
    end

    server = {
      'id' => server['id'],
      'name' => server['name']
    }
  end

  ip['reverse'] = body['reverse']
  ip['server'] = server

  if server
    server['public_ip'] = {
      'dynamic' => false,
      'id' => ip['id'],
      'address' => ip['address']
    }
  end

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

#update_security_group(security_group_id, body) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/scaleway/requests/compute/update_security_group.rb', line 11

def update_security_group(security_group_id, body)
  body = jsonify(body)

  security_group = lookup(:security_groups, security_group_id)

  if body['organization_default'] && default_security_group
    raise_conflict('Cannot have more than one organization default group')
  end

  security_group['description'] = body['description']
  security_group['enable_default_security'] = body['enable_default_security']
  security_group['name'] = body['name']

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

#update_security_group_rule(security_group_id, rule_id, body) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/scaleway/requests/compute/update_security_group_rule.rb', line 11

def update_security_group_rule(security_group_id, rule_id, body)
  body = jsonify(body)

  security_group = lookup(:security_groups, security_group_id)

  rule = data[:security_group_rules][security_group['id']][rule_id]

  raise_unknown_resource(rule_id) unless rule

  rule['action'] = body['action']
  rule['dest_port_from'] = body['dest_port_from']
  rule['direction'] = body['direction']
  rule['ip_range'] = body['ip_range']
  rule['protocol'] = body['protocol']

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

#update_server(server_id, body) ⇒ Object



11
12
13
14
15
16
17
18
19
20
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
59
60
61
62
63
64
65
66
67
# File 'lib/fog/scaleway/requests/compute/update_server.rb', line 11

def update_server(server_id, body)
  body = jsonify(body)

  server = lookup(:servers, server_id)

  bootscript = if body['bootscript'].is_a?(Hash)
                 lookup(:bootscripts, body['bootscript']['id'])
               elsif body['bootscript'].is_a?(String)
                 lookup(:bootscripts, body['bootscript'])
               end

  volumes = {}
  body['volumes'].each do |index, volume|
    volume = lookup(:volumes, volume['id'])

    if volume['server'] && volume['server']['id'] != server['id']
      message = "volume #{volume['id']} is already attached to a server"
      raise_invalid_request_error(message)
    end

    volumes[index] = volume
  end

  server['bootscript'] = bootscript if bootscript
  server['dynamic_ip_required'] = body['dynamic_ip_required']
  server['enable_ipv6'] = body['enable_ipv6']
  server['hostname'] = body['name']
  server['modification_date'] = now
  server['name'] = body['name']
  server['tags'] = body['tags']
  server['volumes'] = volumes

  if server['dynamic_ip_required'] && server['state'] == 'running'
    server['public_ip'] ||= create_dynamic_ip
  elsif !server['dynamic_ip_required'] && server['public_ip'] && server['public_ip']['dynamic']
    server['public_ip'] = nil
  end

  if server['enable_ipv6'] && server['state'] == 'running'
    server['ipv6'] ||= create_ipv6
  elsif !server['enable_ipv6']
    server['ipv6'] = nil
  end

  data[:volumes].each do |id, volume|
    if server['volumes'].any? { |_i, v| v['id'] == id }
      volume['server'] = {
        'id' => server['id'],
        'name' => server['name']
      }
    elsif volume['server'] && volume['server']['id'] == server['id']
      volume['server'] = nil
    end
  end

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

#update_snapshot(snapshot_id, body) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/scaleway/requests/compute/update_snapshot.rb', line 11

def update_snapshot(snapshot_id, body)
  body = jsonify(body)

  snapshot = lookup(:snapshots, snapshot_id)

  snapshot['modification_date'] = now
  snapshot['name'] = body['name']

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

#update_user_data(server_id, key, value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/fog/scaleway/requests/compute/update_user_data.rb', line 15

def update_user_data(server_id, key, value)
  server = lookup(:servers, server_id)

  data[:user_data][server['id']][key] = value

  response(status: 204)
end

#update_volume(volume_id, body) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/scaleway/requests/compute/update_volume.rb', line 11

def update_volume(volume_id, body)
  body = jsonify(body)

  volume = lookup(:volumes, volume_id)

  volume['modification_date'] = now
  volume['name'] = body['name']

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