|
| 1 | +'''Implements the Cloud.com API''' |
| 2 | + |
| 3 | + |
| 4 | +from cloudtool.utils import describe |
| 5 | +import urllib |
| 6 | +import urllib2 |
| 7 | +import os |
| 8 | +import xml.dom.minidom |
| 9 | + |
| 10 | +class CloudAPI: |
| 11 | + |
| 12 | + @describe("server", "Management Server host name or address") |
| 13 | + @describe("responseformat", "Response format: xml or json") |
| 14 | + def __init__(self, |
| 15 | + server="127.0.0.1:8096", |
| 16 | + responseformat="xml", |
| 17 | + ): |
| 18 | + self.__dict__.update(locals()) |
| 19 | + |
| 20 | + def _make_request(self,command,parameters=None): |
| 21 | + '''Command is a string, parameters is a dictionary''' |
| 22 | + if ":" in self.server: |
| 23 | + host,port = self.server.split(":") |
| 24 | + port = int(port) |
| 25 | + else: |
| 26 | + host = self.server |
| 27 | + port = 8096 |
| 28 | + |
| 29 | + url = "http://" + self.server + "/?" |
| 30 | + |
| 31 | + if not parameters: parameters = {} |
| 32 | + parameters["command"] = command |
| 33 | + parameters["response"] = self.responseformat |
| 34 | + querystring = urllib.urlencode(parameters) |
| 35 | + url += querystring |
| 36 | + |
| 37 | + f = urllib2.urlopen(url) |
| 38 | + |
| 39 | + data = f.read() |
| 40 | + |
| 41 | + return data |
| 42 | + |
| 43 | + |
| 44 | +def load_dynamic_methods(): |
| 45 | + '''creates smart function objects for every method in the commands.xml file''' |
| 46 | + |
| 47 | + def getText(nodelist): |
| 48 | + rc = [] |
| 49 | + for node in nodelist: |
| 50 | + if node.nodeType == node.TEXT_NODE: rc.append(node.data) |
| 51 | + return ''.join(rc) |
| 52 | + |
| 53 | + # FIXME figure out installation and packaging |
| 54 | + xmlfile = os.path.join("/etc/cloud/cli/","commands.xml") |
| 55 | + dom = xml.dom.minidom.parse(xmlfile) |
| 56 | + |
| 57 | + for cmd in dom.getElementsByTagName("command"): |
| 58 | + name = getText(cmd.getElementsByTagName('name')[0].childNodes).strip() |
| 59 | + assert name |
| 60 | + |
| 61 | + description = cmd.getElementsByTagName('name')[0].getAttribute("description") |
| 62 | + if description: description = '"""%s"""' % description |
| 63 | + else: description = '' |
| 64 | + arguments = [] |
| 65 | + options = [] |
| 66 | + descriptions = [] |
| 67 | + |
| 68 | + for param in cmd.getElementsByTagName('arg'): |
| 69 | + argname = getText(param.childNodes).strip() |
| 70 | + assert argname |
| 71 | + |
| 72 | + required = param.getAttribute("required").strip() |
| 73 | + if required == 'true': required = True |
| 74 | + elif required == 'false': required = False |
| 75 | + else: raise AssertionError, "Not reached" |
| 76 | + if required: arguments.append(argname) |
| 77 | + options.append(argname) |
| 78 | + |
| 79 | + description = param.getAttribute("description").strip() |
| 80 | + if description: descriptions.append( (argname,description) ) |
| 81 | + |
| 82 | + funcparams = ["self"] + [ "%s=None"%o for o in options ] |
| 83 | + funcparams = ", ".join(funcparams) |
| 84 | + |
| 85 | + code = """ |
| 86 | + def %s(%s): |
| 87 | + %s |
| 88 | + parms = locals() |
| 89 | + del parms["self"] |
| 90 | + for arg in %r: |
| 91 | + if locals()[arg] is None: |
| 92 | + raise TypeError, "%%s is a required option"%%arg |
| 93 | + for k,v in parms.items(): |
| 94 | + if v is None: del parms[k] |
| 95 | + output = self._make_request("%s",parms) |
| 96 | + return output |
| 97 | + """%(name,funcparams,description,arguments,name) |
| 98 | + |
| 99 | + namespace = {} |
| 100 | + exec code.strip() in namespace |
| 101 | + |
| 102 | + func = namespace[name] |
| 103 | + for argname,description in descriptions: |
| 104 | + func = describe(argname,description)(func) |
| 105 | + |
| 106 | + yield (name,func) |
| 107 | + |
| 108 | + |
| 109 | +for name,meth in load_dynamic_methods(): setattr(CloudAPI,name,meth) |
| 110 | + |
| 111 | +implementor = CloudAPI |
| 112 | + |
| 113 | +del name,meth,describe,load_dynamic_methods |
0 commit comments