Class: SecuredCloudRestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/secured_cloud_api_client.rb

Overview

Class SecuredCloudRestClient provides a collection of methods that issue requests to the Secured Cloud RESTful External API.

@author

Alan Vella, Secured Cloud

Class Method Summary collapse

Class Method Details

.assignPublicIpToVM(sc_connection, vmResource, ipFromReserved, privateIpMapping) ⇒ Object

Assigns a public IP to a VM. ipFromReserved: a new IP will be assigned if set to nil privateIpMapping: Private ip mappings are only supported for nodes

with custom network configurations. Should this field be set for 
nodes with a different network configuration, a 405 �Method not 
allowed� error will be returned.


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/secured_cloud_api_client.rb', line 307

def self.assignPublicIpToVM(sc_connection, vmResource, ipFromReserved, privateIpMapping)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Virtual Machine Resource", vmResource)
  if (ipFromReserved != nil) then
    raise ArgumentError, 'ipFromReserved needs to be a valid string' unless ipFromReserved.respond_to?(:to_str)  
  end
  if (privateIpMapping != nil) then
    raise ArgumentError, 'privateIpMapping needs to be a valid string' unless privateIpMapping.respond_to?(:to_str)  
  end
  
  ipFromReserved = (ipFromReserved == nil) ? 'null' : '"' + ipFromReserved + '"'
  privateIpMapping = (privateIpMapping == nil) ? 'null' : '"' + privateIpMapping + '"'    
  jsonBody = '{"ipFromReserved":' + ipFromReserved + ',"privateIpMapping":' + privateIpMapping + '}'
  
  response = HttpClient.sendPOSTRequest( sc_connection, sc_connection.getUrl() + vmResource + "/publicip", jsonBody)
  
  if (response.code == "202") then
    responseBodyAsObject = JSON.parse(response.body)
    responseBodyAsObject.each do |item|
    return "202", item[1]
    end
  else
    return response.code, response['X-Application-Error-Reference'], response['X-Application-Error-Description']
  end
  
end

.createVM(sc_connection, orgResource, nodeResource, name, description, storageGB, memoryMB, vCPUCount, powerStatus, imageResource, osTemplateResource, osPassword) ⇒ Object

Starts a Create VM transaction. Returns the response code as well as the task resource if the transaction is triggered successfully.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/secured_cloud_api_client.rb', line 110

def self.createVM(sc_connection, orgResource, nodeResource, name, description, storageGB, memoryMB, vCPUCount, powerStatus, imageResource, osTemplateResource, osPassword)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Organization Resource", orgResource)
  validate_resource("Node Resource", nodeResource)

  createVMPostBody = createVMJSONBody(name, description, storageGB, memoryMB, vCPUCount, powerStatus, imageResource, osTemplateResource, osPassword)
  response = HttpClient.sendPOSTRequest( sc_connection, sc_connection.getUrl() + orgResource + nodeResource + "/virtualmachine", createVMPostBody)
  
  if (response.code == "202") then
    responseBodyAsObject = JSON.parse(response.body)
    responseBodyAsObject.each do |item|
      return "202", item[1]
    end
  else
    return response.code, response['X-Application-Error-Reference'], response['X-Application-Error-Description']
  end
  
end

.deleteVM(sc_connection, vmResource, releaseIPs) ⇒ Object

Starts a Delete VM transaction. Returns the response code as well as the task resource if the transaction is triggered successfully.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/secured_cloud_api_client.rb', line 342

def self.deleteVM(sc_connection, vmResource, releaseIPs)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Virtual Machine Resource", vmResource)
  
  params = []
  
  if ((releaseIPs != nil) && (releaseIPs)) then
    params.push(["released","true"])
  else
    params.push(["released","false"])
  end
  
  response = HttpClient.sendDELETERequest( sc_connection, sc_connection.getUrl() + vmResource, params)
  
  if (response.code == "202") then
    responseBodyAsObject = JSON.parse(response.body)
    
    responseBodyAsObject.each do |item|
      return "202", item[1]
    end
  else
    return response.code, response['X-Application-Error-Reference'], response['X-Application-Error-Description']
  end
  
end

.getNodesAvailable(sc_connection, orgResource) ⇒ Object

