Class: LeasewebAPI

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/leaseweb-rest-api.rb

Instance Method Summary collapse

Instance Method Details

#apiKeyAuth(apikey) ⇒ Object



16
17
18
# File 'lib/leaseweb-rest-api.rb', line 16

def apiKeyAuth(apikey)
  @options = { headers: { 'X-Lsw-Auth' => apikey, 'Content-Type' => 'application/json' } }
end

#createDNSRecords(domain, host, content, type, priority = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/leaseweb-rest-api.rb', line 91

def createDNSRecords(domain, host, content, type, priority = nil)
  opt = @options.merge!(body: { host: host, content: content, type: type })

  if !priority.nil? && ((type == 'MX') || (type == 'SRV'))
    opt[:body][:priority] = priority
  end

  self.class.post("/v1/domains/#{domain}/dnsRecords", opt)
end

#createPAYGInstance(modelId) ⇒ Object



319
320
321
322
323
# File 'lib/leaseweb-rest-api.rb', line 319

def createPAYGInstance(modelId)
  opt = @options.merge!(model: modelId)

  self.class.post('/v1/payAsYouGo/bareMetals/instances', opt)
end

#createPrivateNetworks(name = '') ⇒ Object

TODO: check post with name



245
246
247
248
249
# File 'lib/leaseweb-rest-api.rb', line 245

def createPrivateNetworks(name = '')
  opt = @options.merge!(body: { name: name })

  self.class.post('/v1/privateNetworks', opt)
end

#createPrivateNetworksBareMetals(id, bareMetalId) ⇒ Object



266
267
268
269
270
# File 'lib/leaseweb-rest-api.rb', line 266

def createPrivateNetworksBareMetals(id, bareMetalId)
  opt = @options.merge!(body: { bareMetalId: bareMetalId })

  self.class.post("/v1/privateNetworks/#{id}/bareMetals", opt)
end

#delete(url) ⇒ Object



68
69
70
# File 'lib/leaseweb-rest-api.rb', line 68

def delete(url)
  self.class.delete(url, @options)
end

#deleteDNSRecord(domain, dnsRecordId) ⇒ Object



115
116
117
# File 'lib/leaseweb-rest-api.rb', line 115

def deleteDNSRecord(domain, dnsRecordId)
  self.class.delete("/v1/domains/#{domain}/dnsRecords/#{dnsRecordId}", @options)
end

#deleteLease(bareMetalId, macAddress) ⇒ Object



235
236
237
# File 'lib/leaseweb-rest-api.rb', line 235

def deleteLease(bareMetalId, macAddress)
  self.class.delete("/v1/bareMetals/#{bareMetalId}/leases/#{macAddress}", @options)
end

#deletePrivateNetwork(id) ⇒ Object



262
263
264
# File 'lib/leaseweb-rest-api.rb', line 262

def deletePrivateNetwork(id)
  self.class.delete("/v1/privateNetworks/#{id}", @options)
end

#deletePrivateNetworksBareMetals(id, bareMetalId) ⇒ Object



272
273
274
# File 'lib/leaseweb-rest-api.rb', line 272

def deletePrivateNetworksBareMetals(id, bareMetalId)
  self.class.delete("/v1/privateNetworks/#{id}/bareMetals/#{bareMetalId}", @options)
end

#destroyPAYGInstance(bareMetalId) ⇒ Object



329
330
331
# File 'lib/leaseweb-rest-api.rb', line 329

def destroyPAYGInstance(bareMetalId)
  self.class.post("/v1/payAsYouGo/bareMetals/instances/#{bareMetalId}/destroy", @options)
end

#get(url) ⇒ Object



38
39
40
# File 'lib/leaseweb-rest-api.rb', line 38

def get(url)
  self.class.get(url, @options)
end

#get_paginated(url, key, offset = 0, limit = 50) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/leaseweb-rest-api.rb', line 42

def get_paginated(url, key, offset = 0, limit = 50)
  data = []

  loop do
    response = self.class.get("#{url}&offset=#{offset}&limit=#{limit}", @options)
    total = response.parsed_response['_metadata']['totalCount']

    data += response.parsed_response[key]

    offset += limit
    break unless offset < total
  end

  data
end

#getBareMetal(bareMetalId) ⇒ Object



129
130
131
# File 'lib/leaseweb-rest-api.rb', line 129

def getBareMetal(bareMetalId)
  self.class.get("/v1/bareMetals/#{bareMetalId}", @options)
end

#getBareMetalsObject

BareMetals



125
126
127
# File 'lib/leaseweb-rest-api.rb', line 125

def getBareMetals
  self.class.get('/v1/bareMetals', @options)
end

#getControlPanel(operatingSystemId, controlPanelId) ⇒ Object



289
290
291
# File 'lib/leaseweb-rest-api.rb', line 289

