| 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 - Manage Grid Proxy Settings""" |
| 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_proxy_settings |
| 22 |
short_description: NetApp StorageGRID manage proxy settings for the grid. |
| 23 |
extends_documentation_fragment: |
| 24 |
- netapp.storagegrid.netapp.sg |
| 25 |
version_added: '21.15.0' |
| 26 |
author: NetApp Ansible Team (@aamirs) <[email protected]> |
| 27 |
description: |
| 28 |
- Update NetApp StorageGRID Proxy settings configuration. |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Proxy settings should be present. |
| 33 |
choices: ['present'] |
| 34 |
default: 'present' |
| 35 |
type: str |
| 36 |
enable: |
| 37 |
description: |
| 38 |
- Enable use of the Admin Node proxy. |
| 39 |
type: bool |
| 40 |
host_name: |
| 41 |
description: |
| 42 |
- The IP address or hostname of the Admin Node proxy. |
| 43 |
type: str |
| 44 |
host_port: |
| 45 |
description: |
| 46 |
- Port for the Admin Node proxy. |
| 47 |
type: int |
| 48 |
username: |
| 49 |
description: |
| 50 |
- Username for the Admin Node proxy. |
| 51 |
type: str |
| 52 |
password: |
| 53 |
description: |
| 54 |
- Password for the Admin Node proxy. |
| 55 |
type: str |
| 56 |
ca_bundle: |
| 57 |
description: |
| 58 |
- CA certificate bundle in concatenated PEM-encoding for TLS enabled connection; null when TLS is disabled. |
| 59 |
type: str |
| 60 |
proxy: |
| 61 |
description: |
| 62 |
- The address of the Storage Node proxy to use for external requests. Can be either protocol://hostname:port or |
| 63 |
protocol://ip:port, where protocol is one of http or socks5. |
| 64 |
type: str |
| 65 |
""" |
| 66 |
|
| 67 |
EXAMPLES = """ |
| 68 |
- name: update Proxy settings on StorageGRID |
| 69 |
na_sg_grid_proxy_settings: |
| 70 |
state: present |
| 71 |
enable: true |
| 72 |
host_name: "proxy.example.com" |
| 73 |
host_port: 8080 |
| 74 |
username: "MyProxyUsername" |
| 75 |
password: "MyProxyPassword" |
| 76 |
ca_bundle: "<CA certificates in concatenated PEM-encoding>" |
| 77 |
proxy: "http://myproxy.example.com:8080" |
| 78 |
""" |
| 79 |
|
| 80 |
RETURN = """ |
| 81 |
resp: |
| 82 |
description: Returns information about the StorageGRID proxy settings. |
| 83 |
returned: If state is 'present'. |
| 84 |
type: dict |
| 85 |
sample: { |
| 86 |
"enable": true, |
| 87 |
"hostname": "proxy.example.com", |
| 88 |
"hostPort": 8080, |
| 89 |
"username": "MyProxyUsername", |
| 90 |
"password": null, |
| 91 |
"caBundle": null, |
| 92 |
"proxy": "http://myproxy.example.com:8080" |
| 93 |
} |
| 94 |
""" |
| 95 |
|
| 96 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 97 |
from ansible.module_utils.basic import AnsibleModule |
| 98 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 99 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 100 |
|
| 101 |
|
| 102 |
class SgGridProxySetting(object): |
| 103 |
""" |
| 104 |
modify proxy settings for StorageGRID |
| 105 |
""" |
| 106 |
|
| 107 |
def __init__(self): |
| 108 |
""" |
| 109 |
Parse arguments, setup state variables, |
| 110 |
check parameters and ensure request module is installed |
| 111 |
""" |
| 112 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 113 |
self.argument_spec.update( |
| 114 |
dict( |
| 115 |
state=dict(type="str", choices=["present"], default="present"), |
| 116 |
enable=dict(required=False, type="bool"), |
| 117 |
host_name=dict(required=False, type="str"), |
| 118 |
host_port=dict(required=False, type="int"), |
| 119 |
username=dict(required=False, type="str"), |
| 120 |
password=dict(required=False, type="str", no_log=True), |
| 121 |
ca_bundle=dict(required=False, type="str"), |
| 122 |
proxy=dict(required=False, type="str") |
| 123 |
) |
| 124 |
) |
| 125 |
|
| 126 |
self.module = AnsibleModule( |
| 127 |
argument_spec=self.argument_spec, |
| 128 |
required_if=[("state", "present", ["host_name", "host_port", "proxy"])], |
| 129 |
supports_check_mode=True, |
| 130 |
) |
| 131 |
|
| 132 |
self.na_helper = NetAppModule() |
| 133 |
|
| 134 |
# set up state variables |
| 135 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 136 |
# Calling generic SG rest_api class |
| 137 |
self.rest_api = SGRestAPI(self.module) |
| 138 |
# Get API version |
| 139 |
self.rest_api.get_sg_product_version() |
| 140 |
self.api_version = self.rest_api.get_api_version() |
| 141 |
|
| 142 |
# Checking for the parameters passed and create new parameters list |
| 143 |
self.data = {} |
| 144 |
self.storage_data = {} |
| 145 |
if self.parameters.get("host_name"): |
| 146 |
self.data["hostname"] = self.parameters["host_name"] |
| 147 |
if self.parameters.get("host_port"): |
| 148 |
self.data["hostPort"] = self.parameters["host_port"] |
| 149 |
if self.parameters.get("ca_bundle"): |
| 150 |
self.data["caBundle"] = self.parameters["ca_bundle"] |
| 151 |
if self.parameters.get("enable") is not None: |
| 152 |
self.data["enable"] = self.parameters["enable"] |
| 153 |
if self.parameters.get("username"): |
| 154 |
self.data["username"] = self.parameters["username"] |
| 155 |
if self.parameters.get("proxy"): |
| 156 |
self.storage_data["proxy"] = self.parameters["proxy"] |
| 157 |
|
| 158 |
def get_admin_proxy(self): |
| 159 |
# Check if tenant account exists |
| 160 |
# Return tenant account info if found, or None |
| 161 |
api = "api/%s/private/admin-proxy" % self.api_version |
| 162 |
|
| 163 |
response, error = self.rest_api.get(api) |
| 164 |
if error: |
| 165 |
self.module.fail_json(msg=error) |
| 166 |
|
| 167 |
return response["data"] |
| 168 |
|
| 169 |
def update_admin_proxy(self): |
| 170 |
api = "api/%s/private/admin-proxy" % self.api_version |
| 171 |
response, error = self.rest_api.put(api, self.data) |
| 172 |
if error: |
| 173 |
self.module.fail_json(msg=error) |
| 174 |
return response["data"] |
| 175 |
|
| 176 |
def get_storage_proxy(self): |
| 177 |
# Check if tenant account exists |
| 178 |
# Return tenant account info if found, or None |
| 179 |
api = "api/%s/private/storage-proxy" % self.api_version |
| 180 |
|
| 181 |
response, error = self.rest_api.get(api) |
| 182 |
if error: |
| 183 |
self.module.fail_json(msg=error) |
| 184 |
|
| 185 |
return response["data"] |
| 186 |
|
| 187 |
def update_storage_proxy(self): |
| 188 |
api = "api/%s/private/storage-proxy" % self.api_version |
| 189 |
response, error = self.rest_api.put(api, self.storage_data) |
| 190 |
if error: |
| 191 |
self.module.fail_json(msg=error) |
| 192 |
|
| 193 |
return response["data"] |
| 194 |
|
| 195 |
def apply(self): |
| 196 |
''' Apply Proxy settings ''' |
| 197 |
|
| 198 |
update_storage = False |
| 199 |
update_admin = False |
| 200 |
|
| 201 |
storage_proxy = self.get_storage_proxy() |
| 202 |
admin_proxy = self.get_admin_proxy() |
| 203 |
|
| 204 |
cd_action = {"storage_proxy": None, "admin_proxy": None} |
| 205 |
|
| 206 |
if storage_proxy: |
| 207 |
cd_action["storage_proxy"] = self.na_helper.get_cd_action(storage_proxy, self.parameters) |
| 208 |
|
| 209 |
if admin_proxy: |
| 210 |
cd_action["admin_proxy"] = self.na_helper.get_cd_action(admin_proxy, self.parameters) |
| 211 |
|
| 212 |
if cd_action["storage_proxy"] is None and self.parameters["state"] == "present": |
| 213 |
update_storage = False |
| 214 |
update_storage = self.na_helper.get_modified_attributes(storage_proxy, self.storage_data) |
| 215 |
update_storage = True |
| 216 |
|
| 217 |
if cd_action["admin_proxy"] is None and self.parameters["state"] == "present": |
| 218 |
update_admin = False |
| 219 |
|
| 220 |
if admin_proxy.get("password") == self.data.get("password"): |
| 221 |
admin_proxy.pop("password", None) |
| 222 |
|
| 223 |
update_admin = self.na_helper.get_modified_attributes(admin_proxy, self.data) |
| 224 |
update_admin = True |
| 225 |
|
| 226 |
result_message = "" |
| 227 |
resp_data = {} |
| 228 |
|
| 229 |
if self.na_helper.changed: |
| 230 |
if self.module.check_mode: |
| 231 |
pass |
| 232 |
else: |
| 233 |
if update_storage: |
| 234 |
storage_resp = self.update_storage_proxy() |
| 235 |
if storage_resp: |
| 236 |
resp_data.update(storage_resp) |
| 237 |
|
| 238 |
if update_admin: |
| 239 |
admin_resp = self.update_admin_proxy() |
| 240 |
if admin_resp: |
| 241 |
resp_data.update(admin_resp) |
| 242 |
result_message = "proxy settings updated successfully." |
| 243 |
|
| 244 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 245 |
|
| 246 |
|
| 247 |
def main(): |
| 248 |
""" |
| 249 |
Main function |
| 250 |
""" |
| 251 |
na_sg_grid_proxy_settings = SgGridProxySetting() |
| 252 |
na_sg_grid_proxy_settings.apply() |
| 253 |
|
| 254 |
|
| 255 |
if __name__ == "__main__": |
| 256 |
main() |