From bbdaa4a870b60ec22097bea0c9d96892eb041d1f Mon Sep 17 00:00:00 2001 From: Cosmin Tupangiu Date: Tue, 23 Apr 2024 13:06:28 +0200 Subject: [PATCH] use query --- plugins/modules/service_catalog_info.py | 21 ++++++++++++------- .../service_catalog_info/tasks/main.yml | 20 ++++++++++++++++-- .../modules/test_service_catalog_info.py | 12 +++++++---- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/plugins/modules/service_catalog_info.py b/plugins/modules/service_catalog_info.py index 71364669..be853680 100644 --- a/plugins/modules/service_catalog_info.py +++ b/plugins/modules/service_catalog_info.py @@ -148,15 +148,19 @@ from ansible.module_utils.basic import AnsibleModule -def get_catalog_info(sc_client, catalog, with_categories=True, with_items=ItemContent.BRIEF): +def get_catalog_info(sc_client, catalog, with_categories, items_config): if with_categories: catalog.categories = sc_client.get_categories(catalog.sys_id) - if with_items == ItemContent.NONE: + if items_config["info"] == ItemContent.NONE: return catalog - items = sc_client.get_items(catalog.sys_id) - if with_items == ItemContent.BRIEF: + query = None + if items_config["query"]: + query = dict(sysparm_text=items_config["query"]) + + items = sc_client.get_items(catalog.sys_id, query) + if items_config["info"] == ItemContent.BRIEF: catalog.items = items return catalog @@ -166,7 +170,10 @@ def get_catalog_info(sc_client, catalog, with_categories=True, with_items=ItemCo def run(module, sc_client): - item_information = ItemContent.from_str(module.params["items_info"]) + items_config = dict( + info=ItemContent.from_str(module.params["items_info"]), + query=module.params["items_query"] + ) fetch_categories = module.params["categories"] @@ -175,14 +182,14 @@ def run(module, sc_client): sc_client, sc_client.get_catalog(module.params["sys_id"]), fetch_categories, - item_information + items_config ) return [catalog.to_ansible()] # fetch all catalogs catalogs = [] for catalog in sc_client.get_catalogs(): - catalog = get_catalog_info(sc_client, catalog, fetch_categories, item_information) + catalog = get_catalog_info(sc_client, catalog, fetch_categories, items_config) catalogs.append(catalog.to_ansible()) return catalogs diff --git a/tests/integration/targets/service_catalog_info/tasks/main.yml b/tests/integration/targets/service_catalog_info/tasks/main.yml index 7100625b..7ff3bea5 100644 --- a/tests/integration/targets/service_catalog_info/tasks/main.yml +++ b/tests/integration/targets/service_catalog_info/tasks/main.yml @@ -9,9 +9,10 @@ servicenow.itsm.service_catalog_info: register: catalogs + - debug: var=catalogs - ansible.builtin.assert: that: - - catalogs | length == 4 + - catalogs | length > 1 - name: Get one catalog only servicenow.itsm.service_catalog_info: @@ -25,7 +26,7 @@ - catalog.records[0].categories | length > 0 - catalog.records[0].sn_items | length == 0 - - name: Get one catalog with categoies and items + - name: Get one catalog with categories and items servicenow.itsm.service_catalog_info: categories: true items_info: brief @@ -39,6 +40,21 @@ - catalog.records[0].sn_items is defined - catalog.records[0].sn_items | length > 0 + - name: Get one catalog with item filtered -- should return none -- + servicenow.itsm.service_catalog_info: + categories: false + items_info: brief + items_query: some_not_existent_field + sys_id: "{{ catalogs.records[0].sys_id }}" + register: catalog + + - ansible.builtin.assert: + that: + - catalog.records | length == 1 + - catalog.records[0].categories | length == 0 + - catalog.records[0].sn_items is defined + - catalog.records[0].sn_items | length == 0 + - name: Get one catalog with categoies and items -- full info -- servicenow.itsm.service_catalog_info: categories: true diff --git a/tests/unit/plugins/modules/test_service_catalog_info.py b/tests/unit/plugins/modules/test_service_catalog_info.py index 1984ffaa..84a06508 100644 --- a/tests/unit/plugins/modules/test_service_catalog_info.py +++ b/tests/unit/plugins/modules/test_service_catalog_info.py @@ -58,7 +58,8 @@ def test_get_without_categories(self, create_module): host="https://my.host.name", username="user", password="pass" ), categories=False, - items_info="none" + items_info="none", + items_query=None ) ) @@ -83,7 +84,8 @@ def test_get_by_sys_id(self, create_module): ), sys_id="catalog_sys_id", categories=False, - items_info="none" + items_info="none", + items_query=None ) ) @@ -107,7 +109,8 @@ def test_get_with_categories(self, create_module): host="https://my.host.name", username="user", password="pass" ), categories=True, - items_info="none" + items_info="none", + items_query=None ) ) @@ -134,7 +137,8 @@ def test_get_with_categories_and_items(self, create_module): host="https://my.host.name", username="user", password="pass" ), categories=True, - items_info="brief" + items_info="brief", + items_query=None ) )