From 49267c68f11ee6b673703d9910cbd46cccfa2caa Mon Sep 17 00:00:00 2001 From: Gevorg-Khachatryaan Date: Tue, 17 Oct 2023 13:27:25 +0400 Subject: [PATCH 1/2] timeout customization --- plugins/inventory/ntnx_prism_vm_inventory.py | 13 +++++++++++-- plugins/module_utils/base_module.py | 1 + plugins/module_utils/entity.py | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index 5273275a3..57d76e6c6 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -53,6 +53,12 @@ - Default length(number of records to retrieve) has been set to 500 default: {"offset": 0, "length": 500} type: dict + timeout: + description: + - timeout for polling of operation, after which module will error out + type: int + required: false + default: 60 validate_certs: description: - Set value to C(False) to skip validation for self signed certificates @@ -76,7 +82,7 @@ class Mock_Module: - def __init__(self, host, port, username, password, validate_certs=False): + def __init__(self, host, port, username, password, timeout=60, validate_certs=False): self.tmpdir = tempfile.gettempdir() self.params = { "nutanix_host": host, @@ -84,6 +90,7 @@ def __init__(self, host, port, username, password, validate_certs=False): "nutanix_username": username, "nutanix_password": password, "validate_certs": validate_certs, + "timeout": timeout, "load_params_without_defaults": False, } @@ -92,7 +99,7 @@ def jsonify(self, data): class InventoryModule(BaseInventoryPlugin, Constructable): - """Nutanix VM dynamic invetory module for ansible""" + """Nutanix VM dynamic inventory module for ansible""" NAME = "nutanix.ncp.ntnx_prism_vm_inventory" @@ -119,6 +126,7 @@ def parse(self, inventory, loader, path, cache=True): self.nutanix_port = self.get_option("nutanix_port") self.data = self.get_option("data") self.validate_certs = self.get_option("validate_certs") + self.timeout = self.get_option("timeout") # Determines if composed variables or groups using nonexistent variables is an error strict = self.get_option("strict") @@ -128,6 +136,7 @@ def parse(self, inventory, loader, path, cache=True): self.nutanix_username, self.nutanix_password, self.validate_certs, + self.timeout, ) vm = vms.VM(module) self.data["offset"] = self.data.get("offset", 0) diff --git a/plugins/module_utils/base_module.py b/plugins/module_utils/base_module.py index 3695b4947..18241ffac 100644 --- a/plugins/module_utils/base_module.py +++ b/plugins/module_utils/base_module.py @@ -33,6 +33,7 @@ class BaseModule(AnsibleModule): ), state=dict(type="str", choices=["present", "absent"], default="present"), wait=dict(type="bool", default=True), + timeout=dict(type="int", required=False), ) def __init__(self, **kwargs): diff --git a/plugins/module_utils/entity.py b/plugins/module_utils/entity.py index a1dc79461..1b264dba0 100644 --- a/plugins/module_utils/entity.py +++ b/plugins/module_utils/entity.py @@ -50,6 +50,9 @@ def create( url = self.base_url + "/{0}".format(endpoint) if endpoint else self.base_url if query: url = self._build_url_with_query(url, query) + + timeout = self.module.params.get("timeout", timeout) + return self._fetch_url( url, method=method, @@ -73,6 +76,9 @@ def read( url = url + "/{0}".format(endpoint) if query: url = self._build_url_with_query(url, query) + + timeout = self.module.params.get("timeout", timeout) + return self._fetch_url( url, method="GET", @@ -97,6 +103,9 @@ def update( url = url + "/{0}".format(endpoint) if query: url = self._build_url_with_query(url, query) + + timeout = self.module.params.get("timeout", timeout) + return self._fetch_url( url, method=method, @@ -144,6 +153,9 @@ def delete( url = url + "/{0}".format(endpoint) if query: url = self._build_url_with_query(url, query) + + timeout = self.module.params.get("timeout", timeout) + return self._fetch_url( url, method="DELETE", @@ -166,6 +178,8 @@ def list( if endpoint: url = url + "/{0}".format(endpoint) + timeout = self.module.params.get("timeout", timeout) + resp = self._fetch_url( url, method="POST", From da4ed1aed4e8da8485496509c135e85c7e70d32e Mon Sep 17 00:00:00 2001 From: Pradeepsingh Bhati Date: Wed, 18 Oct 2023 00:51:26 +0530 Subject: [PATCH 2/2] Make timeout as last attribute of MockModile constructor to avoid mixing up --- plugins/inventory/ntnx_prism_vm_inventory.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/inventory/ntnx_prism_vm_inventory.py b/plugins/inventory/ntnx_prism_vm_inventory.py index 57d76e6c6..aa7b9a55f 100644 --- a/plugins/inventory/ntnx_prism_vm_inventory.py +++ b/plugins/inventory/ntnx_prism_vm_inventory.py @@ -82,7 +82,7 @@ class Mock_Module: - def __init__(self, host, port, username, password, timeout=60, validate_certs=False): + def __init__(self, host, port, username, password, validate_certs=False, timeout=30): self.tmpdir = tempfile.gettempdir() self.params = { "nutanix_host": host, @@ -136,7 +136,7 @@ def parse(self, inventory, loader, path, cache=True): self.nutanix_username, self.nutanix_password, self.validate_certs, - self.timeout, + self.timeout ) vm = vms.VM(module) self.data["offset"] = self.data.get("offset", 0)