Skip to content

Commit

Permalink
Add rulebook_info.py module and fix event_streams option
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Sep 13, 2024
1 parent b111c4e commit 43480d0
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 14 deletions.
1 change: 1 addition & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ action_groups:
- event_stream_info
- project
- project_info
- rulebook_info
- user
37 changes: 25 additions & 12 deletions plugins/modules/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,20 @@
- This parameter is supported in AAP 2.5 and onwards.
If specified for AAP 2.4, value will be ignored.
type: list
elements: str
elements: dict
suboptions:
event_stream:
description:
- The name of the event stream.
type: str
source_name:
description:
- The name of the source.
type: str
rulebook_hash:
description:
- The hash value.
type: str
log_level:
description:
- Allow setting the desired log level.
Expand Down Expand Up @@ -253,16 +266,8 @@ def create_params(
if not is_aap_24 and module.params.get("k8s_service_name"):
activation_params["k8s_service_name"] = module.params["k8s_service_name"]

# Get the event stream ids
event_stream_ids = None
if not is_aap_24 and module.params.get("event-streams"):
event_stream_ids = []
for item in module.params["event_streams"]:
event_stream_id = lookup_resource_id(module, controller, "event-streams", item)
if event_stream_id is not None:
event_stream_ids.append(event_stream_id)
if event_stream_ids is not None:
activation_params["event_streams"] = event_stream_ids
if module.params["event_streams"]:
activation_params["event_streams"] = module.params["event_streams"]

if not is_aap_24 and module.params.get("log_level"):
activation_params["log_level"] = module.params["log_level"]
Expand Down Expand Up @@ -295,7 +300,15 @@ def main() -> None:
organization_name=dict(type="str", aliases=["organization"]),
eda_credentials=dict(type="list", elements="str", aliases=["credentials"]),
k8s_service_name=dict(type="str"),
event_streams=dict(type="list", elements="str"),
event_streams=dict(
type="list",
elements="dict",
options=dict(
event_stream=dict(type="str"),
rulebook_hash=dict(type="str"),
source_name=dict(type="str"),
),
),
swap_single_source=dict(type="bool", default=True),
log_level=dict(type="str", choices=["debug", "info", "error"], default="debug"),
state=dict(choices=["present", "absent"], default="present"),
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/activation_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def main() -> None:
name = module.params.get("name")
controller = Controller(client, module)

# Attempt to look up credential based on the provided name
# Attempt to look up rulebook activation based on the provided name
try:
result = controller.get_one_or_many("activations", name=name)
except EDAError as e:
Expand Down
145 changes: 145 additions & 0 deletions plugins/modules/rulebook_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type


DOCUMENTATION = """
---
module: rulebook_info
author:
- Alina Buzachis (@alinabuzachis)
short_description: List all rulebooks
description:
- List all rulebooks.
version_added: 2.1.0
options:
name:
description:
- The name of the rulebook.
- If only O(name) is set, all rulebooks with the specific name will be listed in different projects.
For a granular filtering, use O(name) in combination with O(project).
type: str
required: false
project_name:
description:
- The name of the project.
type: str
required: false
aliases:
- project
extends_documentation_fragment:
- ansible.eda.eda_controller.auths
"""


EXAMPLES = """
- name: Get information about a rulebook
ansible.eda.rulebook_info:
name: "Example Rulebook Activation"
projectname: "Example Project"
- name: List all rulebooks
ansible.eda.rulebook_info:
"""


RETURN = """
rulebooks:
description: Information about rulebooks.
returned: always
type: list
elements: dict
sample: [
{
"created_at": "2024-09-13T16:13:28.268659Z",
"description": "",
"id": 893,
"modified_at": "2024-09-13T16:13:28.268712Z",
"name": "demo_controller_rulebook.yml",
"organization_id": 1,
"project_id": 177,
"rulesets": "",
"sources": [
{
"name": "__SOURCE_1",
"rulebook_hash": "1e0f22025ab0a4e729fb68bcb9497412c3d9f477ce5a8cb91cc2ef15e35c4dc6",
"source_info": "ansible.eda.range:\n limit: 5\n"
}
]
}
]
"""


from ansible.module_utils.basic import AnsibleModule

from ..module_utils.arguments import AUTH_ARGSPEC
from ..module_utils.client import Client
from ..module_utils.common import lookup_resource_id
from ..module_utils.controller import Controller
from ..module_utils.errors import EDAError


def main() -> None:
argument_spec = dict(
name=dict(type="str", required=False),
project_name=dict(type="str", required=False, aliases=["project"]),
)

argument_spec.update(AUTH_ARGSPEC)

module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

client = Client(
host=module.params.get("controller_host"),
username=module.params.get("controller_username"),
password=module.params.get("controller_password"),
timeout=module.params.get("request_timeout"),
validate_certs=module.params.get("validate_certs"),
)

params = {}
result = []
name = module.params.get("name")

controller = Controller(client, module)

# Get the project id
project_id = None
if module.params.get("project_name"):
project_id = lookup_resource_id(
module, controller, "projects", module.params["project_name"]
)
if project_id is not None:
params = {"data": {"project_id": project_id}}

# Attempt to look up rulebook
try:
rulebooks = controller.get_one_or_many("rulebooks", name=name, **params)
except EDAError as e:
module.fail_json(msg=f"Failed to get rulebook: {e}")

if len(rulebooks) > 0:
for rulebook in rulebooks:
item = rulebook.copy()
try:
sources = controller.get_one_or_many(
f"rulebooks/{item['id']}/sources", name=item["name"]
)
item["sources"] = sources
except EDAError as e:
module.fail_json(msg=f"Failed to get rulebook source list: {e}")
result.append(item)

module.exit_json(rulebooks=result)


if __name__ == "__main__":
main()
34 changes: 33 additions & 1 deletion tests/integration/targets/activation/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
- project_creation is changed
- project_creation is success

- name: List all rulebooks
ansible.eda.rulebook_info:

- name: Create a new decision environment
ansible.eda.decision_environment:
name: "{{ decision_env_name }}"
Expand Down Expand Up @@ -221,6 +224,12 @@
organization_name: Default
register: _result

- name: Get information about a rulebook
ansible.eda.rulebook_info:
name: "{{ rulebook_name }}"
project_name: "{{ project_name }}"
register: _result_rulebook_info

- name: Create a rulebook activation
ansible.eda.activation:
name: "{{ activation_name }}"
Expand All @@ -232,14 +241,37 @@
awx_token_name: "{{ awx_token_name }}"
organization_name: Default
event_streams:
- "{{ event_stream_name }}"
- event_stream: "{{ event_stream_name }}"
source_name: "{{ _result_rulebook_info.rulebooks[0].sources[0].name }}"
rulebook_hash: "{{ _result_rulebook_info.rulebooks[0].sources[0].rulebook_hash }}"
register: _result

- name: Check rulebook activation creation
assert:
that:
- _result.changed

- name: Create a rulebook activation again
ansible.eda.activation:
name: "{{ activation_name }}"
description: "Example Activation description"
project_name: "{{ project_name }}"
rulebook_name: "{{ rulebook_name }}"
decision_environment_name: "{{ decision_env_name }}"
enabled: False
awx_token_name: "{{ awx_token_name }}"
organization_name: Default
event_streams:
- event_stream: "{{ event_stream_name }}"
source_name: "{{ _result_rulebook_info.rulebooks[0].sources[0].name }}"
rulebook_hash: "{{ _result_rulebook_info.rulebooks[0].sources[0].rulebook_hash }}"
register: _result

- name: Check rulebook activation creation did not happen
assert:
that:
- not _result.changed

- name: Delete project
ansible.eda.project:
name: "{{ project_name }}"
Expand Down

0 comments on commit 43480d0

Please sign in to comment.