Skip to content

Commit

Permalink
configuration_item_info: add return_fields parameter
Browse files Browse the repository at this point in the history
* Allow user to specify return fields for the specifed configuration item
  info

Signed-off-by: Toni Moreno Giménez <[email protected]>
Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Toni Moreno Giménez authored and Akasurde committed Feb 20, 2024
1 parent 54d35fe commit 36f0eca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- configuration_item_info - allow user to specify limited return fields for the specified configuration item (https://github.com/ansible-collections/servicenow.itsm/pull/208).
44 changes: 40 additions & 4 deletions plugins/modules/configuration_item_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
short_description: List ServiceNow configuration item
description:
- Retrieve information about ServiceNow configuration item.
- Retrieve information about the ServiceNow configuration items and also attachments related to this CI.
- For more information, refer to the ServiceNow configuration item management documentation at
U(https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/product/configuration-management/concept/c_ITILConfigurationManagement.html).
version_added: 1.0.0
Expand Down Expand Up @@ -53,6 +53,15 @@
- If this parameter is unset when a configuration item info is queried,
the default value C(cmdb_ci) will be used.
type: str
return_fields:
description:
- A list of fields to return.
- If defined you need to add "attachments" as a field to return if you wish also get related attachments data.
- If C(return_fields) is not defined, all fields and also attachments will be returned.
type: list
elements: str
required: false
version_added: 2.3.0
"""

EXAMPLES = r"""
Expand All @@ -64,6 +73,14 @@
servicenow.itsm.configuration_item_info:
sys_id: 01a9ec0d3790200044e0bfc8bcbe5dc3
register: result
- name: Retrieve a specific configuration item by sys_id with limited return fields
servicenow.itsm.configuration_item_info:
sys_id: 01a9ec0d3790200044e0bfc8bcbe5dc3
return_fields:
- name
- sys_id
register: result
- name: Retrieve a specific configuration item by name
servicenow.itsm.configuration_item_info:
Expand Down Expand Up @@ -256,12 +273,27 @@ def run(module, table_client, attachment_client):
module.params, "sys_id", "name", "sysparm_query", "sysparm_display_value"
)

# default yes, only disabled if not selected in return_fields
query_attachments = True

if "return_fields" in module.params and module.params["return_fields"] is not None:
query["sysparm_fields"] = ",".join(module.params["return_fields"])
if "attachments" not in module.params["return_fields"]:
query_attachments = False

if query_attachments:
return [
dict(
mapper.to_ansible(record),
attachments=attachment_client.list_records(
dict(table_name=cmdb_table, table_sys_id=record["sys_id"]),
),
)
for record in table_client.list_records(cmdb_table, query)
]
return [
dict(
mapper.to_ansible(record),
attachments=attachment_client.list_records(
dict(table_name=cmdb_table, table_sys_id=record["sys_id"]),
),
)
for record in table_client.list_records(cmdb_table, query)
]
Expand All @@ -285,6 +317,10 @@ def main():
sys_class_name=dict(
type="str",
),
return_fields=dict(
type="list",
elements="str",
),
),
mutually_exclusive=[("sys_id", "query", "name", "sysparm_query")],
)
Expand Down

0 comments on commit 36f0eca

Please sign in to comment.