def getControlPanel(operatingSystemId, controlPanelId)
  self.class.get("/v1/operatingSystems/#{operatingSystemId}/controlPanels/#{controlPanelId}", @options)
end

#getControlPanels(operatingSystemId) ⇒ Object



285
286
287
# File 'lib/leaseweb-rest-api.rb', line 285

def getControlPanels(operatingSystemId)
  self.class.get("/v1/operatingSystems/#{operatingSystemId}/controlPanels", @options)
end

#getDNSRecord(domain, dnsRecordId) ⇒ Object



101
102
103
# File 'lib/leaseweb-rest-api.rb', line 101

def getDNSRecord(domain, dnsRecordId)
  self.class.get("/v1/domains/#{domain}/dnsRecords/#{dnsRecordId}", @options)
end

#getDNSRecords(domain) ⇒ Object



87
88
89
# File 'lib/leaseweb-rest-api.rb', line 87

def getDNSRecords(domain)
  self.class.get("/v1/domains/#{domain}/dnsRecords", @options)
end

#getDomain(domain) ⇒ Object



77
78
79
# File 'lib/leaseweb-rest-api.rb', line 77

def getDomain(domain)
  self.class.get("/v1/domains/#{domain}", @options)
end

#getDomainsObject

Domains



73
74
75
# File 'lib/leaseweb-rest-api.rb', line 73

def getDomains
  self.class.get('/v1/domains', @options)
end

#getInstallationStatus(bareMetalId) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/leaseweb-rest-api.rb', line 207

def getInstallationStatus(bareMetalId)
  response = self.class.get("/v1/bareMetals/#{bareMetalId}/installationStatus", @options)

  if response['installationStatus'].include?('initRootPassword')
    response['installationStatus']['initRootPassword'] = decrypt(response['installationStatus']['initRootPassword'])
  end

  if response['installationStatus'].include?('rescueModeRootPass')
    response['installationStatus']['rescueModeRootPass'] = decrypt(response['installationStatus']['rescueModeRootPass'])
  end

  response
end

#getIp(ipAddress) ⇒ Object



304
305
306
# File 'lib/leaseweb-rest-api.rb', line 304

def getIp(ipAddress)
  self.class.get("/v1/ips/#{ipAddress}", @options)
end

#getIP(bareMetalId, ipAddress) ⇒ Object



159
160
161
# File 'lib/leaseweb-rest-api.rb', line 159

def getIP(bareMetalId, ipAddress)
  self.class.get("/v1/bareMetals/#{bareMetalId}/ips/#{ipAddress}", @options)
end

#getIpmiCredentials(bareMetalId) ⇒ Object



169
170
171
# File 'lib/leaseweb-rest-api.rb', line 169

def getIpmiCredentials(bareMetalId)
  self.class.get("/v1/bareMetals/#{bareMetalId}/ipmiCredentials", @options)
end

#getIpsObject

IPs



300
301
302
# File 'lib/leaseweb-rest-api.rb', line 300

def getIps
  self.class.get('/v1/ips', @options)
end

#getIPs(bareMetalId) ⇒ Object



155
156
157
# File 'lib/leaseweb-rest-api.rb', line 155

def getIPs(bareMetalId)
  self.class.get("/v1/bareMetals/#{bareMetalId}/ips", @options)
end

#getLease(bareMetalId, macAddress) ⇒ Object



231
232
233
# File 'lib/leaseweb-rest-api.rb', line 231

def getLease(bareMetalId, macAddress)
  self.class.get("/v1/bareMetals/#{bareMetalId}/leases/#{macAddress}", @options)
end

#getLeases(bareMetalId) ⇒ Object



221
222
223
# File 'lib/leaseweb-rest-api.rb', line 221

def getLeases(bareMetalId)
  self.class.get("/v1/bareMetals/#{bareMetalId}/leases", @options)
end

#getNetworkUsage(bareMetalId) ⇒ Object



173
174
175
# File 'lib/leaseweb-rest-api.rb', line 173

def getNetworkUsage(bareMetalId)
  self.class.get("/v1/bareMetals/#{bareMetalId}/networkUsage", @options)
end

#getNetworkUsageBandWidth(bareMetalId, dateFrom, dateTo, format = 'json') ⇒ Object



177
178
179
# File 'lib/leaseweb-rest-api.rb', line 177

def getNetworkUsageBandWidth(bareMetalId, dateFrom, dateTo, format = 'json')
  self.class.get("/v1/bareMetals/#{bareMetalId}/networkUsage/bandWidth", formatRequest(dateFrom, dateTo, format))
end

#getNetworkUsageDataTraffic(bareMetalId, dateFrom, dateTo, format = 'json') ⇒ Object



181
182
183
# File 'lib/leaseweb-rest-api.rb', line 181

