Skip to content

Commit

Permalink
Updated configuration_item to use provided sys_class_name when gettin…
Browse files Browse the repository at this point in the history
…g existing records
  • Loading branch information
Mathijs de Ruiter committed Nov 12, 2024
1 parent d781b0e commit 74e3275
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
18 changes: 10 additions & 8 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 (@EUCTechTopis)
short_description: Manage ServiceNow configuration items
Expand Down Expand Up @@ -289,12 +290,10 @@
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")
configuration_item = table_client.get_record("cmdb_ci", query)
cmdb_table = module.params["sys_class_name"] or "cmdb_ci"
configuration_item = table_client.get_record(cmdb_table, query)

if configuration_item:
cmdb_table = configuration_item["sys_class_name"]
if cmdb_table != "cmdb_ci":
configuration_item = table_client.get_record(cmdb_table, query)

attachment_client.delete_attached_records(
cmdb_table,
Expand Down Expand Up @@ -324,6 +323,7 @@ def build_payload(module, table_client):


def ensure_present(module, table_client, attachment_client):
cmdb_table = module.params["sys_class_name"] or "cmdb_ci"
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")
Expand All @@ -333,10 +333,9 @@ def ensure_present(module, table_client, attachment_client):
)

if not query_sys_id:
configuration_item = table_client.get_record("cmdb_ci", query_name)
configuration_item = table_client.get_record(cmdb_table, query_name)
# 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"
new = mapper.to_ansible(
table_client.create_record(
cmdb_table, mapper.to_snow(payload), module.check_mode
Expand All @@ -360,11 +359,13 @@ 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_table, query_sys_id, must_exist=True)
)
# Check if provided name already exists
if query_name:
configuration_item = table_client.get_record("cmdb_ci", query_name)
configuration_item = table_client.get_record(
old["sys_class_name"], query_name
)
if configuration_item:
old2 = mapper.to_ansible(configuration_item)
if old["sys_id"] != old2["sys_id"]:
Expand All @@ -373,6 +374,7 @@ def ensure_present(module, table_client, attachment_client):
module.params["name"]
)
)

# Update existing record
cmdb_table = old["sys_class_name"]
# If necessary, fetch the record from the table for the extended CI class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- name: Create a base configuration item (check mode)
servicenow.itsm.configuration_item: &ci-create
name: "{{ 'configuration_item_' + lookup('password', '/dev/null chars=ascii_letters,digit length=8') | lower }}"
sys_class_name: cmdb_ci_computer
category: hardware
environment: development
install_status: on_order
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/plugins/modules/test_configuration_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,12 @@ def get_record_side_effect(cmdb_table, query, **kwargs):
return dict(
sys_id="01a9ec0d3790200044e0bfc8bcbe5dc3",
name="my_new_name",
sys_class_name="cmdb_ci",
)
return dict(
sys_id="different_sys_id",
name="my_new_name",
sys_class_name="cmdb_ci",
)

table_client.get_record.side_effect = get_record_side_effect
Expand Down

0 comments on commit 74e3275

Please sign in to comment.