diff --git a/calm/dsl/cli/marketplace.py b/calm/dsl/cli/marketplace.py index 07daa0ed..a15e553b 100644 --- a/calm/dsl/cli/marketplace.py +++ b/calm/dsl/cli/marketplace.py @@ -68,7 +68,6 @@ def get_group_data_value(data_list, field, value_list=False): entity_value = entity["values"] if not entity_value: return None - return ( entity_value[0]["values"] if value_list @@ -347,6 +346,28 @@ def get_mpi_latest_version(name, app_source=None, app_states=[], type=None): return entity_version +def get_mpi_all_versions(name, app_source=None, app_states=[], type=None): + + res = get_mpis_group_call( + name=name, + app_states=app_states, + group_member_count=20, + app_source=app_source, + type=type, + ) + group_results = res["group_results"] + + if not group_results: + LOG.error("No Marketplace Item found with name {}".format(name)) + sys.exit(-1) + + entity_results = group_results[0]["entity_results"] + all_versions = [] + for i in range(0, len(entity_results)): + all_versions.append(get_group_data_value(entity_results[i]["data"], "version")) + return all_versions + + def get_mpi_by_name_n_version(name, version, app_states=[], app_source=None, type=None): """ It will fetch marketplace item with particular version. @@ -1454,10 +1475,25 @@ def reject_marketplace_item(name, version, type=None): ) -def unpublish_marketplace_item(name, version, app_source=None, type=None): +def unpublish_marketplace_item( + name, version, app_source=None, projects=None, all_versions=None, type=None +): client = get_api_client() - if not version: + versions = [] + if version: + versions.append(version) + elif all_versions: + # Fecth all versions + all_versions = get_mpi_all_versions( + name=name, + app_states=[MARKETPLACE_ITEM.STATES.PUBLISHED], + app_source=app_source, + type=type, + ) + versions.extend(all_versions) + LOG.info(versions) + elif not version and not all_versions: # Search for published items, only those can be unpublished LOG.info( "Fetching latest version of published Marketplace Item {} ".format(name) @@ -1468,45 +1504,86 @@ def unpublish_marketplace_item(name, version, app_source=None, type=None): app_source=app_source, type=type, ) - LOG.info(version) + versions.append(version) - LOG.info( - "Fetching details of published marketplace item {} with version {}".format( - name, version + for version in versions: + LOG.info( + "Fetching details of published marketplace item {} with version {}".format( + name, version + ) ) - ) - item = get_mpi_by_name_n_version( - name=name, - version=version, - app_states=[MARKETPLACE_ITEM.STATES.PUBLISHED], - app_source=app_source, - type=type, - ) - item_uuid = item["metadata"]["uuid"] - - res, err = client.market_place.read(item_uuid) - if err: - LOG.error("[{}] - {}".format(err["code"], err["error"])) - sys.exit(-1) + item = get_mpi_by_name_n_version( + name=name, + version=version, + app_states=[MARKETPLACE_ITEM.STATES.PUBLISHED], + app_source=app_source, + type=type, + ) + item_uuid = item["metadata"]["uuid"] - item_data = res.json() - item_data.pop("status", None) - item_data["api_version"] = "3.0" - item_data["spec"]["resources"]["app_state"] = MARKETPLACE_ITEM.STATES.ACCEPTED + res, err = client.market_place.read(item_uuid) + if err: + LOG.error("[{}] - {}".format(err["code"], err["error"])) + sys.exit(-1) - res, err = client.market_place.update(uuid=item_uuid, payload=item_data) - if err: - LOG.error("[{}] - {}".format(err["code"], err["error"])) - sys.exit(-1) + item_data = res.json() + item_data.pop("status", None) + item_data["api_version"] = "3.0" + if projects: + project_name_uuid_map = client.project.get_name_uuid_map( + params={"length": 250} + ) + for project in projects: + # Validating the given projects + if project not in project_name_uuid_map: + LOG.error( + "Project {} does not exist in system to unpublish from MPI".format( + project + ) + ) + sys.exit(-1) + + project_valid = False + for index, project_detail in enumerate( + item_data["spec"]["resources"]["project_reference_list"] + ): + if project == project_detail["name"]: + item_data["spec"]["resources"]["project_reference_list"].pop( + index + ) + project_valid = True + break + + # Validating the project association with MPI + if not project_valid: + LOG.error( + "Project {} is not associated with MPI {} to unpublish".format( + project, name + ) + ) + sys.exit(-1) + else: + item_data["spec"]["resources"][ + "app_state" + ] = MARKETPLACE_ITEM.STATES.ACCEPTED - LOG.info( - "Marketplace Item {} with version {} is unpublished successfully".format( - name, version + res, err = client.market_place.update(uuid=item_uuid, payload=item_data) + if err: + LOG.error("[{}] - {}".format(err["code"], err["error"])) + sys.exit(-1) + additional_log = ( + "from projects {}".format(", ".join(projects)) if projects else "" + ) + LOG.info( + "Marketplace Item {} with version {} is unpublished successfully {}".format( + name, version, additional_log + ) ) - ) -def unpublish_marketplace_bp(name, version, app_source=None): +def unpublish_marketplace_bp( + name, version, app_source=None, projects=None, all_versions=None +): """unpublishes marketplace blueprint""" if not version: @@ -1545,11 +1622,13 @@ def unpublish_marketplace_bp(name, version, app_source=None): "Marketplace blueprint {} with version {} not found".format(name, version) ) sys.exit(-1) - + version = None if all_versions else version unpublish_marketplace_item( name=name, version=version, app_source=app_source, + projects=projects, + all_versions=all_versions, type=MARKETPLACE_ITEM.TYPES.BLUEPRINT, ) diff --git a/calm/dsl/cli/marketplace_bp_commands.py b/calm/dsl/cli/marketplace_bp_commands.py index 600f6b7b..d515aa27 100644 --- a/calm/dsl/cli/marketplace_bp_commands.py +++ b/calm/dsl/cli/marketplace_bp_commands.py @@ -575,7 +575,7 @@ def _reject_marketplace_bp(name, version): @marketplace_unpublish.command("bp") @click.argument("name") @click.option( - "--version", "-v", required=True, help="Version of marketplace blueprint" + "--version", "-v", default=None, help="Version of marketplace blueprint" ) # Required to prevent unwanted unpublish of unknown mpi @click.option( "--source", @@ -584,7 +584,27 @@ def _reject_marketplace_bp(name, version): type=click.Choice(APP_SOURCES), help="App Source of marketplace blueprint", ) -def _unpublish_marketplace_bp(name, version, source): +@click.option( + "--project", + "-p", + "projects", + multiple=True, + help="Unpublishes bp from specific project", +) +@click.option( + "--all_versions", + "-av", + is_flag=True, + default=False, + help="Unpublishes bp from all version", +) +def _unpublish_marketplace_bp(name, version, source, all_versions, projects=[]): """Unpublish marketplace store blueprint""" - unpublish_marketplace_bp(name=name, version=version, app_source=source) + unpublish_marketplace_bp( + name=name, + version=version, + app_source=source, + projects=projects, + all_versions=all_versions, + )