diff --git a/plugins/modules/configuration_item.py b/plugins/modules/configuration_item.py index a283142b..f3a3c09c 100644 --- a/plugins/modules/configuration_item.py +++ b/plugins/modules/configuration_item.py @@ -18,6 +18,7 @@ - Tadej Borovsak (@tadeboro) - Matej Pevec (@mysteriouswolf) - Polona Mihalič (@PolonaM) + - Mathijs de Ruiter (@EUCTechTopis) short_description: Manage ServiceNow configuration items @@ -289,13 +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, configuration_item["sys_id"], @@ -324,6 +322,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") @@ -333,10 +332,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 @@ -360,11 +358,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"]: @@ -373,6 +373,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 diff --git a/tests/integration/targets/itsm_configuration_item/tasks/main.yml b/tests/integration/targets/itsm_configuration_item/tasks/main.yml index 51c799e9..30a1af44 100644 --- a/tests/integration/targets/itsm_configuration_item/tasks/main.yml +++ b/tests/integration/targets/itsm_configuration_item/tasks/main.yml @@ -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 diff --git a/tests/unit/plugins/modules/test_configuration_item.py b/tests/unit/plugins/modules/test_configuration_item.py index a49e7273..fbb06f0a 100644 --- a/tests/unit/plugins/modules/test_configuration_item.py +++ b/tests/unit/plugins/modules/test_configuration_item.py @@ -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