| 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: purefa_directory |
| 20 |
version_added: '1.5.0' |
| 21 |
short_description: Manage FlashArray File System Directories |
| 22 |
description: |
| 23 |
- Create/Delete FlashArray File Systems |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the directory |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Define whether the directory should exist or not. |
| 35 |
default: present |
| 36 |
choices: [ absent, present ] |
| 37 |
type: str |
| 38 |
filesystem: |
| 39 |
description: |
| 40 |
- Name of the filesystem the directory links to. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
path: |
| 44 |
description: |
| 45 |
- Path of the managed directory in the file system |
| 46 |
- If not provided will default to I(name) |
| 47 |
type: str |
| 48 |
rename: |
| 49 |
description: |
| 50 |
- Value to rename the specified directory to |
| 51 |
type: str |
| 52 |
context: |
| 53 |
description: |
| 54 |
- Name of fleet member on which to perform the operation. |
| 55 |
- This requires the array receiving the request is a member of a fleet |
| 56 |
and the context name to be a member of the same fleet. |
| 57 |
type: str |
| 58 |
default: "" |
| 59 |
version_added: '1.39.0' |
| 60 |
extends_documentation_fragment: |
| 61 |
- purestorage.flasharray.purestorage.fa |
| 62 |
""" |
| 63 |
|
| 64 |
EXAMPLES = r""" |
| 65 |
- name: Create directory foo in filesysten bar with path zeta |
| 66 |
purestorage.flasharray.purefa_directory: |
| 67 |
name: foo |
| 68 |
filesystem: bar |
| 69 |
path: zeta |
| 70 |
fa_url: 10.10.10.2 |
| 71 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 72 |
|
| 73 |
- name: Rename directory foo to fin in filesystem bar |
| 74 |
purestorage.flasharray.purefa_directory: |
| 75 |
name: foo |
| 76 |
rename: fin |
| 77 |
filesystem: bar |
| 78 |
fa_url: 10.10.10.2 |
| 79 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 80 |
|
| 81 |
- name: Delete directory foo in filesystem bar |
| 82 |
purestorage.flasharray.purefa_directory: |
| 83 |
name: foo |
| 84 |
filesystem: bar |
| 85 |
state: absent |
| 86 |
fa_url: 10.10.10.2 |
| 87 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 88 |
""" |
| 89 |
|
| 90 |
RETURN = r""" |
| 91 |
""" |
| 92 |
|
| 93 |
HAS_PURESTORAGE = True |
| 94 |
try: |
| 95 |
from pypureclient import flasharray |
| 96 |
except ImportError: |
| 97 |
HAS_PURESTORAGE = False |
| 98 |
|
| 99 |
from ansible.module_utils.basic import AnsibleModule |
| 100 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 101 |
get_array, |
| 102 |
purefa_argument_spec, |
| 103 |
) |
| 104 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 105 |
LooseVersion, |
| 106 |
) |
| 107 |
|
| 108 |
CONTEXT_VERSION = "2.38" |
| 109 |
|
| 110 |
|
| 111 |
def delete_dir(module, array): |
| 112 |
"""Delete a file system directory""" |
| 113 |
api_version = array.get_rest_version() |
| 114 |
changed = True |
| 115 |
if not module.check_mode: |
| 116 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 117 |
res = array.delete_directories( |
| 118 |
names=[module.params["filesystem"] + ":" + module.params["name"]], |
| 119 |
context_names=[module.params["context"]], |
| 120 |
) |
| 121 |
else: |
| 122 |
res = array.delete_directories( |
| 123 |
names=[module.params["filesystem"] + ":" + module.params["name"]] |
| 124 |
) |
| 125 |
if res.status_code != 200: |
| 126 |
module.fail_json( |
| 127 |
msg="Failed to delete file system {0}. {1}".format( |
| 128 |
module.params["name"], res.errors[0].message |
| 129 |
) |
| 130 |
) |
| 131 |
module.exit_json(changed=changed) |
| 132 |
|
| 133 |
|
| 134 |
def rename_dir(module, array): |
| 135 |
"""Rename a file system directory""" |
| 136 |
api_version = array.get_rest_version() |
| 137 |
changed = False |
| 138 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 139 |
target = array.get_directories( |
| 140 |
names=[module.params["filesystem"] + ":" + module.params["rename"]], |
| 141 |
context_names=[module.params["context"]], |
| 142 |
) |
| 143 |
else: |
| 144 |
target = array.get_directories( |
| 145 |
names=[module.params["filesystem"] + ":" + module.params["rename"]] |
| 146 |
) |
| 147 |
if target.status_code != 200: |
| 148 |
if not module.check_mode: |
| 149 |
changed = True |
| 150 |
directory = flasharray.DirectoryPatch( |
| 151 |
name=module.params["filesystem"] + ":" + module.params["rename"] |
| 152 |
) |
| 153 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 154 |
res = array.patch_directories( |
| 155 |
names=[module.params["filesystem"] + ":" + module.params["name"]], |
| 156 |
directory=directory, |
| 157 |
context_names=[module.params["context"]], |
| 158 |
) |
| 159 |
else: |
| 160 |
res = array.patch_directories( |
| 161 |
names=[module.params["filesystem"] + ":" + module.params["name"]], |
| 162 |
directory=directory, |
| 163 |
) |
| 164 |
if res.status_code != 200: |
| 165 |
module.fail_json( |
| 166 |
msg="Failed to delete file system {0}".format(module.params["name"]) |
| 167 |
) |
| 168 |
else: |
| 169 |
module.fail_json( |
| 170 |
msg="Target file system {0} already exists".format(module.params["rename"]) |
| 171 |
) |
| 172 |
module.exit_json(changed=changed) |
| 173 |
|
| 174 |
|
| 175 |
def create_dir(module, array): |
| 176 |
"""Create a file system directory""" |
| 177 |
api_version = array.get_rest_version() |
| 178 |
changed = False |
| 179 |
if not module.params["path"]: |
| 180 |
module.params["path"] = module.params["name"] |
| 181 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 182 |
all_fs = list( |
| 183 |
array.get_directories( |
| 184 |
file_system_names=[module.params["filesystem"]], |
| 185 |
context_names=[module.params["context"]], |
| 186 |
).items |
| 187 |
) |
| 188 |
else: |
| 189 |
all_fs = list( |
| 190 |
array.get_directories(file_system_names=[module.params["filesystem"]]).items |
| 191 |
) |
| 192 |
for check in all_fs: |
| 193 |
if module.params["path"] == check.path[1:]: |
| 194 |
module.fail_json( |
| 195 |
msg="Path {0} already existis in file system {1}".format( |
| 196 |
module.params["path"], module.params["filesystem"] |
| 197 |
) |
| 198 |
) |
| 199 |
changed = True |
| 200 |
if not module.check_mode: |
| 201 |
directory = flasharray.DirectoryPost( |
| 202 |
directory_name=module.params["name"], path=module.params["path"] |
| 203 |
) |
| 204 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 205 |
res = array.post_directories( |
| 206 |
file_system_names=[module.params["filesystem"]], |
| 207 |
directory=directory, |
| 208 |
context_names=[module.params["context"]], |
| 209 |
) |
| 210 |
else: |
| 211 |
res = array.post_directories( |
| 212 |
file_system_names=[module.params["filesystem"]], directory=directory |
| 213 |
) |
| 214 |
if res.status_code != 200: |
| 215 |
module.fail_json( |
| 216 |
msg="Failed to create file system {0}. {1}".format( |
| 217 |
module.params["name"], res.errors[0].message |
| 218 |
) |
| 219 |
) |
| 220 |
module.exit_json(changed=changed) |
| 221 |
|
| 222 |
|
| 223 |
def main(): |
| 224 |
argument_spec = purefa_argument_spec() |
| 225 |
argument_spec.update( |
| 226 |
dict( |
| 227 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 228 |
filesystem=dict(type="str", required=True), |
| 229 |
name=dict(type="str", required=True), |
| 230 |
rename=dict(type="str"), |
| 231 |
path=dict(type="str"), |
| 232 |
context=dict(type="str", default=""), |
| 233 |
) |
| 234 |
) |
| 235 |
|
| 236 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 237 |
|
| 238 |
if not HAS_PURESTORAGE: |
| 239 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 240 |
|
| 241 |
array = get_array(module) |
| 242 |
state = module.params["state"] |
| 243 |
api_version = array.get_rest_version() |
| 244 |
|
| 245 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 246 |
res = array.get_file_systems( |
| 247 |
names=[module.params["filesystem"]], |
| 248 |
context_names=[module.params["context"]], |
| 249 |
) |
| 250 |
else: |
| 251 |
res = array.get_file_systems(names=[module.params["filesystem"]]) |
| 252 |
if res.status_code == 200: |
| 253 |
filesystem = list(res.items)[0] |
| 254 |
else: |
| 255 |
module.fail_json( |
| 256 |
msg="Selected file system {0} does not exist".format( |
| 257 |
module.params["filesystem"] |
| 258 |
) |
| 259 |
) |
| 260 |
res = array.get_directories( |
| 261 |
names=[module.params["filesystem"] + ":" + module.params["name"]] |
| 262 |
) |
| 263 |
exists = bool(res.status_code == 200) |
| 264 |
|
| 265 |
if state == "present" and not exists: |
| 266 |
create_dir(module, array) |
| 267 |
elif ( |
| 268 |
state == "present" |
| 269 |
and exists |
| 270 |
and module.params["rename"] |
| 271 |
and not filesystem.destroyed |
| 272 |
): |
| 273 |
rename_dir(module, array) |
| 274 |
elif state == "absent" and exists: |
| 275 |
delete_dir(module, array) |
| 276 |
|
| 277 |
module.exit_json(changed=False) |
| 278 |
|
| 279 |
|
| 280 |
if __name__ == "__main__": |
| 281 |
main() |