From d1c75d103861d3d4380fc1a98278bb8b1b4b0a2c Mon Sep 17 00:00:00 2001 From: Gevorg-Khachatryaan Date: Thu, 19 Oct 2023 13:54:46 +0400 Subject: [PATCH 1/4] fiql string in filtering --- plugins/doc_fragments/ntnx_info.py | 8 ++++++-- plugins/module_utils/base_info_module.py | 9 +++++++++ plugins/module_utils/entity.py | 10 +++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/plugins/doc_fragments/ntnx_info.py b/plugins/doc_fragments/ntnx_info.py index bc0be2293..387d503b5 100644 --- a/plugins/doc_fragments/ntnx_info.py +++ b/plugins/doc_fragments/ntnx_info.py @@ -31,10 +31,14 @@ class ModuleDocFragment(object): type: int filter: description: - - The filter in FIQL syntax used for the results + - The filter in key-value syntax used for the results type: dict custom_filter: description: - - The filter in FIQL syntax used for the results + - The filter in key-value syntax used for the results type: dict + filter_string: + description: + - The filter in FIQL syntax used for the results + type: str """ diff --git a/plugins/module_utils/base_info_module.py b/plugins/module_utils/base_info_module.py index 14c4f1849..508556a1c 100644 --- a/plugins/module_utils/base_info_module.py +++ b/plugins/module_utils/base_info_module.py @@ -15,12 +15,21 @@ class BaseInfoModule(BaseModule): length=dict(type="int"), filter=dict(type="dict"), custom_filter=dict(type="dict"), + filter_string=dict(type="str"), ) + info_args_mutually_exclusive = [ + ("filter", "filter_string"), + ] + def __init__(self, skip_info_args=False, **kwargs): self.argument_spec = deepcopy(BaseModule.argument_spec) self.argument_spec.pop("state") self.argument_spec.pop("wait") if not skip_info_args: self.argument_spec.update(self.info_argument_spec) + info_args_mutually_exclusive = deepcopy(self.info_args_mutually_exclusive) + if kwargs.get("mutually_exclusive"): + info_args_mutually_exclusive.append(kwargs["mutually_exclusive"]) + kwargs["mutually_exclusive"] = info_args_mutually_exclusive super(BaseInfoModule, self).__init__(**kwargs) diff --git a/plugins/module_utils/entity.py b/plugins/module_utils/entity.py index a1dc79461..2bbc97f54 100644 --- a/plugins/module_utils/entity.py +++ b/plugins/module_utils/entity.py @@ -283,10 +283,14 @@ def get_info_spec(self): else: spec.pop(key) - if params.get("filter", {}).get("name") and params.get("kind") == "vm": - spec["filter"]["vm_name"] = spec["filter"].pop("name") + if params.get("filter"): + if params.get("filter", {}).get("name") and params.get("kind") == "vm": + spec["filter"]["vm_name"] = spec["filter"].pop("name") - spec["filter"] = self._parse_filters(params.get("filter", {})) + spec["filter"] = self._parse_filters(params.get("filter", {})) + + elif params.get("filter_string"): + spec["filter"] = params["filter_string"] return spec, None From 9b2a7b880c92c81c11ca988c35147acef5c72b23 Mon Sep 17 00:00:00 2001 From: Gevorg-Khachatryaan Date: Thu, 19 Oct 2023 15:09:33 +0400 Subject: [PATCH 2/4] fix --- plugins/module_utils/base_info_module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/base_info_module.py b/plugins/module_utils/base_info_module.py index 508556a1c..5adefc4ea 100644 --- a/plugins/module_utils/base_info_module.py +++ b/plugins/module_utils/base_info_module.py @@ -30,6 +30,6 @@ def __init__(self, skip_info_args=False, **kwargs): self.argument_spec.update(self.info_argument_spec) info_args_mutually_exclusive = deepcopy(self.info_args_mutually_exclusive) if kwargs.get("mutually_exclusive"): - info_args_mutually_exclusive.append(kwargs["mutually_exclusive"]) + info_args_mutually_exclusive.extend(kwargs["mutually_exclusive"]) kwargs["mutually_exclusive"] = info_args_mutually_exclusive super(BaseInfoModule, self).__init__(**kwargs) From 38896d284646c3a8ef1194898c49062e79736eb6 Mon Sep 17 00:00:00 2001 From: Alaa Bishtawi Date: Thu, 19 Oct 2023 15:34:09 +0300 Subject: [PATCH 3/4] Add Tests --- .../nutanix_vms_info/tasks/list_vms.yml | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/integration/targets/nutanix_vms_info/tasks/list_vms.yml b/tests/integration/targets/nutanix_vms_info/tasks/list_vms.yml index 1b758596b..991d25733 100644 --- a/tests/integration/targets/nutanix_vms_info/tasks/list_vms.yml +++ b/tests/integration/targets/nutanix_vms_info/tasks/list_vms.yml @@ -1,3 +1,43 @@ +- set_fact: + todelete: [] + +- name: Creat anohter VM with same name + ntnx_vms: + name: "{{ vm.name }}" + cluster: + name: "{{ cluster.name }}" + state: "power_off" + register: output + ignore_errors: true + +- name: Creation Status + assert: + that: + - output.response is defined + - output.response.status.state == 'COMPLETE' + fail_msg: ' Unable to create VM with minimum requiremnts ' + success_msg: ' VM with minimum requiremnts created successfully ' + +- set_fact: + todelete: '{{ todelete + [ output["response"]["metadata"]["uuid"] ] }}' + when: output.response.status.state == 'COMPLETE' +################################################## +- name: List vms using filter_string + ntnx_vms_info: + filter_string: vm_name=={{vm.name}};power_state==off + register: result + ignore_errors: True + +- name: Listing Status + assert: + that: + - result.response is defined + - result.response.entities[0].metadata.uuid == '{{output["response"]["metadata"]["uuid"]}}' + - result.response.metadata.kind == "vm" + - result.response.metadata.total_matches == 1 + fail_msg: " Unable to list vms using filter_string" + success_msg: " VMs listed successfully using filter_string " +################################################## - name: List vms using name filter criteria ntnx_vms_info: filter: @@ -44,3 +84,11 @@ fail_msg: " Unable to list vms " success_msg: " VMs listed successfully " #################################################### +- name: Delete all Created VMs + ntnx_vms: + state: absent + vm_uuid: '{{ item }}' + register: result + loop: '{{ todelete }}' +- set_fact: + todelete: [] \ No newline at end of file From 0d54eadc67892c05da458c5686447291e66f86f2 Mon Sep 17 00:00:00 2001 From: Gevorg-Khachatryaan Date: Tue, 24 Oct 2023 14:42:58 +0400 Subject: [PATCH 4/4] example for filter string --- examples/vm_info.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/vm_info.yml b/examples/vm_info.yml index 53e1e01ea..53d2928cb 100644 --- a/examples/vm_info.yml +++ b/examples/vm_info.yml @@ -22,7 +22,13 @@ kind: vm register: result ignore_errors: True - + + - name: List vms using FIQL filter string + ntnx_vms_info: + filter_string: "vm_name=={{vm.name}};power_state==off" + register: result + ignore_errors: True + - name: List vms using length, offset and ascending vm_name sorting ntnx_vms_info: length: 10