From dce8cf62a8b9446c27ae0b2ee58980ec0bf0d6fb Mon Sep 17 00:00:00 2001 From: Pradeepsingh Bhati Date: Mon, 12 Sep 2022 17:54:42 +0530 Subject: [PATCH 1/2] Consider cluster as well for getting subnet uuid --- plugins/module_utils/prism/subnets.py | 18 +++++++++++++++++- plugins/module_utils/prism/vms.py | 22 +++++++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/plugins/module_utils/prism/subnets.py b/plugins/module_utils/prism/subnets.py index 3d8766cdf..1672a9235 100644 --- a/plugins/module_utils/prism/subnets.py +++ b/plugins/module_utils/prism/subnets.py @@ -154,7 +154,23 @@ def get_subnet_uuid(config, module): if "name" in config or "subnet_name" in config: subnet = Subnet(module) name = config.get("name") or config.get("subnet_name") - uuid = subnet.get_uuid(name) + uuid = "" + + # incase subnet of particular cluster is needed + if config.get("cluster_uuid"): + filter_spec = {"filter": "{0}=={1}".format("name", name)} + resp = subnet.list( + data=filter_spec + ) + entities = resp.get("entities") if resp else None + if entities: + for entity in entities: + if entity["status"]["cluster_reference"]["uuid"] == config["cluster_uuid"]: + uuid = entity["metadata"]["uuid"] + break + else: + uuid = subnet.get_uuid(name) + if not uuid: error = "Subnet {0} not found.".format(name) return None, error diff --git a/plugins/module_utils/prism/vms.py b/plugins/module_utils/prism/vms.py index 03fc167af..354be7374 100644 --- a/plugins/module_utils/prism/vms.py +++ b/plugins/module_utils/prism/vms.py @@ -10,13 +10,13 @@ from ansible.module_utils.basic import _load_params -from .clusters import Cluster +from .clusters import Cluster, get_cluster_uuid from .groups import get_entity_uuid from .images import get_image_uuid from .prism import Prism from .projects import Project from .spec.categories_mapping import CategoriesMapping -from .subnets import Subnet +from .subnets import get_subnet_uuid class VM(Prism): @@ -246,12 +246,20 @@ def _build_spec_networks(self, payload, networks): uuid = network["subnet"]["uuid"] elif network.get("subnet", {}).get("name"): - subnet = Subnet(self.module) name = network["subnet"]["name"] - uuid = subnet.get_uuid(name) - if not uuid: - error = "Subnet {0} not found.".format(name) - return None, error + + # consider cluster as well to get correct subnet + cluster_uuid, err = get_cluster_uuid(self.module.params.get("cluster"), self.module) + if err: + return None, err + + config = { + "name": name, + "cluster_uuid": cluster_uuid + } + uuid, err = get_subnet_uuid(config, self.module) + if err: + return None, err nic["subnet_reference"]["uuid"] = uuid From 2fec758bc8598f6a6dd70607efc31e67b8c53f0b Mon Sep 17 00:00:00 2001 From: Pradeepsingh Bhati Date: Mon, 12 Sep 2022 23:10:26 +0530 Subject: [PATCH 2/2] Fix incase of update --- plugins/module_utils/prism/vms.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/module_utils/prism/vms.py b/plugins/module_utils/prism/vms.py index 354be7374..ca28829c5 100644 --- a/plugins/module_utils/prism/vms.py +++ b/plugins/module_utils/prism/vms.py @@ -248,8 +248,14 @@ def _build_spec_networks(self, payload, networks): elif network.get("subnet", {}).get("name"): name = network["subnet"]["name"] - # consider cluster as well to get correct subnet - cluster_uuid, err = get_cluster_uuid(self.module.params.get("cluster"), self.module) + # consider cluster as well to get subnet from given cluster only + cluster_ref = None + if self.module.params.get("cluster"): + cluster_ref = self.module.params["cluster"] + else: + cluster_ref = payload["spec"]["cluster_reference"] + + cluster_uuid, err = get_cluster_uuid(cluster_ref, self.module) if err: return None, err