| 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_export |
| 20 |
version_added: '1.5.0' |
| 21 |
short_description: Manage FlashArray File System Exports |
| 22 |
description: |
| 23 |
- Create/Delete FlashArray File Systems Exports |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the export |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Define whether the export should exist or not. |
| 35 |
- You must specify an NFS or SMB policy, or both on creation and deletion. |
| 36 |
default: present |
| 37 |
choices: [ absent, present ] |
| 38 |
type: str |
| 39 |
filesystem: |
| 40 |
description: |
| 41 |
- Name of the filesystem the export applies to |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
directory: |
| 45 |
description: |
| 46 |
- Name of the managed directory in the file system the export applies to |
| 47 |
type: str |
| 48 |
required: true |
| 49 |
nfs_policy: |
| 50 |
description: |
| 51 |
- Name of NFS Policy to apply to the export |
| 52 |
type: str |
| 53 |
smb_policy: |
| 54 |
description: |
| 55 |
- Name of SMB Policy to apply to the export |
| 56 |
type: str |
| 57 |
context: |
| 58 |
description: |
| 59 |
- Name of fleet member on which to perform the operation. |
| 60 |
- This requires the array receiving the request is a member of a fleet |
| 61 |
and the context name to be a member of the same fleet. |
| 62 |
type: str |
| 63 |
default: "" |
| 64 |
version_added: '1.39.0' |
| 65 |
extends_documentation_fragment: |
| 66 |
- purestorage.flasharray.purestorage.fa |
| 67 |
""" |
| 68 |
|
| 69 |
EXAMPLES = r""" |
| 70 |
- name: Create NFS and SMB exports for directory foo in filesysten bar |
| 71 |
purestorage.flasharray.purefa_export: |
| 72 |
name: export1 |
| 73 |
filesystem: bar |
| 74 |
directory: foo |
| 75 |
nfs_policy: nfs-example |
| 76 |
smb_policy: smb-example |
| 77 |
fa_url: 10.10.10.2 |
| 78 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 79 |
|
| 80 |
- name: Delete NFS export for directory foo in filesystem bar |
| 81 |
purestorage.flasharray.purefa_export: |
| 82 |
name: export1 |
| 83 |
filesystem: bar |
| 84 |
directory: foo |
| 85 |
nfs_policy: nfs-example |
| 86 |
state: absent |
| 87 |
fa_url: 10.10.10.2 |
| 88 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 89 |
""" |
| 90 |
|
| 91 |
RETURN = r""" |
| 92 |
""" |
| 93 |
|
| 94 |
HAS_PURESTORAGE = True |
| 95 |
try: |
| 96 |
from pypureclient import flasharray |
| 97 |
except ImportError: |
| 98 |
HAS_PURESTORAGE = False |
| 99 |
|
| 100 |
from ansible.module_utils.basic import AnsibleModule |
| 101 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 102 |
get_array, |
| 103 |
purefa_argument_spec, |
| 104 |
) |
| 105 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 106 |
LooseVersion, |
| 107 |
) |
| 108 |
|
| 109 |
MIN_REQUIRED_API_VERSION = "2.3" |
| 110 |
CONTEXT_VERSION = "2.42" |
| 111 |
|
| 112 |
|
| 113 |
def delete_export(module, array): |
| 114 |
"""Delete a file system export""" |
| 115 |
changed = False |
| 116 |
api_version = array.get_rest_version() |
| 117 |
all_policies = [] |
| 118 |
directory = module.params["filesystem"] + ":" + module.params["directory"] |
| 119 |
if not module.params["nfs_policy"] and not module.params["smb_policy"]: |
| 120 |
module.fail_json(msg="At least one policy must be provided") |
| 121 |
if module.params["nfs_policy"]: |
| 122 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 123 |
policy_exists = bool( |
| 124 |
array.get_directory_exports( |
| 125 |
export_names=[module.params["name"]], |
| 126 |
policy_names=[module.params["nfs_policy"]], |
| 127 |
directory_names=[directory], |
| 128 |
context_names=[module.params["context"]], |
| 129 |
).status_code |
| 130 |
== 200 |
| 131 |
) |
| 132 |
else: |
| 133 |
policy_exists = bool( |
| 134 |
array.get_directory_exports( |
| 135 |
export_names=[module.params["name"]], |
| 136 |
policy_names=[module.params["nfs_policy"]], |
| 137 |
directory_names=[directory], |
| 138 |
).status_code |
| 139 |
== 200 |
| 140 |
) |
| 141 |
if policy_exists: |
| 142 |
all_policies.append(module.params["nfs_policy"]) |
| 143 |
if module.params["smb_policy"]: |
| 144 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 145 |
policy_exists = bool( |
| 146 |
array.get_directory_exports( |
| 147 |
export_names=[module.params["name"]], |
| 148 |
policy_names=[module.params["smb_policy"]], |
| 149 |
directory_names=[directory], |
| 150 |
context_names=[module.params["context"]], |
| 151 |
).status_code |
| 152 |
== 200 |
| 153 |
) |
| 154 |
else: |
| 155 |
policy_exists = bool( |
| 156 |
array.get_directory_exports( |
| 157 |
export_names=[module.params["name"]], |
| 158 |
policy_names=[module.params["smb_policy"]], |
| 159 |
directory_names=[directory], |
| 160 |
).status_code |
| 161 |
== 200 |
| 162 |
) |
| 163 |
if policy_exists: |
| 164 |
all_policies.append(module.params["smb_policy"]) |
| 165 |
if all_policies: |
| 166 |
changed = True |
| 167 |
if not module.check_mode: |
| 168 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 169 |
res = array.delete_directory_exports( |
| 170 |
export_names=[module.params["name"]], |
| 171 |
policy_names=all_policies, |
| 172 |
context_names=[module.params["context"]], |
| 173 |
) |
| 174 |
else: |
| 175 |
res = array.delete_directory_exports( |
| 176 |
export_names=[module.params["name"]], policy_names=all_policies |
| 177 |
) |
| 178 |
if res.status_code != 200: |
| 179 |
module.fail_json( |
| 180 |
msg="Failed to delete file system export {0}. {1}".format( |
| 181 |
module.params["name"], res.errors[0].message |
| 182 |
) |
| 183 |
) |
| 184 |
module.exit_json(changed=changed) |
| 185 |
|
| 186 |
|
| 187 |
def create_export(module, array): |
| 188 |
"""Create a file system export""" |
| 189 |
changed = False |
| 190 |
api_version = array.get_rest_version() |
| 191 |
if not module.params["nfs_policy"] and not module.params["smb_policy"]: |
| 192 |
module.fail_json(msg="At least one policy must be provided") |
| 193 |
all_policies = [] |
| 194 |
if module.params["nfs_policy"]: |
| 195 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 196 |
res = array.get_policies_nfs( |
| 197 |
names=[module.params["nfs_policy"]], |
| 198 |
context_names=[module.params["context"]], |
| 199 |
) |
| 200 |
else: |
| 201 |
res = array.get_policies_nfs(names=[module.params["nfs_policy"]]) |
| 202 |
if bool(res.status_code != 200): |
| 203 |
module.fail_json( |
| 204 |
msg="NFS Policy {0} does not exist.".format(module.params["nfs_policy"]) |
| 205 |
) |
| 206 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 207 |
res = array.get_directory_exports( |
| 208 |
export_names=[module.params["name"]], |
| 209 |
policy_names=[module.params["nfs_policy"]], |
| 210 |
context_names=[module.params["context"]], |
| 211 |
) |
| 212 |
else: |
| 213 |
res = array.get_directory_exports( |
| 214 |
export_names=[module.params["name"]], |
| 215 |
policy_names=[module.params["nfs_policy"]], |
| 216 |
) |
| 217 |
if bool(res.status_code != 200): |
| 218 |
all_policies.append(module.params["nfs_policy"]) |
| 219 |
if module.params["smb_policy"]: |
| 220 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 221 |
res = array.get_policies_smb( |
| 222 |
names=[module.params["smb_policy"]], |
| 223 |
context_names=[module.params["context"]], |
| 224 |
) |
| 225 |
else: |
| 226 |
res = array.get_policies_smb(names=[module.params["smb_policy"]]) |
| 227 |
if bool(res.status_code != 200): |
| 228 |
module.fail_json( |
| 229 |
msg="SMB Policy {0} does not exist.".format(module.params["smb_policy"]) |
| 230 |
) |
| 231 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 232 |
res = array.get_directory_exports( |
| 233 |
export_names=[module.params["name"]], |
| 234 |
policy_names=[module.params["smb_policy"]], |
| 235 |
context_names=[module.params["context"]], |
| 236 |
) |
| 237 |
else: |
| 238 |
res = array.get_directory_exports( |
| 239 |
export_names=[module.params["name"]], |
| 240 |
policy_names=[module.params["smb_policy"]], |
| 241 |
) |
| 242 |
if bool(res.status_code != 200): |
| 243 |
all_policies.append(module.params["smb_policy"]) |
| 244 |
if all_policies: |
| 245 |
export = flasharray.DirectoryExportPost(export_name=module.params["name"]) |
| 246 |
changed = True |
| 247 |
if not module.check_mode: |
| 248 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 249 |
res = array.post_directory_exports( |
| 250 |
directory_names=[ |
| 251 |
module.params["filesystem"] + ":" + module.params["directory"] |
| 252 |
], |
| 253 |
exports=export, |
| 254 |
policy_names=all_policies, |
| 255 |
context_names=[module.params["context"]], |
| 256 |
) |
| 257 |
else: |
| 258 |
res = array.post_directory_exports( |
| 259 |
directory_names=[ |
| 260 |
module.params["filesystem"] + ":" + module.params["directory"] |
| 261 |
], |
| 262 |
exports=export, |
| 263 |
policy_names=all_policies, |
| 264 |
) |
| 265 |
if res.status_code != 200: |
| 266 |
module.fail_json( |
| 267 |
msg="Failed to create file system exports for {0}:{1}. Error: {2}".format( |
| 268 |
module.params["filesystem"], |
| 269 |
module.params["directory"], |
| 270 |
res.errors[0].message, |
| 271 |
) |
| 272 |
) |
| 273 |
module.exit_json(changed=changed) |
| 274 |
|
| 275 |
|
| 276 |
def main(): |
| 277 |
argument_spec = purefa_argument_spec() |
| 278 |
argument_spec.update( |
| 279 |
dict( |
| 280 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 281 |
filesystem=dict(type="str", required=True), |
| 282 |
directory=dict(type="str", required=True), |
| 283 |
name=dict(type="str", required=True), |
| 284 |
nfs_policy=dict(type="str"), |
| 285 |
smb_policy=dict(type="str"), |
| 286 |
context=dict(type="str", default=""), |
| 287 |
) |
| 288 |
) |
| 289 |
|
| 290 |
required_if = [["state", "present", ["filesystem", "directory"]]] |
| 291 |
module = AnsibleModule( |
| 292 |
argument_spec, required_if=required_if, supports_check_mode=True |
| 293 |
) |
| 294 |
|
| 295 |
if not HAS_PURESTORAGE: |
| 296 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 297 |
|
| 298 |
array = get_array(module) |
| 299 |
api_version = array.get_rest_version() |
| 300 |
if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version): |
| 301 |
module.fail_json( |
| 302 |
msg="FlashArray REST version not supported. " |
| 303 |
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION) |
| 304 |
) |
| 305 |
state = module.params["state"] |
| 306 |
|
| 307 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 308 |
exists = bool( |
| 309 |
array.get_directory_exports( |
| 310 |
export_names=[module.params["name"]], |
| 311 |
context_names=[module.params["context"]], |
| 312 |
).status_code |
| 313 |
== 200 |
| 314 |
) |
| 315 |
else: |
| 316 |
exists = bool( |
| 317 |
array.get_directory_exports( |
| 318 |
export_names=[module.params["name"]] |
| 319 |
).status_code |
| 320 |
== 200 |
| 321 |
) |
| 322 |
|
| 323 |
if state == "present": |
| 324 |
create_export(module, array) |
| 325 |
elif state == "absent" and exists: |
| 326 |
delete_export(module, array) |
| 327 |
|
| 328 |
module.exit_json(changed=False) |
| 329 |
|
| 330 |
|
| 331 |
if __name__ == "__main__": |
| 332 |
main() |