Skip to content

Commit

Permalink
implement check_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tupyy committed Apr 12, 2024
1 parent 73be0f0 commit 9a1ea22
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 42 deletions.
27 changes: 18 additions & 9 deletions plugins/module_utils/cmdb_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ def to_json(self):
)

@classmethod
def from_values(cls, type_sys_id, target_sys_id):
def from_values(cls, type_sys_id, type_name, target_sys_id, target_name):
d = dict(
sys_id=None,
type=dict(value=type_sys_id, display_value=""),
target=dict(value=target_sys_id, display_value=""),
type=dict(value=type_sys_id, display_value=type_name),
target=dict(value=target_sys_id, display_value=target_name),
)
return cls(d)

Expand Down Expand Up @@ -110,7 +110,7 @@ def remove(self, direction, relation):
return
self.tainted.append((direction, "remove", relation))

def update(self, api_path, generic_client):
def update(self, api_path, generic_client, check_mode=False):
"""
Update updates the configuration item with the tainted relations.
Due to the behaviour of the caller, this method is either called for adding
Expand All @@ -119,8 +119,10 @@ def update(self, api_path, generic_client):
if len(self.tainted) == 0:
return

payload = self.__create_payload()
payload = self.__create_payload(check_mode)
if payload:
if check_mode:
return CmdbItemRelations(payload)
result = generic_client.create_record(
api_path, payload, check_mode=False, query=None
)
Expand All @@ -134,7 +136,8 @@ def update(self, api_path, generic_client):
for dir, action, rel in self.tainted:
if action == "add":
continue
generic_client.delete_record_by_sys_id(api_path, rel.sys_id)
if not check_mode:
generic_client.delete_record_by_sys_id(api_path, rel.sys_id)
for idx, r in enumerate(clone):
if r[1].sys_id == rel.sys_id:
clone.relations.pop(idx)
Expand All @@ -150,7 +153,7 @@ def to_json(self):
result[INBOUND_KEY].append(rel.to_json())
return result

def __create_payload(self):
def __create_payload(self, check_mode=False):
"""
Create payload for added relations
Return: payload if there're relation to be added. None otherwise
Expand All @@ -167,11 +170,17 @@ def __create_payload(self):
# so we add the key only if we have to (inbound or outbound).
if OUTBOUND_KEY not in payload:
payload[OUTBOUND_KEY] = []
payload.get(OUTBOUND_KEY).append(rel.to_payload())
if check_mode:
payload.get(OUTBOUND_KEY).append(rel.to_json())
else:
payload.get(OUTBOUND_KEY).append(rel.to_payload())
elif dir == INBOUND:
if INBOUND_KEY not in payload:
payload[INBOUND_KEY] = []
payload.get(INBOUND_KEY).append(rel.to_payload())
if check_mode:
payload.get(INBOUND_KEY).append(rel.to_json())
else:
payload.get(INBOUND_KEY).append(rel.to_payload())

if len(payload.get(INBOUND_KEY, [])) + len(payload.get(OUTBOUND_KEY, [])) > 0:
return payload
Expand Down
72 changes: 51 additions & 21 deletions plugins/modules/configuration_item_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,22 @@
- The class of the configuration item.
type: str
required: true
target_ids:
targets:
description:
- List of id the configuration items associated with the parent.
- List of configuration items to be associated with the parent.
type: list
elements: str
elements: dict
suboptions:
name:
description:
- Name of the configuration item
type: str
required: true
sys_id:
description:
- Sys_id of the configuration item
type: str
required: true
required: true
"""

Expand All @@ -74,17 +85,19 @@
state: present
parent_sys_id: "{{ parent_sys_id }}"
parent_classname: cmdb_ci_linux_server
target_ids:
- target1_id
targets:
- name: target1
sys_id: target1_id
- name: Remove relation
servicenow.itsm.configuration_item_relations:
direction: outbound
state: absent
parent_sys_id: "{{ parent_sys_id }}"
parent_classname: cmdb_ci_linux_server
target_ids:
- target1_id
targets:
- name: target1
sys_id: target1_id
- name: Update relation by adding one more target
servicenow.itsm.configuration_item_relations:
Expand All @@ -93,9 +106,9 @@
state: present
parent_sys_id: "{{ owner_sys_id }}"
parent_classname: cmdb_ci_linux_server
target_ids:
- target1_id
- new_target_id
targets:
- name: target1
sys_id: target1_id
"""

RETURN = r"""
Expand All @@ -116,11 +129,11 @@
"value": "015633570a0a0bc70029121512d46ede"
"""

from ansible.module_utils.basic import AnsibleModule
from ..module_utils import arguments, client, errors, generic
from ..module_utils import cmdb_relation as cmdb
from ..module_utils.configuration_item import PAYLOAD_FIELDS_MAPPING
from ..module_utils.utils import get_mapper
from ..module_utils.configuration_item import PAYLOAD_FIELDS_MAPPING
from ..module_utils import cmdb_relation as cmdb
from ..module_utils import arguments, client, errors, generic
from ansible.module_utils.basic import AnsibleModule

CMDB_INSTANCE_BASE_API_PATH = "api/now/cmdb/instance"
CMDB_RELATION_TYPE_API_PATH = "/api/now/table/cmdb_rel_type"
Expand Down Expand Up @@ -156,12 +169,17 @@ def ensure_present(module, generic_client):
relations = cmdb.CmdbItemRelations(parent_ci)

