Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add return_fields parmeter to the configuration_item_info module #208

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.4.0
"""

EXAMPLES = r"""
Expand All @@ -65,6 +74,14 @@
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:
name: my-configuration-item
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
Akasurde marked this conversation as resolved.
Show resolved Hide resolved

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
Loading