def getNetworkUsageDataTraffic(bareMetalId, dateFrom, dateTo, format = 'json')
  self.class.get("/v1/bareMetals/#{bareMetalId}/networkUsage/dataTraffic", formatRequest(dateFrom, dateTo, format))
end

#getOauthToken(clientId, clientSecret) ⇒ Object



20
21
22
23
24
25
# File 'lib/leaseweb-rest-api.rb', line 20

def getOauthToken(clientId, clientSecret)
  response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: clientId, password: clientSecret }, body: { grant_type: 'client_credentials' })
  access_token = response.parsed_response['access_token']

  @options = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
end

#getOperatingSystem(operatingSystemId) ⇒ Object



281
282
283
# File 'lib/leaseweb-rest-api.rb', line 281

def getOperatingSystem(operatingSystemId)
  self.class.get("/v1/operatingSystems/#{operatingSystemId}", @options)
end

#getOperatingSystemsObject

Operating Systems



277
278
279
# File 'lib/leaseweb-rest-api.rb', line 277

def getOperatingSystems
  self.class.get('/v1/operatingSystems', @options)
end

#getPartitionSchema(operatingSystemId, bareMetalId) ⇒ Object



293
294
295
296
297
# File 'lib/leaseweb-rest-api.rb', line 293

def getPartitionSchema(operatingSystemId, bareMetalId)
  opt = @options.merge!(query: { serverPackId: bareMetalId })

  self.class.get("/v1/operatingSystems/#{operatingSystemId}/partitionSchema", opt)
end

#getPasswordToken(username, password, client_id, client_secret) ⇒ Object



27
28
29
30
31
32
# File 'lib/leaseweb-rest-api.rb', line 27

def getPasswordToken(username, password, client_id, client_secret)
  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 })
  access_token = response.parsed_response['access_token']

  @options = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
end

#getPAYGInstance(bareMetalId) ⇒ Object



325
326
327
# File 'lib/leaseweb-rest-api.rb', line 325

def getPAYGInstance(bareMetalId)
  self.class.get("/v1/payAsYouGo/bareMetals/instances/#{bareMetalId}", @options)
end

#getPAYGInstancesObject

Pay as you go



315
316
317
# File 'lib/leaseweb-rest-api.rb', line 315

def getPAYGInstances
  self.class.get('/v1/payAsYouGo/bareMetals/instances', @options)
end

#getPAYGModelInstance(modelId) ⇒ Object



337
338
339
# File 'lib/leaseweb-rest-api.rb', line 337

def getPAYGModelInstance(modelId)
  self.class.get("/v1/payAsYouGo/bareMetals/models/#{modelId}", @options)
end

#getPAYGModelsObject



333
334
335
# File 'lib/leaseweb-rest-api.rb', line 333

def getPAYGModels
  self.class.get('/v1/payAsYouGo/bareMetals/models', @options)
end

#getPowerStatus(bareMetalId) ⇒ Object



151
152
153
# File 'lib/leaseweb-rest-api.rb', line 151

def getPowerStatus(bareMetalId)
  self.class.get("/v1/bareMetals/#{bareMetalId}/powerStatus", @options)
end

#getPrivateNetwork(id) ⇒ Object



251
252
253
# File 'lib/leaseweb-rest-api.rb', line 251

def getPrivateNetwork(id)
  self.class.get("/v1/privateNetworks/#{id}", @options)
end

#getPrivateNetworksObject

Private Networks



240
241
242
# File 'lib/leaseweb-rest-api.rb', line 240

def getPrivateNetworks
  self.class.get('/v1/privateNetworks', @options)
end

#getRescueImagesObject

Rescue



120
121
122
# File 'lib/leaseweb-rest-api.rb', line 120

def getRescueImages
  self.class.get('/v1/rescueImages', @options)
end

#getRootPassword(bareMetalId, format = 'json') ⇒ Object



201
202
203
204
205
# File 'lib/leaseweb-rest-api.rb', line 201

def getRootPassword(bareMetalId, format = 'json')
  opt = @options.merge!(headers: formatHeader(format))

  self.class.get("/v1/bareMetals/#{bareMetalId}/rootPassword", opt)
end

#getSwitchPort(bareMetalId) ⇒ Object



139
140
141
# File 'lib/leaseweb-rest-api.rb', line 139

def getSwitchPort(bareMetalId)
  self.class.get("/v1/bareMetals/#{bareMetalId}/switchPort", @options)
end

#installServer(bareMetalId, osId, hdd = []) ⇒ Object



189
190
191
192
193
# File 'lib/leaseweb-rest-api.rb', line 189

def installServer(bareMetalId, osId, hdd = [])
  opt = @options.merge!(body: { osId: osId, hdd: hdd }, query_string_normalizer: ->(h) { HashToURIConversion.new.to_params(h) })

  self.class.post("/v1/bareMetals/#{bareMetalId}/install", opt)
