From c7c1e434e5159eb7d3326053ba3c6a8948160f0c Mon Sep 17 00:00:00 2001 From: Gevorg-Khachatryaan Date: Wed, 14 Sep 2022 13:55:17 +0400 Subject: [PATCH 1/8] fix for inventory to get all entities --- plugins/inventory/ntnx_prism_vm_inventory.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index 43c0ba216..b167dc8bb 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -93,6 +93,7 @@ class InventoryModule(BaseInventoryPlugin): """Nutanix VM dynamic invetory module for ansible""" NAME = "nutanix.ncp.ntnx_prism_vm_inventory" + max_length = 500 def verify_file(self, path): """Verify inventory configuration file""" @@ -126,7 +127,20 @@ def parse(self, inventory, loader, path, cache=True): self.validate_certs, ) vm = vms.VM(module) - resp = vm.list(self.data) + if self.data["length"] > self.max_length: + resp = {"entities": []} + total_length = self.data["length"] + while True: + self.data["length"] = self.max_length + sub_resp = vm.list(self.data) + resp["entities"].append(sub_resp["entities"]) + total_length -= self.max_length + if total_length <= 0: + break + self.data["length"] = total_length if total_length < self.max_length else self.max_length + self.data["offset"] += self.max_length + else: + resp = vm.list(self.data) keys_to_strip_from_resp = [ "disk_list", "vnuma_config", From 61909203a8027dd83e4136bc323cf80b85bcc9ba Mon Sep 17 00:00:00 2001 From: Gevorg Khachatryan <95351366+Gevorg-Khachatryan-97@users.noreply.github.com> Date: Mon, 19 Sep 2022 12:17:15 +0400 Subject: [PATCH 2/8] Update plugins/inventory/ntnx_prism_vm_inventory.py Co-authored-by: Pradeepsingh Bhati --- plugins/inventory/ntnx_prism_vm_inventory.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index b167dc8bb..176ad6bee 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -127,6 +127,7 @@ def parse(self, inventory, loader, path, cache=True): self.validate_certs, ) vm = vms.VM(module) + self.data["offset"]=self.data.get("offset", 0) if self.data["length"] > self.max_length: resp = {"entities": []} total_length = self.data["length"] From 27a56e62fe7ec987948e28183a27ebefad34d2d2 Mon Sep 17 00:00:00 2001 From: Gevorg Khachatryan <95351366+Gevorg-Khachatryan-97@users.noreply.github.com> Date: Mon, 19 Sep 2022 12:17:41 +0400 Subject: [PATCH 3/8] Update plugins/inventory/ntnx_prism_vm_inventory.py Co-authored-by: Pradeepsingh Bhati --- plugins/inventory/ntnx_prism_vm_inventory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index 176ad6bee..5434c40ad 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -134,7 +134,7 @@ def parse(self, inventory, loader, path, cache=True): while True: self.data["length"] = self.max_length sub_resp = vm.list(self.data) - resp["entities"].append(sub_resp["entities"]) + resp["entities"].extend(sub_resp["entities"]) total_length -= self.max_length if total_length <= 0: break From 3ce920bebc41b5e972cba4e438ddc89ff438b31c Mon Sep 17 00:00:00 2001 From: Gevorg-Khachatryaan Date: Mon, 19 Sep 2022 12:29:24 +0400 Subject: [PATCH 4/8] fix for inventory to get all entities --- plugins/inventory/ntnx_prism_vm_inventory.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index 5434c40ad..3b078d42f 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -127,19 +127,20 @@ def parse(self, inventory, loader, path, cache=True): self.validate_certs, ) vm = vms.VM(module) - self.data["offset"]=self.data.get("offset", 0) + self.data["offset"] = self.data.get("offset", 0) if self.data["length"] > self.max_length: + spec = self.data.copy() resp = {"entities": []} total_length = self.data["length"] while True: - self.data["length"] = self.max_length - sub_resp = vm.list(self.data) + spec["length"] = self.max_length + sub_resp = vm.list(spec) resp["entities"].extend(sub_resp["entities"]) total_length -= self.max_length if total_length <= 0: break - self.data["length"] = total_length if total_length < self.max_length else self.max_length - self.data["offset"] += self.max_length + spec["length"] = total_length if total_length < self.max_length else self.max_length + spec["offset"] += self.max_length else: resp = vm.list(self.data) keys_to_strip_from_resp = [ From cd4a3420181bd5b3623a95c72e266c3c8591cd15 Mon Sep 17 00:00:00 2001 From: Gevorg-Khachatryaan Date: Mon, 19 Sep 2022 13:36:07 +0400 Subject: [PATCH 5/8] fix for inventory to get all entities --- plugins/inventory/ntnx_prism_vm_inventory.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index 3b078d42f..8812be67c 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -5,6 +5,8 @@ from __future__ import absolute_import, division, print_function +from copy import deepcopy + __metaclass__ = type DOCUMENTATION = r""" @@ -129,11 +131,11 @@ def parse(self, inventory, loader, path, cache=True): vm = vms.VM(module) self.data["offset"] = self.data.get("offset", 0) if self.data["length"] > self.max_length: - spec = self.data.copy() + spec = deepcopy(self.data) resp = {"entities": []} - total_length = self.data["length"] + total_length = spec["length"] + spec["length"] = self.max_length while True: - spec["length"] = self.max_length sub_resp = vm.list(spec) resp["entities"].extend(sub_resp["entities"]) total_length -= self.max_length From d6cdb34dc60f1d5440733b123475c0893374ad91 Mon Sep 17 00:00:00 2001 From: Gevorg-Khachatryaan Date: Tue, 20 Sep 2022 13:33:38 +0400 Subject: [PATCH 6/8] move fixes from inventory to vm's module --- plugins/inventory/ntnx_prism_vm_inventory.py | 18 ++---------- plugins/module_utils/prism/vms.py | 29 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index 8812be67c..bf96c8ec1 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -95,7 +95,6 @@ class InventoryModule(BaseInventoryPlugin): """Nutanix VM dynamic invetory module for ansible""" NAME = "nutanix.ncp.ntnx_prism_vm_inventory" - max_length = 500 def verify_file(self, path): """Verify inventory configuration file""" @@ -130,21 +129,7 @@ def parse(self, inventory, loader, path, cache=True): ) vm = vms.VM(module) self.data["offset"] = self.data.get("offset", 0) - if self.data["length"] > self.max_length: - spec = deepcopy(self.data) - resp = {"entities": []} - total_length = spec["length"] - spec["length"] = self.max_length - while True: - sub_resp = vm.list(spec) - resp["entities"].extend(sub_resp["entities"]) - total_length -= self.max_length - if total_length <= 0: - break - spec["length"] = total_length if total_length < self.max_length else self.max_length - spec["offset"] += self.max_length - else: - resp = vm.list(self.data) + resp = vm.list(self.data) keys_to_strip_from_resp = [ "disk_list", "vnuma_config", @@ -180,6 +165,7 @@ def parse(self, inventory, loader, path, cache=True): self.inventory.add_host(vm_name, group=cluster) self.inventory.set_variable(vm_name, "ansible_host", vm_ip) self.inventory.set_variable(vm_name, "uuid", vm_uuid) + self.inventory.set_variable(vm_name, "name", vm_name) # Add hostvars for key in keys_to_strip_from_resp: diff --git a/plugins/module_utils/prism/vms.py b/plugins/module_utils/prism/vms.py index 03fc167af..fd2463945 100644 --- a/plugins/module_utils/prism/vms.py +++ b/plugins/module_utils/prism/vms.py @@ -45,6 +45,35 @@ def __init__(self, module): "remove_categories": CategoriesMapping.build_remove_all_categories_spec, } + def list( + self, + data=None, + endpoint=None, + use_base_url=False, + raise_error=True, + no_response=False, + timeout=30, + max_length=500, + ): + + if data.get("length", 0) > max_length: + spec = deepcopy(data) + resp = {"entities": []} + total_length = spec["length"] + spec["length"] = max_length + while True: + sub_resp = super(VM, self).list(spec) + resp["entities"].extend(sub_resp["entities"]) + total_length -= max_length + if total_length <= 0: + break + spec["length"] = total_length if total_length < max_length else max_length + spec["offset"] += max_length + else: + resp = super(VM, self).list(data) + + return resp + @staticmethod def is_on(payload): return True if payload["spec"]["resources"]["power_state"] == "ON" else False From ad7db252998c3656f9290a831cb85a42172c8514 Mon Sep 17 00:00:00 2001 From: Pradeepsingh Bhati Date: Wed, 21 Sep 2022 17:53:05 +0530 Subject: [PATCH 7/8] Add metadata to info module when length > 500 --- plugins/inventory/ntnx_prism_vm_inventory.py | 1 - plugins/module_utils/prism/vms.py | 14 +++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index bf96c8ec1..498a995d7 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -5,7 +5,6 @@ from __future__ import absolute_import, division, print_function -from copy import deepcopy __metaclass__ = type diff --git a/plugins/module_utils/prism/vms.py b/plugins/module_utils/prism/vms.py index fd2463945..c2b2cbf20 100644 --- a/plugins/module_utils/prism/vms.py +++ b/plugins/module_utils/prism/vms.py @@ -55,23 +55,31 @@ def list( timeout=30, max_length=500, ): - if data.get("length", 0) > max_length: spec = deepcopy(data) resp = {"entities": []} + total_matches = None total_length = spec["length"] spec["length"] = max_length + spec["offset"] = spec.get("offset", 0) while True: sub_resp = super(VM, self).list(spec) resp["entities"].extend(sub_resp["entities"]) + total_matches = sub_resp["metadata"].get("total_matches") total_length -= max_length if total_length <= 0: break - spec["length"] = total_length if total_length < max_length else max_length + spec["length"] = ( + total_length if total_length < max_length else max_length + ) spec["offset"] += max_length + + resp["metadata"] = {} + resp["metadata"] = data + resp["metadata"]["total_matches"] = total_matches + resp["metadata"]["length"] = len(resp["entities"]) else: resp = super(VM, self).list(data) - return resp @staticmethod From b1643fb4dc5a294cea5962597b09c59ddd845af4 Mon Sep 17 00:00:00 2001 From: Pradeepsingh Bhati Date: Wed, 21 Sep 2022 17:57:37 +0530 Subject: [PATCH 8/8] Minor fix --- plugins/module_utils/prism/vms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/module_utils/prism/vms.py b/plugins/module_utils/prism/vms.py index c2b2cbf20..9a08af855 100644 --- a/plugins/module_utils/prism/vms.py +++ b/plugins/module_utils/prism/vms.py @@ -74,7 +74,6 @@ def list( ) spec["offset"] += max_length - resp["metadata"] = {} resp["metadata"] = data resp["metadata"]["total_matches"] = total_matches resp["metadata"]["length"] = len(resp["entities"])