|
| 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() |
0 commit comments