-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rulebook_info.py module and fix event_streams option
Signed-off-by: Alina Buzachis <[email protected]>
- Loading branch information
1 parent
b111c4e
commit 1d070d1
Showing
5 changed files
with
213 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,5 @@ action_groups: | |
- event_stream_info | ||
- project | ||
- project_info | ||
- rulebook_info | ||
- user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#!/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" | ||
- name: List all rulebooks | ||
ansible.eda.rulebook_info: | ||
""" | ||
|
||
|
||
RETURN = """ | ||
activations: | ||
description: Information about rulebook activations. | ||
returned: always | ||
type: list | ||
elements: dict | ||
sample: [ | ||
{ | ||
"id": 1, | ||
"name": "Test activation", | ||
"description": "A test activation", | ||
"is_enabled": true, | ||
"status": "running", | ||
"extra_var": "", | ||
"decision_environment_id": 1, | ||
"project_id": 2, | ||
"rulebook_id": 1, | ||
"organization_id": 1, | ||
"restart_policy": "on-failure", | ||
"restart_count": 2, | ||
"rulebook_name": "Test rulebook", | ||
"current_job_id": "2", | ||
"rules_count": 2, | ||
"rules_fired_count": 2, | ||
"created_at": "2024-08-10T14:22:30.123Z", | ||
"modified_at": "2024-08-15T11:45:00.987Z", | ||
"status_message": "Activation is running successfully.", | ||
"awx_token_id": 1, | ||
"log_level": "info", | ||
"eda_credentials": [], | ||
"k8s_service_name": "", | ||
"event_streams": [], | ||
"swap_single_source": false | ||
} | ||
] | ||
""" | ||
|
||
|
||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters