Skip to content

Commit 6a5cb81

Browse files
author
Mathijs de Ruiter
committed
Updated configuration_item to use provided sys_class_name when getting existing records
1 parent d781b0e commit 6a5cb81

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

plugins/modules/configuration_item.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Tadej Borovsak (@tadeboro)
1919
- Matej Pevec (@mysteriouswolf)
2020
- Polona Mihalič (@PolonaM)
21+
- Mathijs de Ruiter (@EUCTechTopis)
2122
2223
short_description: Manage ServiceNow configuration items
2324
@@ -289,12 +290,10 @@
289290
def ensure_absent(module, table_client, attachment_client):
290291
mapper = get_mapper(module, "configuration_item_mapping", PAYLOAD_FIELDS_MAPPING)
291292
query = utils.filter_dict(module.params, "sys_id", "name")
292-
configuration_item = table_client.get_record("cmdb_ci", query)
293+
cmdb_table = module.params["sys_class_name"] or "cmdb_ci"
294+
configuration_item = table_client.get_record(cmdb_table, query)
293295

294296
if configuration_item:
295-
cmdb_table = configuration_item["sys_class_name"]
296-
if cmdb_table != "cmdb_ci":
297-
configuration_item = table_client.get_record(cmdb_table, query)
298297

299298
attachment_client.delete_attached_records(
300299
cmdb_table,
@@ -324,6 +323,7 @@ def build_payload(module, table_client):
324323

325324

326325
def ensure_present(module, table_client, attachment_client):
326+
cmdb_table = module.params["sys_class_name"] or "cmdb_ci"
327327
mapper = get_mapper(module, "configuration_item_mapping", PAYLOAD_FIELDS_MAPPING)
328328
query_sys_id = utils.filter_dict(module.params, "sys_id")
329329
query_name = utils.filter_dict(module.params, "name")
@@ -333,10 +333,9 @@ def ensure_present(module, table_client, attachment_client):
333333
)
334334

335335
if not query_sys_id:
336-
configuration_item = table_client.get_record("cmdb_ci", query_name)
336+
configuration_item = table_client.get_record(cmdb_table, query_name)
337337
# User did not specify existing CI, so we need to create a new one.
338338
if not configuration_item:
339-
cmdb_table = module.params["sys_class_name"] or "cmdb_ci"
340339
new = mapper.to_ansible(
341340
table_client.create_record(
342341
cmdb_table, mapper.to_snow(payload), module.check_mode
@@ -360,19 +359,9 @@ def ensure_present(module, table_client, attachment_client):
360359
else:
361360
# Get existing record using provided sys_id
362361
old = mapper.to_ansible(
363-
table_client.get_record("cmdb_ci", query_sys_id, must_exist=True)
362+
table_client.get_record(cmdb_table, query_sys_id, must_exist=True)
364363
)
365-
# Check if provided name already exists
366-
if query_name:
367-
configuration_item = table_client.get_record("cmdb_ci", query_name)
368-
if configuration_item:
369-
old2 = mapper.to_ansible(configuration_item)
370-
if old["sys_id"] != old2["sys_id"]:
371-
raise errors.ServiceNowError(
372-
"Record with the name {0} already exists.".format(
373-
module.params["name"]
374-
)
375-
)
364+
376365
# Update existing record
377366
cmdb_table = old["sys_class_name"]
378367
# If necessary, fetch the record from the table for the extended CI class

tests/integration/targets/itsm_configuration_item/tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- name: Create a base configuration item (check mode)
99
servicenow.itsm.configuration_item: &ci-create
1010
name: "{{ 'configuration_item_' + lookup('password', '/dev/null chars=ascii_letters,digit length=8') | lower }}"
11+
sys_class_name: cmdb_ci_computer
1112
category: hardware
1213
environment: development
1314
install_status: on_order

0 commit comments

Comments
 (0)