Example #1
0
 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]
Example #2
0
 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)
Example #3
0
 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)
Example #4
0
 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
Example #5
0
 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)
Example #6
0
 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
Example #7
0
    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)
Example #8
0
    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)
Example #9
0
    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]
Example #10
0
 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
Example #11
0
 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]