Skip to content

Commit a3e8837

Browse files
authored
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]>
1 parent 379959e commit a3e8837

File tree

8 files changed

+638
-0
lines changed

8 files changed

+638
-0
lines changed
+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright: Contributors to the Ansible project
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
7+
from __future__ import absolute_import, division, print_function
8+
9+
__metaclass__ = type
10+
11+
DOCUMENTATION = """
12+
---
13+
module: decision_environment
14+
author:
15+
- Nikhil Jain (@jainnikhil30)
16+
- Abhijeet Kasurde (@akasurde)
17+
short_description: Create, update or delete decision environment in EDA Controller
18+
description:
19+
- This module allows user to create, update or delete decision environment in a EDA controller.
20+
version_added: '2.0.0'
21+
options:
22+
name:
23+
description:
24+
- The name of the decision environment.
25+
type: str
26+
required: true
27+
new_name:
28+
description:
29+
- Setting this option will change the existing name.
30+
type: str
31+
description:
32+
description:
33+
- The description of the decision environment.
34+
type: str
35+
image_url:
36+
description:
37+
- Image URL of the decision environment.
38+
type: str
39+
credential:
40+
description:
41+
- Name of the credential to associate with the decision environment.
42+
type: str
43+
state:
44+
description:
45+
- Desired state of the resource.
46+
default: "present"
47+
choices: ["present", "absent"]
48+
type: str
49+
extends_documentation_fragment:
50+
- ansible.eda.eda_controller.auths
51+
"""
52+
53+
EXAMPLES = """
54+
- name: Create EDA Decision Environment
55+
ansible.eda.decision_environment:
56+
controller_host: https://my_eda_host/
57+
controller_username: admin
58+
controller_password: MySuperSecretPassw0rd
59+
name: "Example Decision Environment"
60+
description: "Example Decision Environment description"
61+
image_url: "quay.io/test"
62+
credential: "Example Credential"
63+
state: present
64+
65+
- name: Update the name of the Decision Environment
66+
ansible.eda.decision_environment:
67+
controller_host: https://my_eda_host/
68+
controller_username: admin
69+
controller_password: MySuperSecretPassw0rd
70+
name: "Example Decision Environment"
71+
new_name: "Latest Example Decision Environment"
72+
state: present
73+
74+
- name: Delete the the Decision Environment
75+
ansible.eda.decision_environment:
76+
controller_host: https://my_eda_host/
77+
controller_username: admin
78+
controller_password: MySuperSecretPassw0rd
79+
name: "Example Decision Environment"
80+
state: absent
81+
"""
82+
83+
RETURN = """
84+
id:
85+
description: ID of the decision environment
86+
returned: when exists
87+
type: int
88+
sample: 37
89+
"""
90+
91+
from ansible.module_utils.basic import AnsibleModule
92+
93+
from ..module_utils.arguments import AUTH_ARGSPEC
94+
from ..module_utils.client import Client
95+
from ..module_utils.controller import Controller
96+
from ..module_utils.errors import EDAError
97+
98+
99+
def main():
100+
argument_spec = dict(
101+
name=dict(required=True),
102+
new_name=dict(),
103+
description=dict(),
104+
image_url=dict(),
105+
credential=dict(),
106+
state=dict(choices=["present", "absent"], default="present"),
107+
)
108+
109+
argument_spec.update(AUTH_ARGSPEC)
110+
111+
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
112+
113+
client = Client(
114+
host=module.params.get("controller_host"),
115+
username=module.params.get("controller_username"),
116+
password=module.params.get("controller_password"),
117+
timeout=module.params.get("request_timeout"),
118+
validate_certs=module.params.get("validate_certs"),
119+
)
120+
121+
decision_environment_endpoint = "decision-environments"
122+
controller = Controller(client, module)
123+
124+
decision_environment_name = module.params.get("name")
125+
new_name = module.params.get("new_name")
126+
description = module.params.get("description")
127+
image_url = module.params.get("image_url")
128+
state = module.params.get("state")
129+
credential = module.params.get("credential")
130+
ret = {}
131+
132+
try:
133+
decision_environment_type = controller.get_one_or_many(
134+
decision_environment_endpoint, name=decision_environment_name
135+
)
136+
except EDAError as eda_err:
137+
module.fail_json(msg=str(eda_err))
138+
139+
if state == "absent":
140+
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
141+
try:
142+
ret = controller.delete_if_needed(
143+
decision_environment_type, endpoint=decision_environment_endpoint
144+
)
145+
except EDAError as eda_err:
146+
module.fail_json(msg=str(eda_err))
147+
module.exit_json(**ret)
148+
149+
decision_environment_type_params = {}
150+
if description:
151+
decision_environment_type_params["description"] = description
152+
if image_url:
153+
decision_environment_type_params["image_url"] = image_url
154+
155+
credential_type = None
156+
if credential:
157+
try:
158+
credential_type = controller.get_one_or_many(
159+
"eda-credentials", name=credential
160+
)
161+
except EDAError as eda_err:
162+
module.fail_json(msg=str(eda_err))
163+
164+
if credential_type is not None:
165+
# this is resolved earlier, so save an API call and don't do it again
166+
# in the loop above
167+
decision_environment_type_params["credential"] = credential_type["id"]
168+
169+
if new_name:
170+
decision_environment_type_params["name"] = new_name
171+
elif decision_environment_type:
172+
decision_environment_type_params["name"] = controller.get_item_name(
173+
decision_environment_type
174+
)
175+
else:
176+
decision_environment_type_params["name"] = decision_environment_name
177+
178+
# If the state was present and we can let the module build or update
179+
# the existing decision environment type,
180+
# this will return on its own
181+
try:
182+
ret = controller.create_or_update_if_needed(
183+
decision_environment_type,
184+
decision_environment_type_params,
185+
endpoint=decision_environment_endpoint,
186+
item_type="decision environment type",
187+
)
188+
except EDAError as eda_err:
189+
module.fail_json(msg=str(eda_err))
190+
191+
module.exit_json(**ret)
192+
193+
194+
if __name__ == "__main__":
195+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright: Contributors to the Ansible project
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
7+
from __future__ import absolute_import, division, print_function
8+
9+
__metaclass__ = type
10+
11+
DOCUMENTATION = """
12+
---
13+
module: decision_environment_info
14+
author:
15+
- Abhijeet Kasurde (@akasurde)
16+
short_description: List a decision environment in EDA Controller
17+
description:
18+
- This module allows user to list a decision environment in a EDA controller.
19+
version_added: '2.0.0'
20+
options:
21+
name:
22+
description:
23+
- The name of the decision environment.
24+
type: str
25+
extends_documentation_fragment:
26+
- ansible.eda.eda_controller.auths
27+
"""
28+
29+
EXAMPLES = """
30+
- name: List all EDA Decision Environments
31+
ansible.eda.decision_environment_info:
32+
controller_host: https://my_eda_host/
33+
controller_username: admin
34+
controller_password: MySuperSecretPassw0rd
35+
36+
- name: List a particular EDA Decision Environments
37+
ansible.eda.decision_environment_info:
38+
controller_host: https://my_eda_host/
39+
controller_username: admin
40+
controller_password: MySuperSecretPassw0rd
41+
name: Example
42+
"""
43+
44+
RETURN = """
45+
decision_environments:
46+
description: List of dict containing information about decision environments
47+
returned: when exists
48+
type: list
49+
sample: [
50+
{
51+
"created_at": "2024-08-15T21:12:52.218969Z",
52+
"description": "Example decision environment description",
53+
"eda_credential_id": null,
54+
"id": 35,
55+
"image_url": "https://quay.io/repository/ansible/eda-server",
56+
"modified_at": "2024-08-15T21:12:52.218994Z",
57+
"name": "Example Decision environment",
58+
"organization_id": 1
59+
}
60+
]
61+
"""
62+
63+
from ansible.module_utils.basic import AnsibleModule
64+
65+
from ..module_utils.arguments import AUTH_ARGSPEC
66+
from ..module_utils.client import Client
67+
from ..module_utils.common import to_list_of_dict
68+
from ..module_utils.controller import Controller
69+
from ..module_utils.errors import EDAError
70+
71+
72+
def main():
73+
argument_spec = dict(
74+
name=dict(),
75+
)
76+
77+
argument_spec.update(AUTH_ARGSPEC)
78+
79+
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
80+
81+
client = Client(
82+
host=module.params.get("controller_host"),
83+
username=module.params.get("controller_username"),
84+
password=module.params.get("controller_password"),
85+
timeout=module.params.get("request_timeout"),
86+
validate_certs=module.params.get("validate_certs"),
87+
)
88+
89+
decision_environment_endpoint = "decision-environments"
90+
controller = Controller(client, module)
91+
92+
decision_environment_name = module.params.get("name")
93+
ret = {}
94+
95+
try:
96+
ret = controller.get_one_or_many(
97+
decision_environment_endpoint,
98+
name=decision_environment_name,
99+
want_one=False,
100+
)
101+
except EDAError as eda_err:
102+
module.fail_json(msg=str(eda_err))
103+
104+
module.exit_json(decision_environments=to_list_of_dict(ret))
105+
106+
107+
if __name__ == "__main__":
108+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
# Copyright: Contributors to the Ansible project
3+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
5+
- name: Create decision environment in check mode
6+
ansible.eda.decision_environment:
7+
controller_host: "{{ controller_host }}"
8+
controller_username: "{{ controller_user }}"
9+
validate_certs: "{{ validate_certs }}"
10+
name: "{{ decision_env_name }}"
11+
description: "Example decision environment description"
12+
image_url: "{{ image_url }}"
13+
state: present
14+
check_mode: true
15+
register: r
16+
17+
- name: Check decision environment in check mode
18+
assert:
19+
that:
20+
- r.changed
21+
22+
- name: Create decision environment
23+
ansible.eda.decision_environment:
24+
controller_host: "{{ controller_host }}"
25+
controller_username: "{{ controller_user }}"
26+
validate_certs: "{{ validate_certs }}"
27+
name: "{{ decision_env_name }}"
28+
description: "Example decision environment description"
29+
image_url: "{{ image_url }}"
30+
state: present
31+
register: r
32+
33+
- name: Check decision environment creation
34+
assert:
35+
that:
36+
- r.changed
37+
38+
- name: Create decision environment again
39+
ansible.eda.decision_environment:
40+
controller_host: "{{ controller_host }}"
41+
controller_username: "{{ controller_user }}"
42+
validate_certs: "{{ validate_certs }}"
43+
name: "{{ decision_env_name }}"
44+
description: "Example decision environment description"
45+
image_url: "{{ image_url }}"
46+
state: present
47+
register: r
48+
49+
- name: Check decision environment is not created again
50+
assert:
51+
that:
52+
- not r.changed
53+
54+
- name: Delete decision environment in check mode
55+
ansible.eda.decision_environment:
56+
controller_host: "{{ controller_host }}"
57+
controller_username: "{{ controller_user }}"
58+
validate_certs: "{{ validate_certs }}"
59+
name: "{{ decision_env_name }}"
60+
state: absent
61+
check_mode: true
62+
register: r
63+
64+
- name: Check if decision environment deleted in check mode
65+
assert:
66+
that:
67+
- r.changed
68+
69+
- name: Delete decision environment
70+
ansible.eda.decision_environment:
71+
controller_host: "{{ controller_host }}"
72+
controller_username: "{{ controller_user }}"
73+
validate_certs: "{{ validate_certs }}"
74+
name: "{{ decision_env_name }}"
75+
state: absent
76+
register: r
77+
78+
- name: Check if delete decision environment
79+
assert:
80+
that:
81+
- r.changed

0 commit comments

Comments
 (0)