leaseweb-rest-api 1.1.6 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e9018557ac9362a130a25aaa7c2aadc75273d136
4
- data.tar.gz: 828f59328828cbc462eb8065372fb369e9483382
2
+ SHA256:
3
+ metadata.gz: 505edde91242f9a8cca44007236f98d3b118c01d215d79369ca43ebebbdf3660
4
+ data.tar.gz: 94056e4073bb03f3172e4e74988403b8af18674887d4f984b084f56cbc7b6ffd
5
5
  SHA512:
6
- metadata.gz: 1cde407ae24825f2a08e869278f8fa73bf9f4832d89b7f744f0bc66c7c7f51fb815d025d68aefa151b2f8873b4aa1d1c272318c1e682fb71fdd1a5eb8e44c145
7
- data.tar.gz: b5798e7e9b5b693c19124c694e1b3a517528165112469e155c2d4bb8b25a879a08f9d5591d7fb0d0f3e7c49d35c3957e1e1e51f85a5660355f7377279d85689e
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
- # Gemfile.lock
31
- # .ruby-version
32
- # .ruby-gemset
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
@@ -8,4 +8,7 @@ Style/VariableName:
8
8
  Enabled: no
9
9
 
10
10
  Metrics/LineLength:
11
- Max: 300
11
+ Max: 300
12
+
13
+ Metrics/MethodLength:
14
+ Max: 20
@@ -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
- ## Installation
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
- ## Usage
29
+ Usage
30
+ -----
39
31
 
40
- Start by creating a new instance of the `LeasewebAPI` class, and passing your api key, private key and private key password.
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 = 'e12b534e-3bf6-4208-89e6-e43798b3c30f'
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 Hash.
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
- ### Managing servers
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
- ## Contribute
111
+
112
+ Contribute
113
+ ----------
122
114
 
123
115
  1. Fork it
124
116
  2. Create your feature branch (`git checkout -b my-new-feature`)
@@ -1,17 +1,15 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'leaseweb-rest-api'
3
- s.version = '1.1.6'
4
- s.authors = 'Arnoud Vermeer'
5
- s.email = 'a.vermeer@tech.leaseweb.com'
6
- s.license = 'Apache'
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.add_dependency 'httparty'
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
@@ -1,10 +1,9 @@
1
- #!/usr/bin/env ruby
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 = "/tmp/lsw-rest-api"
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 = { headers: { 'X-Lsw-Auth' => apikey, 'Content-Type' => 'application/json' } }
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 (access_token == false)
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 = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
35
- end
36
-
37
- def getPasswordToken(username, password, client_id, client_secret)
38
- access_token = validate_token(client_id)
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
- # Domains
112
- def getDomains
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
- def createDNSRecords(domain, host, content, type, priority = nil)
131
- opt = @options.merge!(body: { host: host, content: content, type: type })
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
- if !priority.nil? && ((type == 'MX') || (type == 'SRV'))
134
- opt[:body][:priority] = priority
73
+ break unless offset < total
135
74
  end
136
75
 
137
- self.class.post("/v1/domains/#{domain}/dnsRecords", opt)
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 updateDNSRecord(domain, dnsRecordId, host, content, type, priority = nil)
145
- opt = @options.merge!(body: { id: dnsRecordId, host: host, content: content, type: type })
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 deleteDNSRecord(domain, dnsRecordId)
155
- self.class.delete("/v1/domains/#{domain}/dnsRecords/#{dnsRecordId}", @options)
83
+ def powerOnDedicatedServer(server_id)
84
+ self.class.post("/bareMetals/v2/servers/#{server_id}/powerOn", @options)
156
85
  end
157
86
 
158
- # Rescue
159
- def getRescueImages
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
- # BareMetals
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
- def getBareMetal(bareMetalId)
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 updateBareMetal(bareMetalId, reference)
173
- opt = @options.merge!(body: { reference: reference })
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
- self.class.put("/v1/bareMetals/#{bareMetalId}", opt)
176
- end
99
+ return partialResult if partialResult['errorMessage']
177
100
 
178
- def getSwitchPort(bareMetalId)
179
- self.class.get("/v1/bareMetals/#{bareMetalId}/switchPort", @options)
180
- end
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
- def postSwitchPortOpen(bareMetalId)
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
- def postSwitchPortClose(bareMetalId)
187
- self.class.post("/v1/bareMetals/#{bareMetalId}/switchPort/close", @options)
111
+ result
188
112
  end
189
113
 
