Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated configuration_item to use provided sys_class_name #413

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 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,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"],
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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"]:
Expand All @@ -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
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
Loading