Skip to content

Commit 74e3275

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

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

plugins/modules/configuration_item.py

Lines changed: 10 additions & 8 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,11 +359,13 @@ 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
)
365364
# Check if provided name already exists
366365
if query_name:
367-
configuration_item = table_client.get_record("cmdb_ci", query_name)
366+
configuration_item = table_client.get_record(
367+
old["sys_class_name"], query_name
368+
)
368369
if configuration_item:
369370
old2 = mapper.to_ansible(configuration_item)
370371
if old["sys_id"] != old2["sys_id"]:
@@ -373,6 +374,7 @@ def ensure_present(module, table_client, attachment_client):
373374
module.params["name"]
374375
)
375376
)
377+
376378
# Update existing record
377379
cmdb_table = old["sys_class_name"]
378380
# 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

tests/unit/plugins/modules/test_configuration_item.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,12 @@ def get_record_side_effect(cmdb_table, query, **kwargs):
743743
return dict(
744744
sys_id="01a9ec0d3790200044e0bfc8bcbe5dc3",
745745
name="my_new_name",
746+
sys_class_name="cmdb_ci",
746747
)
747748
return dict(
748749
sys_id="different_sys_id",
749750
name="my_new_name",
751+
sys_class_name="cmdb_ci",
750752
)
751753

752754
table_client.get_record.side_effect = get_record_side_effect

0 commit comments

Comments
 (0)