190
- def getPowerStatus(bareMetalId)
191
- self.class.get("/v1/bareMetals/#{bareMetalId}/powerStatus", @options)
114
+ def getDedicatedServerByIp(ip)
115
+ self.class.get("/internal/dedicatedserverapi/v2/servers?ip=#{ip}", @options)
192
116
  end
193
117
 
194
- def getIPs(bareMetalId)
195
- self.class.get("/v1/bareMetals/#{bareMetalId}/ips", @options)
118
+ def getDedicatedServer(server_id)
119
+ self.class.get("/bareMetals/v2/servers/#{server_id}", @options)
196
120
  end
197
121
 
198
- def getIP(bareMetalId, ipAddress)
199
- self.class.get("/v1/bareMetals/#{bareMetalId}/ips/#{ipAddress}", @options)
122
+ def getDedicatedServerHardware(server_id)
123
+ self.class.get("/bareMetals/v2/servers/#{server_id}/hardwareInfo", @options)
200
124
  end
201
125
 
202
- def updateIP(bareMetalId, ipAddress, reverseLookup = '', nullRouted = 0)
203
- opt = @options.merge!(body: { reverseLookup: reverseLookup, nullRouted: nullRouted })
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 getIpmiCredentials(bareMetalId)
209
- self.class.get("/v1/bareMetals/#{bareMetalId}/ipmiCredentials", @options)
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 getNetworkUsage(bareMetalId)
213
- self.class.get("/v1/bareMetals/#{bareMetalId}/networkUsage", @options)
136
+ def removeDedicatedServerDhcpReservation(server_id)
137
+ self.class.delete("/bareMetals/v2/servers/#{server_id}/leases", @options)
214
138
  end
215
139
 
216
- def getNetworkUsageBandWidth(bareMetalId, dateFrom, dateTo, format = 'json')
217
- self.class.get("/v1/bareMetals/#{bareMetalId}/networkUsage/bandWidth", formatRequest(dateFrom, dateTo, format))
140
+ def powerOnVirtualServer(server_id)
141
+ self.class.post("/cloud/v2/virtualServers/#{server_id}/powerOn", @options)
218
142
  end
219
143
 
220
- def getNetworkUsageDataTraffic(bareMetalId, dateFrom, dateTo, format = 'json')
221
- self.class.get("/v1/bareMetals/#{bareMetalId}/networkUsage/dataTraffic", formatRequest(dateFrom, dateTo, format))
144
+ def powerOffVirtualServer(server_id)
145
+ self.class.post("/cloud/v2/virtualServers/#{server_id}/powerOff", @options)
222
146
  end
223
147
 
224
- def postReboot(bareMetalId)
225
- self.class.post("/v1/bareMetals/#{bareMetalId}/reboot", @options)
148
+ def rebootVirtualServer(server_id)
149
+ self.class.post("/cloud/v2/virtualServers/#{server_id}/reboot", @options)
226
150
  end
227
151
 
228
- def installServer(bareMetalId, osId, hdd = [])
229
- opt = @options.merge!(body: { osId: osId, hdd: hdd }, query_string_normalizer: ->(h) { HashToURIConversion.new.to_params(h) })
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 getRootPassword(bareMetalId, format = 'json')
241
- opt = @options.merge!(headers: formatHeader(format))
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
- def getInstallationStatus(bareMetalId)
247
- response = self.class.get("/v1/bareMetals/#{bareMetalId}/installationStatus", @options)
160
+ return partialResult if partialResult['errorMessage']
248
161
 
249
- if response['installationStatus'].include?('initRootPassword')
250
- response['installationStatus']['initRootPassword'] = decrypt(response['installationStatus']['initRootPassword'])
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 response['installationStatus'].include?('rescueModeRootPass')
254
- response['installationStatus']['rescueModeRootPass'] = decrypt(response['installationStatus']['rescueModeRootPass'])
255
- end
170
+ return getVirtualServers(result) if result['virtualServers'].size < partialResult['_metadata']['totalCount']
256
171
 
257
- response
172
+ result
258
173
  end
259
174
 
260
- def getLeases(bareMetalId)
261
- self.class.get("/v1/bareMetals/#{bareMetalId}/leases", @options)
175
+ def getVirtualServer(server_id)
176
+ self.class.get("/cloud/v2/virtualServers/#{server_id}", @options)
262
177
  end
263
178
 
264
- def setLease(bareMetalId, bootFileName)
265
- opt = @options.merge!(body: { bootFileName: bootFileName })
179
+ def getVirtualServerControlPanelCredentials(server_id)
180
+ self.class.get("/cloud/v2/virtualServers/#{server_id}/credentials/CONTROL_PANEL", @options)
181
+ end
266
182
 