Returns a list of available nodes for a given organization.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/secured_cloud_api_client.rb', line 41

def self.getNodesAvailable(sc_connection, orgResource)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Organization Resource", orgResource)
  
  response = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + orgResource + "/node" )
  nodesAvailable = []
  responseBodyAsObject = JSON.parse(response.body)
  responseBodyAsObject.each do |item|
    nodesAvailable.push(item["resourceURL"])
  end
  
  return nodesAvailable
end

.getOsTemplateDetails(sc_connection, osTemplateResource) ⇒ Object

Returns the details of an OS template



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/secured_cloud_api_client.rb', line 79

def self.getOsTemplateDetails(sc_connection, osTemplateResource)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("OS Template", osTemplateResource)    

  osResponse = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + osTemplateResource )
  responseBodyAsObject = JSON.parse(osResponse.body)
  osName = osVersion = minimumStorageSpace = defaultAdministratorUsername = diskExpandable = ""
  
  responseBodyAsObject.each do |item|
    if item[0] == "name" then
    osName = item[1]
    elsif item[0] == "version" then
    osVersion = item[1]
    elsif item[0] == "minimumStorageSpace" then
    minimumStorageSpace = item[1]
    elsif item[0] == "defaultAdministratorUsername" then
    defaultAdministratorUsername = item[1]
    elsif item[0] == "diskExpandable" then
    diskExpandable = item[1]
    end       
  end    
  
  return OsTemplate.new(osName, osVersion, minimumStorageSpace, defaultAdministratorUsername, diskExpandable)   
end

.getOsTemplatesAvailable(sc_connection, orgResource, nodeResource) ⇒ Object

Returns a list of available OS templates for a given organization and node.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/secured_cloud_api_client.rb', line 59

def self.getOsTemplatesAvailable(sc_connection, orgResource, nodeResource)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Organization Resource", orgResource)
  validate_resource("Node Resource", nodeResource)

  response = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + orgResource + nodeResource + "/ostemplate")
  osTemplatesAvailable = []
  responseBodyAsObject = JSON.parse(response.body)
  
  responseBodyAsObject.each do |item|
    osTemplatesAvailable.push(item["resourceURL"])
  end
  
  return osTemplatesAvailable
end

.getTaskStatus(sc_connection, taskResource) ⇒ Object

Returns the status of a task.

Raises:

  • (ArgumentError)


422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/secured_cloud_api_client.rb', line 422

def self.getTaskStatus(sc_connection, taskResource)
  
  #validation
  raise ArgumentError, 'SecuredCloudConnection needs to be initialized' unless sc_connection != nil
  raise ArgumentError, 'Task Resource needs to be initialized' unless taskResource != nil
  raise ArgumentError, 'Task Resource needs to be a valid string' unless taskResource.respond_to?(:to_s)

  response = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + taskResource )
  responseBodyAsObject = JSON.parse(response.body)
  percentageComplete, requestStateEnum, result, errorCode, errorMessage, latestTaskDescription, processDescription, createdTimestamp, lastUpdatedTimeStamp = ""
  
  responseBodyAsObject.each do |item|
    if item[0] == "percentageComplete" then
      percentageComplete = item[1]
    elsif item[0] == "requestStateEnum" then
      requestStateEnum = item[1]
    elsif item[0] == "result" then
      result = item[1]
    elsif item[0] == "errorCode" then
      errorCode = item[1]
    elsif item[0] == "errorMessage" then
      errorMessage = item[1]
    elsif item[0] == "latestTaskDescription" then
      latestTaskDescription = item[1]
    elsif item[0] == "processDescription" then
      processDescription = item[1]
    elsif item[0] == "createdTimestamp" then
      createdTimestamp = item[1]
    elsif item[0] == "lastUpdatedTimeStamp" then
      lastUpdatedTimeStamp = item[1]
    end 
  end   
  
  return TaskStatus.new(percentageComplete, requestStateEnum, result, errorCode, errorMessage, latestTaskDescription, processDescription, createdTimestamp, lastUpdatedTimeStamp)
end

.getVMDetails(sc_connection, vmResource) ⇒ Object

