| 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_file |
| 20 |
version_added: '1.22.0' |
| 21 |
short_description: Manage FlashArray File Copies |
| 22 |
description: |
| 23 |
- Copy FlashArray File from one filesystem to another |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
source_file: |
| 28 |
description: |
| 29 |
- Name of the file to copy |
| 30 |
- Include full path from the perspective of the source managed directory |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
source_dir: |
| 34 |
description: |
| 35 |
- Name of the source managed directory containing the source file to be copied |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
target_file: |
| 39 |
description: |
| 40 |
- Name of the file to copy to |
| 41 |
- Include full path from the perspective of the target managed directory |
| 42 |
- If not provided the file will be copied to the relative path specified by I(name) |
| 43 |
type: str |
| 44 |
target_dir: |
| 45 |
description: |
| 46 |
- Name of the target managed directory containing the source file to be copied |
| 47 |
- If not provided will use managed directory specified by I(source_dir) |
| 48 |
type: str |
| 49 |
overwrite: |
| 50 |
description: |
| 51 |
- Define whether to overwrite an existing target file |
| 52 |
type: bool |
| 53 |
default: false |
| 54 |
extends_documentation_fragment: |
| 55 |
- purestorage.flasharray.purestorage.fa |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = r""" |
| 59 |
- name: Copy a file from dir foo to dir bar |
| 60 |
purestorage.flasharray.purefa_file: |
| 61 |
source_file: "/directory1/file1" |
| 62 |
source_dir: "fs1:root" |
| 63 |
target_file: "/diff_dir/file1" |
| 64 |
target_dir: "fs1:root" |
| 65 |
fa_url: 10.10.10.2 |
| 66 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 67 |
|
| 68 |
- name: Copy a file in a directory to the same directory with a different name |
| 69 |
purestorage.flasharray.purefa_file: |
| 70 |
source_file: "/directory1/file1" |
| 71 |
source_dir: "fs1:root" |
| 72 |
target_file: "/directory_1/file2" |
| 73 |
fa_url: 10.10.10.2 |
| 74 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 75 |
|
| 76 |
- name: Copy a file in a directory to an existing file with overwrite |
| 77 |
purestorage.flasharray.purefa_file: |
| 78 |
source_file: "/directory1/file1" |
| 79 |
source_dir: "fs1:root" |
| 80 |
target_file: "/diff_dir/file1" |
| 81 |
target_dir: "fs2:root" |
| 82 |
overwrite: true |
| 83 |
fa_url: 10.10.10.2 |
| 84 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 85 |
""" |
| 86 |
|
| 87 |
RETURN = r""" |
| 88 |
""" |
| 89 |
|
| 90 |
HAS_PURESTORAGE = True |
| 91 |
try: |
| 92 |
from pypureclient import flasharray |
| 93 |
except ImportError: |
| 94 |
HAS_PURESTORAGE = False |
| 95 |
|
| 96 |
from ansible.module_utils.basic import AnsibleModule |
| 97 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 98 |
get_array, |
| 99 |
purefa_argument_spec, |
| 100 |
) |
| 101 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 102 |
LooseVersion, |
| 103 |
) |
| 104 |
|
| 105 |
MIN_REQUIRED_API_VERSION = "2.26" |
| 106 |
|
| 107 |
|
| 108 |
def _check_dirs(module, array): |
| 109 |
if array.get_directories(names=[module.params["source_dir"]]).status_code != 200: |
| 110 |
module.fail_json( |
| 111 |
msg="Source directory {0} does not exist".format( |
| 112 |
module.params["source_dir"] |
| 113 |
) |
| 114 |
) |
| 115 |
if array.get_directories(names=[module.params["target_dir"]]).status_code != 200: |
| 116 |
module.fail_json( |
| 117 |
msg="Target directory {0} does not exist".format( |
| 118 |
module.params["target_dir"] |
| 119 |
) |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
def main(): |
| 124 |
argument_spec = purefa_argument_spec() |
| 125 |
argument_spec.update( |
| 126 |
dict( |
| 127 |
overwrite=dict(type="bool", default=False), |
| 128 |
source_file=dict(type="str", required=True), |
| 129 |
source_dir=dict(type="str", required=True), |
| 130 |
target_file=dict(type="str"), |
| 131 |
target_dir=dict(type="str"), |
| 132 |
) |
| 133 |
) |
| 134 |
|
| 135 |
required_one_of = [["target_file", "target_dir"]] |
| 136 |
module = AnsibleModule( |
| 137 |
argument_spec, required_one_of=required_one_of, supports_check_mode=True |
| 138 |
) |
| 139 |
|
| 140 |
if not HAS_PURESTORAGE: |
| 141 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 142 |
|
| 143 |
array = get_array(module) |
| 144 |
api_version = array.get_rest_version() |
| 145 |
|
| 146 |
if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version): |
| 147 |
module.fail_json( |
| 148 |
msg="FlashArray REST version not supported. " |
| 149 |
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION) |
| 150 |
) |
| 151 |
|
| 152 |
if not module.params["target_file"]: |
| 153 |
module.params["target_file"] = module.params["source_file"] |
| 154 |
if not module.params["target_dir"]: |
| 155 |
module.params["target_dir"] = module.params["source_dir"] |
| 156 |
if ":" not in module.params["target_dir"]: |
| 157 |
module.fail_json(msg="Target Directory is not formatted correctly") |
| 158 |
if ":" not in module.params["source_dir"]: |
| 159 |
module.fail_json(msg="Source Directory is not formatted correctly") |
| 160 |
_check_dirs(module, array) |
| 161 |
changed = True |
| 162 |
if not module.check_mode: |
| 163 |
res = array.post_files( |
| 164 |
source_file=flasharray.FilePost( |
| 165 |
source=flasharray.ReferenceWithType( |
| 166 |
name=module.params["source_dir"], resource_type="directories" |
| 167 |
), |
| 168 |
source_path=module.params["source_file"], |
| 169 |
), |
| 170 |
overwrite=module.params["overwrite"], |
| 171 |
paths=[module.params["target_file"]], |
| 172 |
directory_names=[module.params["target_dir"]], |
| 173 |
) |
| 174 |
if res.status_code != 200: |
| 175 |
module.fail_json( |
| 176 |
msg="Failed to copy file. Error: {0}".format(res.errors[0].message) |
| 177 |
) |
| 178 |
|
| 179 |
module.exit_json(changed=changed) |
| 180 |
|
| 181 |
|
| 182 |
if __name__ == "__main__": |
| 183 |
main() |