Class: Compute

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/modules/compute-be.rb

Instance Method Summary collapse

Instance Method Details

#apiversions(apiendpoint) ⇒ Object



26
27
28
29
30
# File 'lib/modules/compute-be.rb', line 26

def apiversions(apiendpoint)
    self.class.base_uri apiendpoint
    apiversionsarray = self.class.get('/api/versions')
 return apiversionsarray
end

#catalogitems(computetoken, cataloghref, apiversion) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/modules/compute-be.rb', line 65

def catalogitems (computetoken, cataloghref, apiversion)
   catalogitemsarray = instancedetails(computetoken, cataloghref, "", apiversion)
   if catalogitemsarray["Catalog"]["CatalogItems"] != nil 
   	catalogitemsarray["Catalog"]["CatalogItems"]["CatalogItem"] = hashtoarray(catalogitemsarray["Catalog"]["CatalogItems"]["CatalogItem"])
   end
   return catalogitemsarray
end

#catalogs(computetoken, computeapiendpoint, apiversion) ⇒ Object



58
59
60
61
62
# File 'lib/modules/compute-be.rb', line 58

def catalogs (computetoken, computeapiendpoint, apiversion)
   catalogsarray = instancedetails(computetoken, computeapiendpoint, "/api/catalogs/query", apiversion)
   catalogsarray["QueryResultRecords"]["CatalogRecord"] = hashtoarray(catalogsarray["QueryResultRecords"]["CatalogRecord"])
   return catalogsarray
end

#hashtoarray(structure) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/modules/compute-be.rb', line 15

def hashtoarray(structure)
	 #this function work arounds an issue of httparty (and other REST clients apparently) that do not comply to the XML Schema correctly
	 #in some circumstances the httparty response contains a hash whereas it should be an array of hash with one item 
	 #this function takes input a JSON structure and check if it's a hash. If it is, it will turn it into an array of hash with one element
	 #if the input is already an Array of hash it will do nothing
	 #for further reference:  http://stackoverflow.com/questions/28282125/httparty-response-interpretation-in-ruby/ 
	 structure = [structure] unless structure.is_a? Array
	 return structure
end

#instancedetails(computetoken, computeapiendpoint, query, apiversion) ⇒ Object



44
45
46
47
48
# File 'lib/modules/compute-be.rb', line 44

def instancedetails(computetoken, computeapiendpoint, query, apiversion)
	self.class.default_options[:headers] = { "Accept" => "application/*+xml;version=" + apiversion, "x-vcloud-authorization" => computetoken }    
	instancedetails = self.class.get(computeapiendpoint + query)
  return instancedetails 
end

#login(username, password, orgname, computeapiendpoint, apiversion) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/modules/compute-be.rb', line 33

def (username, password, orgname, computeapiendpoint, apiversion)
	credentials = username + "@" + orgname
  self.class.base_uri computeapiendpoint
	self.class.basic_auth credentials, password
	self.class.default_options[:headers] = {"Accept" => "application/*+xml;version=" + apiversion}
  response = self.class.post('/api/sessions')
  computetoken = response.headers['x-vcloud-authorization']
  return computetoken 
end

#networkdetails(computetoken, networkhref, apiversion) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/modules/compute-be.rb', line 94

def networkdetails (computetoken, networkhref, apiversion)
   networkdetails = instancedetails(computetoken, networkhref, "", apiversion)
   if networkdetails["OrgVdcNetwork"]["Configuration"]["IpScopes"]["IpScope"]["IpRanges"]
   	networkdetails["OrgVdcNetwork"]["Configuration"]["IpScopes"]["IpScope"]["IpRanges"]["IpRange"] = hashtoarray(networkdetails["OrgVdcNetwork"]["Configuration"]["IpScopes"]["IpScope"]["IpRanges"]["IpRange"]) 
   end #if networkdetails["OrgVdcNetwork"]["Configuration"]["IpScopes"]["IpScope"]["IpRanges"]
   return networkdetails
end

#networks(computetoken, vdchref, apiversion) ⇒ Object



86
87
88
89
90
91
# File 'lib/modules/compute-be.rb', line 86

def networks (computetoken, vdchref, apiversion)
   vdcdetails = instancedetails(computetoken, vdchref, "", apiversion)
   networksarray = vdcdetails["Vdc"]["AvailableNetworks"]
   networksarray["Network"] = hashtoarray(networksarray["Network"])
   return networksarray
end

#orgvapps(computetoken, computeapiendpoint, apiversion) ⇒ Object



80
81
82
83
# File 'lib/modules/compute-be.rb', line 80

def orgvapps (computetoken, computeapiendpoint, apiversion)
   orgvappsarray = instancedetails(computetoken, computeapiendpoint, "/api/vApps/query", apiversion)
   return orgvappsarray
end

#orgvms(computetoken, computeapiendpoint, apiversion) ⇒ Object



74
75
76
77
# File 'lib/modules/compute-be.rb', line 74

def orgvms (computetoken, computeapiendpoint, apiversion)
   orgvmsarray = instancedetails(computetoken, computeapiendpoint, "/api/vms/query", apiversion)
   return orgvmsarray
end

#vdcs(computetoken, computeapiendpoint, apiversion) ⇒ Object



51
52
53
54
55
# File 'lib/modules/compute-be.rb', line 51

def vdcs (computetoken, computeapiendpoint, apiversion)
   vdcsarray = instancedetails(computetoken, computeapiendpoint, "/api/admin/vdcs/query", apiversion)
   vdcsarray["QueryResultRecords"]["OrgVdcRecord"] = hashtoarray(vdcsarray["QueryResultRecords"]["OrgVdcRecord"])
   return vdcsarray
end