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 6a5cb81
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
25 changes: 7 additions & 18 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,19 +359,9 @@ 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)
if configuration_item:
old2 = mapper.to_ansible(configuration_item)
if old["sys_id"] != old2["sys_id"]:
raise errors.ServiceNowError(
"Record with the name {0} already exists.".format(
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

0 comments on commit 6a5cb81

Please sign in to comment.