267
- self.class.post("/v1/bareMetals/#{bareMetalId}/leases", opt)
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 getLease(bareMetalId, macAddress)
271
- self.class.get("/v1/bareMetals/#{bareMetalId}/leases/#{macAddress}", @options)
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 deleteLease(bareMetalId, macAddress)
275
- self.class.delete("/v1/bareMetals/#{bareMetalId}/leases/#{macAddress}", @options)
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('/v1/privateNetworks', @options)
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('/v1/privateNetworks', opt)
204
+ self.class.post('/bareMetals/v2/privateNetworks', opt)
288
205
  end
289
206
 
290
207
  def getPrivateNetwork(id)
291
- self.class.get("/v1/privateNetworks/#{id}", @options)
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("/v1/privateNetworks/#{id}", opt)
215
+ self.class.put("/bareMetals/v2/privateNetworks/#{id}", opt)
299
216
  end
300
217
 
301
218
  def deletePrivateNetwork(id)
302
- self.class.delete("/v1/privateNetworks/#{id}", @options)
219
+ self.class.delete("/bareMetals/v2/privateNetworks/#{id}", @options)
303
220
  end
304
221
 
305
- def createPrivateNetworksBareMetals(id, bareMetalId)
306
- opt = @options.merge!(body: { bareMetalId: bareMetalId })
222
+ def addDedicatedServerToPrivateNetwork(id, server_id)
223
+ opt = @options.merge!(body: { serverId: server_id }.to_json)
307
224
 
308
- self.class.post("/v1/privateNetworks/#{id}/bareMetals", opt)
225
+ self.class.post("/bareMetals/v2/privateNetworks/#{id}/servers", opt)
309
226
  end
310
227
 
311
- def deletePrivateNetworksBareMetals(id, bareMetalId)
312
- self.class.delete("/v1/privateNetworks/#{id}/bareMetals/#{bareMetalId}", @options)
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('/v1/operatingSystems', @options)
234
+ self.class.get('/bareMetals/v2/operatingSystems', @options)
318
235
  end
319
236
 
320
- def getOperatingSystem(operatingSystemId)
321
- self.class.get("/v1/operatingSystems/#{operatingSystemId}", @options)
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(operatingSystemId)
325
- self.class.get("/v1/operatingSystems/#{operatingSystemId}/controlPanels", @options)
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(operatingSystemId, controlPanelId)
329
- self.class.get("/v1/operatingSystems/#{operatingSystemId}/controlPanels/#{controlPanelId}", @options)
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('/v1/ips', @options)
251
+ self.class.get('/ipMgmt/v2/ips', @options)
341
252
  end
342
253
 
343
- def getIp(ipAddress)
344
- self.class.get("/v1/ips/#{ipAddress}", @options)
254
+ def getIp(ip)
255
+ self.class.get("/ipMgmt/v2/ips/#{ip}", @options)
345
256
  end
346
257
 
347
- def updateIp(ipAddress, reverseLookup = '', nullRouted = 0)
348
- opt = @options.merge!(body: { reverseLookup: reverseLookup, nullRouted: nullRouted })
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("/v1/ips/#{ipAddress}", opt)
261
+ self.class.put("/ipMgmt/v2/ips/#{ip}", opt)
351
262
  end
352
263
 
353
- # Pay as you go
354
- def getPAYGInstances
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 createPAYGInstance(modelId)
359
- opt = @options.merge!(model: modelId)
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
- def getPAYGInstance(bareMetalId)
365
- self.class.get("/v1/payAsYouGo/bareMetals/instances/#{bareMetalId}", @options)
366
- end
272
+ protected
367
273
 
368
- def destroyPAYGInstance(bareMetalId)
369
- self.class.post("/v1/payAsYouGo/bareMetals/instances/#{bareMetalId}/destroy", @options)
370
- end
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
- def getPAYGModels
373
- self.class.get('/v1/payAsYouGo/bareMetals/models', @options)
374
- end
280
+ return content['access_token'] if expires_at > DateTime.now
375
281
 
376
- def getPAYGModelInstance(modelId)
377
- self.class.get("/v1/payAsYouGo/bareMetals/models/#{modelId}", @options)
378
- end
282
+ File.delete(file)
283
+ rescue
284
+ return false
285
+ end
379
286
 
380
- protected
287
+ false
288
+ end
381
289
 
382
- def decrypt(string)
383
- @private_key.private_decrypt(Base64.decode64(string))
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('%d-%m-%Y')
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(dateFrom, dateTo, format)
401
- @options.merge!(query: { dateFrom: dateFormat(dateFrom), dateTo: dateFormat(dateTo) }, headers: formatHeader(format))
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