| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2025, NetApp Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
"""NetApp StorageGRID - Recovery Packages""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
ANSIBLE_METADATA = { |
| 14 |
"metadata_version": "1.1", |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
module: na_sg_grid_recovery_package |
| 22 |
short_description: Retrieve the recovery package from StorageGRID |
| 23 |
extends_documentation_fragment: |
| 24 |
- netapp.storagegrid.netapp.sg |
| 25 |
version_added: '21.16.0' |
| 26 |
author: NetApp Ansible Team (@aamirs) <[email protected]> |
| 27 |
description: |
| 28 |
- Create recovery package on NetApp StorageGRID. |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Whether the recovery package should be present or absent. |
| 33 |
choices: ['present', 'absent'] |
| 34 |
default: 'present' |
| 35 |
type: str |
| 36 |
passphrase: |
| 37 |
description: |
| 38 |
- the password used during maintenance procedures to make changes to the grid topology. |
| 39 |
type: str |
| 40 |
""" |
| 41 |
|
| 42 |
EXAMPLES = """ |
| 43 |
- name: Create StorageGRID recovery package |
| 44 |
netapp.storagegrid.na_sg_grid_recovery_package: |
| 45 |
api_url: "https://<storagegrid-endpoint-url>" |
| 46 |
auth_token: "storagegrid-auth-token" |
| 47 |
validate_certs: false |
| 48 |
state: present |
| 49 |
passphrase: "{{ storagegrid_passphrase }}" |
| 50 |
""" |
| 51 |
|
| 52 |
RETURN = """ |
| 53 |
resp: |
| 54 |
description: Returns information about the StorageGRID recovery package. |
| 55 |
returned: success |
| 56 |
type: dict |
| 57 |
sample: { |
| 58 |
"file_saved": "sgws-recovery-package-431636-rev1.zip" |
| 59 |
} |
| 60 |
""" |
| 61 |
|
| 62 |
import re |
| 63 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 64 |
from ansible.module_utils.basic import AnsibleModule |
| 65 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 66 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 67 |
|
| 68 |
|
| 69 |
class SgGridRecoveryPackage: |
| 70 |
""" |
| 71 |
Download StorageGRID Recovery package |
| 72 |
""" |
| 73 |
|
| 74 |
def __init__(self): |
| 75 |
""" |
| 76 |
Parse arguments, setup state variables, |
| 77 |
check parameters and ensure request module is installed |
| 78 |
""" |
| 79 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 80 |
self.argument_spec.update( |
| 81 |
dict( |
| 82 |
state=dict(required=False, type="str", choices=["present", "absent"], default="present"), |
| 83 |
passphrase=dict(type='str', required=False, no_log=True), |
| 84 |
) |
| 85 |
) |
| 86 |
|
| 87 |
self.module = AnsibleModule( |
| 88 |
argument_spec=self.argument_spec, |
| 89 |
required_if=[("state", "present", ["passphrase"])], |
| 90 |
supports_check_mode=True, |
| 91 |
) |
| 92 |
|
| 93 |
self.na_helper = NetAppModule() |
| 94 |
|
| 95 |
# set up state variables |
| 96 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 97 |
# Calling generic SG rest_api class |
| 98 |
self.rest_api = SGRestAPI(self.module) |
| 99 |
# Get API version |
| 100 |
self.rest_api.get_sg_product_version(api_root="grid") |
| 101 |
|
| 102 |
# Checking for the parameters passed and create new parameters list |
| 103 |
self.data = {} |
| 104 |
self.data["passphrase"] = self.parameters["passphrase"] |
| 105 |
|
| 106 |
def create_recovery_package(self): |
| 107 |
# Create recovery package |
| 108 |
api = "api/v3/grid/recovery-package" |
| 109 |
|
| 110 |
response, error = self.rest_api.post(api, self.data) |
| 111 |
|
| 112 |
if error: |
| 113 |
self.module.fail_json(msg=error) |
| 114 |
|
| 115 |
if not response: |
| 116 |
self.module.fail_json(msg="ERROR: Received empty response from API.") |
| 117 |
|
| 118 |
content_disposition = response.headers.get("Content-Disposition", "") |
| 119 |
match = re.search(r'filename="?([^"]+)"?', content_disposition) |
| 120 |
if match: |
| 121 |
file_name = match.group(1) |
| 122 |
|
| 123 |
with open(file_name, "wb") as f: |
| 124 |
f.write(response.content) |
| 125 |
|
| 126 |
return {"file_saved", file_name}, None |
| 127 |
|
| 128 |
def apply(self): |
| 129 |
""" |
| 130 |
Perform pre-checks, call functions and exit |
| 131 |
""" |
| 132 |
recovery_package = None |
| 133 |
|
| 134 |
cd_action = self.na_helper.get_cd_action(recovery_package, self.parameters) |
| 135 |
|
| 136 |
result_message = "" |
| 137 |
resp_data = None |
| 138 |
if self.na_helper.changed: |
| 139 |
if self.module.check_mode: |
| 140 |
pass |
| 141 |
|
| 142 |
if cd_action == "create": |
| 143 |
resp_data = self.create_recovery_package() |
| 144 |
result_message = "recovery package downloaded successfully" |
| 145 |
|
| 146 |
self.module.exit_json(changed=self.na_helper.changed, resp=resp_data, msg=result_message) |
| 147 |
|
| 148 |
|
| 149 |
def main(): |
| 150 |
""" |
| 151 |
Main function |
| 152 |
""" |
| 153 |
na_sg_grid_recovery_package = SgGridRecoveryPackage() |
| 154 |
na_sg_grid_recovery_package.apply() |
| 155 |
|
| 156 |
|
| 157 |
if __name__ == '__main__': |
| 158 |
main() |