changed = False
for target_id in module.params["target_ids"]:
existing_relation = relations.get(module.params["direction"], target_id)
for target in module.params["targets"]:
existing_relation = relations.get(module.params["direction"], target["sys_id"])
if not existing_relation:
relations.add(
module.params["direction"],
cmdb.CmdbRelation.from_values(relation_type_sys_id, target_id),
cmdb.CmdbRelation.from_values(
relation_type_sys_id,
module.params["name"],
target["sys_id"],
target["name"],
),
)
changed = True

Expand All @@ -176,6 +194,7 @@ def ensure_present(module, generic_client):
]
),
generic_client,
module.check_mode,
)

return (
Expand Down Expand Up @@ -213,8 +232,8 @@ def ensure_absent(module, generic_client):
relations = cmdb.CmdbItemRelations(parent_ci)

changed = False
for target_id in module.params["target_ids"]:
existing_relation = relations.get(module.params["direction"], target_id)
for target in module.params["targets"]:
existing_relation = relations.get(module.params["direction"], target["sys_id"])
if existing_relation:
relations.remove(module.params["direction"], existing_relation)
changed = True
Expand All @@ -230,6 +249,7 @@ def ensure_absent(module, generic_client):
]
),
generic_client,
module.check_mode,
)
return (
True,
Expand Down Expand Up @@ -286,9 +306,19 @@ def main():
type="str",
required=True,
),
target_ids=dict(
targets=dict(
type="list",
elements="str",
elements="dict",
options=dict(
name=dict(
type="str",
required=True,
),
sys_id=dict(
type="str",
required=True,
),
),
required=True,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,38 @@
that:
- servers.results | length == 3

- name: Add relation between the first server and the rest
servicenow.itsm.configuration_item_relations:
- name: Add relation between the first server and the rest -- check mode --
servicenow.itsm.configuration_item_relations: &create-relations-data
parent_sys_id: "{{ servers.results[0].record.sys_id }}"
parent_classname: cmdb_ci_linux_server
state: present
name: Cools::Cooled By
target_ids:
- "{{ servers.results[1].record.sys_id }}"
- "{{ servers.results[2].record.sys_id }}"
targets:
- name: "{{ servers.results[1].record.name }}"
sys_id: "{{ servers.results[1].record.sys_id }}"
- name: "{{ servers.results[2].record.name }}"
sys_id: "{{ servers.results[2].record.sys_id }}"
register: relations
check_mode: true

- ansible.builtin.assert:
that:
- relations.record.outbound_relations | length == 2

- name: Make sure the relations don't exist
servicenow.itsm.configuration_item_relations_info:
sys_id: "{{ servers.results[0].record.sys_id }}"
classname: cmdb_ci_linux_server
register: relations

- ansible.builtin.assert:
that:
- relations.record.outbound_relations | length == 0

- name: Add relation between the first server and the rest
servicenow.itsm.configuration_item_relations: *create-relations-data
register: relations


- name: Retrieve relations for the first server
servicenow.itsm.configuration_item_relations_info:
Expand All @@ -46,19 +64,36 @@
- relations.record.outbound_relations | length == 2
- relations.record.inbound_relations | length == 0

- name: Remove relation
servicenow.itsm.configuration_item_relations:
- name: Remove relation -- check mode --
servicenow.itsm.configuration_item_relations: &delete-relations-data
parent_sys_id: "{{ servers.results[0].record.sys_id }}"
parent_classname: cmdb_ci_linux_server
state: absent
target_ids:
- "{{ servers.results[1].record.sys_id }}"
- "{{ servers.results[2].record.sys_id }}"
targets:
- name: "{{ servers.results[1].record.name }}"
sys_id: "{{ servers.results[1].record.sys_id }}"
- name: "{{ servers.results[2].record.name }}"
sys_id: "{{ servers.results[2].record.sys_id }}"
register: deleted_relation
check_mode: true

- ansible.builtin.assert:
that:
- deleted_relation.record.outbound_relations | length == 0

- name: Make sure the relations are still present
servicenow.itsm.configuration_item_relations_info:
sys_id: "{{ servers.results[0].record.sys_id }}"
classname: cmdb_ci_linux_server
register: relations

- ansible.builtin.assert:
that:
- relations.record.outbound_relations | length == 2
- relations.record.inbound_relations | length == 0

- name: Remove relation
servicenow.itsm.configuration_item_relations: *delete-relations-data

- name: Retrieve relations for the first server
servicenow.itsm.configuration_item_relations_info:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def test_add_relations(self, create_module, generic_client):
parent_classname="cmdb_ci_linux_server",
name="Cools:Cooled by",
direction="outbound",
target_ids=["target_id_1"],
targets=[
dict(sys_id="target_id_1", name="target")
],
)
)

Expand Down Expand Up @@ -128,7 +130,9 @@ def test_add_relations(self, create_module, generic_client):
parent_classname="cmdb_ci_linux_server",
name="Cools:Cooled by",
direction="outbound",
target_ids=["target_id_1"],
targets=[
dict(sys_id="target_id_1", name="target")
],
)
)

Expand Down

0 comments on commit 9a1ea22

Please sign in to comment.