Skip to content

Commit

Permalink
Fix for issue 187 (#202)
Browse files Browse the repository at this point in the history
* Fixing project update and adding account addition through cli switches

* Adding test for accoun updation through cli switches

* Fixing project update using dsl file and adding test for it

* Added changes in readme
  • Loading branch information
abhijeetkaurav1st authored Nov 23, 2021
1 parent a4b8957 commit 909aba1
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ Use `calm get roles` to list all roles in PC. The below roles are relevant for C
- Create project on Calm Server: `calm create project --file <project_file_location> --name <project_name> --description <description>`.
- List projects: `calm get projects`. Get projects, optionally filtered by a string
- Describe project: `calm describe project <project_name>`. It will print summary of project.
- Update project using dsl file: `calm update project <project_name> --file <project_file_location>`.
- Update project using cli switches: `calm update project <project_name> --add_user/--remove_user <user_name> --add_group/--remove_group <group_name>`.
- Update project using dsl file: `calm update project <project_name> --file <project_file_location>`. Environments will not be updated as part of this operation.
- Update project using cli switches: `calm update project <project_name> --add_user/--remove_user <user_name> --add_group/--remove_group <group_name> --add_account/--remove_account <account_name>`.
- Delete project: `calm delete project <project_name>`.

### Environments
Expand Down
9 changes: 9 additions & 0 deletions calm/dsl/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def __init__(self, connection):
self.CALM_PROJECTS_PREFIX = ResourceAPI.ROOT + "/calm_projects"
self.CALM_PROJECTS_ITEM = self.CALM_PROJECTS_PREFIX + "/{}"
self.CALM_PROJECTS_PENDING_TASKS = self.CALM_PROJECTS_ITEM + "/pending_tasks/{}"
self.CALM_PROJECTS_USAGE = self.CALM_PROJECTS_ITEM + "/usage"

def create(self, payload):

Expand All @@ -31,6 +32,14 @@ def create(self, payload):

return super().create(payload)

def usage(self, uuid, payload):
return self.connection._call(
self.CALM_PROJECTS_USAGE.format(uuid),
verify=False,
request_json=payload,
method=REQUEST.METHOD.POST,
)

def delete(self, uuid):
return self.connection._call(
self.CALM_PROJECTS_ITEM.format(uuid),
Expand Down
31 changes: 21 additions & 10 deletions calm/dsl/cli/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ def create_environment_from_dsl_class(env_cls, env_name="", metadata=dict()):
return create_environment(env_payload)


def update_project_envs(project_name, env_uuids=[]):
def update_project_envs(project_name, remove_env_uuids=[], add_env_uuids=[]):
"""
Update project with the environment reference list if not present
Args:
project_name(str): Name of project
env_uuids(list): list of environment uuids
remove_env_uuids(list): list of env uuids to be removed from project
add_env_uuids(list): list of env uuid to be added in project
Returns: None
"""

if not env_uuids:
if not (remove_env_uuids or add_env_uuids):
return

project_payload = get_project(project_name)
Expand All @@ -170,11 +170,16 @@ def update_project_envs(project_name, env_uuids=[]):
env_list = project_payload["spec"]["resources"].get(
"environment_reference_list", []
)
for _eu in env_uuids:
for _eu in add_env_uuids:
env_list.append({"kind": "environment", "uuid": _eu})

final_env_list = []
for _edata in env_list:
if _edata["uuid"] not in remove_env_uuids:
final_env_list.append(_edata)

project_payload["spec"]["resources"]["environment_reference_list"] = final_env_list
project_uuid = project_payload["metadata"]["uuid"]
project_payload["spec"]["resources"]["environment_reference_list"] = env_list

# TODO remove this infunction imports
from .projects import update_project
Expand Down Expand Up @@ -229,12 +234,13 @@ def create_environment_from_dsl_file(env_file, env_name, project_name):
ContextObj.reset_configuration()

LOG.info("Updating project for environment configuration")
update_project_envs(project_name, [env_std_out.get("uuid")])
update_project_envs(project_name, add_env_uuids=[env_std_out.get("uuid")])
LOG.info("Project updated successfully")

click.echo(json.dumps(env_std_out, indent=4, separators=(",", ": ")))

LOG.info("Updating environments cache ...")
LOG.info("Updating projects and environments cache ...")
Cache.sync_table(cache_type=CACHE.ENTITY.PROJECT)
Cache.sync_table(cache_type=CACHE.ENTITY.ENVIRONMENT)
LOG.info("[Done]")

Expand Down Expand Up @@ -300,7 +306,8 @@ def update_environment_from_dsl_file(env_name, env_file, project_name):
}
click.echo(json.dumps(stdout_dict, indent=4, separators=(",", ": ")))

LOG.info("Updating environments cache ...")
LOG.info("Updating projects and environments cache ...")
Cache.sync_table(cache_type=CACHE.ENTITY.PROJECT)
Cache.sync_table(cache_type=CACHE.ENTITY.ENVIRONMENT)
LOG.info("[Done]")

Expand Down Expand Up @@ -483,6 +490,10 @@ def delete_environment(environment_name, project_name):
raise Exception("[{}] - {}".format(err["code"], err["error"]))
LOG.info("Environment {} deleted".format(environment_name))

LOG.info("Updating environments cache ...")
LOG.info("Updating project for environment configuration")
update_project_envs(project_name, remove_env_uuids=[environment_id])

LOG.info("Updating environments and projects cache ...")
Cache.sync_table(cache_type=CACHE.ENTITY.PROJECT)
Cache.sync_table(cache_type=CACHE.ENTITY.ENVIRONMENT)
LOG.info("[Done]")
22 changes: 22 additions & 0 deletions calm/dsl/cli/project_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ def _describe_project(project_name, out):
multiple=True,
default=[],
)
@click.option(
"--add_account",
"-aa",
"add_account_list",
help="name of account to be added",
multiple=True,
default=[],
)
@click.option(
"--remove_account",
"-ra",
"remove_account_list",
help="name of account to be removed",
multiple=True,
default=[],
)
@click.option(
"--remove_user",
"-ru",
Expand All @@ -159,6 +175,8 @@ def _update_project(
project_file,
add_user_list,
add_group_list,
add_account_list,
remove_account_list,
remove_user_list,
remove_group_list,
):
Expand All @@ -177,6 +195,8 @@ def _update_project(
project_file
or add_user_list
or add_group_list
or add_account_list
or remove_account_list
or remove_user_list
or remove_group_list
):
Expand All @@ -201,4 +221,6 @@ def _update_project(
add_group_list=add_group_list,
remove_user_list=remove_user_list,
remove_group_list=remove_group_list,
add_account_list=add_account_list,
remove_account_list=remove_account_list,
)
Loading

0 comments on commit 909aba1

Please sign in to comment.