leaseweb-rest-api 1.0.1 → 1.0.2

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
2
  SHA1:
3
- metadata.gz: 743087b4ba877486ef230d512f40cfa5af62345e
4
- data.tar.gz: 1589cce027ee933dfb9281156af2f488ada15144
3
+ metadata.gz: adc4d1eef0cc5d6c853af1e1bfd5283241140d15
4
+ data.tar.gz: 906b653fd5dbeeeaab1e91e0c29ce2b5b6afcf86
5
5
  SHA512:
6
- metadata.gz: 6df59f6bf86d14b98453d71440fbe968bfe32208ad3688362017ea59742d87b5e97f55c872c352538ac3d882945c264fb0fb5e4ccf3135e815416fc8e4114369
7
- data.tar.gz: 71ccdd0968f9a6fe4e896b25085d05f53cc38709874c49ec76803c982cedaea0bfad0b4e836b23978c929644c5dab7368407cf29944f273d838bf251409069ed
6
+ metadata.gz: 4269770ca1487cd9c82315b7a1cf5df14e36496bfe7289e722f11f3fae5d79f1a87ec6949adf5a6a257ceae21b2f4ab0e85f2063af5239ed5c617a095aae3343
7
+ data.tar.gz: 1b0efc5c9ed2eaad0685eb670f10019fdc41e78334ae6b49d9d86c4f3e5f5c5aacf887b8f97e39e031bb896fe6d532fe493aead9e4c73cfa775d3f688b04c118
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gem 'gemspec'
4
+ gem 'rake'
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'leaseweb-rest-api'
3
- s.version = '1.0.1'
3
+ s.version = '1.0.2'
4
4
  s.authors = 'Arnoud Vermeer'
5
5
  s.email = 'a.vermeer@tech.leaseweb.com'
6
6
  s.license = 'Apache'
@@ -14,4 +14,4 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.add_development_dependency 'rspec'
16
16
  s.add_development_dependency 'webmock'
17
- end
17
+ end
@@ -1,6 +1,6 @@
1
1
  class HashToURIConversion
2
2
  def to_params(hash)
3
- params = hash.map { |k,v| normalize_param(k,v) }.join
3
+ params = hash.map { |k, v| normalize_param(k, v) }.join
4
4
  params.chop! # trailing &
5
5
  params
6
6
  end
@@ -12,7 +12,7 @@ class HashToURIConversion
12
12
  if value.is_a?(Array)
13
13
  param << value.each_with_index.map { |element, i| normalize_param("#{key}[#{i}]", element) }.join
14
14
  elsif value.is_a?(Hash)
15
- stack << [key,value]
15
+ stack << [key, value]
16
16
  else
17
17
  param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
18
18
  end
@@ -29,4 +29,4 @@ class HashToURIConversion
29
29
 
30
30
  param
31
31
  end
32
- end
32
+ end
@@ -3,23 +3,56 @@
3
3
  require 'httparty'
4
4
  require 'base64'
5
5
  require 'time'
6
+ require 'json'
6
7
  require_relative 'hash-to-uri-conversion'
7
8
 
8
9
  class LeasewebAPI
9
10
  include HTTParty
10
11
  format :json
11
- #debug_output $stderr
12
+ # debug_output $stderr
12
13
 
13
14
  base_uri 'https://api.leaseweb.com/v1'
14
15
 
15
- def initialize (apikey, privateKey, password)
16
- @options = { headers: { "X-Lsw-Auth" => apikey } }
17
- @private_key = OpenSSL::PKey::RSA.new(File.read(privateKey),password)
16
+ def initialize(apikey = nil, privateKey = nil, password = nil, clientId = nil, clientSecret = nil)
17
+ @auth_token_url = 'https://auth.leaseweb.com/token'
18
+ if !apikey.nil?
19
+ @options = { headers: { 'X-Lsw-Auth' => apikey } }
20
+ elsif !clientId.nil? && !clientSecret.nil?
21
+ access_token = getOauthToken(clientId, clientSecret)['access_token']
22
+ @options = { headers: { 'Authorization' => "Bearer #{access_token}" } }
23
+ else
24
+ puts 'Your API credentials are required.'
25
+ exit
26
+ end
27
+ @private_key = OpenSSL::PKey::RSA.new(File.read(privateKey), password) unless privateKey.nil? || password.nil?
28
+ end
29
+
30
+ def getOauthToken(clientId, clientSecret)
31
+ auth = { username: clientId, password: clientSecret }
32
+ self.class.post(@auth_token_url, basic_auth: auth, body: { grant_type: 'client_credentials' })
33
+ end
34
+
35
+ def get(url)
36
+ self.class.get(url, @options)
37
+ end
38
+
39
+ def post(url, body)
40
+ opt = @options.merge!(body: body)
41
+ self.class.post(url, opt)
42
+ end
43
+
44
+ def put(url, body)
45
+ opt = @options.merge!(body: body)
46
+ self.class.put(url, opt)
47
+ end
48
+
49
+ def delete(url)
50
+ self.class.delete(url, @options)
18
51
  end
