| 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_virtualhost |
| 20 |
version_added: '1.6.0' |
| 21 |
short_description: Manage FlashBlade Object Store Virtual Hosts |
| 22 |
description: |
| 23 |
- Add or delete FlashBlade Object Store Virtual Hosts |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the Object Store Virtual Host |
| 30 |
- A hostname or domain by which the array can be addressed for virtual |
| 31 |
hosted-style S3 requests. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
state: |
| 35 |
description: |
| 36 |
- Define whether the Object Store Virtual Host should be added or deleted |
| 37 |
default: present |
| 38 |
choices: [ absent, present ] |
| 39 |
type: str |
| 40 |
context: |
| 41 |
description: |
| 42 |
- Name of fleet member on which to perform the operation. |
| 43 |
- This requires the array receiving the request is a member of a fleet |
| 44 |
and the context name to be a member of the same fleet. |
| 45 |
type: str |
| 46 |
default: "" |
| 47 |
version_added: "1.22.0" |
| 48 |
extends_documentation_fragment: |
| 49 |
- purestorage.flashblade.purestorage.fb |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
- name: Add Object Store Virtual Host |
| 54 |
purestorage.flashblade.purefb_virtualhost: |
| 55 |
name: "s3.acme.com" |
| 56 |
fb_url: 10.10.10.2 |
| 57 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 58 |
|
| 59 |
- name: Delete Object Store Virtual Host |
| 60 |
purestorage.flashblade.purefb_virtualhost: |
| 61 |
name: "nohost.acme.com" |
| 62 |
state: absent |
| 63 |
fb_url: 10.10.10.2 |
| 64 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 65 |
""" |
| 66 |
|
| 67 |
RETURN = r""" |
| 68 |
""" |
| 69 |
|
| 70 |
from ansible.module_utils.basic import AnsibleModule |
| 71 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 72 |
get_system, |
| 73 |
purefb_argument_spec, |
| 74 |
) |
| 75 |
|
| 76 |
MAX_HOST_COUNT = 10 |
| 77 |
CONTEXT_API_VERSION = "2.17" |
| 78 |
|
| 79 |
|
| 80 |
def delete_host(module, blade): |
| 81 |
"""Delete Object Store Virtual Host""" |
| 82 |
changed = False |
| 83 |
api_version = list(blade.get_versions().items) |
| 84 |
if module.params["name"] == "s3.amazonaws.com": |
| 85 |
module.warn("s3.amazonaws.com is a reserved name and cannot be deleted") |
| 86 |
else: |
| 87 |
changed = True |
| 88 |
if not module.check_mode: |
| 89 |
if CONTEXT_API_VERSION in api_version: |
| 90 |
res = blade.delete_object_store_virtual_hosts( |
| 91 |
names=[module.params["name"]], |
| 92 |
context_names=[module.params["context"]], |
| 93 |
) |
| 94 |
else: |
| 95 |
res = blade.delete_object_store_virtual_hosts( |
| 96 |
names=[module.params["name"]] |
| 97 |
) |
| 98 |
if res.status_code != 200: |
| 99 |
module.fail_json( |
| 100 |
msg="Failed to delete Object Store Virtual Host {0}".format( |
| 101 |
module.params["name"] |
| 102 |
) |
| 103 |
) |
| 104 |
module.exit_json(changed=changed) |
| 105 |
|
| 106 |
|
| 107 |
def add_host(module, blade): |
| 108 |
"""Add Object Store Virtual Host""" |
| 109 |
changed = True |
| 110 |
api_version = list(blade.get_versions().items) |
| 111 |
if not module.check_mode: |
| 112 |
if CONTEXT_API_VERSION in api_version: |
| 113 |
res = blade.post_object_store_virtual_hosts( |
| 114 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 115 |
) |
| 116 |
else: |
| 117 |
res = blade.post_object_store_virtual_hosts(names=[module.params["name"]]) |
| 118 |
if res.status_code != 200: |
| 119 |
module.fail_json( |
| 120 |
msg="Failed to add Object Store Virtual Host {0}".format( |
| 121 |
module.params["name"] |
| 122 |
) |
| 123 |
) |
| 124 |
module.exit_json(changed=changed) |
| 125 |
|
| 126 |
|
| 127 |
def main(): |
| 128 |
argument_spec = purefb_argument_spec() |
| 129 |
argument_spec.update( |
| 130 |
dict( |
| 131 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 132 |
name=dict(type="str", required=True), |
| 133 |
context=dict(type="str", default=""), |
| 134 |
) |
| 135 |
) |
| 136 |
|
| 137 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 138 |
|
| 139 |
blade = get_system(module) |
| 140 |
api_version = list(blade.get_versions().items) |
| 141 |
state = module.params["state"] |
| 142 |
|
| 143 |
if CONTEXT_API_VERSION in api_version: |
| 144 |
exists = bool( |
| 145 |
blade.get_object_store_virtual_hosts( |
| 146 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 147 |
).status_code |
| 148 |
== 200 |
| 149 |
) |
| 150 |
else: |
| 151 |
exists = bool( |
| 152 |
blade.get_object_store_virtual_hosts( |
| 153 |
names=[module.params["name"]] |
| 154 |
).status_code |
| 155 |
== 200 |
| 156 |
) |
| 157 |
if CONTEXT_API_VERSION in api_version: |
| 158 |
vhosts = blade.get_object_store_virtual_hosts( |
| 159 |
context_names=[module.params["context"]] |
| 160 |
) |
| 161 |
else: |
| 162 |
vhosts = blade.get_object_store_virtual_hosts() |
| 163 |
if vhosts.total_item_count < MAX_HOST_COUNT: |
| 164 |
if not exists and state == "present": |
| 165 |
add_host(module, blade) |
| 166 |
elif exists and state == "absent": |
| 167 |
delete_host(module, blade) |
| 168 |
else: |
| 169 |
module.warn("Maximum Object Store Virtual Host reached.") |
| 170 |
|
| 171 |
module.exit_json(changed=False) |
| 172 |
|
| 173 |
|
| 174 |
if __name__ == "__main__": |
| 175 |
main() |