Skip to content

Commit

Permalink
use query
Browse files Browse the repository at this point in the history
  • Loading branch information
tupyy committed Apr 23, 2024
1 parent 5388d7b commit bbdaa4a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
21 changes: 14 additions & 7 deletions plugins/modules/service_catalog_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"]

Expand All @@ -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
Expand Down
20 changes: 18 additions & 2 deletions tests/integration/targets/service_catalog_info/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/plugins/modules/test_service_catalog_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)

Expand All @@ -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
)
)

Expand All @@ -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
)
)

Expand All @@ -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
)
)

Expand Down

0 comments on commit bbdaa4a

Please sign in to comment.