19
52
 
20
53
  # Domains
21
54
  def getDomains
22
- self.class.get("/domains", @options)
55
+ self.class.get('/domains', @options)
23
56
  end
24
57
 
25
58
  def getDomain(domain)
@@ -27,7 +60,7 @@ class LeasewebAPI
27
60
  end
28
61
 
29
62
  def updateDomain(domain, ttl)
30
- opt = @options.merge!({ :body => { :ttl => ttl } })
63
+ opt = @options.merge!(body: { ttl: ttl })
31
64
 
32
65
  self.class.put("/domains/#{domain}", opt)
33
66
  end
@@ -37,9 +70,9 @@ class LeasewebAPI
37
70
  end
38
71
 
39
72
  def createDNSRecords(domain, host, content, type, priority = nil)
40
- opt = @options.merge!({ :body => { :host => host, :content => content, :type => type } })
73
+ opt = @options.merge!(body: { host: host, content: content, type: type })
41
74
 
42
- if not priority.nil? and (type == 'MX' or type == 'SRV')
75
+ if !priority.nil? && ((type == 'MX') || (type == 'SRV'))
43
76
  opt[:body][:priority] = priority
44
77
  end
45
78
 
@@ -51,9 +84,9 @@ class LeasewebAPI
51
84
  end
52
85
 
53
86
  def updateDNSRecord(domain, dnsRecordId, host, content, type, priority = nil)
54
- opt = @options.merge!({ :body => { :id => dnsRecordId, :host => host, :content => content, :type => type } })
87
+ opt = @options.merge!(body: { id: dnsRecordId, host: host, content: content, type: type })
55
88
 
56
- if not priority.nil? and (type == 'MX' or type == 'SRV')
89
+ if !priority.nil? && ((type == 'MX') || (type == 'SRV'))
57
90
  opt[:body][:priority] = priority
58
91
  end
59
92
 
@@ -66,12 +99,12 @@ class LeasewebAPI
66
99
 
67
100
  # Rescue
68
101
  def getRescueImages
69
- self.class.get("/rescueImages", @options)
102
+ self.class.get('/rescueImages', @options)
70
103
  end
71
104
 
72
105
  # BareMetals
73
106
  def getBareMetals
74
- self.class.get("/bareMetals", @options)
107
+ self.class.get('/bareMetals', @options)
75
108
  end
76
109
 
77
110
  def getBareMetal(bareMetalId)
@@ -79,7 +112,7 @@ class LeasewebAPI
79
112
  end
80
113
 
81
114
  def updateBareMetal(bareMetalId, reference)
82
- opt = @options.merge!({ :body => { :reference => reference } })
115
+ opt = @options.merge!(body: { reference: reference })
83
116
 
84
117
  self.class.put("/bareMetals/#{bareMetalId}", opt)
85
118
  end
@@ -108,8 +141,8 @@ class LeasewebAPI
108
141
  self.class.get("/bareMetals/#{bareMetalId}/ips/#{ipAddress}", @options)
109
142
  end
110
143
 
111
- def updateIP(bareMetalId, ipAddress, reverseLookup='', nullRouted=0)
112
- opt = @options.merge!({ :body => { :reverseLookup => reverseLookup, :nullRouted => nullRouted } })
144
+ def updateIP(bareMetalId, ipAddress, reverseLookup = '', nullRouted = 0)
145
+ opt = @options.merge!(body: { reverseLookup: reverseLookup, nullRouted: nullRouted })
113
146
 
114
147
  self.class.put("/bareMetals/#{bareMetalId}/ips/#{ipAddress}", opt)
115
148
  end
@@ -122,11 +155,11 @@ class LeasewebAPI
122
155
  self.class.get("/bareMetals/#{bareMetalId}/networkUsage", @options)
123
156
  end
124
157
 
125
- def getNetworkUsageBandWidth(bareMetalId, dateFrom, dateTo, format='json')
158
+ def getNetworkUsageBandWidth(bareMetalId, dateFrom, dateTo, format = 'json')
126
159
  self.class.get("/bareMetals/#{bareMetalId}/networkUsage/bandWidth", formatRequest(dateFrom, dateTo, format))
127
160
  end
128
161
 