Gets the details of a VM



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/secured_cloud_api_client.rb', line 132

def self.getVMDetails(sc_connection, vmResource)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Virtual Machine Resource", vmResource)
  
  response = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + vmResource )
  responseBodyAsObject = JSON.parse(response.body)
  name, description, storageGB, storageType, memoryMB, vCPUs, powerStatus, nodeResource, organizationResource,
  operatingSystemTemplate, imageResource, disks, macAddress, deducedPrivateIps, ipMappings = ""
  
  responseBodyAsObject.each do |item|
    if item[0] == "name" then
      name = item[1]
    elsif item[0] == "description" then
      description = item[1]
    elsif item[0] == "storageGB" then
    storageGB = item[1]
    elsif item[0] == "storageType" then
      storageType = item[1]
    elsif item[0] == "memoryMB" then
      memoryMB = item[1]
    elsif item[0] == "vCPUs" then
      vCPUs = item[1]
    elsif item[0] == "powerStatus" then
      powerStatus = item[1]
    elsif item[0] == "nodeResource" then
      nodeResource = item[1]["resourceURL"]
    elsif item[0] == "organizationResource" then
      organizationResource = item[1]["resourceURL"]
    elsif item[0] == "operatingSystemTemplate" && !item[1].nil? then
      operatingSystemTemplate = item[1]["resourceURL"]
    elsif item[0] == "imageResource" then
      imageResource = item[1]
    elsif item[0] == "disks" then
      diskArray = item[1]
      disks = []
      diskArray.each do |disk|
        disks.push(disk["resourceURL"])          
      end
    elsif item[0] == "macAddress" then
      macAddress = item[1]
    elsif item[0] == "deducedPrivateIps" then
      deducedPrivateIps = item[1]        
    elsif item[0] == "ipMappings" then
      ipMappingsArray = item[1]
      ipMappings = []
      ipMappingsArray.each do |ipMapping|
        ipMappingToPush = IpMapping.new(ipMapping["privateIp"], ipMapping["publicIps"])
        ipMappings.push(ipMappingToPush)          
      end
    end 
  end 
            
  return VirtualMachine.new(name, description, storageGB, storageType, memoryMB, vCPUs, powerStatus, nodeResource, organizationResource,
    operatingSystemTemplate, imageResource, disks, macAddress, deducedPrivateIps, ipMappings)
end

.getVMPrivateIpDetails(sc_connection, privateIpResource) ⇒ Object

Returns the details of a VM private IP



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/secured_cloud_api_client.rb', line 268

def self.getVMPrivateIpDetails(sc_connection, privateIpResource)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Private IP Resource", privateIpResource)
  
  response = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + privateIpResource )
  responseBodyAsObject = JSON.parse(response.body)
  ipAddress, assignedTo, publicIpMappings, nodeResource, organizationResource = ""
  
  responseBodyAsObject.each do |item|
    if item[0] == "ipAddress" then
      ipAddress = item[1]
    elsif item[0] == "assignedTo" then
      vmResourceArray = item[1]
      assignedTo = []
      vmResourceArray.each do |vmResource|
        assignedTo.push(vmResource["resourceURL"])          
      end
    elsif item[0] == "publicIpMappings" then
      publicIpMappings = item[1]
    elsif item[0] == "nodeResource" then
      nodeResource = item[1]["resourceURL"]
    elsif item[0] == "organizationResource" then
      organizationResource = item[1]["resourceURL"]
    end
  end
  
  return VmPrivateIp.new(ipAddress, assignedTo, publicIpMappings, nodeResource, organizationResource)

end

.getVMPrivateIPs(sc_connection, vmResource) ⇒ Object

Returns all private IPs on a VM



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/secured_cloud_api_client.rb', line 211

def self.getVMPrivateIPs(sc_connection, vmResource)

  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Virtual Machine Resource", vmResource)

  response = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + vmResource + "/privateip")
  privateIps = []
  responseBodyAsObject = JSON.parse(response.body)
  responseBodyAsObject.each do |item|
    privateIps.push(item["resourceURL"])
  end
  
  return privateIps
  
end

.getVMPublicIpDetails(sc_connection, publicIpResource) ⇒ Object

