| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the Ansible project |
| 5 |
# Apache License, Version 2.0 (see LICENSE or https://www.apache.org/licenses/LICENSE-2.0) |
| 6 |
|
| 7 |
|
| 8 |
from __future__ import (absolute_import, division, print_function) |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: alpaca_group |
| 15 |
|
| 16 |
short_description: Manage ALPACA Operator groups via REST API |
| 17 |
|
| 18 |
version_added: '1.0.0' |
| 19 |
|
| 20 |
extends_documentation_fragment: |
| 21 |
- pcg.alpaca_operator.api_connection |
| 22 |
|
| 23 |
description: This module allows you to create, rename or delete ALPACA Operator groups using the REST API. |
| 24 |
|
| 25 |
options: |
| 26 |
name: |
| 27 |
description: Name of the group. |
| 28 |
version_added: '1.0.0' |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
new_name: |
| 32 |
description: > |
| 33 |
Optional new name for the group. If the group specified in O(name) exists, |
| 34 |
it will be renamed to this value. If the group does not exist, a new group will |
| 35 |
be created using this value. |
| 36 |
version_added: '1.0.0' |
| 37 |
required: false |
| 38 |
type: str |
| 39 |
state: |
| 40 |
description: Desired state of the group. |
| 41 |
version_added: '1.0.0' |
| 42 |
required: false |
| 43 |
default: present |
| 44 |
choices: [present, absent] |
| 45 |
type: str |
| 46 |
|
| 47 |
requirements: |
| 48 |
- ALPACA Operator >= 5.6.0 |
| 49 |
|
| 50 |
attributes: |
| 51 |
check_mode: |
| 52 |
description: Can run in check_mode and return changed status prediction without modifying target. |
| 53 |
support: full |
| 54 |
|
| 55 |
author: |
| 56 |
- Jan-Karsten Hansmeyer (@pcg) |
| 57 |
''' |
| 58 |
|
| 59 |
EXAMPLES = r''' |
| 60 |
- name: Ensure group exists |
| 61 |
pcg.alpaca_operator.alpaca_group: |
| 62 |
name: testgroup01 |
| 63 |
state: present |
| 64 |
api_connection: |
| 65 |
host: localhost |
| 66 |
port: 8443 |
| 67 |
protocol: https |
| 68 |
username: secret |
| 69 |
password: secret |
| 70 |
tls_verify: false |
| 71 |
|
| 72 |
- name: Ensure group is absent |
| 73 |
pcg.alpaca_operator.alpaca_group: |
| 74 |
name: testgroup01 |
| 75 |
state: absent |
| 76 |
api_connection: |
| 77 |
host: localhost |
| 78 |
port: 8443 |
| 79 |
protocol: https |
| 80 |
username: secret |
| 81 |
password: secret |
| 82 |
tls_verify: false |
| 83 |
|
| 84 |
- name: Rename an existing group |
| 85 |
pcg.alpaca_operator.alpaca_group: |
| 86 |
name: testgroup01 |
| 87 |
new_name: testgroup_renamed |
| 88 |
state: present |
| 89 |
api_connection: |
| 90 |
host: localhost |
| 91 |
port: 8443 |
| 92 |
protocol: https |
| 93 |
username: secret |
| 94 |
password: secret |
| 95 |
tls_verify: false |
| 96 |
''' |
| 97 |
|
| 98 |
RETURN = r''' |
| 99 |
changed: |
| 100 |
description: Indicates whether any change was made to the group |
| 101 |
version_added: '1.0.0' |
| 102 |
returned: always |
| 103 |
type: bool |
| 104 |
sample: true |
| 105 |
|
| 106 |
msg: |
| 107 |
description: Human-readable message describing the outcome |
| 108 |
version_added: '1.0.0' |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: Group created |
| 112 |
|
| 113 |
id: |
| 114 |
description: Numeric ID of the group (if known or newly created) |
| 115 |
version_added: '1.0.0' |
| 116 |
returned: when state is present or absent and group exists |
| 117 |
type: int |
| 118 |
sample: 42 |
| 119 |
|
| 120 |
name: |
| 121 |
description: Name of the group (new or existing) |
| 122 |
version_added: '1.0.0' |
| 123 |
returned: always |
| 124 |
type: str |
| 125 |
sample: testgroup01 |
| 126 |
''' |
| 127 |
|
| 128 |
from ansible_collections.pcg.alpaca_operator.plugins.module_utils._alpaca_api import get_token, get_api_connection_argument_spec, api_call |
| 129 |
from ansible.module_utils.basic import AnsibleModule |
| 130 |
|
| 131 |
|
| 132 |
def find_group(api_url, headers, name, verify): |
| 133 |
"""Find group by name""" |
| 134 |
response = api_call("GET", "{0}/groups".format(api_url), headers=headers, verify=verify) |
| 135 |
for group in response.json(): |
| 136 |
if group["name"] == name: |
| 137 |
return group |
| 138 |
return None |
| 139 |
|
| 140 |
|
| 141 |
def main(): |
| 142 |
module = AnsibleModule( |
| 143 |
argument_spec=dict( |
| 144 |
name=dict(type='str', required=True), |
| 145 |
new_name=dict(type='str', required=False), |
| 146 |
state=dict(type='str', required=False, default='present', choices=['present', 'absent']), |
| 147 |
api_connection=get_api_connection_argument_spec() |
| 148 |
), |
| 149 |
supports_check_mode=True, |
| 150 |
) |
| 151 |
|
| 152 |
name = module.params['name'] |
| 153 |
new_name = module.params.get('new_name') |
| 154 |
state = module.params['state'] |
| 155 |
api_host = module.params['api_connection']['host'] |
| 156 |
api_port = module.params['api_connection']['port'] |
| 157 |
api_protocol = module.params['api_connection']['protocol'] |
| 158 |
api_username = module.params['api_connection']['username'] |
| 159 |
api_password = module.params['api_connection']['password'] |
| 160 |
api_tls_verify = module.params['api_connection']['tls_verify'] |
| 161 |
api_url = "{0}://{1}:{2}/api".format(api_protocol, api_host, api_port) |
| 162 |
api_token = get_token(api_url, api_username, api_password, api_tls_verify) |
| 163 |
|
| 164 |
headers = {"Authorization": "Bearer {0}".format(api_token)} |
| 165 |
group = find_group(api_url, headers, name, api_tls_verify) |
| 166 |
|
| 167 |
if state == 'present': |
| 168 |
if group: |
| 169 |
# If renaming is requested and name differs, perform update |
| 170 |
if new_name and new_name != name: |
| 171 |
if module.check_mode: |
| 172 |
module.exit_json(changed=True, msg="Group would be renamed", id=group["id"], name=new_name) |
| 173 |
|
| 174 |
if not find_group(api_url, headers, new_name, api_tls_verify): |
| 175 |
response = api_call( |
| 176 |
"PUT", |
| 177 |
"{0}/groups/{1}".format(api_url, group["id"]), |
| 178 |
headers=headers, |
| 179 |
verify=api_tls_verify, |
| 180 |
json={"name": new_name}, |
| 181 |
module=module, |
| 182 |
fail_msg="Failed to rename group" |
| 183 |
) |
| 184 |
|
| 185 |
if response.status_code not in [200]: |
| 186 |
module.fail_json(msg="Failed to rename group: {0}".format(response.text)) |
| 187 |
|
| 188 |
module.exit_json(changed=True, msg="Group renamed", id=group["id"], name=new_name) |
| 189 |
|
| 190 |
# No changes needed |
| 191 |
module.exit_json(changed=False, msg="Group already exists", id=group["id"], name=group["name"]) |
| 192 |
|
| 193 |
# Create the group if it doesn't exist |
| 194 |
if new_name: |
| 195 |
name = new_name |
| 196 |
|
| 197 |
if not find_group(api_url, headers, name, api_tls_verify): |
| 198 |
if module.check_mode: |
| 199 |
module.exit_json(changed=True, msg="Group would be created", name=name) |
| 200 |
|
| 201 |
response = api_call( |
| 202 |
"POST", |
| 203 |
"{0}/groups".format(api_url), |
| 204 |
headers=headers, |
| 205 |
verify=api_tls_verify, |
| 206 |
json={"name": name}, |
| 207 |
module=module, |
| 208 |
fail_msg="Failed to create group" |
| 209 |
) |
| 210 |
module.exit_json(changed=True, msg="Group created", id=response.json()["id"], name=name) |
| 211 |
|
| 212 |
module.exit_json(changed=False, msg="Group already exists", name=name) |
| 213 |
|
| 214 |
if state == 'absent': |
| 215 |
if not group: |
| 216 |
module.exit_json(changed=False, msg="Group does not exist", name=name) |
| 217 |
if module.check_mode: |
| 218 |
module.exit_json(changed=True, msg="Group would be deleted", id=group["id"], name=name) |
| 219 |
|
| 220 |
group_id = group["id"] |
| 221 |
response = api_call( |
| 222 |
"DELETE", |
| 223 |
"{0}/groups/{1}".format(api_url, group_id), |
| 224 |
headers=headers, |
| 225 |
verify=api_tls_verify, |
| 226 |
module=module, |
| 227 |
fail_msg="Failed to delete group" |
| 228 |
) |
| 229 |
|
| 230 |
if response.status_code not in [204]: |
| 231 |
module.fail_json(msg="Failed to delete group: {0}".format(response.text)) |
| 232 |
module.exit_json(changed=True, msg="Group deleted", id=group["id"], name=name) |
| 233 |
|
| 234 |
|
| 235 |
if __name__ == '__main__': |
| 236 |
main() |