end

#post(url, body) ⇒ Object



58
59
60
61
# File 'lib/leaseweb-rest-api.rb', line 58

def post(url, body)
  opt = @options.merge!(body: body)
  self.class.post(url, opt)
end

#postReboot(bareMetalId) ⇒ Object



185
186
187
# File 'lib/leaseweb-rest-api.rb', line 185

def postReboot(bareMetalId)
  self.class.post("/v1/bareMetals/#{bareMetalId}/reboot", @options)
end

#postResqueMode(bareMetalId, osId) ⇒ Object



195
196
197
198
199
# File 'lib/leaseweb-rest-api.rb', line 195

def postResqueMode(bareMetalId, osId)
  opt = @options.merge!(body: { osId: osId })

  self.class.post("/v1/bareMetals/#{bareMetalId}/rescueMode", opt)
end

#postSwitchPortClose(bareMetalId) ⇒ Object



147
148
149
# File 'lib/leaseweb-rest-api.rb', line 147

def postSwitchPortClose(bareMetalId)
  self.class.post("/v1/bareMetals/#{bareMetalId}/switchPort/close", @options)
end

#postSwitchPortOpen(bareMetalId) ⇒ Object



143
144
145
# File 'lib/leaseweb-rest-api.rb', line 143

def postSwitchPortOpen(bareMetalId)
  self.class.post("/v1/bareMetals/#{bareMetalId}/switchPort/open", @options)
end

#put(url, body) ⇒ Object



63
64
65
66
# File 'lib/leaseweb-rest-api.rb', line 63

def put(url, body)
  opt = @options.merge!(body: body)
  self.class.put(url, opt)
end

#readPrivateKey(privateKey, password) ⇒ Object



34
35
36
# File 'lib/leaseweb-rest-api.rb', line 34

def readPrivateKey(privateKey, password)
  @private_key = OpenSSL::PKey::RSA.new(File.read(privateKey), password)
end

#setLease(bareMetalId, bootFileName) ⇒ Object



225
226
227
228
229
# File 'lib/leaseweb-rest-api.rb', line 225

def setLease(bareMetalId, bootFileName)
  opt = @options.merge!(body: { bootFileName: bootFileName })

  self.class.post("/v1/bareMetals/#{bareMetalId}/leases", opt)
end

#updateBareMetal(bareMetalId, reference) ⇒ Object



133
134
135
136
137
# File 'lib/leaseweb-rest-api.rb', line 133

def updateBareMetal(bareMetalId, reference)
  opt = @options.merge!(body: { reference: reference })

  self.class.put("/v1/bareMetals/#{bareMetalId}", opt)
end

#updateDNSRecord(domain, dnsRecordId, host, content, type, priority = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/leaseweb-rest-api.rb', line 105

def updateDNSRecord(domain, dnsRecordId, host, content, type, priority = nil)
  opt = @options.merge!(body: { id: dnsRecordId, host: host, content: content, type: type })

  if !priority.nil? && ((type == 'MX') || (type == 'SRV'))
    opt[:body][:priority] = priority
  end

  self.class.put("/v1/domains/#{domain}/dnsRecords/#{dnsRecordId}", opt)
end

#updateDomain(domain, ttl) ⇒ Object



81
82
83
84
85
# File 'lib/leaseweb-rest-api.rb', line 81

def updateDomain(domain, ttl)
  opt = @options.merge!(body: { ttl: ttl })

  self.class.put("/v1/domains/#{domain}", opt)
end

#updateIP(bareMetalId, ipAddress, reverseLookup = '', nullRouted = 0) ⇒ Object



163
164
165
166
167
# File 'lib/leaseweb-rest-api.rb', line 163

def updateIP(bareMetalId, ipAddress, reverseLookup = '', nullRouted = 0)
  opt = @options.merge!(body: { reverseLookup: reverseLookup, nullRouted: nullRouted })

  self.class.put("/v1/bareMetals/#{bareMetalId}/ips/#{ipAddress}", opt)
end

#updateIp(ipAddress, reverseLookup = '', nullRouted = 0) ⇒ Object



308
309
310
311
312
# File 'lib/leaseweb-rest-api.rb', line 308

def updateIp(ipAddress, reverseLookup = '', nullRouted = 0)
  opt = @options.merge!(body: { reverseLookup: reverseLookup, nullRouted: nullRouted })

  self.class.put("/v1/ips/#{ipAddress}", opt)
end

#updatePrivateNetwork(id, name = '') ⇒ Object

TODO: Check with Jeroen if it works



256
257
258
259
260
# File 'lib/leaseweb-rest-api.rb', line 256

def updatePrivateNetwork(id, name = '')
  opt = @options.merge!(body: { name: name })

  self.class.put("/v1/privateNetworks/#{id}", opt)
end