129
- def getNetworkUsageDataTraffic(bareMetalId, dateFrom, dateTo, format='json')
162
+ def getNetworkUsageDataTraffic(bareMetalId, dateFrom, dateTo, format = 'json')
130
163
  self.class.get("/bareMetals/#{bareMetalId}/networkUsage/dataTraffic", formatRequest(dateFrom, dateTo, format))
131
164
  end
132
165
 
@@ -135,19 +168,19 @@ class LeasewebAPI
135
168
  end
136
169
 
137
170
  def installServer(bareMetalId, osId, hdd = [])
138
- opt = @options.merge!({ :body => { :osId => osId, :hdd => hdd }, query_string_normalizer: ->(h){ HashToURIConversion.new().to_params(h) } })
171
+ opt = @options.merge!(body: { osId: osId, hdd: hdd }, query_string_normalizer: ->(h) { HashToURIConversion.new.to_params(h) })
139
172
 
140
173
  self.class.post("/bareMetals/#{bareMetalId}/install", opt)
141
174
  end
142
175
 
143
176
  def postResqueMode(bareMetalId, osId)
144
- opt = @options.merge!({ :body => { :osId => osId } })
177
+ opt = @options.merge!(body: { osId: osId })
145
178
 
146
179
  self.class.post("/bareMetals/#{bareMetalId}/rescueMode", opt)
147
180
  end
148
181
 
149
- def getRootPassword(bareMetalId, format='json')
150
- opt = @options.merge!({ :headers => self.formatHeader(format) })
182
+ def getRootPassword(bareMetalId, format = 'json')
183
+ opt = @options.merge!(headers: formatHeader(format))
151
184
 
152
185
  self.class.get("/bareMetals/#{bareMetalId}/rootPassword", opt)
153
186
  end
@@ -155,12 +188,12 @@ class LeasewebAPI
155
188
  def getInstallationStatus(bareMetalId)
156
189
  response = self.class.get("/bareMetals/#{bareMetalId}/installationStatus", @options)
157
190
 
158
- if response["installationStatus"].include?('initRootPassword')
159
- response["installationStatus"]["initRootPassword"] = self.decrypt(response["installationStatus"]["initRootPassword"])
191
+ if response['installationStatus'].include?('initRootPassword')
192
+ response['installationStatus']['initRootPassword'] = decrypt(response['installationStatus']['initRootPassword'])
160
193
  end
161
194
 
162
- if response["installationStatus"].include?('rescueModeRootPass')
163
- response["installationStatus"]["rescueModeRootPass"] = self.decrypt(response["installationStatus"]["rescueModeRootPass"])
195
+ if response['installationStatus'].include?('rescueModeRootPass')
196
+ response['installationStatus']['rescueModeRootPass'] = decrypt(response['installationStatus']['rescueModeRootPass'])
164
197
  end
165
198
 
166
199
  response
@@ -171,7 +204,7 @@ class LeasewebAPI
171
204
  end
172
205
 
173
206
  def setLease(bareMetalId, bootFileName)
174
- opt = @options.merge!({ :body => { :bootFileName => bootFileName } })
207
+ opt = @options.merge!(body: { bootFileName: bootFileName })
175
208
 
176
209
  self.class.post("/bareMetals/#{bareMetalId}/leases", opt)
177
210
  end
@@ -184,16 +217,24 @@ class LeasewebAPI
184
217
  self.class.delete("/bareMetals/#{bareMetalId}/leases/#{macAddress}", @options)
185
218
  end
186
219
 
220
+ # New install call
221
+ def install(bareMetalId, operatingSystemId, options = {})
222
+ options[:operatingSystemId] = operatingSystemId
223
+ opt = @options.merge!(body: options.to_json)
224
+
225
+ self.class.post("/bmpapi/bareMetals/#{bareMetalId}/install", opt)
226
+ end
227
+
187
228
  # Private Networks
188
229
  def getPrivateNetworks
189
- self.class.get("/privateNetworks", @options)
230
+ self.class.get('/privateNetworks', @options)
190
231
  end
191
232
 
192
233
  # TODO: check post with name
193
- def createPrivateNetworks(name='')
194
- opt = @options.merge!({ :body => { :name => name } })
234
+ def createPrivateNetworks(name = '')
235
+ opt = @options.merge!(body: { name: name })
195
236
 
196
- self.class.post("/privateNetworks", opt)
237
+ self.class.post('/privateNetworks', opt)
197
238
  end
198
239
 
199
240
  def getPrivateNetwork(id)
@@ -201,8 +242,8 @@ class LeasewebAPI
201
242
  end
202
243
 
203
244
  # TODO: Check with Jeroen if it works
