fog-scaleway 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +9 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +58 -0
  8. data/Rakefile +21 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/fog-scaleway.gemspec +31 -0
  12. data/lib/fog/scaleway.rb +15 -0
  13. data/lib/fog/scaleway/account.rb +306 -0
  14. data/lib/fog/scaleway/client.rb +54 -0
  15. data/lib/fog/scaleway/compute.rb +350 -0
  16. data/lib/fog/scaleway/errors.rb +37 -0
  17. data/lib/fog/scaleway/models/account/organization.rb +42 -0
  18. data/lib/fog/scaleway/models/account/organizations.rb +22 -0
  19. data/lib/fog/scaleway/models/account/token.rb +60 -0
  20. data/lib/fog/scaleway/models/account/tokens.rb +22 -0
  21. data/lib/fog/scaleway/models/account/user.rb +69 -0
  22. data/lib/fog/scaleway/models/account/users.rb +23 -0
  23. data/lib/fog/scaleway/models/compute/bootscript.rb +19 -0
  24. data/lib/fog/scaleway/models/compute/bootscripts.rb +22 -0
  25. data/lib/fog/scaleway/models/compute/image.rb +83 -0
  26. data/lib/fog/scaleway/models/compute/images.rb +22 -0
  27. data/lib/fog/scaleway/models/compute/ip.rb +70 -0
  28. data/lib/fog/scaleway/models/compute/ips.rb +22 -0
  29. data/lib/fog/scaleway/models/compute/security_group.rb +77 -0
  30. data/lib/fog/scaleway/models/compute/security_group_rule.rb +59 -0
  31. data/lib/fog/scaleway/models/compute/security_group_rules.rb +34 -0
  32. data/lib/fog/scaleway/models/compute/security_groups.rb +22 -0
  33. data/lib/fog/scaleway/models/compute/server.rb +243 -0
  34. data/lib/fog/scaleway/models/compute/servers.rb +47 -0
  35. data/lib/fog/scaleway/models/compute/snapshot.rb +90 -0
  36. data/lib/fog/scaleway/models/compute/snapshots.rb +22 -0
  37. data/lib/fog/scaleway/models/compute/task.rb +31 -0
  38. data/lib/fog/scaleway/models/compute/tasks.rb +22 -0
  39. data/lib/fog/scaleway/models/compute/volume.rb +96 -0
  40. data/lib/fog/scaleway/models/compute/volumes.rb +22 -0
  41. data/lib/fog/scaleway/request_helper.rb +32 -0
  42. data/lib/fog/scaleway/requests/account/create_token.rb +62 -0
  43. data/lib/fog/scaleway/requests/account/delete_token.rb +21 -0
  44. data/lib/fog/scaleway/requests/account/get_organization.rb +19 -0
  45. data/lib/fog/scaleway/requests/account/get_organization_quotas.rb +21 -0
  46. data/lib/fog/scaleway/requests/account/get_token.rb +19 -0
  47. data/lib/fog/scaleway/requests/account/get_token_permissions.rb +21 -0
  48. data/lib/fog/scaleway/requests/account/get_user.rb +19 -0
  49. data/lib/fog/scaleway/requests/account/list_organizations.rb +19 -0
  50. data/lib/fog/scaleway/requests/account/list_tokens.rb +19 -0
  51. data/lib/fog/scaleway/requests/account/update_token.rb +27 -0
  52. data/lib/fog/scaleway/requests/account/update_user.rb +39 -0
  53. data/lib/fog/scaleway/requests/compute/create_image.rb +61 -0
  54. data/lib/fog/scaleway/requests/compute/create_ip.rb +68 -0
  55. data/lib/fog/scaleway/requests/compute/create_security_group.rb +49 -0
  56. data/lib/fog/scaleway/requests/compute/create_security_group_rule.rb +49 -0
  57. data/lib/fog/scaleway/requests/compute/create_server.rb +129 -0
  58. data/lib/fog/scaleway/requests/compute/create_snapshot.rb +54 -0
  59. data/lib/fog/scaleway/requests/compute/create_volume.rb +88 -0
  60. data/lib/fog/scaleway/requests/compute/delete_image.rb +27 -0
  61. data/lib/fog/scaleway/requests/compute/delete_ip.rb +29 -0
  62. data/lib/fog/scaleway/requests/compute/delete_security_group.rb +31 -0
  63. data/lib/fog/scaleway/requests/compute/delete_security_group_rule.rb +23 -0
  64. data/lib/fog/scaleway/requests/compute/delete_server.rb +40 -0
  65. data/lib/fog/scaleway/requests/compute/delete_snapshot.rb +29 -0
  66. data/lib/fog/scaleway/requests/compute/delete_task.rb +21 -0
  67. data/lib/fog/scaleway/requests/compute/delete_user_data.rb +21 -0
  68. data/lib/fog/scaleway/requests/compute/delete_volume.rb +31 -0
  69. data/lib/fog/scaleway/requests/compute/execute_server_action.rb +101 -0
  70. data/lib/fog/scaleway/requests/compute/get_bootscript.rb +19 -0
  71. data/lib/fog/scaleway/requests/compute/get_container.rb +19 -0
  72. data/lib/fog/scaleway/requests/compute/get_dashboard.rb +37 -0
  73. data/lib/fog/scaleway/requests/compute/get_image.rb +19 -0
  74. data/lib/fog/scaleway/requests/compute/get_ip.rb +19 -0
  75. data/lib/fog/scaleway/requests/compute/get_security_group.rb +19 -0
  76. data/lib/fog/scaleway/requests/compute/get_security_group_rule.rb +23 -0
  77. data/lib/fog/scaleway/requests/compute/get_server.rb +48 -0
  78. data/lib/fog/scaleway/requests/compute/get_snapshot.rb +19 -0
  79. data/lib/fog/scaleway/requests/compute/get_task.rb +25 -0
  80. data/lib/fog/scaleway/requests/compute/get_user_data.rb +25 -0
  81. data/lib/fog/scaleway/requests/compute/get_volume.rb +19 -0
  82. data/lib/fog/scaleway/requests/compute/list_bootscripts.rb +23 -0
  83. data/lib/fog/scaleway/requests/compute/list_containers.rb +19 -0
  84. data/lib/fog/scaleway/requests/compute/list_images.rb +27 -0
  85. data/lib/fog/scaleway/requests/compute/list_ips.rb +19 -0
  86. data/lib/fog/scaleway/requests/compute/list_security_group_rules.rb +21 -0
  87. data/lib/fog/scaleway/requests/compute/list_security_groups.rb +19 -0
  88. data/lib/fog/scaleway/requests/compute/list_server_actions.rb +21 -0
  89. data/lib/fog/scaleway/requests/compute/list_servers.rb +19 -0
  90. data/lib/fog/scaleway/requests/compute/list_snapshots.rb +19 -0
  91. data/lib/fog/scaleway/requests/compute/list_tasks.rb +19 -0
  92. data/lib/fog/scaleway/requests/compute/list_user_data.rb +21 -0
  93. data/lib/fog/scaleway/requests/compute/list_volumes.rb +19 -0
  94. data/lib/fog/scaleway/requests/compute/update_image.rb +31 -0
  95. data/lib/fog/scaleway/requests/compute/update_ip.rb +47 -0
  96. data/lib/fog/scaleway/requests/compute/update_security_group.rb +29 -0
  97. data/lib/fog/scaleway/requests/compute/update_security_group_rule.rb +31 -0
  98. data/lib/fog/scaleway/requests/compute/update_server.rb +71 -0
  99. data/lib/fog/scaleway/requests/compute/update_snapshot.rb +24 -0
  100. data/lib/fog/scaleway/requests/compute/update_user_data.rb +25 -0
  101. data/lib/fog/scaleway/requests/compute/update_volume.rb +24 -0
  102. data/lib/fog/scaleway/version.rb +5 -0
  103. metadata +285 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5303513c33ef6ae3857f4151f5ca012fa86d2c47
4
+ data.tar.gz: 9a223682b88e327c3e9cd08d296eb35ba71e7da2
5
+ SHA512:
6
+ metadata.gz: 6320bec3011290491e7afee7e0ce68a3431f04efa20c8a8c2d439bb7c21f06fc0515570d8ef0964faff0b20bc184ca390bbcfbdbb03a493a4e8e44710de50cc0
7
+ data.tar.gz: 8714c0dc342be29bb5ff57fd359fd3706b6d43778f2674687ab4fc3538bc2702e20ede17e48495aaf4155f6e015d509bc65515531ce61bcb930c99ed80399a41
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p648
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ before_install:
6
+ - gem install bundler
7
+ script:
8
+ - bundle exec rake test:units FOG_MOCK=true COVERAGE=true
9
+ - bundle exec rake test:integration FOG_MOCK=true
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fog-scaleway.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Satoshi Matsumoto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Fog::Scaleway
2
+
3
+ [![Travis](https://img.shields.io/travis/kaorimatz/fog-scaleway.svg?style=flat-square)](https://travis-ci.org/kaorimatz/fog-scaleway)
4
+ [![Coveralls](https://img.shields.io/coveralls/kaorimatz/fog-scaleway.svg?style=flat-square)](https://coveralls.io/github/kaorimatz/fog-scaleway)
5
+
6
+ Fog provider gem to support [Scaleway](https://www.scaleway.com/).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'fog-scaleway', git: 'https://github.com/kaorimatz/fog-scaleway.git'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ ## Usage
21
+
22
+ Put your credentials to the fog configuration file:
23
+
24
+ ```yaml
25
+ default:
26
+ scaleway_organization: <YOUR_ORGANIZATION_UUID>
27
+ scaleway_token: <YOUR_TOKEN>
28
+ ```
29
+
30
+ Create a connection to the service:
31
+
32
+ ```ruby
33
+ compute = Fog::Compute[:scaleway]
34
+ ```
35
+
36
+ Manage servers and resources using the connection:
37
+
38
+ ```ruby
39
+ server = compute.servers.bootstrap
40
+
41
+ server.ssh('uname').first.stdout # => "Linux\r\n"
42
+
43
+ server.terminate
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test:integration` to run the integration tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kaorimatz/fog-scaleway.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rubocop/rake_task'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.name = 'test:units'
8
+ t.description = 'Run unit tests'
9
+ t.test_files = FileList['test/units/**/test_*.rb']
10
+ t.warning = false
11
+ end
12
+
13
+ Rake::TestTask.new do |t|
14
+ t.libs << 'test'
15
+ t.name = 'test:integration'
16
+ t.description = 'Run integration tests'
17
+ t.test_files = FileList['test/integration/**/test_*.rb']
18
+ t.warning = false
19
+ end
20
+
21
+ RuboCop::RakeTask.new
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'fog/scaleway'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fog/scaleway/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fog-scaleway'
8
+ spec.version = Fog::Scaleway::VERSION
9
+ spec.authors = ['Satoshi Matsumoto']
10
+ spec.email = ['kaorimatz@gmail.com']
11
+
12
+ spec.summary = 'Fog provider for Scaleway'
13
+ spec.description = 'Fog provider gem to support Scaleway.'
14
+ spec.homepage = 'https://github.com/kaorimatz/fog-scaleway'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'coveralls'
22
+ spec.add_development_dependency 'minitest'
23
+ spec.add_development_dependency 'net-ssh'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'simplecov'
28
+
29
+ spec.add_dependency 'fog-core', '~> 1.42'
30
+ spec.add_dependency 'fog-json', '~> 1.0'
31
+ end
@@ -0,0 +1,15 @@
1
+ require 'fog/core'
2
+ require 'fog/json'
3
+ require 'fog/scaleway/version'
4
+
5
+ module Fog
6
+ module Scaleway
7
+ extend Fog::Provider
8
+
9
+ autoload :Account, File.expand_path('../scaleway/account', __FILE__)
10
+ autoload :Compute, File.expand_path('../scaleway/compute', __FILE__)
11
+
12
+ service :account, 'Account'
13
+ service :compute, 'Compute'
14
+ end
15
+ end
@@ -0,0 +1,306 @@
1
+ require 'fog/scaleway/client'
2
+ require 'fog/scaleway/errors'
3
+ require 'fog/scaleway/request_helper'
4
+
5
+ module Fog
6
+ module Scaleway
7
+ class Account < Fog::Service
8
+ class InvalidRequestError < Error; end
9
+ class InvalidAuth < Error; end
10
+ class UnknownResource < Error; end
11
+ class AuthorizationRequired < Error; end
12
+ class APIError < Error; end
13
+
14
+ requires :scaleway_token
15
+ recognizes :scaleway_email
16
+ secrets :scaleway_token
17
+
18
+ model_path 'fog/scaleway/models/account'
19
+
20
+ model :token
21
+ collection :tokens
22
+ model :organization
23
+ collection :organizations
24
+ model :user
25
+ collection :users
26
+
27
+ request_path 'fog/scaleway/requests/account'
28
+
29
+ # Tokens
30
+ request :create_token
31
+ request :list_tokens
32
+ request :get_token
33
+ request :update_token
34
+ request :delete_token
35
+ request :get_token_permissions
36
+
37
+ # Organizations
38
+ request :list_organizations
39
+ request :get_organization
40
+ request :get_organization_quotas
41
+
42
+ # Users
43
+ request :get_user
44
+ request :update_user
45
+
46
+ class Real
47
+ include Fog::Scaleway::RequestHelper
48
+
49
+ def initialize(options)
50
+ @token = options[:scaleway_token]
51
+ @email = options[:scaleway_email]
52
+ end
53
+
54
+ def request(params)
55
+ client.request(params)
56
+ rescue Excon::Errors::HTTPStatusError => error
57
+ decoded = Fog::Scaleway::Errors.decode_error(error)
58
+ raise if decoded.nil?
59
+
60
+ type = decoded[:type]
61
+ message = decoded[:message]
62
+
63
+ raise case type
64
+ when 'invalid_request_error', 'invalid_auth', 'unknown_resource', 'authorization_required'
65
+ Fog::Scaleway::Account.const_get(camelize(type)).slurp(error, message)
66
+ when 'api_error'
67
+ Fog::Scaleway::Account::APIError.slurp(error, message)
68
+ else
69
+ Fog::Scaleway::Account::Error.slurp(error, message)
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def client
76
+ @client ||= Fog::Scaleway::Client.new('https://account.scaleway.com', @token)
77
+ end
78
+
79
+ def camelize(str)
80
+ str.split('_').collect(&:capitalize).join
81
+ end
82
+ end
83
+
84
+ class Mock
85
+ TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%6N%:z'.freeze
86
+
87
+ ORGANIZATION = {
88
+ 'address_line2' => 'ADDRESS LINE2',
89
+ 'address_country_code' => 'ADDRESS COUNTRY CODE',
90
+ 'address_line1' => 'ADDRESS LINE1',
91
+ 'support_level' => 'SUPPORT LEVEL',
92
+ 'name' => 'ORGANIZATION',
93
+ 'modification_date' => Time.now.utc.strftime(TIME_FORMAT),
94
+ 'currency' => 'CURRENCY',
95
+ 'locale' => 'LOCALE',
96
+ 'customer_class' => 'CUSTOMER CLASS',
97
+ 'support_id' => 'SUPPORT ID',
98
+ 'creation_date' => Time.now.utc.strftime(TIME_FORMAT),
99
+ 'address_postal_code' => 'ADDRESS POSTAL CODE',
100
+ 'address_city_name' => 'ADDRESS CITY NAME',
101
+ 'address_subdivision_code' => 'ADDRESS SUBDIVISION CODE',
102
+ 'timezone' => 'TIMEZONE',
103
+ 'vat_number' => nil,
104
+ 'support_pin' => 'SUPPORT PIN',
105
+ 'id' => '7c91ef18-eea8-4f4f-bfca-deaea872338c',
106
+ 'warnings' => [],
107
+ 'users' => [{
108
+ 'phone_number' => nil,
109
+ 'firstname' => 'FIRSTNAME',
110
+ 'lastname' => 'LASTNAME',
111
+ 'creation_date' => Time.now.utc.strftime(TIME_FORMAT),
112
+ 'ssh_public_keys' => [],
113
+ 'id' => 'e9459194-0f66-4958-b3c3-01e623d21566',
114
+ 'organizations' => [{
115
+ 'id' => '7c91ef18-eea8-4f4f-bfca-deaea872338c',
116
+ 'name' => 'ORGANIZATION'
117
+ }],
118
+ 'modification_date' => Time.now.utc.strftime(TIME_FORMAT),
119
+ 'roles' => [{
120
+ 'organization' => {
121
+ 'id' => '7c91ef18-eea8-4f4f-bfca-deaea872338c',
122
+ 'name' => 'ORGANIZATION'
123
+ },
124
+ 'role' => 'ROLE'
125
+ }],
126
+ 'fullname' => 'FULLNAME',
127
+ 'email' => 'scaleway@example.com'
128
+ }]
129
+ }.freeze
130
+
131
+ ORGANIZATION_QUOTAS = {
132
+ 'sis' => 100,
133
+ 'servers_type_VC1M' => 25,
134
+ 'servers_type_VC1L' => 10,
135
+ 'servers_type_C1' => 2,
136
+ 'servers_type_C2M' => 5,
137
+ 'servers_type_C2L' => 4,
138
+ 'ips' => 10,
139
+ 'snapshots' => 25,
140
+ 'servers' => 10,
141
+ 'security_rules' => 30,
142
+ 'servers_type_C2S' => 10,
143
+ 'trusted' => 1,
144
+ 'volumes' => 20,
145
+ 'images' => 100,
146
+ 'servers_type_VC1S' => 50,
147
+ 'invites' => 100,
148
+ 'security_groups' => 100
149
+ }.freeze
150
+
151
+ TOKEN_PERMISSIONS = {
152
+ 'account' => {
153
+ 'token:*' => [ORGANIZATION['users'][0]['id']],
154
+ 'organization:*' => [ORGANIZATION['id']],
155
+ 'user:*' => [ORGANIZATION['users'][0]['id']]
156
+ },
157
+ 'task' => { 'tasks:*' => ["#{ORGANIZATION['id']}:*"] },
158
+ 'compute' => {
159
+ 'security_groups:*' => ["#{ORGANIZATION['id']}:*"],
160
+ 'images:*' => ["#{ORGANIZATION['id']}:*"],
161
+ 'servers:*' => ["#{ORGANIZATION['id']}:*"],
162
+ 'ips:*' => ["#{ORGANIZATION['id']}:*"],
163
+ 'snapshots:*' => ["#{ORGANIZATION['id']}:*"],
164
+ 'volumes:*' => ["#{ORGANIZATION['id']}:*"]
165
+ },
166
+ 'billing' => {
167
+ 'usages:*' => ["#{ORGANIZATION['id']}:*"],
168
+ 'invoices:*' => ["#{ORGANIZATION['id']}:*"]
169
+ },
170
+ 'storage' => {
171
+ 'storage:*' => ["#{ORGANIZATION['id']}:*"]
172
+ },
173
+ 'dns' => {
174
+ 'dns:*' => ["#{ORGANIZATION['id']}:*"]
175
+ },
176
+ 'ticket' => {
177
+ 'tickets:*' => [ORGANIZATION['users'][0]['id']]
178
+ },
179
+ 'payment' => {
180
+ 'cards:*' => ["#{ORGANIZATION['id']}:*"]
181
+ }
182
+ }.freeze
183
+
184
+ def self.data
185
+ @data ||= Hash.new do |hash, token|
186
+ hash[token] = {
187
+ organizations: { ORGANIZATION['id'] => ORGANIZATION },
188
+ organization_quotas: Hash.new(ORGANIZATION_QUOTAS),
189
+ tokens: {},
190
+ token_permissions: Hash.new(TOKEN_PERMISSIONS),
191
+ users: { ORGANIZATION['users'][0]['id'] => ORGANIZATION['users'][0] }
192
+ }
193
+ end
194
+ end
195
+
196
+ def initialize(options)
197
+ @token = options[:scaleway_token]
198
+ @email = options[:scaleway_email]
199
+ end
200
+
201
+ def self.reset
202
+ @data = nil
203
+ end
204
+
205
+ private
206
+
207
+ def data
208
+ self.class.data[@token]
209
+ end
210
+
211
+ def lookup(type, id)
212
+ data[type][id] || raise_unknown_resource(id)
213
+ end
214
+
215
+ def response(params)
216
+ params[:headers] ||= {}
217
+ params[:headers]['Content-Type'] ||= 'application/json'
218
+
219
+ params[:body] = encode_body(params)
220
+
221
+ response = Excon::Response.new(params)
222
+
223
+ response.body = decode_body(response)
224
+
225
+ response
226
+ end
227
+
228
+ def encode_body(params)
229
+ body = params[:body]
230
+ content_type = params[:headers]['Content-Type']
231
+
232
+ if body.nil? || body.is_a?(String)
233
+ body
234
+ elsif content_type =~ %r{application/json.*}i
235
+ Fog::JSON.encode(body)
236
+ else
237
+ body.to_s
238
+ end
239
+ end
240
+
241
+ def decode_body(response)
242
+ body = response.body
243
+ content_type = response.headers['Content-Type']
244
+
245
+ if !body.nil? && !body.empty? && content_type =~ %r{application/json.*}i
246
+ Fog::JSON.decode(body)
247
+ else
248
+ body
249
+ end
250
+ end
251
+
252
+ def raise_invalid_request_error(message)
253
+ raise Fog::Scaleway::Account::InvalidRequestError, message
254
+ end
255
+
256
+ def raise_invalid_auth(message)
257
+ raise Fog::Scaleway::Account::InvalidAuth, message
258
+ end
259
+
260
+ def raise_unknown_resource(id)
261
+ raise Fog::Scaleway::Account::UnknownResource, "\"#{id}\" not found"
262
+ end
263
+
264
+ def now
265
+ Time.now.utc.strftime(TIME_FORMAT)
266
+ end
267
+
268
+ def jsonify(value)
269
+ Fog::JSON.decode(Fog::JSON.encode(value))
270
+ end
271
+
272
+ def generate_fingerprint(ssh_public_key)
273
+ require 'base64'
274
+ require 'digest/md5'
275
+
276
+ type, key, comment = ssh_public_key.split(' ', 3)
277
+
278
+ unless type == 'ssh-rsa'
279
+ raise ArgumentError, "Unsupported public key format: #{type}"
280
+ end
281
+
282
+ size = decode_public_key(Base64.decode64(key)).last.num_bytes * 8
283
+
284
+ fingerprint = Digest::MD5.hexdigest(Base64.decode64(key)).scan(/../).join(':')
285
+
286
+ "#{size} #{fingerprint} #{comment} (RSA)"
287
+ end
288
+
289
+ def decode_public_key(key)
290
+ type, *nums = unpack_public_key(key)
291
+ [type] + nums.map { |n| OpenSSL::BN.new(n, 2) }
292
+ end
293
+
294
+ def unpack_public_key(key)
295
+ vs = []
296
+ until key.empty?
297
+ n, key = key.unpack('Na*')
298
+ v, key = key.unpack("a#{n}a*")
299
+ vs << v
300
+ end
301
+ vs
302
+ end
303
+ end
304
+ end
305
+ end
306
+ end