Skip to content

Commit

Permalink
Changed query logic and added sys_class_name to query
Browse files Browse the repository at this point in the history
  • Loading branch information
EUCTechTopics authored and Mathijs de Ruiter committed Sep 2, 2024
1 parent 0872e1e commit ddd57e3
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions plugins/modules/configuration_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Tadej Borovsak (@tadeboro)
- Matej Pevec (@mysteriouswolf)
- Polona Mihalič (@PolonaM)
- Mathijs de Ruiter (@EUCTechTopics)
short_description: Manage ServiceNow configuration items
Expand Down Expand Up @@ -288,7 +289,7 @@

def ensure_absent(module, table_client, attachment_client):
mapper = get_mapper(module, "configuration_item_mapping", PAYLOAD_FIELDS_MAPPING)
query = utils.filter_dict(module.params, "sys_id", "name")
query = utils.filter_dict(module.params, "sys_id", "name", "sys_class_name")
configuration_item = table_client.get_record("cmdb_ci", query)

if configuration_item:
Expand Down Expand Up @@ -325,15 +326,14 @@ def build_payload(module, table_client):

def ensure_present(module, table_client, attachment_client):
mapper = get_mapper(module, "configuration_item_mapping", PAYLOAD_FIELDS_MAPPING)
query_sys_id = utils.filter_dict(module.params, "sys_id")
query_name = utils.filter_dict(module.params, "name")
query = utils.filter_dict(module.params, "sys_id", "name", "sys_class_name")
payload = build_payload(module, table_client)
attachments = attachment.transform_metadata_list(
module.params["attachments"], module.sha256
)

if not query_sys_id:
configuration_item = table_client.get_record("cmdb_ci", query_name)
if not module.params["sys_id"]:
configuration_item = table_client.get_record("cmdb_ci", query)
# User did not specify existing CI, so we need to create a new one.
if not configuration_item:
cmdb_table = module.params["sys_class_name"] or "cmdb_ci"
Expand All @@ -360,11 +360,11 @@ def ensure_present(module, table_client, attachment_client):
else:
# Get existing record using provided sys_id
old = mapper.to_ansible(
table_client.get_record("cmdb_ci", query_sys_id, must_exist=True)
table_client.get_record("cmdb_ci", query, must_exist=True)
)
# Check if provided name already exists
if query_name:
configuration_item = table_client.get_record("cmdb_ci", query_name)
if module.params["name"]:
configuration_item = table_client.get_record("cmdb_ci", query)
if configuration_item:
old2 = mapper.to_ansible(configuration_item)
if old["sys_id"] != old2["sys_id"]:
Expand All @@ -379,7 +379,7 @@ def ensure_present(module, table_client, attachment_client):
if cmdb_table != "cmdb_ci":
old = mapper.to_ansible(
table_client.get_record(
cmdb_table, query_sys_id or query_name, must_exist=True
cmdb_table, query, must_exist=True
)
)

Expand Down

0 comments on commit ddd57e3

Please sign in to comment.