+ dirpath, filename = os.path.split(path)
+ flname, file_extension = os.path.splitext(path)
+ if file_extension != '.ovf':
+ self.logger.debug("Wrong file extension {} connector support only OVF container.".format(file_extension))
+ raise Exception("Wrong container. vCloud director supports only OVF.")
+
+ self.logger.debug("File name {} Catalog Name {} file path {} ".format(filename,
+ catalog_name,
+ path))
+ try:
+ client = self.connect()
+ if not client:
+ raise Exception("Failed to connect vCD")
+ org = Org(client, resource=client.get_org())
+ catalogs = org.list_catalogs()
+ except Exception as exp:
+ self.logger.debug("Failed get catalogs() with Exception {} ".format(exp))
+ raise Exception("Failed get catalogs() with Exception {} ".format(exp))
+
+ if len(catalogs) == 0:
+ self.logger.info("Creating a new catalog entry {} in vcloud director".format(catalog_name))
+ result = org.create_catalog(catalog_name, catalog_name)
+ if result is None:
+ raise Exception("Failed to create new catalog {} ".format(catalog_name))
+ result = self.upload_ovf(org=org, catalog_name=catalog_name, image_name=filename.split(".")[0],
+ media_file_name=path, description='medial_file_name', progress=progress)
+ if not result:
+ raise Exception("Failed to create vApp template for catalog {} ".format(catalog_name))
+ return self.get_catalogid(catalog_name, catalogs)
+ else:
+ for catalog in catalogs:
+ # search for existing catalog if we find same name we return ID
+ if catalog['name'] == catalog_name:
+ self.logger.debug("Found existing catalog entry for {} "
+ "catalog id {}".format(catalog_name,
+ self.get_catalogid(catalog_name, catalogs)))
+ return self.get_catalogid(catalog_name, catalogs)
+
+ # if we didn't find existing catalog we create a new one and upload image.
+ self.logger.debug("Creating new catalog entry {} - {}".format(catalog_name, catalog_name))
+ result = org.create_catalog(catalog_name, catalog_name)
+ if result is None:
+ raise Exception("Failed to create new catalog {} ".format(catalog_name))
+
+ result = self.upload_ovf(org=org, catalog_name=catalog_name, image_name=filename.split(".")[0],
+ media_file_name=path, description='medial_file_name', progress=progress)
+ if not result:
+ raise Exception("Failed create vApp template for catalog {} ".format(catalog_name))
+
+ def get_catalogid(self, catalog_name=None, catalogs=None):
+ """ Method check catalog and return catalog ID in UUID format.
+
+ Args
+ catalog_name: catalog name as string
+ catalogs: list of catalogs.
+
+ Return: catalogs uuid
+ """
+
+ for catalog in catalogs:
+ if catalog['name'] == catalog_name:
+ catalog_id = catalog['id']
+ return catalog_id
+ return None
+
+ def upload_ovf(self, org=None, catalog_name=None, image_name=None, media_file_name=None,