Skip to content

Commit

Permalink
Calm unpublish marketplace improvements (nutanix#297)
Browse files Browse the repository at this point in the history
1. `In case of invalid project: Project doesn't exist in system`

<img width="1246" alt="Screenshot 2023-10-09 at 11 24 06 PM"
src="https://github.com/ideadevice/calm-dsl/assets/52671728/b87d885b-d7a4-4cca-b4f5-5c13611f1388">

```===========================================================================```
2. `In case of invalid project: Project is not associated with MPI`
<img width="1263" alt="Screenshot 2023-10-09 at 11 23 54 PM" src="https://github.com/ideadevice/calm-dsl/assets/52671728/54df9e38-21db-4082-964c-4483dc00c9bc">

```===========================================================================```
3. `In case of unpublish from all versions from specific project`
<img width="1241" alt="Screenshot 2023-10-09 at 11 51 01 PM"
src="https://github.com/ideadevice/calm-dsl/assets/52671728/a0a73f17-af0e-4c2b-bd09-bd99a549cfe7">

```===========================================================================```
4. `Incase of unpublish from specific version specific project`
<img width="1277" alt="Screenshot 2023-10-09 at 11 25 03 PM" src="https://github.com/ideadevice/calm-dsl/assets/52671728/d73d4807-3fac-4ed1-b28f-09d18c1de204">

```===========================================================================```
5. `Incase of unpublish from all versions`
<img width="1092" alt="Screenshot 2023-10-10 at 12 04 46 AM"
src="https://github.com/ideadevice/calm-dsl/assets/52671728/928a09a9-74ef-444b-9da8-aa4ceab0eb9c">

```===========================================================================```
6. `Incase of unpublish from specific version`
<img width="1257" alt="Screenshot 2023-10-09 at 11 25 36 PM" src="https://github.com/ideadevice/calm-dsl/assets/52671728/3754b817-9a12-4a0e-9356-09b9d80c65a3">

(cherry picked from commit 9ffa0b3f737f800fa6aaa21d0a5322a07659f47e)
  • Loading branch information
GullapalliAkhil authored and dwivediprab committed Mar 15, 2024
1 parent 1008e58 commit 188235f
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 38 deletions.
149 changes: 114 additions & 35 deletions calm/dsl/cli/marketplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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,
)

Expand Down
26 changes: 23 additions & 3 deletions calm/dsl/cli/marketplace_bp_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
)

0 comments on commit 188235f

Please sign in to comment.