Skip to content

Commit

Permalink
Flat module arguments
Browse files Browse the repository at this point in the history
Due to complexity of having a dict with dict as options, we changed the
modules args by flatting them.
  • Loading branch information
tupyy committed Apr 22, 2024
1 parent 5ef4b97 commit 5ede7eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 123 deletions.
89 changes: 25 additions & 64 deletions plugins/modules/service_catalog_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,46 @@
description:
- If set to V(true), the categories will be fetched from ServiceNow.
type: bool
items:
default: False
items_info:
description:
- List of options for fetching service catalog items.
- If set the items for each catalog will be fetched.
type: dict
suboptions:
content:
description:
- Content type of the item.
- Set to V(full), if the whole item will be fetched.
type: str
choices: [full, brief]
required: true
query:
description:
- Query for the item content.
- For more information, please refer to
U(https://developer.servicenow.com/dev.do#!/reference/api/utah/rest/c_ServiceCatalogAPI#servicecat-GET-items)
type: str
- Set to V(full), if the whole item will be fetched.
type: str
choices: [full, brief, none]
default: none
items_query:
description:
- Query for the item content.
- For more information, please refer to
U(https://developer.servicenow.com/dev.do#!/reference/api/utah/rest/c_ServiceCatalogAPI#servicecat-GET-items)
type: str
"""

EXAMPLES = r"""
- name: Return all catalogs without categories but with items (brief information)
servicenow.itsm.service_catalog_info:
categories: false
items:
content: brief
items_info: brief
- name: Return service catalog without categories but with items (brief information)
servicenow.itsm.service_catalog_info:
sys_id: "{{ service_catalog.sys_id }}"
categories: false
items:
content: brief
items_info: full
- name: Return service catalog with categories and with items (full information)
servicenow.itsm.service_catalog_info:
sys_id: "{{ service_catalog.sys_id }}"
categories: true
items:
content: full
items_info: full
- name: Return service catalog with categories and with all items containing word "iPhone"
servicenow.itsm.service_catalog_info:
sys_id: "{{ service_catalog.sys_id }}"
categories: true
items:
content: full
query: iPhone
items_info: full
items_query: iPhone
"""

RETURN = r"""
Expand Down Expand Up @@ -174,38 +165,12 @@ def get_catalog_info(sc_client, catalog, with_categories=True, with_items=ItemCo
return catalog


def validate_params(params):
missing = []
if "items" in params:
if "content" not in params["items"]:
missing.append(
'Missing required subparameter "content" of "items" paramter')
elif not params["items"]["content"]:
missing.append(
'Missing value for required subparameter "content" of "items"')
if "query" in params["items"] and not params["items"]["query"]:
missing.append('Missing value for "query" subparameter of "items"')

if "sys_id" in params and not params["sys_id"]:
missing.append(
'Missing value for "sys_id"')

if missing:
raise errors.ServiceNowError(
"Missing required paramters: {0}". format(", ".join(missing))
)


def run(module, sc_client):
validate_params(module.params)

item_information = ItemContent.NONE
if "items" in module.params:
item_information = ItemContent.from_str(module.params["items"]["content"])
item_information = ItemContent.from_str(module.params["items_info"])

fetch_categories = module.params["categories"]

if "sys_id" in module.params:
if "sys_id" in module.params and module.params["sys_id"]:
catalog = get_catalog_info(
sc_client,
sc_client.get_catalog(module.params["sys_id"]),
Expand All @@ -231,18 +196,14 @@ def main():
),
categories=dict(
type="bool",
default=False,
),
items=dict(
type="dict",
options=dict(
content=dict(
type="str",
choices=["brief", "full"],
required=True
),
query=dict(type="str"),
)
items_info=dict(
type="str",
choices=["brief", "full", "none"],
default="none",
),
items_query=dict(type="str")
)

module = AnsibleModule(
Expand Down
63 changes: 4 additions & 59 deletions tests/unit/plugins/modules/test_service_catalog_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import pytest
from ansible_collections.servicenow.itsm.plugins.module_utils import (
errors,
service_catalog
)
from ansible_collections.servicenow.itsm.plugins.modules import (
Expand All @@ -23,61 +22,6 @@
)


class TestParamsValidation:
def test_validation_1(self):
params = dict(
instance=dict(
host="https://my.host.name", username="user", password="pass"
),
categories=False,
items=dict(
content="brief",
query=""
)
)

with pytest.raises(errors.ServiceNowError):
service_catalog_info.validate_params(params)

def test_validation_2(self):
params = dict(
instance=dict(
host="https://my.host.name", username="user", password="pass"
),
categories=False,
items=dict(
content=dict(),
)
)

with pytest.raises(errors.ServiceNowError):
service_catalog_info.validate_params(params)

def test_validation_3(self):
params = dict(
instance=dict(
host="https://my.host.name", username="user", password="pass"
),
categories=False,
items=dict()
)

with pytest.raises(errors.ServiceNowError):
service_catalog_info.validate_params(params)

def test_validation_4(self):
params = dict(
instance=dict(
host="https://my.host.name", username="user", password="pass"
),
sys_id="",
categories=False,
)

with pytest.raises(errors.ServiceNowError):
service_catalog_info.validate_params(params)


class TestModule:
class GenericClientMock:
def __init__(self, retured_data):
Expand Down Expand Up @@ -114,6 +58,7 @@ def test_get_without_categories(self, create_module):
host="https://my.host.name", username="user", password="pass"
),
categories=False,
items_info="none"
)
)

Expand All @@ -138,6 +83,7 @@ def test_get_by_sys_id(self, create_module):
),
sys_id="catalog_sys_id",
categories=False,
items_info="none"
)
)

Expand All @@ -161,6 +107,7 @@ def test_get_with_categories(self, create_module):
host="https://my.host.name", username="user", password="pass"
),
categories=True,
items_info="none"
)
)

Expand All @@ -187,9 +134,7 @@ def test_get_with_categories_and_items(self, create_module):
host="https://my.host.name", username="user", password="pass"
),
categories=True,
items=dict(
content="brief"
)
items_info="brief"
)
)

Expand Down

0 comments on commit 5ede7eb

Please sign in to comment.