Skip to content

Commit

Permalink
Calm DSL support for Calm v3.3.2 (#220)
Browse files Browse the repository at this point in the history
* Added correct info library task cli help command (#101)

* Set description in spec for library task (#102)

* Task/m fix calm 28448 runbook 332 changes (#107) (#108)

* Add logic to use version based rubook runner api

* Formating

* Squashed commit of the following:

commit 351b5c0
Author: abhijeetkaurav1st <abhijeet.kaurav@nutanix.com>
Date:   Thu Dec 16 14:14:30 2021 +0530

    Minor fixes for undefined attributes at profile model for version setup < 3.3.0.

commit 7ea5982
Author: Abhijeet Singh Kaurav <abhijeet.kaurav@nutanix.com>
Date:   Wed Dec 15 19:03:06 2021 +0530

    Added support for get_runbook_action and macro in ahv-disk-images (#206)

    * Allow macro in image names

    * Adding example that shows macor usage in vm-disk-image and vm-nic

    * Adding helper get_runbook_action for getting action out of imported runbook

    * Adding exsiting machine example and minor improvements

    * minor fixes in models

    * Fixing runbook examples

commit bc9dc6a
Author: Abhijeet Singh Kaurav <abhijeet.kaurav@nutanix.com>
Date:   Mon Dec 13 20:25:01 2021 +0530

    Use ASCENDING order for list_all query (#205)

    fixes #200

* Fix acps commands with projects filter incase of IAMv2 setup (#129)

* Add implementation to use projects_internal api for getting acp info instead of acp list api

* Add logic for limit and offset in get_acps_from_project method

* Add Minor comments and changes

* Remove not required imports

* formatting

(cherry picked from commit 07b89d1baca31339560569b37cf5ead0db198ce1)

* Add 3.3.2 version for calmversion

Co-authored-by: Nirbhay Bagmar <nirbhay.bagmar@nutanix.com>
Co-authored-by: Pradeepsingh Bhati <88029526+bhati-pradeep@users.noreply.github.com>
Co-authored-by: Pradeepsingh Bhati <pradeep.bhati@nutanix.com>
  • Loading branch information
4 people authored Mar 9, 2022
1 parent e465896 commit 0c12474
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CalmVersion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.0
3.3.2
12 changes: 11 additions & 1 deletion calm/dsl/api/runbook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
from distutils.version import LooseVersion as LV


from .resource import ResourceAPI
from .connection import REQUEST
Expand All @@ -14,6 +16,7 @@ def __init__(self, connection):
self.UPDATE_USING_NAMES = self.PREFIX + "/{}/update"
self.RUNBOOK_RUNLOGS_LIST = self.PREFIX + "/runlogs/list"
self.RUN = self.PREFIX + "/{}/run"
self.EXECUTE = self.PREFIX + "/{}/execute" # For calm versions >= 3.3.2
self.POLL_RUN = self.PREFIX + "/runlogs/{}"
self.PAUSE = self.PREFIX + "/runlogs/{}/pause"
self.PLAY = self.PREFIX + "/runlogs/{}/play"
Expand Down Expand Up @@ -223,8 +226,15 @@ def list_runbook_runlogs(self, params=None):
)

def run(self, uuid, payload):
from calm.dsl.store.version import Version

calm_version = Version.get_version("Calm")
runbook_run_api = self.RUN
if LV(calm_version) >= LV("3.3.2"):
runbook_run_api = self.EXECUTE

return self.connection._call(
self.RUN.format(uuid),
runbook_run_api.format(uuid),
verify=False,
request_json=payload,
method=REQUEST.METHOD.POST,
Expand Down
94 changes: 72 additions & 22 deletions calm/dsl/cli/acps.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,57 @@
LOG = get_logging_handle(__name__)


def get_acps_from_project(client, project_uuid, **kwargs):
"""This routine gets acps from project using project uuid"""

# get project details
projects_intermal_obj = get_resource_api("projects_internal", client.connection)
proj_info, err = projects_intermal_obj.read(project_uuid)
if err:
return None, err

proj_info = proj_info.json()

# construct acp info dict
acps = {}
acps["entities"] = []
role_uuid = kwargs.get("role_uuid", None)
acp_name = kwargs.get("acp_name", None)
limit = kwargs.get("limit", 20)
offset = kwargs.get("offset", 0)

terminate = False
for acp in proj_info["status"]["access_control_policy_list_status"]:

# role uuid filter
if (
role_uuid
and role_uuid
!= acp["access_control_policy_status"]["resources"]["role_reference"][
"uuid"
]
):
continue

# acp name filter
if acp_name and acp_name != acp["access_control_policy_status"]["name"]:
continue
elif acp_name:
terminate = True

(acps["entities"]).append(
{"status": acp["access_control_policy_status"], "metadata": acp["metadata"]}
)

if terminate:
break

acps["metadata"] = {"total_matches": len(acps["entities"])}

acps["entities"] = acps["entities"][offset : offset + limit]
return acps, None


def get_acps(name, project_name, filter_by, limit, offset, quiet, out):
"""Get the acps, optionally filtered by a string"""

Expand All @@ -38,15 +89,18 @@ def get_acps(name, project_name, filter_by, limit, offset, quiet, out):
filter_query = get_name_query([name])
if filter_by:
filter_query = filter_query + ";(" + filter_by + ")"
if project_uuid:
filter_query = filter_query + ";(project_reference=={})".format(project_uuid)
if filter_query.startswith(";"):
filter_query = filter_query[1:]

if filter_query:
params["filter"] = filter_query

res, err = client.acp.list(params=params)
if project_uuid:
res, err = get_acps_from_project(
client, project_uuid, limit=limit, offset=offset
)
else:
res, err = client.acp.list(params=params)
res = res.json()

if err:
ContextObj = get_context()
Expand All @@ -56,7 +110,6 @@ def get_acps(name, project_name, filter_by, limit, offset, quiet, out):
LOG.warning("Cannot fetch acps from {}".format(pc_ip))
return

res = res.json()
total_matches = res["metadata"]["total_matches"]
if total_matches > limit:
LOG.warning(
Expand All @@ -83,7 +136,6 @@ def get_acps(name, project_name, filter_by, limit, offset, quiet, out):
table = PrettyTable()
table.field_names = [
"NAME",
"STATE",
"REFERENCED_ROLE",
"REFERENCED_PROJECT",
"UUID",
Expand All @@ -99,7 +151,6 @@ def get_acps(name, project_name, filter_by, limit, offset, quiet, out):
table.add_row(
[
highlight_text(row["name"]),
highlight_text(row["state"]),
highlight_text(role),
highlight_text(project_name),
highlight_text(metadata["uuid"]),
Expand Down Expand Up @@ -185,19 +236,16 @@ def create_acp(role, project, acp_users, acp_groups, name):
role_cache_data = Cache.get_entity_data(entity_type=CACHE.ENTITY.ROLE, name=role)
role_uuid = role_cache_data["uuid"]

# Check if there is an existing acp with given (project-role) tuple
params = {
"length": 1000,
"filter": "role_uuid=={};project_reference=={}".format(role_uuid, project_uuid),
}
res, err = client.acp.list(params)
limit = 1000
res, err = get_acps_from_project(
client, project_uuid, role_uuid=role_uuid, limit=limit
)
if err:
return None, err

response = res.json()
entities = response.get("entities", None)

if entities:
if res["metadata"]["total_matches"] > 0:
LOG.error(
"ACP {} already exists for given role in project".format(
entities[0]["status"]["name"]
Expand Down Expand Up @@ -384,21 +432,23 @@ def describe_acp(acp_name, project_name, out):
LOG.error("Project '{}' not found".format(project_name))
sys.exit(-1)

params = {
"length": 1000,
"filter": "(name=={});(project_reference=={})".format(acp_name, project_uuid),
}
acp_map = client.acp.get_name_uuid_map(params)
limit = 1000
res, err = get_acps_from_project(
client, project_uuid, acp_name=acp_name, limit=limit
)

if err:
return None, err

acp_uuid = acp_map.get(acp_name, "")
if not acp_uuid:
if res["metadata"]["total_matches"] == 0:
LOG.error(
"No ACP found with name '{}' and project '{}'".format(
acp_name, project_name
)
)
sys.exit(-1)

acp_uuid = res["entities"][0]["metadata"]["uuid"]
LOG.info("Fetching acp {} details".format(acp_name))
res, err = client.acp.read(acp_uuid)
if err:
Expand Down
6 changes: 6 additions & 0 deletions calm/dsl/cli/library_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ def create_library_task_from_json(
else:
name = task_payload.get("spec").get("name")

if description:
task_payload["spec"]["description"] = description

return create_update_library_task(
client,
task_payload,
Expand All @@ -414,6 +417,9 @@ def create_library_task_from_dsl(
else:
name = task_payload.get("spec").get("name")

if description:
task_payload["spec"]["description"] = description

return create_update_library_task(
client,
task_payload,
Expand Down
2 changes: 1 addition & 1 deletion calm/dsl/cli/library_tasks_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _create_task(task_file, name, description, force):
(-f | --file) supports:\n
\t.py - Python DSL\n
\t.json - Full json payload download from Calm API (v3 #GET) or using `calm describe <task_name> -o json`\n
\t.json - Full json payload download from Calm API (v3 #GET) or using `calm describe library task <task_name> -o json `\n
Examples:\n
calm create library task --name=HTTPGetVM -f HTTPGetVM.py\n
Expand Down

0 comments on commit 0c12474

Please sign in to comment.