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

Add activation and activation_info modules #254

Merged
merged 1 commit into from
Aug 16, 2024
Merged
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
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ alinabuzachis
hdrs
testuser
testsecret
keygen
2 changes: 2 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
requires_ansible: ">=2.15.0" # AAP 2.4 or newer
action_groups:
eda:
- activation
- activation_info
- credential
- credential_info
- credential_type
Expand Down
366 changes: 366 additions & 0 deletions plugins/modules/activation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,366 @@
#!/usr/bin/python
# coding: utf-8 -*-

# 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: activation
author:
- "Nikhil Jain (@jainnikhil30)"
- "Alina Buzachis (@alinabuzachis)"
short_description: Manage rulebook activations in the EDA Controller
description:
- This module allows the user to create or delete rulebook activations in the EDA Controller.
options:
name:
description:
- The name of the rulebook activation.
type: str
required: true
description:
description:
- The description of the rulebook activation.
type: str
project_name:
description:
- The name of the project associated with the rulebook activation.
type: str
aliases:
- project
rulebook_name:
description:
- The name of the rulebook associated with the rulebook activation.
type: str
aliases:
- rulebook
extra_vars:
description:
- The extra variables for the rulebook activation.
type: str
restart_policy:
description:
- The restart policy for the rulebook activation.
default: "always"
choices: ["on-failure", "always", "never"]
type: str
enabled:
description:
- Whether the rulebook activation is enabled or not.
type: bool
default: true
decision_environment_name:
description:
- The name of the decision environment associated with the rulebook activation.
type: str
aliases:
- decision_environment
awx_token_name:
description:
- The token ID of the AWX controller.
type: str
aliases:
- awx_token
- token
organization_name:
description:
- The name of the organization.
type: str
aliases:
- organization
eda_credentials:
description:
- A list of IDs for EDA credentials used by the rulebook activation.
type: list
elements: str
aliases:
- credentials
k8s_service_name:
description:
- The name of the Kubernetes service associated with this rulebook activation.
type: str
webhooks:
description:
- A list of webhook IDs associated with the rulebook activation.
type: list
elements: str
swap_single_source:
description:
- Allow swapping of single sources in a rulebook without name match.
type: bool
default: true
event_streams:
description:
- A list of IDs representing the event streams that this rulebook activation listens to.
type: list
elements: int
log_level:
description:
- Allow setting the desired log level.
type: str
default: "debug"
choices: ["debug", "info", "error"]
state:
description:
- Desired state of the resource.
default: "present"
choices: ["present", "absent"]
type: str
extends_documentation_fragment:
- ansible.eda.eda_controller.auths
notes:
- Rulebook Activation API does not support PATCH method, due to this reason the module will
not perform any modification when an existing rulebook activation is found.
"""

EXAMPLES = """
- name: Create a rulebook activation
ansible.eda.activation:
name: "Example Rulebook Activation"
description: "Example Rulebook Activation description"
project_name: "Example Project"
rulebook_name: "hello_controller.yml"
decision_environment_name: "Example Decision Environment"
enabled: False
awx_token_name: "Example Token"

