leaseweb-rest-api 1.1.6 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +4 -4
- data/.rubocop.yml +4 -1
- data/{readme.md → README.md} +17 -25
- data/leaseweb-rest-api.gemspec +6 -8
- data/lib/leaseweb-rest-api.rb +139 -230
- metadata +11 -45
- data/lib/hash-to-uri-conversion.rb +0 -32
- data/spec/lib/leaseweb-rest-api_spec.rb +0 -648
- data/spec/spec_helper.rb +0 -17
- data/test/id_rsa +0 -54
- data/test/id_rsa.pub.pem +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 505edde91242f9a8cca44007236f98d3b118c01d215d79369ca43ebebbdf3660
|
4
|
+
data.tar.gz: 94056e4073bb03f3172e4e74988403b8af18674887d4f984b084f56cbc7b6ffd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5ebd3f32db5bd37531e03f311dd9bb18a9d22e9ff64fc87bef86399f9885d1ac40ba704ba56d538613b7a68c41ca47d102f0ab13c1773751942f9fda0bc1bb1
|
7
|
+
data.tar.gz: 1914470197ad5f49fc83a144267d939855bbb951a2c29617477b4b5b2e95ccf38aa536f2c0ee343eba75225029e87acdbbf631c02346a6660e4b91cf300c7a9b
|
data/.gitignore
CHANGED
@@ -27,9 +27,9 @@ build/
|
|
27
27
|
|
28
28
|
# for a library or gem, you might want to ignore these files since the code is
|
29
29
|
# intended to run in multiple environments; otherwise, check them in:
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
Gemfile.lock
|
31
|
+
.ruby-version
|
32
|
+
.ruby-gemset
|
33
33
|
|
34
34
|
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
-
.rvmrc
|
35
|
+
.rvmrc
|
data/.rubocop.yml
CHANGED
data/{readme.md → README.md}
RENAMED
@@ -1,9 +1,11 @@
|
|
1
1
|
leaseweb-rest-api
|
2
|
-
|
2
|
+
=================
|
3
3
|
|
4
4
|
Rubygem to talk to Leaseweb's API
|
5
5
|
|
6
|
-
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
7
9
|
|
8
10
|
Add this line to your application's Gemfile:
|
9
11
|
|
@@ -23,30 +25,18 @@ Or install it yourself:
|
|
23
25
|
$ gem install leaseweb-rest-api
|
24
26
|
```
|
25
27
|
|
26
|
-
## Generating a public/private keypair
|
27
|
-
|
28
|
-
Make sure you add a strong password to your SSH key!
|
29
|
-
|
30
|
-
```
|
31
|
-
ssh-keygen -t rsa -b 4096 -C "test@example.com" -f id_rsa
|
32
|
-
openssl rsa -in id_rsa -pubout > id_rsa.pub.pem
|
33
|
-
rm id_rsa.pub
|
34
|
-
```
|
35
|
-
|
36
|
-
Copy the content of id_rsa.pub.pem to the 'Public RSA Key'-field your [SSC API page](https://secure.leaseweb.nl/en/sscApi). Click 'Show API key' for your API key. Keep your id_rsa file private.
|
37
28
|
|
38
|
-
|
29
|
+
Usage
|
30
|
+
-----
|
39
31
|
|
40
|
-
Start by creating a new instance of the `LeasewebAPI` class, and passing your
|
32
|
+
Start by creating a new instance of the `LeasewebAPI` class, and passing your
|
33
|
+
api key, private key and private key password.
|
41
34
|
|
42
35
|
```ruby
|
43
|
-
api_key = '
|
44
|
-
privateKeyFile = './id_rsa'
|
45
|
-
password = 'my_super_strong_s3cr3t_passw0rd'
|
36
|
+
api_key = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
46
37
|
|
47
38
|
api = LeasewebAPI.new
|
48
39
|
api.apiKeyAuth(api_key)
|
49
|
-
api.readPrivateKey(privateKeyFile, password)
|
50
40
|
```
|
51
41
|
|
52
42
|
or via oAuth:
|
@@ -54,19 +44,19 @@ or via oAuth:
|
|
54
44
|
```ruby
|
55
45
|
client_id = 'sadasdasd.asdasdasd.com'
|
56
46
|
client_secret = 'a844f7c131a7b63e4129cbcf88352034ef11c86481e0cb2ed3653c07345a113b'
|
57
|
-
privateKeyFile = './id_rsa'
|
58
|
-
password = 'my_super_strong_s3cr3t_passw0rd'
|
59
47
|
|
60
48
|
api = LeasewebAPI.new
|
61
49
|
api.getOauthToken(client_id, client_secret)
|
62
|
-
api.readPrivateKey(privateKeyFile, password)
|
63
50
|
```
|
64
51
|
|
65
|
-
All return values are the direct JSON responses from Leaseweb converted into a
|
52
|
+
All return values are the direct JSON responses from Leaseweb converted into a
|
53
|
+
Hash.
|
66
54
|
|
67
55
|
See: [documentation](http://developer.leaseweb.com/docs/)
|
68
56
|
|
69
|
-
|
57
|
+
|
58
|
+
Managing servers
|
59
|
+
----------------
|
70
60
|
|
71
61
|
List my baremetal servers:
|
72
62
|
|
@@ -118,7 +108,9 @@ List all my domains:
|
|
118
108
|
puts api.getDomains
|
119
109
|
```
|
120
110
|
|
121
|
-
|
111
|
+
|
112
|
+
Contribute
|
113
|
+
----------
|
122
114
|
|
123
115
|
1. Fork it
|
124
116
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
data/leaseweb-rest-api.gemspec
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'leaseweb-rest-api'
|
3
|
-
s.version = '
|
4
|
-
s.
|
5
|
-
s.
|
6
|
-
s.
|
3
|
+
s.version = '2.0.0'
|
4
|
+
s.date = '2020-04-01'
|
5
|
+
s.authors = 'Leaseweb Product Owners'
|
6
|
+
s.email = 'productowners@leaseweb.com'
|
7
|
+
s.license = 'Apache-2.0'
|
7
8
|
s.summary = 'Leaseweb REST API client for Ruby'
|
8
9
|
s.description = 'Leaseweb REST API client for Ruby.'
|
9
10
|
s.homepage = 'https://github.com/LeaseWeb/leaseweb-rest-api'
|
10
11
|
|
11
12
|
s.files = `git ls-files`.split("\n")
|
12
13
|
|
13
|
-
s.
|
14
|
-
|
15
|
-
s.add_development_dependency 'rspec'
|
16
|
-
s.add_development_dependency 'webmock'
|
14
|
+
s.add_runtime_dependency 'httparty', '~> 0.18.0'
|
17
15
|
end
|
data/lib/leaseweb-rest-api.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'httparty'
|
4
4
|
require 'base64'
|
5
5
|
require 'time'
|
6
6
|
require 'json'
|
7
|
-
require_relative 'hash-to-uri-conversion'
|
8
7
|
|
9
8
|
class LeasewebAPI
|
10
9
|
include HTTParty
|
@@ -14,86 +13,40 @@ class LeasewebAPI
|
|
14
13
|
base_uri 'https://api.leaseweb.com'
|
15
14
|
|
16
15
|
def initialize
|
17
|
-
@tmpdir =
|
16
|
+
@tmpdir = '/tmp/lsw-rest-api'
|
18
17
|
Dir.mkdir(@tmpdir) unless Dir.exist?(@tmpdir)
|
19
18
|
end
|
20
19
|
|
21
20
|
def apiKeyAuth(apikey)
|
22
|
-
@options = {
|
21
|
+
@options = {
|
22
|
+
headers: {
|
23
|
+
'X-Lsw-Auth' => apikey,
|
24
|
+
'Content-Type' => 'application/json'
|
25
|
+
}
|
26
|
+
}
|
23
27
|
end
|
24
28
|
|
25
29
|
def getOauthToken(client_id, client_secret)
|
26
30
|
access_token = validate_token(client_id)
|
27
31
|
|
28
|
-
if
|
32
|
+
if access_token == false
|
29
33
|
response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: client_id, password: client_secret }, body: { grant_type: 'client_credentials' })
|
30
34
|
access_token = response.parsed_response['access_token']
|
31
35
|
cache_token(client_id, access_token, response.parsed_response['expires_in'])
|
32
36
|
end
|
33
37
|
|
34
|
-
@options = {
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
if (access_token == false)
|
41
|
-
response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: client_id, password: client_secret }, body: { grant_type: 'password', username: username, password: password })
|
42
|
-
access_token = response.parsed_response['access_token']
|
43
|
-
cache_token(client_id, access_token, response.parsed_response['expires_in'])
|
44
|
-
end
|
45
|
-
|
46
|
-
@options = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
|
47
|
-
end
|
48
|
-
|
49
|
-
def validate_token(client_id)
|
50
|
-
begin
|
51
|
-
file = "#{@tmpdir}/#{client_id}.json"
|
52
|
-
content = JSON.parse(File.read(file))
|
53
|
-
expires_at = DateTime.parse(content['expires_at'])
|
54
|
-
|
55
|
-
if expires_at > DateTime.now
|
56
|
-
return content['access_token']
|
57
|
-
else
|
58
|
-
File.delete(file)
|
59
|
-
end
|
60
|
-
rescue
|
61
|
-
return false
|
62
|
-
end
|
63
|
-
|
64
|
-
return false
|
65
|
-
end
|
66
|
-
|
67
|
-
def cache_token(client_id, access_token, expires_in)
|
68
|
-
file = "#{@tmpdir}/#{client_id}.json"
|
69
|
-
content = { access_token: access_token, expires_at: Time.now.getutc + expires_in }.to_json
|
70
|
-
File.write(file, content)
|
71
|
-
end
|
72
|
-
|
73
|
-
def readPrivateKey(privateKey, password)
|
74
|
-
@private_key = OpenSSL::PKey::RSA.new(File.read(privateKey), password)
|
38
|
+
@options = {
|
39
|
+
headers: {
|
40
|
+
'Authorization' => "Bearer #{access_token}",
|
41
|
+
'Content-Type' => 'application/json'
|
42
|
+
}
|
43
|
+
}
|
75
44
|
end
|
76
45
|
|
77
46
|
def get(url)
|
78
47
|
self.class.get(url, @options)
|
79
48
|
end
|
80
49
|
|
81
|
-
def get_paginated(url, key, offset = 0, limit = 50)
|
82
|
-
data = []
|
83
|
-
|
84
|
-
loop do
|
85
|
-
response = self.class.get("#{url}&offset=#{offset}&limit=#{limit}", @options)
|
86
|
-
total = response.parsed_response['_metadata']['totalCount']
|
87
|
-
|
88
|
-
data += response.parsed_response[key]
|
89
|
-
|
90
|
-
offset += limit
|
91
|
-
break unless offset < total
|
92
|
-
end
|
93
|
-
|
94
|
-
data
|
95
|
-
end
|
96
|
-
|
97
50
|
def post(url, body)
|
98
51
|
opt = @options.merge!(body: body)
|
99
52
|
self.class.post(url, opt)
|
@@ -108,283 +61,240 @@ class LeasewebAPI
|
|
108
61
|
self.class.delete(url, @options)
|
109
62
|
end
|
110
63
|
|
111
|
-
|
112
|
-
|
113
|
-
self.class.get('/v1/domains', @options)
|
114
|
-
end
|
115
|
-
|
116
|
-
def getDomain(domain)
|
117
|
-
self.class.get("/v1/domains/#{domain}", @options)
|
118
|
-
end
|
119
|
-
|
120
|
-
def updateDomain(domain, ttl)
|
121
|
-
opt = @options.merge!(body: { ttl: ttl })
|
122
|
-
|
123
|
-
self.class.put("/v1/domains/#{domain}", opt)
|
124
|
-
end
|
125
|
-
|
126
|
-
def getDNSRecords(domain)
|
127
|
-
self.class.get("/v1/domains/#{domain}/dnsRecords", @options)
|
128
|
-
end
|
64
|
+
def get_paginated(url, key, offset = 0, limit = 50)
|
65
|
+
data = []
|
129
66
|
|
130
|
-
|
131
|
-
|
67
|
+
loop do
|
68
|
+
response = self.class.get("#{url}&offset=#{offset}&limit=#{limit}", @options)
|
69
|
+
total = response.parsed_response['_metadata']['totalCount']
|
70
|
+
data += response.parsed_response[key]
|
71
|
+
offset += limit
|
132
72
|
|
133
|
-
|
134
|
-
opt[:body][:priority] = priority
|
73
|
+
break unless offset < total
|
135
74
|
end
|
136
75
|
|
137
|
-
|
138
|
-
end
|
139
|
-
|
140
|
-
def getDNSRecord(domain, dnsRecordId)
|
141
|
-
self.class.get("/v1/domains/#{domain}/dnsRecords/#{dnsRecordId}", @options)
|
76
|
+
data
|
142
77
|
end
|
143
78
|
|
144
|
-
def
|
145
|
-
|
146
|
-
|
147
|
-
if !priority.nil? && ((type == 'MX') || (type == 'SRV'))
|
148
|
-
opt[:body][:priority] = priority
|
149
|
-
end
|
150
|
-
|
151
|
-
self.class.put("/v1/domains/#{domain}/dnsRecords/#{dnsRecordId}", opt)
|
79
|
+
def rebootDedicatedServer(server_id)
|
80
|
+
self.class.post("/bareMetals/v2/servers/#{server_id}/powerCycle", @options)
|
152
81
|
end
|
153
82
|
|
154
|
-
def
|
155
|
-
self.class.
|
83
|
+
def powerOnDedicatedServer(server_id)
|
84
|
+
self.class.post("/bareMetals/v2/servers/#{server_id}/powerOn", @options)
|
156
85
|
end
|
157
86
|
|
158
|
-
|
159
|
-
|
160
|
-
self.class.get('/v1/rescueImages', @options)
|
161
|
-
end
|
87
|
+
def launchRescueModeDedicatedServer(server_id, rescue_image_id, ssh_key)
|
88
|
+
self.class.post("/bareMetals/v2/servers/#{server_id}/cancelActiveJob", @options)
|
162
89
|
|
163
|
-
|
164
|
-
def getBareMetals
|
165
|
-
self.class.get('/v1/bareMetals', @options)
|
166
|
-
end
|
90
|
+
opt = @options.merge!(body: { rescueImageId: rescue_image_id, sshKeys: ssh_key, powerCycle: true }.to_json)
|
167
91
|
|
168
|
-
|
169
|
-
self.class.get("/v1/bareMetals/#{bareMetalId}", @options)
|
92
|
+
self.class.post("/bareMetals/v2/servers/#{server_id}/rescueMode", opt)
|
170
93
|
end
|
171
94
|
|
172
|
-
def
|
173
|
-
|
95
|
+
def getDedicatedServers(result = nil)
|
96
|
+
partialSize = (result && result['servers'] && result['servers'].size) || 0
|
97
|
+
partialResult = self.class.get("/bareMetals/v2/servers?offset=#{partialSize}&limit=50", @options)
|
174
98
|
|
175
|
-
|
176
|
-
end
|
99
|
+
return partialResult if partialResult['errorMessage']
|
177
100
|
|
178
|
-
|
179
|
-
|
180
|
-
|
101
|
+
if result.nil?
|
102
|
+
result = partialResult
|
103
|
+
else
|
104
|
+
result['servers'] += partialResult['servers']
|
105
|
+
result['_metadata']['offset'] = 0
|
106
|
+
result['_metadata']['limit'] = partialResult['_metadata']['totalCount']
|
107
|
+
end
|
181
108
|
|
182
|
-
|
183
|
-
self.class.post("/v1/bareMetals/#{bareMetalId}/switchPort/open", @options)
|
184
|
-
end
|
109
|
+
return getDedicatedServers(result) if result['servers'].size < partialResult['_metadata']['totalCount']
|
185
110
|
|
186
|
-
|
187
|
-
self.class.post("/v1/bareMetals/#{bareMetalId}/switchPort/close", @options)
|
111
|
+
result
|
188
112
|
end
|
189
113
|
|
190
|
-
def
|
191
|
-
self.class.get("/
|
114
|
+
def getDedicatedServerByIp(ip)
|
115
|
+
self.class.get("/internal/dedicatedserverapi/v2/servers?ip=#{ip}", @options)
|
192
116
|
end
|
193
117
|
|
194
|
-
def
|
195
|
-
self.class.get("/
|
118
|
+
def getDedicatedServer(server_id)
|
119
|
+
self.class.get("/bareMetals/v2/servers/#{server_id}", @options)
|
196
120
|
end
|
197
121
|
|
198
|
-
def
|
199
|
-
self.class.get("/
|
122
|
+
def getDedicatedServerHardware(server_id)
|
123
|
+
self.class.get("/bareMetals/v2/servers/#{server_id}/hardwareInfo", @options)
|
200
124
|
end
|
201
125
|
|
202
|
-
def
|
203
|
-
|
204
|
-
|
205
|
-
self.class.put("/v1/bareMetals/#{bareMetalId}/ips/#{ipAddress}", opt)
|
126
|
+
def getDedicatedServerDhcpReservation(server_id)
|
127
|
+
result = self.class.get("/bareMetals/v2/servers/#{server_id}/leases", @options)
|
128
|
+
result['leases'].pop
|
206
129
|
end
|
207
130
|
|
208
|
-
def
|
209
|
-
|
131
|
+
def createDedicatedServerDhcpReservation(server_id, boot_file_name)
|
132
|
+
opt = @options.merge!(body: { bootFileName: boot_file_name }.to_json)
|
133
|
+
self.class.post("/bareMetals/v2/servers/#{server_id}/leases", opt)
|
210
134
|
end
|
211
135
|
|
212
|
-
def
|
213
|
-
self.class.
|
136
|
+
def removeDedicatedServerDhcpReservation(server_id)
|
137
|
+
self.class.delete("/bareMetals/v2/servers/#{server_id}/leases", @options)
|
214
138
|
end
|
215
139
|
|
216
|
-
def
|
217
|
-
self.class.
|
140
|
+
def powerOnVirtualServer(server_id)
|
141
|
+
self.class.post("/cloud/v2/virtualServers/#{server_id}/powerOn", @options)
|
218
142
|
end
|
219
143
|
|
220
|
-
def
|
221
|
-
self.class.
|
144
|
+
def powerOffVirtualServer(server_id)
|
145
|
+
self.class.post("/cloud/v2/virtualServers/#{server_id}/powerOff", @options)
|
222
146
|
end
|
223
147
|
|
224
|
-
def
|
225
|
-
self.class.post("/
|
148
|
+
def rebootVirtualServer(server_id)
|
149
|
+
self.class.post("/cloud/v2/virtualServers/#{server_id}/reboot", @options)
|
226
150
|
end
|
227
151
|
|
228
|
-
def
|
229
|
-
|
230
|
-
|
231
|
-
self.class.post("/v1/bareMetals/#{bareMetalId}/install", opt)
|
232
|
-
end
|
233
|
-
|
234
|
-
def postResqueMode(bareMetalId, osId)
|
235
|
-
opt = @options.merge!(body: { osId: osId })
|
236
|
-
|
237
|
-
self.class.post("/v1/bareMetals/#{bareMetalId}/rescueMode", opt)
|
152
|
+
def reinstallVirtualServer(server_id)
|
153
|
+
self.class.post("/cloud/v2/virtualServers/#{server_id}/reinstall", @options)
|
238
154
|
end
|
239
155
|
|
240
|
-
def
|
241
|
-
|
242
|
-
|
243
|
-
self.class.get("/v1/bareMetals/#{bareMetalId}/rootPassword", opt)
|
244
|
-
end
|
156
|
+
def getVirtualServers(result = nil)
|
157
|
+
partialSize = (result && result['virtualServers'] && result['virtualServers'].size) || 0
|
158
|
+
partialResult = self.class.get("/cloud/v2/virtualServers?offset=#{partialSize}&limit=50", @options)
|
245
159
|
|
246
|
-
|
247
|
-
response = self.class.get("/v1/bareMetals/#{bareMetalId}/installationStatus", @options)
|
160
|
+
return partialResult if partialResult['errorMessage']
|
248
161
|
|
249
|
-
if
|
250
|
-
|
162
|
+
if result.nil?
|
163
|
+
result = partialResult
|
164
|
+
else
|
165
|
+
result['virtualServers'] += partialResult['virtualServers']
|
166
|
+
result['_metadata']['offset'] = 0
|
167
|
+
result['_metadata']['limit'] = partialResult['_metadata']['totalCount']
|
251
168
|
end
|
252
169
|
|
253
|
-
if
|
254
|
-
response['installationStatus']['rescueModeRootPass'] = decrypt(response['installationStatus']['rescueModeRootPass'])
|
255
|
-
end
|
170
|
+
return getVirtualServers(result) if result['virtualServers'].size < partialResult['_metadata']['totalCount']
|
256
171
|
|
257
|
-
|
172
|
+
result
|
258
173
|
end
|
259
174
|
|
260
|
-
def
|
261
|
-
self.class.get("/
|
175
|
+
def getVirtualServer(server_id)
|
176
|
+
self.class.get("/cloud/v2/virtualServers/#{server_id}", @options)
|
262
177
|
end
|
263
178
|
|
264
|
-
def
|
265
|
-
|
179
|
+
def getVirtualServerControlPanelCredentials(server_id)
|
180
|
+
self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/CONTROL_PANEL", @options)
|
181
|
+
end
|
266
182
|
|
267
|
-
|
183
|
+
def getVirtualServerControlPanelCredentialsForUser(server_id, username)
|
184
|
+
self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/CONTROL_PANEL/#{username}", @options)
|
268
185
|
end
|
269
186
|
|
270
|
-
def
|
271
|
-
self.class.get("/
|
187
|
+
def getVirtualServerOsCredentials(server_id)
|
188
|
+
self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/OPERATING_SYSTEM", @options)
|
272
189
|
end
|
273
190
|
|
274
|
-
def
|
275
|
-
self.class.
|
191
|
+
def getVirtualServerOsCredentialsForUser(server_id, username)
|
192
|
+
self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/OPERATING_SYSTEM/#{username}", @options)
|
276
193
|
end
|
277
194
|
|
278
195
|
# Private Networks
|
279
196
|
def getPrivateNetworks
|
280
|
-
self.class.get('/
|
197
|
+
self.class.get('/bareMetals/v2/privateNetworks', @options)
|
281
198
|
end
|
282
199
|
|
283
200
|
# TODO: check post with name
|
284
201
|
def createPrivateNetworks(name = '')
|
285
|
-
opt = @options.merge!(body: { name: name })
|
202
|
+
opt = @options.merge!(body: { name: name }.to_json)
|
286
203
|
|
287
|
-
self.class.post('/
|
204
|
+
self.class.post('/bareMetals/v2/privateNetworks', opt)
|
288
205
|
end
|
289
206
|
|
290
207
|
def getPrivateNetwork(id)
|
291
|
-
self.class.get("/
|
208
|
+
self.class.get("/bareMetals/v2/privateNetworks/#{id}", @options)
|
292
209
|
end
|
293
210
|
|
294
211
|
# TODO: Check with Jeroen if it works
|
295
212
|
def updatePrivateNetwork(id, name = '')
|
296
|
-
opt = @options.merge!(body: { name: name })
|
213
|
+
opt = @options.merge!(body: { name: name }.to_json)
|
297
214
|
|
298
|
-
self.class.put("/
|
215
|
+
self.class.put("/bareMetals/v2/privateNetworks/#{id}", opt)
|
299
216
|
end
|
300
217
|
|
301
218
|
def deletePrivateNetwork(id)
|
302
|
-
self.class.delete("/
|
219
|
+
self.class.delete("/bareMetals/v2/privateNetworks/#{id}", @options)
|
303
220
|
end
|
304
221
|
|
305
|
-
def
|
306
|
-
opt = @options.merge!(body: {
|
222
|
+
def addDedicatedServerToPrivateNetwork(id, server_id)
|
223
|
+
opt = @options.merge!(body: { serverId: server_id }.to_json)
|
307
224
|
|
308
|
-
self.class.post("/
|
225
|
+
self.class.post("/bareMetals/v2/privateNetworks/#{id}/servers", opt)
|
309
226
|
end
|
310
227
|
|
311
|
-
def
|
312
|
-
self.class.delete("/
|
228
|
+
def removeDedicatedServerFromPrivateNetwork(id, server_id)
|
229
|
+
self.class.delete("/bareMetals/v2/privateNetworks/#{id}/servers/#{server_id}", @options)
|
313
230
|
end
|
314
231
|
|
315
232
|
# Operating Systems
|
316
233
|
def getOperatingSystems
|
317
|
-
self.class.get('/
|
234
|
+
self.class.get('/bareMetals/v2/operatingSystems', @options)
|
318
235
|
end
|
319
236
|
|
320
|
-
def getOperatingSystem(
|
321
|
-
self.class.get("/
|
237
|
+
def getOperatingSystem(operating_system_id)
|
238
|
+
self.class.get("/bareMetals/v2/operatingSystems/#{operating_system_id}", @options)
|
322
239
|
end
|
323
240
|
|
324
|
-
def getControlPanels(
|
325
|
-
self.class.get("/
|
241
|
+
def getControlPanels(operating_system_id)
|
242
|
+
self.class.get("/bareMetals/v2/operatingSystems/#{operating_system_id}/controlPanels", @options)
|
326
243
|
end
|
327
244
|
|
328
|
-
def getControlPanel(
|
329
|
-
self.class.get("/
|
330
|
-
end
|
331
|
-
|
332
|
-
def getPartitionSchema(operatingSystemId, bareMetalId)
|
333
|
-
opt = @options.merge!(query: { serverPackId: bareMetalId })
|
334
|
-
|
335
|
-
self.class.get("/v1/operatingSystems/#{operatingSystemId}/partitionSchema", opt)
|
245
|
+
def getControlPanel(operating_system_id, control_panel_id)
|
246
|
+
self.class.get("/bareMetals/v2/operatingSystems/#{operating_system_id}/controlPanels/#{control_panel_id}", @options)
|
336
247
|
end
|
337
248
|
|
338
249
|
# IPs
|
339
250
|
def getIps
|
340
|
-
self.class.get('/
|
251
|
+
self.class.get('/ipMgmt/v2/ips', @options)
|
341
252
|
end
|
342
253
|
|
343
|
-
def getIp(
|
344
|
-
self.class.get("/
|
254
|
+
def getIp(ip)
|
255
|
+
self.class.get("/ipMgmt/v2/ips/#{ip}", @options)
|
345
256
|
end
|
346
257
|
|
347
|
-
def updateIp(
|
348
|
-
opt = @options.merge!(body: { reverseLookup:
|
258
|
+
def updateIp(ip, reverse_lookup = '', null_routed = 0)
|
259
|
+
opt = @options.merge!(body: { reverseLookup: reverse_lookup, nullRouted: null_routed }.to_json)
|
349
260
|
|
350
|
-
self.class.put("/
|
261
|
+
self.class.put("/ipMgmt/v2/ips/#{ip}", opt)
|
351
262
|
end
|
352
263
|
|
353
|
-
|
354
|
-
|
355
|
-
self.class.get('/v1/payAsYouGo/bareMetals/instances', @options)
|
264
|
+
def getDedicatedServerBandwidthMetrics(server_id, date_from, date_to, format = 'json')
|
265
|
+
self.class.get("/bareMetals/v2/servers/#{server_id}/metrics/bandwidth", formatRequest(date_from, date_to, 'AVG', format))
|
356
266
|
end
|
357
267
|
|
358
|
-
def
|
359
|
-
|
360
|
-
|
361
|
-
self.class.post('/v1/payAsYouGo/bareMetals/instances', opt)
|
268
|
+
def getDedicatedServerDatatrafficMetrics(server_id, date_from, date_to, format = 'json')
|
269
|
+
self.class.get("/bareMetals/v2/servers/#{server_id}/metrics/datatraffic", formatRequest(date_from, date_to, 'SUM', format))
|
362
270
|
end
|
363
271
|
|
364
|
-
|
365
|
-
self.class.get("/v1/payAsYouGo/bareMetals/instances/#{bareMetalId}", @options)
|
366
|
-
end
|
272
|
+
protected
|
367
273
|
|
368
|
-
def
|
369
|
-
|
370
|
-
|
274
|
+
def validate_token(client_id)
|
275
|
+
begin
|
276
|
+
file = "#{@tmpdir}/#{client_id}.json"
|
277
|
+
content = JSON.parse(File.read(file))
|
278
|
+
expires_at = DateTime.parse(content['expires_at'])
|
371
279
|
|
372
|
-
|
373
|
-
self.class.get('/v1/payAsYouGo/bareMetals/models', @options)
|
374
|
-
end
|
280
|
+
return content['access_token'] if expires_at > DateTime.now
|
375
281
|
|
376
|
-
|
377
|
-
|
378
|
-
|
282
|
+
File.delete(file)
|
283
|
+
rescue
|
284
|
+
return false
|
285
|
+
end
|
379
286
|
|
380
|
-
|
287
|
+
false
|
288
|
+
end
|
381
289
|
|
382
|
-
def
|
383
|
-
@
|
290
|
+
def cache_token(client_id, access_token, expires_in)
|
291
|
+
file = "#{@tmpdir}/#{client_id}.json"
|
292
|
+
content = { access_token: access_token, expires_at: Time.now.getutc + expires_in }.to_json
|
293
|
+
File.write(file, content)
|
384
294
|
end
|
385
295
|
|
386
296
|
def dateFormat(date)
|
387
|
-
Date.parse(date).strftime('%
|
297
|
+
Date.parse(date).strftime('%Y-%m-%dT00:00:00Z')
|
388
298
|
end
|
389
299
|
|
390
300
|
def formatHeader(format)
|
@@ -393,11 +303,10 @@ class LeasewebAPI
|
|
393
303
|
else
|
394
304
|
{ 'Accept' => 'image/png' }.merge!(@options[:headers])
|
395
305
|
end
|
396
|
-
|
397
306
|
header
|
398
307
|
end
|
399
308
|
|
400
|
-
def formatRequest(
|
401
|
-
@options.merge!(query: {
|
309
|
+
def formatRequest(date_from, date_to, aggregation, format)
|
310
|
+
@options.merge!(query: { from: dateFormat(date_from), to: dateFormat(date_to), aggregation: aggregation, granularity: 'DAY' }, headers: formatHeader(format))
|
402
311
|
end
|
403
312
|
end
|