-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decision environment module (#248)
* Added module to manage decision environment * Added module to gather information about decision environment * tests Signed-off-by: Abhijeet Kasurde <[email protected]>
- Loading branch information
Showing
8 changed files
with
638 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,195 @@ | ||
#!/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: decision_environment | ||
author: | ||
- Nikhil Jain (@jainnikhil30) | ||
- Abhijeet Kasurde (@akasurde) | ||
short_description: Create, update or delete decision environment in EDA Controller | ||
description: | ||
- This module allows user to create, update or delete decision environment in a EDA controller. | ||
version_added: '2.0.0' | ||
options: | ||
name: | ||
description: | ||
- The name of the decision environment. | ||
type: str | ||
required: true | ||
new_name: | ||
description: | ||
- Setting this option will change the existing name. | ||
type: str | ||
description: | ||
description: | ||
- The description of the decision environment. | ||
type: str | ||
image_url: | ||
description: | ||
- Image URL of the decision environment. | ||
type: str | ||
credential: | ||
description: | ||
- Name of the credential to associate with the decision environment. | ||
type: str | ||
state: | ||
description: | ||
- Desired state of the resource. | ||
default: "present" | ||
choices: ["present", "absent"] | ||
type: str | ||
extends_documentation_fragment: | ||
- ansible.eda.eda_controller.auths | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: Create EDA Decision Environment | ||
ansible.eda.decision_environment: | ||
controller_host: https://my_eda_host/ | ||
controller_username: admin | ||
controller_password: MySuperSecretPassw0rd | ||
name: "Example Decision Environment" | ||
description: "Example Decision Environment description" | ||
image_url: "quay.io/test" | ||
credential: "Example Credential" | ||
state: present | ||
- name: Update the name of the Decision Environment | ||
ansible.eda.decision_environment: | ||
controller_host: https://my_eda_host/ | ||
controller_username: admin | ||
controller_password: MySuperSecretPassw0rd | ||
name: "Example Decision Environment" | ||
new_name: "Latest Example Decision Environment" | ||
state: present | ||
- name: Delete the the Decision Environment | ||
ansible.eda.decision_environment: | ||
controller_host: https://my_eda_host/ | ||
controller_username: admin | ||
controller_password: MySuperSecretPassw0rd | ||
name: "Example Decision Environment" | ||
state: absent | ||
""" | ||
|
||
RETURN = """ | ||
id: | ||
description: ID of the decision environment | ||
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 main(): | ||
argument_spec = dict( | ||
name=dict(required=True), | ||
new_name=dict(), | ||
description=dict(), | ||
image_url=dict(), | ||
credential=dict(), | ||
state=dict(choices=["present", "absent"], default="present"), | ||
) | ||
|
||
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"), | ||
) | ||
|
||
decision_environment_endpoint = "decision-environments" | ||
controller = Controller(client, module) | ||
|
||
decision_environment_name = module.params.get("name") | ||
new_name = module.params.get("new_name") | ||
description = module.params.get("description") | ||
image_url = module.params.get("image_url") | ||
state = module.params.get("state") | ||
credential = module.params.get("credential") | ||
ret = {} | ||
|
||
try: | ||
decision_environment_type = controller.get_one_or_many( | ||
decision_environment_endpoint, name=decision_environment_name | ||
) | ||
except EDAError as eda_err: | ||
module.fail_json(msg=str(eda_err)) | ||
|
||
if state == "absent": | ||
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this | ||
try: | ||
ret = controller.delete_if_needed( | ||
decision_environment_type, endpoint=decision_environment_endpoint | ||
) | ||
except EDAError as eda_err: | ||
module.fail_json(msg=str(eda_err)) | ||
module.exit_json(**ret) | ||
|
||
decision_environment_type_params = {} | ||
if description: | ||
decision_environment_type_params["description"] = description | ||
if image_url: | ||
decision_environment_type_params["image_url"] = image_url | ||
|
||
credential_type = None | ||
if credential: | ||
try: | ||
credential_type = controller.get_one_or_many( | ||
"eda-credentials", name=credential | ||
) | ||
except EDAError as eda_err: | ||
module.fail_json(msg=str(eda_err)) | ||
|
||
if credential_type is not None: | ||
# this is resolved earlier, so save an API call and don't do it again | ||
# in the loop above | ||
decision_environment_type_params["credential"] = credential_type["id"] | ||
|
||
if new_name: | ||
decision_environment_type_params["name"] = new_name | ||
elif decision_environment_type: | ||
decision_environment_type_params["name"] = controller.get_item_name( | ||
decision_environment_type | ||
) | ||
else: | ||
decision_environment_type_params["name"] = decision_environment_name | ||
|
||
# If the state was present and we can let the module build or update | ||
# the existing decision environment type, | ||
# this will return on its own | ||
try: | ||
ret = controller.create_or_update_if_needed( | ||
decision_environment_type, | ||
decision_environment_type_params, | ||
endpoint=decision_environment_endpoint, | ||
item_type="decision environment type", | ||
) | ||
except EDAError as eda_err: | ||
module.fail_json(msg=str(eda_err)) | ||
|
||
module.exit_json(**ret) | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#!/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: decision_environment_info | ||
author: | ||
- Abhijeet Kasurde (@akasurde) | ||
short_description: List a decision environment in EDA Controller | ||
description: | ||
- This module allows user to list a decision environment in a EDA controller. | ||
version_added: '2.0.0' | ||
options: | ||
name: | ||
description: | ||
- The name of the decision environment. | ||
type: str | ||
extends_documentation_fragment: | ||
- ansible.eda.eda_controller.auths | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: List all EDA Decision Environments | ||
ansible.eda.decision_environment_info: | ||
controller_host: https://my_eda_host/ | ||
controller_username: admin | ||
controller_password: MySuperSecretPassw0rd | ||
- name: List a particular EDA Decision Environments | ||
ansible.eda.decision_environment_info: | ||
controller_host: https://my_eda_host/ | ||
controller_username: admin | ||
controller_password: MySuperSecretPassw0rd | ||
name: Example | ||
""" | ||
|
||
RETURN = """ | ||
decision_environments: | ||
description: List of dict containing information about decision environments | ||
returned: when exists | ||
type: list | ||
sample: [ | ||
{ | ||
"created_at": "2024-08-15T21:12:52.218969Z", | ||
"description": "Example decision environment description", | ||
"eda_credential_id": null, | ||
"id": 35, | ||
"image_url": "https://quay.io/repository/ansible/eda-server", | ||
"modified_at": "2024-08-15T21:12:52.218994Z", | ||
"name": "Example Decision environment", | ||
"organization_id": 1 | ||
} | ||
] | ||
""" | ||
|
||
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 to_list_of_dict | ||
from ..module_utils.controller import Controller | ||
from ..module_utils.errors import EDAError | ||
|
||
|
||
def main(): | ||
argument_spec = dict( | ||
name=dict(), | ||
) | ||
|
||
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"), | ||
) | ||
|
||
decision_environment_endpoint = "decision-environments" | ||
controller = Controller(client, module) | ||
|
||
decision_environment_name = module.params.get("name") | ||
ret = {} | ||
|
||
try: | ||
ret = controller.get_one_or_many( | ||
decision_environment_endpoint, | ||
name=decision_environment_name, | ||
want_one=False, | ||
) | ||
except EDAError as eda_err: | ||
module.fail_json(msg=str(eda_err)) | ||
|
||
module.exit_json(decision_environments=to_list_of_dict(ret)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
81 changes: 81 additions & 0 deletions
81
tests/integration/targets/decision_environment/tasks/create.yml
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,81 @@ | ||
--- | ||
# Copyright: Contributors to the Ansible project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
- name: Create decision environment in check mode | ||
ansible.eda.decision_environment: | ||
controller_host: "{{ controller_host }}" | ||
controller_username: "{{ controller_user }}" | ||
validate_certs: "{{ validate_certs }}" | ||
name: "{{ decision_env_name }}" | ||
description: "Example decision environment description" | ||
image_url: "{{ image_url }}" | ||
state: present | ||
check_mode: true | ||
register: r | ||
|
||
- name: Check decision environment in check mode | ||
assert: | ||
that: | ||
- r.changed | ||
|
||
- name: Create decision environment | ||
ansible.eda.decision_environment: | ||
controller_host: "{{ controller_host }}" | ||
controller_username: "{{ controller_user }}" | ||
validate_certs: "{{ validate_certs }}" | ||
name: "{{ decision_env_name }}" | ||
description: "Example decision environment description" | ||
image_url: "{{ image_url }}" | ||
state: present | ||
register: r | ||
|
||
- name: Check decision environment creation | ||
assert: | ||
that: | ||
- r.changed | ||
|
||
- name: Create decision environment again | ||
ansible.eda.decision_environment: | ||
controller_host: "{{ controller_host }}" | ||
controller_username: "{{ controller_user }}" | ||
validate_certs: "{{ validate_certs }}" | ||
name: "{{ decision_env_name }}" | ||
description: "Example decision environment description" | ||
image_url: "{{ image_url }}" | ||
state: present | ||
register: r | ||
|
||
- name: Check decision environment is not created again | ||
assert: | ||
that: | ||
- not r.changed | ||
|
||
- name: Delete decision environment in check mode | ||
ansible.eda.decision_environment: | ||
controller_host: "{{ controller_host }}" | ||
controller_username: "{{ controller_user }}" | ||
validate_certs: "{{ validate_certs }}" | ||
name: "{{ decision_env_name }}" | ||
state: absent | ||
check_mode: true | ||
register: r | ||
|
||
- name: Check if decision environment deleted in check mode | ||
assert: | ||
that: | ||
- r.changed | ||
|
||
- name: Delete decision environment | ||
ansible.eda.decision_environment: | ||
controller_host: "{{ controller_host }}" | ||
controller_username: "{{ controller_user }}" | ||
validate_certs: "{{ validate_certs }}" | ||
name: "{{ decision_env_name }}" | ||
state: absent | ||
register: r | ||
|
||
- name: Check if delete decision environment | ||
assert: | ||
that: | ||
- r.changed |
Oops, something went wrong.