- name: Delete a rulebook activation
ansible.eda.activation:
name: "Example Rulebook Activation"
state: absent
"""


RETURN = """
id:
description: ID of the rulebook activation.
returned: when exists
type: int
sample: 37
"""


from ansible.module_utils.basic import AnsibleModule

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


def lookup(module, controller, endpoint, name):
result = None
try:
result = controller.resolve_name_to_id(endpoint, name)
except EDAError as e:
module.fail_json(msg=f"Failed to lookup resource: {e}")
return result

Check warning on line 163 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L158-L163

Added lines #L158 - L163 were not covered by tests


def create_params(module, controller):
activation_params = {}

Check warning on line 167 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L167

Added line #L167 was not covered by tests

# Get the project id
project_id = None

Check warning on line 170 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L170

Added line #L170 was not covered by tests
if module.params.get("project_name"):
project_id = lookup(

Check warning on line 172 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L172

Added line #L172 was not covered by tests
module, controller, "projects", module.params["project_name"]
)
if project_id is not None:
activation_params["project_id"] = project_id

Check warning on line 176 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L176

Added line #L176 was not covered by tests

# Get the rulebook id
rulebook = None
params = {}

Check warning on line 180 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L179-L180

Added lines #L179 - L180 were not covered by tests
if project_id is not None:
params = {"data": {"project_id": project_id}}

Check warning on line 182 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L182

Added line #L182 was not covered by tests
if module.params.get("rulebook_name"):
try:
rulebook = controller.get_one_or_many(

Check warning on line 185 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L184-L185

Added lines #L184 - L185 were not covered by tests
"rulebooks", name=module.params["rulebook_name"], **params
)
except EDAError as e:
module.fail_json(msg=f"Failed to lookup rulebook: {e}")

Check warning on line 189 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L188-L189

Added lines #L188 - L189 were not covered by tests
if rulebook is not None:
activation_params["rulebook_id"] = rulebook["id"]

Check warning on line 191 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L191

Added line #L191 was not covered by tests

# Get the decision environment id
decision_environment_id = None

Check warning on line 194 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L194

Added line #L194 was not covered by tests
if module.params.get("decision_environment_name"):
decision_environment_id = lookup(

Check warning on line 196 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L196

Added line #L196 was not covered by tests
module,
controller,
"decision-environments",
module.params["decision_environment_name"],
)
if decision_environment_id is not None:
activation_params["decision_environment_id"] = decision_environment_id

Check warning on line 203 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L203

Added line #L203 was not covered by tests

# Get the organization id
organization_id = None

Check warning on line 206 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L206

Added line #L206 was not covered by tests
if module.params.get("organization_name"):
organization_id = lookup(

Check warning on line 208 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L208

Added line #L208 was not covered by tests
module, controller, "organizations", module.params["organization_name"]
)
if organization_id is not None:
activation_params["organization_id"] = organization_id

Check warning on line 212 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L212

Added line #L212 was not covered by tests

if module.params.get("description"):
activation_params["description"] = module.params["description"]

Check warning on line 215 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L215

Added line #L215 was not covered by tests

if module.params.get("extra_vars"):
activation_params["extra_var"] = module.params["extra_vars"]

Check warning on line 218 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L218

Added line #L218 was not covered by tests

# Get the AWX token id
awx_token_id = None

Check warning on line 221 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L221

Added line #L221 was not covered by tests
if module.params.get("awx_token_name"):
awx_token_id = lookup(

Check warning on line 223 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L223

Added line #L223 was not covered by tests
module, controller, "/users/me/awx-tokens/", module.params["awx_token_name"]
)
if awx_token_id is not None:
activation_params["awx_token_id"] = awx_token_id

Check warning on line 227 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L227

Added line #L227 was not covered by tests

if module.params.get("restart_policy"):
activation_params["restart_policy"] = module.params["restart_policy"]

Check warning on line 230 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L230

Added line #L230 was not covered by tests

if module.params.get("enabled"):
activation_params["is_enabled"] = module.params["enabled"]

Check warning on line 233 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L233

Added line #L233 was not covered by tests

if module.params.get("event_streams"):
activation_params["event_streams"] = module.params["event_streams"]

Check warning on line 236 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L236

Added line #L236 was not covered by tests

# Get the eda credential ids
eda_credential_ids = None

Check warning on line 239 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L239

Added line #L239 was not covered by tests
if module.params.get("eda_credentials"):
eda_credential_ids = []

Check warning on line 241 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L241

Added line #L241 was not covered by tests
for item in module.params["eda_credentials"]:
cred_id = lookup(module, controller, "eda-credentials", item)

Check warning on line 243 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L243

Added line #L243 was not covered by tests
if cred_id is not None:
eda_credential_ids.append(cred_id)

Check warning on line 245 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L245

Added line #L245 was not covered by tests

if eda_credential_ids is not None:
activation_params["eda_credentials"] = eda_credential_ids

Check warning on line 248 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L248

Added line #L248 was not covered by tests

if module.params.get("k8s_service_name"):
activation_params["k8s_service_name"] = module.params["k8s_service_name"]

Check warning on line 251 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L251

Added line #L251 was not covered by tests

# Get the webhook ids
webhooks_ids = None

Check warning on line 254 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L254

Added line #L254 was not covered by tests
if module.params.get("webhooks"):
webhooks_ids = []

Check warning on line 256 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L256

Added line #L256 was not covered by tests
for item in module.params["webhooks"]:
webhook_id = lookup(module, controller, "webhooks", item)

Check warning on line 258 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L258

Added line #L258 was not covered by tests
if webhook_id is not None:
webhooks_ids.append(webhook_id)

Check warning on line 260 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L260

Added line #L260 was not covered by tests
if webhooks_ids is not None:
activation_params["webhooks"] = webhooks_ids

Check warning on line 262 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L262

Added line #L262 was not covered by tests

if module.params.get("log_level"):
activation_params["log_level"] = module.params["log_level"]

Check warning on line 265 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L265

Added line #L265 was not covered by tests

if module.params.get("swap_single_source"):
activation_params["swap_single_source"] = module.params["swap_single_source"]

Check warning on line 268 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L268

Added line #L268 was not covered by tests

return activation_params

Check warning on line 270 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L270

Added line #L270 was not covered by tests


def main():
argument_spec = dict(
name=dict(type="str", required=True),
description=dict(type="str"),
project_name=dict(type="str", aliases=["project"]),
rulebook_name=dict(type="str", aliases=["rulebook"]),
extra_vars=dict(type="str"),
restart_policy=dict(
type="str",
default="always",
choices=[
"on-failure",
"always",
"never",
],
),
enabled=dict(type="bool", default=True),
decision_environment_name=dict(type="str", aliases=["decision_environment"]),
awx_token_name=dict(type="str", aliases=["awx_token", "token"]),
organization_name=dict(type="str", aliases=["organization"]),
event_streams=dict(type="list", elements="int"),
eda_credentials=dict(type="list", elements="str", aliases=["credentials"]),
k8s_service_name=dict(type="str"),
webhooks=dict(type="list", elements="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"),
)

argument_spec.update(AUTH_ARGSPEC)

required_if = [
("state", "present", ("name", "rulebook_name", "decision_environment_name"))
]

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

client = Client(

Check warning on line 312 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L312

Added line #L312 was not covered by tests
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"),
)

name = module.params.get("name")
state = module.params.get("state")

Check warning on line 321 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L320-L321

Added lines #L320 - L321 were not covered by tests

controller = Controller(client, module)

Check warning on line 323 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L323

Added line #L323 was not covered by tests

# Attempt to find rulebook activation based on the provided name
try:
activation = controller.get_one_or_many("activations", name=name)
except EDAError as e:
module.fail_json(msg=f"Failed to get rulebook activation: {e}")

Check warning on line 329 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L326-L329

Added lines #L326 - L329 were not covered by tests

if state == "absent":
try:
result = controller.delete_if_needed(activation, endpoint="activations")
module.exit_json(**result)
except EDAError as e:
module.fail_json(msg=f"Failed to delete rulebook activation: {e}")

Check warning on line 336 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L332-L336

Added lines #L332 - L336 were not covered by tests

if activation:
module.exit_json(

Check warning on line 339 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L339

Added line #L339 was not covered by tests
msg=f"A rulebook activation with name: {name} already exists. "
"The module does not support modifying a rulebook activation.",
**{"changed": False, "id": activation["id"]},
)

# Activation Data that will be sent for create/update
activation_params = create_params(module, controller)
activation_params["name"] = (

Check warning on line 347 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L346-L347

Added lines #L346 - L347 were not covered by tests
controller.get_item_name(activation) if activation else name
)

# If the state was present and we can let the module build or update the
# existing activation, this will return on its own
try:
result = controller.create_or_update_if_needed(

Check warning on line 354 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L353-L354

Added lines #L353 - L354 were not covered by tests
activation,
activation_params,
endpoint="activations",
item_type="activation",
)
module.exit_json(**result)
except EDAError as e:
module.fail_json(msg=f"Failed to create/update rulebook activation: {e}")

Check warning on line 362 in plugins/modules/activation.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/activation.py#L360-L362

Added lines #L360 - L362 were not covered by tests


if __name__ == "__main__":
main()
Loading