Skip to content

Commit

Permalink
fiql string in filtering (#423)
Browse files Browse the repository at this point in the history
* fiql string in filtering

* fix

* Add Tests

* example for filter string

---------

Co-authored-by: Gevorg-Khachatryaan <Gevorg1050.1>
Co-authored-by: Alaa Bishtawi <[email protected]>
  • Loading branch information
Gevorg-Khachatryan-97 and alaa-bish authored Oct 25, 2023
1 parent 73e846b commit e3629ff
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
8 changes: 7 additions & 1 deletion examples/vm_info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions plugins/doc_fragments/ntnx_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
9 changes: 9 additions & 0 deletions plugins/module_utils/base_info_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 7 additions & 3 deletions plugins/module_utils/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions tests/integration/targets/nutanix_vms_info/tasks/list_vms.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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: []

0 comments on commit e3629ff

Please sign in to comment.