+ vca = self.connect_as_admin()
+ if not vca:
+ raise vimconn.vimconnConnectionException("self.connect() is failed")
+ # Get Catalog details
+ url_list = [self.vca.host, '/api/catalog/', image_id]
+ catalog_herf = ''.join(url_list)
+ response = Http.get(url=catalog_herf,
+ headers=vca.vcloud_session.get_vcloud_headers(),
+ verify=vca.verify,
+ logger=vca.logger)
+
+ if response.status_code != requests.codes.ok:
+ self.logger.debug("delete_image():GET REST API call {} failed. "\
+ "Return status code {}".format(catalog_herf,
+ response.status_code))
+ raise vimconn.vimconnNotFoundException("Fail to get image {}".format(image_id))
+
+ lxmlroot_respond = lxmlElementTree.fromstring(response.content)
+ namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
+ namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
+
+ catalogItems_section = lxmlroot_respond.find("xmlns:CatalogItems",namespaces)
+ catalogItems = catalogItems_section.iterfind("xmlns:CatalogItem",namespaces)
+ for catalogItem in catalogItems:
+ catalogItem_href = catalogItem.attrib['href']
+
+ #GET details of catalogItem
+ response = Http.get(url=catalogItem_href,
+ headers=vca.vcloud_session.get_vcloud_headers(),
+ verify=vca.verify,
+ logger=vca.logger)
+
+ if response.status_code != requests.codes.ok:
+ self.logger.debug("delete_image():GET REST API call {} failed. "\
+ "Return status code {}".format(catalog_herf,
+ response.status_code))
+ raise vimconn.vimconnNotFoundException("Fail to get catalogItem {} for catalog {}".format(
+ catalogItem,
+ image_id))
+
+ lxmlroot_respond = lxmlElementTree.fromstring(response.content)
+ namespaces = {prefix:uri for prefix,uri in lxmlroot_respond.nsmap.iteritems() if prefix}
+ namespaces["xmlns"]= "http://www.vmware.com/vcloud/v1.5"
+ catalogitem_remove_href = lxmlroot_respond.find("xmlns:Link[@rel='remove']",namespaces).attrib['href']
+
+ #Remove catalogItem
+ response = Http.delete(url= catalogitem_remove_href,
+ headers=vca.vcloud_session.get_vcloud_headers(),
+ verify=vca.verify,
+ logger=vca.logger)
+ if response.status_code == requests.codes.no_content:
+ self.logger.debug("Deleted Catalog item {}".format(catalogItem))
+ else:
+ raise vimconn.vimconnException("Fail to delete Catalog Item {}".format(catalogItem))
+
+ #Remove catalog
+ url_list = [self.vca.host, '/api/admin/catalog/', image_id]
+ catalog_remove_herf = ''.join(url_list)
+ response = Http.delete(url= catalog_remove_herf,
+ headers=vca.vcloud_session.get_vcloud_headers(),
+ verify=vca.verify,
+ logger=vca.logger)
+
+ if response.status_code == requests.codes.no_content:
+ self.logger.debug("Deleted Catalog {}".format(image_id))
+ return image_id
+ else:
+ raise vimconn.vimconnException("Fail to delete Catalog {}".format(image_id))