204
- def updatePrivateNetwork(id, name='')
205
- opt = @options.merge!({ :body => { :name => name } })
245
+ def updatePrivateNetwork(id, name = '')
246
+ opt = @options.merge!(body: { name: name })
206
247
 
207
248
  self.class.put("/privateNetworks/#{id}", opt)
208
249
  end
@@ -212,7 +253,7 @@ class LeasewebAPI
212
253
  end
213
254
 
214
255
  def createPrivateNetworksBareMetals(id, bareMetalId)
215
- opt = @options.merge!({ :body => { :bareMetalId => bareMetalId } })
256
+ opt = @options.merge!(body: { bareMetalId: bareMetalId })
216
257
 
217
258
  self.class.post("/privateNetworks/#{id}/bareMetals", opt)
218
259
  end
@@ -223,7 +264,7 @@ class LeasewebAPI
223
264
 
224
265
  # Operating Systems
225
266
  def getOperatingSystems
226
- self.class.get("/operatingSystems", @options)
267
+ self.class.get('/operatingSystems', @options)
227
268
  end
228
269
 
229
270
  def getOperatingSystem(operatingSystemId)
@@ -239,27 +280,55 @@ class LeasewebAPI
239
280
  end
240
281
 
241
282
  def getPartitionSchema(operatingSystemId, bareMetalId)
242
- opt = @options.merge!({ :query => { :serverPackId => bareMetalId } })
283
+ opt = @options.merge!(query: { serverPackId: bareMetalId })
243
284
 
244
285
  self.class.get("/operatingSystems/#{operatingSystemId}/partitionSchema", opt)
245
286
  end
246
287
 
247
288
  # IPs
248
289
  def getIps
249
- self.class.get("/ips", @options)
290
+ self.class.get('/ips', @options)
250
291
  end
251
292
 
252
293
  def getIp(ipAddress)
253
294
  self.class.get("/ips/#{ipAddress}", @options)
254
295
  end
255
296
 
256
- def updateIp(ipAddress, reverseLookup='', nullRouted=0)
257
- opt = @options.merge!({ :body => { :reverseLookup => reverseLookup, :nullRouted => nullRouted } })
297
+ def updateIp(ipAddress, reverseLookup = '', nullRouted = 0)
298
+ opt = @options.merge!(body: { reverseLookup: reverseLookup, nullRouted: nullRouted })
258
299
 
259
300
  self.class.put("/ips/#{ipAddress}", opt)
260
301
  end
261
302
 
303
+ # Pay as you go
304
+ def getPAYGInstances
305
+ self.class.get('/payAsYouGo/bareMetals/instances', @options)
306
+ end
307
+
308
+ def createPAYGInstance(modelId)
309
+ opt = @options.merge!(model: modelId)
310
+
311
+ self.class.post('/payAsYouGo/bareMetals/instances', opt)
312
+ end
313
+
314
+ def getPAYGInstance(bareMetalId)
315
+ self.class.get("/payAsYouGo/bareMetals/instances/#{bareMetalId}", @options)
316
+ end
317
+
318
+ def destroyPAYGInstance(bareMetalId)
319
+ self.class.post("/payAsYouGo/bareMetals/instances/#{bareMetalId}/destroy", @options)
320
+ end
321
+
322
+ def getPAYGModels
323
+ self.class.get('/payAsYouGo/bareMetals/models', @options)
324
+ end
325
+
326
+ def getPAYGModelInstance(modelId)
327
+ self.class.get("/payAsYouGo/bareMetals/models/#{modelId}", @options)
328
+ end
329
+
262
330
  protected
331
+
263
332
  def decrypt(string)
264
333
  @private_key.private_decrypt(Base64.decode64(string))
265
334
  end
@@ -269,16 +338,16 @@ class LeasewebAPI
269
338
  end
270
339
 
271
340
  def formatHeader(format)
272
- if format == 'json'
273
- header = { 'Accept' => 'application/json' }.merge!(@options[:headers])
274
- else
275
- header = { 'Accept' => 'image/png' }.merge!(@options[:headers])
276
- end
341
+ header = if format == 'json'
342
+ { 'Accept' => 'application/json' }.merge!(@options[:headers])
343
+ else
344
+ { 'Accept' => 'image/png' }.merge!(@options[:headers])
345
+ end
277
346
 
278
347
  header
279
348
  end
280
349
 
281
350
  def formatRequest(dateFrom, dateTo, format)
282
- @options.merge!({ :query => { :dateFrom => self.dateFormat(dateFrom), :dateTo => self.dateFormat(dateTo) }, :headers => self.formatHeader(format) })
351
+ @options.merge!(query: { dateFrom: dateFormat(dateFrom), dateTo: dateFormat(dateTo) }, headers: formatHeader(format))
283
352
  end
284
353
  end