| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["preview"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
--- |
| 19 |
module: purefb_certgrp |
| 20 |
version_added: '1.4.0' |
| 21 |
short_description: Manage FlashBlade Certifcate Groups |
| 22 |
description: |
| 23 |
- Manage certifcate groups for FlashBlades |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Create or delete certifcate group |
| 30 |
default: present |
| 31 |
type: str |
| 32 |
choices: [ absent, present ] |
| 33 |
name: |
| 34 |
description: |
| 35 |
- Name of the certificate group |
| 36 |
type: str |
| 37 |
certificates: |
| 38 |
description: |
| 39 |
- List of certifcates to add to a policy on creation |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
extends_documentation_fragment: |
| 43 |
- purestorage.flashblade.purestorage.fb |
| 44 |
""" |
| 45 |
|
| 46 |
EXAMPLES = r""" |
| 47 |
- name: Create a certifcate group |
| 48 |
purestorage.flashblade.purefb_certgrp: |
| 49 |
name: test_grp |
| 50 |
fb_url: 10.10.10.2 |
| 51 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 52 |
- name: Create a cerifcate group and add existing certificates |
| 53 |
purestorage.flashblade.purefb_certgrp: |
| 54 |
name: test_grp |
| 55 |
certifcates: |
| 56 |
- cert1 |
| 57 |
- cert2 |
| 58 |
fb_url: 10.10.10.2 |
| 59 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 60 |
- name: Delete a certifcate from a group |
| 61 |
purestorage.flashblade.purefb_certgrp: |
| 62 |
name: test_grp |
| 63 |
certificates: |
| 64 |
- cert2 |
| 65 |
state: absent |
| 66 |
fb_url: 10.10.10.2 |
| 67 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 68 |
- name: Delete a certifcate group |
| 69 |
purestorage.flashblade.purefb_certgrp: |
| 70 |
name: test_grp |
| 71 |
state: absent |
| 72 |
fb_url: 10.10.10.2 |
| 73 |
api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6 |
| 74 |
""" |
| 75 |
|
| 76 |
RETURN = r""" |
| 77 |
""" |
| 78 |
|
| 79 |
from ansible.module_utils.basic import AnsibleModule |
| 80 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 81 |
get_system, |
| 82 |
purefb_argument_spec, |
| 83 |
) |
| 84 |
|
| 85 |
|
| 86 |
def delete_certgrp(module, blade): |
| 87 |
"""Delete certifcate group""" |
| 88 |
changed = True |
| 89 |
if not module.check_mode: |
| 90 |
res = blade.delete_certificate_groups(names=[module.params["name"]]) |
| 91 |
if res.status_code != 200: |
| 92 |
module.fail_json( |
| 93 |
msg="Failed to delete certifcate group {0}.".format( |
| 94 |
module.params["name"] |
| 95 |
) |
| 96 |
) |
| 97 |
module.exit_json(changed=changed) |
| 98 |
|
| 99 |
|
| 100 |
def create_certgrp(module, blade): |
| 101 |
"""Create certifcate group""" |
| 102 |
changed = True |
| 103 |
if not module.check_mode: |
| 104 |
res = blade.post_certificate_groups(names=[module.params["name"]]) |
| 105 |
if res.sttaus_code != 200: |
| 106 |
module.fail_json( |
| 107 |
msg="Failed to create certificate group {0}. Error: {1}".format( |
| 108 |
module.params["name"], res.errors[0].message |
| 109 |
) |
| 110 |
) |
| 111 |
if module.params["certificates"]: |
| 112 |
res = blade.post_certificate_groups_certificates( |
| 113 |
certificate_names=module.params["certificates"], |
| 114 |
certificate_group_names=[module.params["name"]], |
| 115 |
) |
| 116 |
if res.status_code != 200: |
| 117 |
blade.delete_certificate_groups(names=[module.params["name"]]) |
| 118 |
module.fail_json( |
| 119 |
msg="Failed to add certifcates {0}. Error: {1}".format( |
| 120 |
module.params["certificates"], res.errors[0].message |
| 121 |
) |
| 122 |
) |
| 123 |
module.exit_json(changed=changed) |
| 124 |
|
| 125 |
|
| 126 |
def update_certgrp(module, blade): |
| 127 |
"""Update certificate group""" |
| 128 |
changed = False |
| 129 |
res = blade.get_certificate_group_certificates( |
| 130 |
certificate_group_names=[module.params["name"]] |
| 131 |
) |
| 132 |
if res.status_code != 200: |
| 133 |
module.fail_json( |
| 134 |
msg="Failed to get certifates list for group {0}. Error: {1}".format( |
| 135 |
module.params["name"], res.errors[0].message |
| 136 |
) |
| 137 |
) |
| 138 |
certs = list(res.items) |
| 139 |
if certs: |
| 140 |
if module.params["state"] == "present": |
| 141 |
changed = True |
| 142 |
if not module.check_mode: |
| 143 |
res = blade.post_certificate_group_certificates( |
| 144 |
certificate_names=module.params["certificates"], |
| 145 |
certificate_group_names=[module.params["name"]], |
| 146 |
) |
| 147 |
if res.status_code != 200: |
| 148 |
module.fail_json( |
| 149 |
msg="Failed to add certifcates {0}. Error: {1}".format( |
| 150 |
module.params["certificates"], res.errors[0].message |
| 151 |
) |
| 152 |
) |
| 153 |
else: |
| 154 |
current = [] |
| 155 |
for cert in range(len(certs)): |
| 156 |
current.append(certs[cert].member.name) |
| 157 |
for new_cert in range(len(module.params["certificates"])): |
| 158 |
certificate = module.params["certificates"][new_cert] |
| 159 |
if certificate in current: |
| 160 |
if module.params["state"] == "absent": |
| 161 |
changed = True |
| 162 |
if not module.check_mode: |
| 163 |
res = blade.delete_certificate_group_certificates( |
| 164 |
certificate_names=[certificate], |
| 165 |
certificate_group_names=[module.params["name"]], |
| 166 |
) |
| 167 |
if res.status_code != 200: |
| 168 |
module.fail_json( |
| 169 |
msg="Failed to delete certifcate {0} from group {1}. Error: {2}".format( |
| 170 |
certificate, |
| 171 |
module.params["name"], |
| 172 |
res.errors[0].message, |
| 173 |
) |
| 174 |
) |
| 175 |
else: |
| 176 |
if module.params["state"] == "present": |
| 177 |
changed = True |
| 178 |
if not module.check_mode: |
| 179 |
res = blade.post_certificate_group_certificates( |
| 180 |
certificate_names=[certificate], |
| 181 |
certificate_group_names=[module.params["name"]], |
| 182 |
) |
| 183 |
if res.status_code != 200: |
| 184 |
module.fail_json( |
| 185 |
msg="Failed to add certifcate {0} to group {1}. Error: {2}".format( |
| 186 |
certificate, |
| 187 |
module.params["name"], |
| 188 |
res.errors[0].message, |
| 189 |
) |
| 190 |
) |
| 191 |
module.exit_json(changed=changed) |
| 192 |
|
| 193 |
|
| 194 |
def main(): |
| 195 |
argument_spec = purefb_argument_spec() |
| 196 |
argument_spec.update( |
| 197 |
dict( |
| 198 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 199 |
name=dict(type="str"), |
| 200 |
certificates=dict(type="list", elements="str"), |
| 201 |
) |
| 202 |
) |
| 203 |
|
| 204 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 205 |
|
| 206 |
state = module.params["state"] |
| 207 |
blade = get_system(module) |
| 208 |
|
| 209 |
certgrp = bool( |
| 210 |
blade.get_certificate_groups(names=[module.params["name"]]).status_code == 200 |
| 211 |
) |
| 212 |
|
| 213 |
if certgrp and state == "present" and module.params["certificates"]: |
| 214 |
update_certgrp(module, blade) |
| 215 |
elif state == "present" and not certgrp: |
| 216 |
create_certgrp(module, blade) |
| 217 |
elif state == "absent" and certgrp: |
| 218 |
if module.params["certificates"]: |
| 219 |
update_certgrp(module, blade) |
| 220 |
else: |
| 221 |
delete_certgrp(module, blade) |
| 222 |
|
| 223 |
module.exit_json(changed=False) |
| 224 |
|
| 225 |
|
| 226 |
if __name__ == "__main__": |
| 227 |
main() |