From e3629ff5c0211c451db25a12612d969c8ca01b5d Mon Sep 17 00:00:00 2001 From: Gevorg Khachatryan Date: Wed, 25 Oct 2023 14:46:58 +0400 Subject: [PATCH] fiql string in filtering (#423) * fiql string in filtering * fix * Add Tests * example for filter string --------- Co-authored-by: Gevorg-Khachatryaan Co-authored-by: Alaa Bishtawi --- examples/vm_info.yml | 8 +++- plugins/doc_fragments/ntnx_info.py | 8 +++- plugins/module_utils/base_info_module.py | 9 ++++ plugins/module_utils/entity.py | 10 ++-- .../nutanix_vms_info/tasks/list_vms.yml | 48 +++++++++++++++++++ 5 files changed, 77 insertions(+), 6 deletions(-) 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 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..5adefc4ea 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.extend(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 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