def getVMTemplates(self): resp = requests.get(url=self.api + '/vAppTemplate/' + self.id, headers=self.headers) tree = treeify_xml(resp) children = tree.find('{*}Children') vms = children.findall('{*}Vm') return [VMTemplate(template, self.vcloud) for template in vms]
def addUsers(self, users=None, timeout: int = 300, check_time: int = 5, perms: str = "ReadOnly"): """ Add users to vapp :param users: :param timeout: :param check_time: :param perms: FullControl, Change, ReadOnly :return: """ resp = requests.get(url=self.href + '/controlAccess', headers=self.headers) tree = treeify_xml(resp) access_settings = tree.find('{*}AccessSettings') if access_settings is None: access_settings = etree.SubElement(tree, "AccessSettings") self.waitOnReady(timeout=timeout, check_time=check_time) if users is not None: for user in users: acl = self._generateACLParams(user, perms=perms) access_settings.append(acl) params = etree.tostring(tree) tree = self._action(self.href + '/action/controlAccess', data=params, timeout=timeout, checkTime=check_time)
def getRole(self, name): resp = requests.get(url=self.api + f"/query?type=role&filter=name=={name}", headers=self.headers) tree = treeify_xml(resp) result = tree.find('{*}RoleRecord') if result is None: return None return Role(result, self)
def getVMs(self): resp = requests.get(url=self.href, headers=self.headers) tree = treeify_xml(resp) children = tree.find('{*}Children') vms = children.findall('{*}Vm') l = [] for vm in vms: l.append(VM(vm, self.vcloud)) return l
def importUser(self, name, role): params = self._generateUserParams(name, role) resp = requests.post(url=self.api + '/admin/org/' + self.id + '/users', headers=self.headers, data=params) tree = treeify_xml(resp) if 'Error' in tree.tag: print('Error:', tree.attrib['message']) return None return User(tree, self.vcloud)
def createUser(self, name, password, role): newApi = self.href.replace('api', 'api/admin') params = self._generateUserCreateParams(name, password, role) resp = requests.post(url=newApi + '/users', headers=self.headers, data=params) tree = treeify_xml(resp) if tree is not None: return User(tree, self) else: return None
def renew(self, deploy_seconds: int = 86400, storage_seconds: int = 0): resp = requests.get(url=self.href + '/leaseSettingsSection', headers=self.headers) tree = treeify_xml(resp) tree.find('{*}DeploymentLeaseInSeconds').text = str(deploy_seconds) tree.find('{*}StorageLeaseInSeconds').text = str(storage_seconds) lease_section = etree.tostring(tree, encoding="utf-8", method="xml").decode('utf-8') requests.put(url=self.href + '/leaseSettingsSection', data=lease_section, headers=self.headers)
def deploy(self, vdc, name=None): params = self.vcloud.genInstantiateVAppTemplateParams( vAppHref=self.href, name=name) resp = requests.post(url=self.vcloud.api + "/vdc/" + vdc.id + "/action/instantiateVAppTemplate", headers=self.headers, data=params) tree = treeify_xml(resp) if 'Error' in tree.tag: print('Error:', tree.attrib['message']) return None return vApp(tree, self.vcloud)
def getTemplates(self, filter='*', vdc=None): if vdc is not None: resp = requests.get( url=self.api + '/vAppTemplates/query?pageSize=128&filter=(name==' + filter + ';vdc==' + vdc.href + ')', headers=self.headers) else: resp = requests.get( url=self.api + '/vAppTemplates/query?pageSize=128&filter=(name==' + filter + ')', headers=self.headers) tree = treeify_xml(resp) results = tree.findall('{*}VAppTemplateRecord') return [VAppTemplate(template, self.vcloud) for template in results]
def _action(self, actionPath, requestType='POST', data=None, timeout: int = 300, checkTime: int = 5): self.waitOnReady(timeout=timeout, check_time=checkTime) if requestType.upper() == 'POST': resp = requests.post(url=actionPath, headers=self.headers, data=data) if requestType.upper() == 'GET': resp = requests.get(url=actionPath, headers=self.headers) if requestType.upper() == 'DELETE': resp = requests.delete(url=actionPath, headers=self.headers) if requestType.upper() == 'PUT': resp = requests.put(url=actionPath, headers=self.headers, data=data) tree = treeify_xml(resp) if 'Error' in tree.tag: print('Error:', tree.attrib['message']) raise Exception(tree.attrib['message']) return tree
def objectify_xml(self, response: requests.Response, obj_type) -> list: tree = treeify_xml(response) result = tree.findall(obj_type.findString) return [obj_type(data, self) for data in result]