def get_isolated_network_href(self, vdc_name, network_name):
        vdc = self.get_vdc(vdc_name)
        if vdc is None:
            LOG.error("Get_isolated_network_href error, cannot get vdc:"
                      "%s info", vdc_name)
            excep_msg = "Get_isolated_network_href error, cannot find the "\
                "vdc:%s" % (vdc_name)
            raise exceptions.VCloudDriverException(excep_msg)

        link = filter(lambda link: link.get_rel() == "orgVdcNetworks",
                      vdc.get_Link())
        vcloud_headers = self.vcloud_session.get_vcloud_headers()
        response = self._invoke_api(requests, 'get',
                                    link[0].get_href(),
                                    headers=vcloud_headers,
                                    verify=self.verify)
        queryResultRecords = queryRecordViewType.parseString(response.content,
                                                             True)
        if response.status_code == requests.codes.forbidden:
            excep_msg = "Get_isolated_network_href error, network_name:%s"\
                % (network_name)
            raise exceptions.ForbiddenException(excep_msg)
        if response.status_code == requests.codes.ok:
            for record in queryResultRecords.get_Record():
                if record.name == network_name:
                    return record.href
        elif response.status_code == requests.codes.forbidden:
            excep_msg = "Get_isolated_network_href forbidden, network_name:%s"\
                % (network_name)
            raise exceptions.ForbiddenException(excep_msg)
        else:
            excep_msg = "Get_isolated_network_href failed response:%s"\
                % (response)
            raise exceptions.VCloudDriverException(excep_msg)
    def get_vapp(self, vdc, vapp_name):
        vapp = super(VCA, self).get_vapp(vdc, vapp_name)

        if not vapp:
            LOG.error("cannot get vapp %s info" % vapp_name)
            raise exceptions.ForbiddenException("cannot get vapp %s info" % vapp_name)

        return VAPP(vapp.me, vapp.headers, vapp.verify)
    def create_isolated_vdc_network(self, vdc_name, network_name, gateway_name,
                                    start_address, end_address, gateway_ip,
                                    netmask, dns1=None, dns2=None,
                                    dns_suffix=None):
        vdc = self.get_vdc(vdc_name)
        if vdc is None:
            LOG.error("Create isolated vdc network error, cannot get vdc:"
                      "%s info", vdc_name)
            raise exceptions.VCloudDriverException("Create isolated vdc"
                                                   " network error, cannot"
                                                   "find the vdc_name:" +
                                                   vdc_name)
        iprange = IpRangeType(StartAddress=start_address,
                              EndAddress=end_address)
        ipranges = IpRangesType(IpRange=[iprange])
        ipscope = IpScopeType(IsInherited=False,
                              Gateway=gateway_ip,
                              Netmask=netmask,
                              Dns1=dns1,
                              Dns2=dns2,
                              DnsSuffix=dns_suffix,
                              IpRanges=ipranges)
        ipscopes = IpScopesType(IpScope=[ipscope])

        configuration = NetworkConfigurationType(IpScopes=ipscopes,
                                                 FenceMode="isolated")
        net = OrgVdcNetworkType(name=network_name,
                                Description="Network created name is " +
                                network_name,
                                EdgeGateway=None, Configuration=configuration,
                                IsShared=False)
        namespacedef = 'xmlns="http://www.vmware.com/vcloud/v1.5"'
        content_type = "application/vnd.vmware.vcloud.orgVdcNetwork+xml"
        body = '<?xml version="1.0" encoding="UTF-8"?>{0}'.format(
            CommonUtils.convertPythonObjToStr(net, name='OrgVdcNetwork',
                                              namespacedef=namespacedef))
        postlink = filter(lambda link: link.get_type() == content_type,
                          vdc.get_Link())[0].href
        headers = self.vcloud_session.get_vcloud_headers()
        headers["Content-Type"] = content_type
        response = self._invoke_api(requests, 'post',
                                    postlink,
                                    data=body,
                                    headers=headers,
                                    verify=self.verify)

        if response.status_code == requests.codes.forbidden:
            raise exceptions.ForbiddenException("Create_isolated_vdc_network"
                                                "error, network_name:" +
                                                network_name)
        if response.status_code == requests.codes.created:
            network = networkType.parseString(response.content, True)
            task = network.get_Tasks().get_Task()[0]
            return (True, task)
        else:
            return (False, response.content)
 def enableDownload(self):
     headers = self.headers
     url = '%s/action/enableDownload' % self.me.get_href()
     self.response = requests.post(
         url,
         data={},
         headers=headers,
         verify=self.verify)
     if self.response.status_code == requests.codes.accepted:
         return taskType.parseString(self.response.content, True)
     elif self.response.status_code == requests.codes.forbidden:
         LOG.error("enableDownloadfailed: forbidden vapp %s" % self.name)
         raise exceptions.ForbiddenException("enableDownloadfailed: forbidden vapp %s" % self.name)
     else:
         return False
 def get_task_result(self, task):
     """
     Wait the task finished, and return the result
     """
     headers = self.vcloud_session.get_vcloud_headers()
     headers["Content-Type"] = "application/vnd.vmware.vcloud.task+xml"
     response = self._invoke_api(requests, 'get',
                                 task.href, headers=headers,
                                 verify=self.verify)
     if response.status_code == requests.codes.forbidden:
         excep_msg = "Execute task error, task is: %s" \
             % (task)
         raise exceptions.ForbiddenException(excep_msg)
     if response.status_code == requests.codes.ok:
         doc = self.parsexml_(response.content)
         return doc.attrib
     return {'status': "Error"}
    def get_ovf_descriptor(self):
        link = filter(
            lambda link: link.get_rel() == 'download:default',
            self.me.get_Link())
        if not link:
            print "Can Not get download url"
            return

        headers = self.headers
        url = link[0].get_href()
        self.response = requests.get(url, headers=headers, verify=self.verify)
        if self.response.status_code == requests.codes.ok:
            return vAppType.parseString(self.response.content, True)
        elif self.response.status_code == requests.codes.forbidden:
            LOG.error("get_ovf_descriptor failed: forbidden. vapp %s " % self.name)
            raise exceptions.ForbiddenException("get_ovf_descriptor failed: forbidden. vapp %s" % self.name)
        else:
            return False
 def get_vdc(self, vdc_name):
     if self.vcloud_session and self.vcloud_session.organization:
         refs = filter(lambda ref: ref.name == vdc_name and ref.type_ ==
                       'application/vnd.vmware.vcloud.vdc+xml',
                       self.vcloud_session.organization.Link)
         if len(refs) == 1:
             headers = self.vcloud_session.get_vcloud_headers()
             response = self._invoke_api(requests, 'get',
                                         refs[0].href,
                                         headers=headers,
                                         verify=self.verify)
             if response.status_code == requests.codes.ok:
                 return vdcType.parseString(response.content, True)
             elif response.status_code == requests.codes.forbidden:
                 excep_msg = "Get_vdc forbidden, vdc_name:%s" % (vdc_name)
                 raise exceptions.ForbiddenException(excep_msg)
             else:
                 excep_msg = "Get_vdc failed, response:%s" % (response)
                 raise exceptions.VCloudDriverException(excep_msg)
    def delete_isolated_vdc_network(self, vdc_name, network_name):
        netref = self.get_isolated_network_href(vdc_name, network_name)
        if netref is None:
            return (True, None)
        vcloud_headers = self.vcloud_session.get_vcloud_headers()
        response = self._invoke_api(requests, 'delete',
                                    netref,
                                    headers=vcloud_headers,
                                    verify=self.verify)

        if response.status_code == requests.codes.forbidden:
            excep_msg = "Delete_isolated_vdc_network error, network_name:%s"\
                % (network_name)
            raise exceptions.ForbiddenException(excep_msg)
        if response.status_code == requests.codes.bad_request:
            excep_msg = "Delete_isolated_vdc_network bad request, network_name:%s"\
                % (network_name)
            raise exceptions.DeleteException(excep_msg)
        if response.status_code == requests.codes.accepted:
            task = taskType.parseString(response.content, True)
            return (True, task)
        else:
            return (False, response.content)