Returns the details of a VM public IP



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/secured_cloud_api_client.rb', line 230

def self.getVMPublicIpDetails(sc_connection, publicIpResource)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Public IP Resource", publicIpResource)
  
  response = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + publicIpResource )
  responseBodyAsObject = JSON.parse(response.body)
  ipAddress, ipType, reserved, assignedTo, privateIpMapping, nodeResource, organizationResource = ""
  
  responseBodyAsObject.each do |item|
    if item[0] == "ipAddress" then
      ipAddress = item[1]
    elsif item[0] == "ipType" then
      ipType = item[1]
    elsif item[0] == "reserved" then
      reserved = item[1]
    elsif item[0] == "assignedTo" then
      vmResourceArray = item[1]
      assignedTo = []
      vmResourceArray.each do |vmResource|
        assignedTo.push(vmResource["resourceURL"])          
      end
    elsif item[0] == "privateIpMapping" then
      privateIpMapping = item[1]
    elsif item[0] == "nodeResource" then
      nodeResource = item[1]["resourceURL"]
    elsif item[0] == "organizationResource" then
      organizationResource = item[1]["resourceURL"]
    end
  end 
  
  return VmPublicIp.new(ipAddress, ipType, reserved, assignedTo, privateIpMapping, nodeResource, organizationResource)

end

.getVMPublicIPs(sc_connection, vmResource) ⇒ Object

Returns all public IPs on a VM



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/secured_cloud_api_client.rb', line 192

def self.getVMPublicIPs(sc_connection, vmResource)

  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Virtual Machine Resource", vmResource)

  response = HttpClient.sendGETRequest( sc_connection, sc_connection.getUrl() + vmResource + "/publicip")
  publicIps = []
  responseBodyAsObject = JSON.parse(response.body)
  responseBodyAsObject.each do |item|
    publicIps.push(item["resourceURL"])
  end
  return publicIps
  
end

.powerVM(sc_connection, vmResource, powerState) ⇒ Object

Starts a Power On/Off transaction. Returns the response code as well as the task resource if the transaction is triggered successfully.

Raises:

  • (ArgumentError)


374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/secured_cloud_api_client.rb', line 374

def self.powerVM(sc_connection, vmResource, powerState)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Virtual Machine Resource", vmResource)
  raise ArgumentError, 'powerState needs to be initialized' unless powerState != nil
  raise ArgumentError, 'powerState needs to be a valid string' unless powerState.respond_to?(:to_str)
  
  params = []
  params.push(["powerState", powerState]) 
  response = HttpClient.sendPUTRequest( sc_connection, sc_connection.getUrl() + vmResource + "/power", params)
  
  if (response.code == "202") then
    responseBodyAsObject = JSON.parse(response.body)
    responseBodyAsObject.each do |item|
      return "202", item[1]
    end
  else
    return response.code, response['X-Application-Error-Reference'], response['X-Application-Error-Description']
  end
end

.rebootVM(sc_connection, vmResource) ⇒ Object

Starts a Reboot VM transaction. Returns the response code as well as the task resource if the transaction is triggered successfully.



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/secured_cloud_api_client.rb', line 400

def self.rebootVM(sc_connection, vmResource)
  
  #validation
  validate_sc_connection(sc_connection)
  validate_resource("Virtual Machine Resource", vmResource)
  
  response = HttpClient.sendPUTRequest( sc_connection, sc_connection.getUrl() + vmResource + "/reboot")
  if (response.code == "202") then
    responseBodyAsObject = JSON.parse(response.body)
    responseBodyAsObject.each do |item|
    return "202", item[1]
  end
  else
    return response.code, response['X-Application-Error-Reference'], response['X-Application-Error-Description']
  end
  
end

.validate_resource(resourceName, resource) ⇒ Object

validation for resource URLs used by all api calls

Raises:

  • (ArgumentError)


33
34
35
36
# File 'lib/secured_cloud_api_client.rb', line 33

def self.validate_resource(resourceName, resource)
  raise ArgumentError, resourceName.to_s + ' needs to be initialized' unless resource != nil
  raise ArgumentError, resourceName.to_s + ' needs to be a valid string' unless resource.respond_to?(:to_str)
end