| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2021-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 Load Balancer Endpoints""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = """ |
| 13 |
module: na_sg_grid_gateway |
| 14 |
short_description: Manage Load balancer (gateway) endpoints on StorageGRID. |
| 15 |
extends_documentation_fragment: |
| 16 |
- netapp.storagegrid.netapp.sg |
| 17 |
version_added: '21.7.0' |
| 18 |
author: NetApp Ansible Team (@jkandati) <[email protected]> |
| 19 |
description: |
| 20 |
- Create or Update Load Balancer Endpoints on StorageGRID. |
| 21 |
- This module is idempotent if I(private_key) is not specified. |
| 22 |
- The module will match an existing config based on I(port) and I(display_name). |
| 23 |
- If multiple load balancer endpoints exist utilizing the same port and display name, use I(gateway_id) to select the intended endpoint. |
| 24 |
options: |
| 25 |
state: |
| 26 |
description: |
| 27 |
- Whether the specified load balancer endpoint should be configured. |
| 28 |
type: str |
| 29 |
choices: ['present', 'absent'] |
| 30 |
default: present |
| 31 |
gateway_id: |
| 32 |
description: |
| 33 |
- ID of the load balancer endpoint. |
| 34 |
type: str |
| 35 |
version_added: '21.9.0' |
| 36 |
display_name: |
| 37 |
description: |
| 38 |
- A display name for the configuration. |
| 39 |
- This parameter can be modified if I(gateway_id) is also specified. |
| 40 |
type: str |
| 41 |
port: |
| 42 |
description: |
| 43 |
- The TCP port to serve traffic on. |
| 44 |
- This parameter cannot be modified after the load balancer endpoint has been created. |
| 45 |
type: int |
| 46 |
required: true |
| 47 |
secure: |
| 48 |
description: |
| 49 |
- Whether the load balancer endpoint serves HTTP or HTTPS traffic. |
| 50 |
- This parameter cannot be modified after the load balancer endpoint has been created. |
| 51 |
type: bool |
| 52 |
default: true |
| 53 |
enable_ipv4: |
| 54 |
description: |
| 55 |
- Indicates whether to listen for connections on IPv4. |
| 56 |
type: bool |
| 57 |
default: true |
| 58 |
enable_ipv6: |
| 59 |
description: |
| 60 |
- Indicates whether to listen for connections on IPv6. |
| 61 |
type: bool |
| 62 |
default: true |
| 63 |
binding_mode: |
| 64 |
description: |
| 65 |
- Binding mode to restrict accessibility of the load balancer endpoint. |
| 66 |
- A binding mode other than I(global) requires StorageGRID 11.5 or greater. |
| 67 |
type: str |
| 68 |
choices: ['global', 'ha-groups', 'node-interfaces'] |
| 69 |
default: 'global' |
| 70 |
version_added: '21.9.0' |
| 71 |
ha_groups: |
| 72 |
description: |
| 73 |
- A set of StorageGRID HA Groups by name or UUID to bind the load balancer endpoint to. |
| 74 |
- Option is ignored unless I(binding_mode=ha-groups). |
| 75 |
type: list |
| 76 |
elements: str |
| 77 |
version_added: '21.9.0' |
| 78 |
node_interfaces: |
| 79 |
description: |
| 80 |
- A set of StorageGRID node interfaces to bind the load balancer endpoint to. |
| 81 |
type: list |
| 82 |
elements: dict |
| 83 |
suboptions: |
| 84 |
node: |
| 85 |
description: |
| 86 |
- Name of the StorageGRID node. |
| 87 |
type: str |
| 88 |
interface: |
| 89 |
description: |
| 90 |
- The interface to bind to. eth0 corresponds to the Grid Network, eth1 to the Admin Network, and eth2 to the Client Network. |
| 91 |
type: str |
| 92 |
version_added: '21.9.0' |
| 93 |
node_type: |
| 94 |
description: |
| 95 |
- A list of node types to bind to. |
| 96 |
- This Load Balancer Endpoint will listen on all nodes of this type. |
| 97 |
- The adminNode type includes both primary and non-primary admin nodes. |
| 98 |
type: str |
| 99 |
choices: ['adminNode', 'apiGatewayNode'] |
| 100 |
version_added: '21.14.0' |
| 101 |
enable_tenant_manager: |
| 102 |
description: |
| 103 |
- Whether to use the port bind for tenant manager traffic. |
| 104 |
type: bool |
| 105 |
version_added: '21.14.0' |
| 106 |
enable_grid_manager: |
| 107 |
description: |
| 108 |
- Whether to use the port bind for grid manager traffic. |
| 109 |
type: bool |
| 110 |
version_added: '21.14.0' |
| 111 |
default_service_type: |
| 112 |
description: |
| 113 |
- The type of service to proxy through the load balancer. |
| 114 |
type: str |
| 115 |
choices: ['s3', 'swift', 'management'] |
| 116 |
default: 's3' |
| 117 |
closed_on_untrusted_client_network: |
| 118 |
description: |
| 119 |
- Whether to close the port on an untrusted Client Network. |
| 120 |
type: bool |
| 121 |
required: false |
| 122 |
version_added: '21.16.0' |
| 123 |
server_certificate: |
| 124 |
description: |
| 125 |
- X.509 server certificate in PEM-encoding. |
| 126 |
- Omit if using default certificates. |
| 127 |
type: str |
| 128 |
required: false |
| 129 |
private_key: |
| 130 |
description: |
| 131 |
- Certficate private key in PEM-encoding. |
| 132 |
- Required if I(server_certificate) is not empty. |
| 133 |
type: str |
| 134 |
required: false |
| 135 |
ca_bundle: |
| 136 |
description: |
| 137 |
- Intermediate CA certificate bundle in concatenated PEM-encoding. |
| 138 |
- Omit when there is no intermediate CA. |
| 139 |
type: str |
| 140 |
required: false |
| 141 |
|
| 142 |
""" |
| 143 |
EXAMPLES = """ |
| 144 |
- name: Create and Upload Certificate to a Gateway Endpoint with global binding |
| 145 |
netapp.storagegrid.na_sg_grid_gateway: |
| 146 |
api_url: "https://<storagegrid-endpoint-url>" |
| 147 |
auth_token: "storagegrid-auth-token" |
| 148 |
displayName: "FabricPool Endpoint" |
| 149 |
port: 10443 |
| 150 |
secure: true |
| 151 |
enable_ipv4: true |
| 152 |
enable_ipv6: true |
| 153 |
default_service_type: "s3" |
| 154 |
server_certificate: | |
| 155 |
-----BEGIN CERTIFICATE----- |
| 156 |
MIIC6DCCAdACCQC7l4WukhKD0zANBgkqhkiG9w0BAQsFADA2..swCQYDVQQGEwJB |
| 157 |
BAMMHnNnYW4wMS5kZXYubWljcm9icmV3Lm5ldGFwcC5hdTCC..IwDQYJKoZIhvcN |
| 158 |
AQEBBQADggEPADCCAQoCggEBAMvjm9I35lmKcC7ITVL8+QiZ..lvdkbfZCUQrfdy |
| 159 |
71inP+XmPjs0rnkhICA9ItODteRcVlO+t7nDTfm7HgG0mJFk..m0ffyEYrcx24qu |
| 160 |
S7gXYQjRsJmrep1awoaCa20BMGuqK2WKI3IvZ7YiT22qkBqK..+hIFffX6u3Jy+B |
| 161 |
77pR6YcATtpMHW/AaOx+OX9l80dIRsRZKMDxYQ== |
| 162 |
-----END CERTIFICATE----- |
| 163 |
private_key: | |
| 164 |
-----BEGIN PRIVATE KEY----- |
| 165 |
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIB..DL45vSN+ZZinAu |
| 166 |
L25W0+cz1Oi69AKkI7d9nbFics2ay5+7o+4rKqf3en2R4MSx..vy+iDlOmATib5O |
| 167 |
x8TN5pJ9AgMBAAECggEADDLM8tHXXUoUFihzv+BUwff8p8Yc..cXFcSes+xTd5li |
| 168 |
po8lNsx/v2pQx4ByBkuaYLZGIEXOWS6gkp44xhIXgQKBgQD4..7862u5HLbmhrV3 |
| 169 |
vs8nC69b3QKBgQDacCD8d8JpwPbg8t2VjXM3UvdmgAaLUfU7..DWV+W3jqzmDOoN |
| 170 |
zWVgPbPNj0UmzvLDbgxLoxe77wjn2BHsAJVAfJ9VeQKBgGqF..gYO+wHR8lJUoa5 |
| 171 |
ZEe8Upy2oBtvND/0dnwO2ym2FGsBJN0Gr4NKdG5vkzLsthKk..Rm0ikwEUOUZQKE |
| 172 |
K8J5yEVeo9K2v3wggtq8fYn6 |
| 173 |
-----END PRIVATE KEY----- |
| 174 |
validate_certs: false |
| 175 |
|
| 176 |
- name: Create a HTTP Gateway Endpoint with HA Group Binding |
| 177 |
netapp.storagegrid.na_sg_grid_gateway: |
| 178 |
api_url: "https://<storagegrid-endpoint-url>" |
| 179 |
auth_token: "storagegrid-auth-token" |
| 180 |
displayName: "App Endpoint 1" |
| 181 |
port: 10501 |
| 182 |
secure: false |
| 183 |
enable_ipv4: true |
| 184 |
enable_ipv6: true |
| 185 |
default_service_type: "s3" |
| 186 |
binding_mode: ha-groups |
| 187 |
ha_groups: site1_ha_group |
| 188 |
validate_certs: false |
| 189 |
|
| 190 |
- name: Create a HTTP Gateway Endpoint with Node Interface Binding |
| 191 |
netapp.storagegrid.na_sg_grid_gateway: |
| 192 |
api_url: "https://<storagegrid-endpoint-url>" |
| 193 |
auth_token: "storagegrid-auth-token" |
| 194 |
displayName: "App Endpoint 2" |
| 195 |
port: 10502 |
| 196 |
secure: false |
| 197 |
enable_ipv4: true |
| 198 |
enable_ipv6: true |
| 199 |
default_service_type: "s3" |
| 200 |
binding_mode: node-interfaces |
| 201 |
node_interfaecs: |
| 202 |
- node: SITE1_ADM1 |
| 203 |
interface: eth2 |
| 204 |
- node: SITE2_ADM1 |
| 205 |
interface: eth2 |
| 206 |
validate_certs: false |
| 207 |
|
| 208 |
- name: Delete Gateway Endpoint |
| 209 |
netapp.storagegrid.na_sg_grid_gateway: |
| 210 |
api_url: "https://<storagegrid-endpoint-url>" |
| 211 |
auth_token: "storagegrid-auth-token" |
| 212 |
displayName: "App Endpoint 2" |
| 213 |
port: 10502 |
| 214 |
default_service_type: "s3" |
| 215 |
validate_certs: false |
| 216 |
""" |
| 217 |
|
| 218 |
RETURN = """ |
| 219 |
resp: |
| 220 |
description: Returns information about the StorageGRID Load Balancer Endpoint. |
| 221 |
returned: success |
| 222 |
type: dict |
| 223 |
sample: { |
| 224 |
"id": "ffffffff-ffff-ffff-ffff-ffffffffffff", |
| 225 |
"displayName": "ansibletest-secure", |
| 226 |
"enableIPv4": True, |
| 227 |
"enableIPv6": True, |
| 228 |
"port": 10443, |
| 229 |
"secure": True, |
| 230 |
"accountId": "0", |
| 231 |
"defaultServiceType": "s3", |
| 232 |
"certSource": "plaintext", |
| 233 |
"plaintextCertData": { |
| 234 |
"serverCertificateEncoded": "-----BEGIN CERTIFICATE-----MIIC6DCCAdACCQC7l4WukhKD0zANBgkqhkiG9w0BAQsFADA2MQswCQYDVQQGE...-----END CERTIFICATE-----", |
| 235 |
"caBundleEncoded": "-----BEGIN CERTIFICATE-----MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELM...-----END CERTIFICATE-----", |
| 236 |
"metadata": {...} |
| 237 |
} |
| 238 |
} |
| 239 |
""" |
| 240 |
|
| 241 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 242 |
from ansible.module_utils.basic import AnsibleModule |
| 243 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 244 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 245 |
|
| 246 |
|
| 247 |
class SgGridGateway: |
| 248 |
""" |
| 249 |
Create, modify and delete Gateway entries for StorageGRID |
| 250 |
""" |
| 251 |
|
| 252 |
def __init__(self): |
| 253 |
""" |
| 254 |
Parse arguments, setup state variables, |
| 255 |
check parameters and ensure request module is installed |
| 256 |
""" |
| 257 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 258 |
self.argument_spec.update( |
| 259 |
dict( |
| 260 |
# Arguments for Creating Gateway Port |
| 261 |
state=dict(required=False, type="str", choices=["present", "absent"], default="present"), |
| 262 |
gateway_id=dict(required=False, type="str"), |
| 263 |
display_name=dict(required=False, type="str"), |
| 264 |
port=dict(required=True, type="int"), |
| 265 |
secure=dict(required=False, type="bool", default=True), |
| 266 |
enable_ipv4=dict(required=False, type="bool", default=True), |
| 267 |
enable_ipv6=dict(required=False, type="bool", default=True), |
| 268 |
enable_tenant_manager=dict(required=False, type="bool"), |
| 269 |
enable_grid_manager=dict(required=False, type="bool"), |
| 270 |
closed_on_untrusted_client_network=dict(required=False, type="bool"), |
| 271 |
binding_mode=dict( |
| 272 |
required=False, type="str", choices=["global", "ha-groups", "node-interfaces"], default="global" |
| 273 |
), |
| 274 |
ha_groups=dict(required=False, type="list", elements="str"), |
| 275 |
node_interfaces=dict( |
| 276 |
required=False, |
| 277 |
type="list", |
| 278 |
elements="dict", |
| 279 |
options=dict( |
| 280 |
node=dict(required=False, type="str"), |
| 281 |
interface=dict(required=False, type="str"), |
| 282 |
), |
| 283 |
), |
| 284 |
node_type=dict(required=False, type="str", choices=["adminNode", "apiGatewayNode"]), |
| 285 |
# Arguments for setting Gateway Virtual Server |
| 286 |
default_service_type=dict(required=False, type="str", choices=["s3", "swift", "management"], default="s3"), |
| 287 |
server_certificate=dict(required=False, type="str"), |
| 288 |
ca_bundle=dict(required=False, type="str"), |
| 289 |
private_key=dict(required=False, type="str", no_log=True), |
| 290 |
) |
| 291 |
) |
| 292 |
|
| 293 |
parameter_map_gateway = { |
| 294 |
"gateway_id": "id", |
| 295 |
"display_name": "displayName", |
| 296 |
"port": "port", |
| 297 |
"secure": "secure", |
| 298 |
"enable_ipv4": "enableIPv4", |
| 299 |
"enable_ipv6": "enableIPv6", |
| 300 |
} |
| 301 |
parameter_map_server = { |
| 302 |
"server_certificate": "serverCertificateEncoded", |
| 303 |
"ca_bundle": "caBundleEncoded", |
| 304 |
"private_key": "privateKeyEncoded", |
| 305 |
} |
| 306 |
self.module = AnsibleModule( |
| 307 |
argument_spec=self.argument_spec, |
| 308 |
required_if=[ |
| 309 |
("state", "present", ["display_name"]), |
| 310 |
("default_service_type", "management", ["enable_tenant_manager", "enable_grid_manager"], True), |
| 311 |
], |
| 312 |
supports_check_mode=True, |
| 313 |
) |
| 314 |
|
| 315 |
self.na_helper = NetAppModule() |
| 316 |
|
| 317 |
# set up state variables |
| 318 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 319 |
# Calling generic SG rest_api class |
| 320 |
self.rest_api = SGRestAPI(self.module) |
| 321 |
# Get API version |
| 322 |
self.rest_api.get_sg_product_version() |
| 323 |
self.api_version = self.rest_api.get_api_version() |
| 324 |
|
| 325 |
# Checking for the parameters passed and create new parameters list |
| 326 |
|
| 327 |
# Parameters for creating a new gateway port configuration |
| 328 |
self.data_gateway = {} |
| 329 |
self.data_gateway["accountId"] = "0" |
| 330 |
|
| 331 |
for k in parameter_map_gateway.keys(): |
| 332 |
if self.parameters.get(k) is not None: |
| 333 |
self.data_gateway[parameter_map_gateway[k]] = self.parameters[k] |
| 334 |
|
| 335 |
# Parameters for setting a gateway virtual server configuration for a gateway port |
| 336 |
self.data_server = {} |
| 337 |
self.data_server["defaultServiceType"] = self.parameters["default_service_type"] |
| 338 |
|
| 339 |
if self.parameters["secure"]: |
| 340 |
self.data_server["plaintextCertData"] = {} |
| 341 |
self.data_server["certSource"] = "plaintext" |
| 342 |
|
| 343 |
for k in parameter_map_server.keys(): |
| 344 |
if self.parameters.get(k) is not None: |
| 345 |
self.data_server["plaintextCertData"][parameter_map_server[k]] = self.parameters[k] |
| 346 |
|
| 347 |
if self.parameters["binding_mode"] != "global": |
| 348 |
self.rest_api.fail_if_not_sg_minimum_version("non-global binding mode", 11, 5) |
| 349 |
if self.parameters["default_service_type"] == "management": |
| 350 |
self.rest_api.fail_if_not_sg_minimum_version("management default service type", 11, 8) |
| 351 |
|
| 352 |
self.data_gateway["pinTargets"] = { |
| 353 |
"haGroups": [], |
| 354 |
"nodeInterfaces": [], |
| 355 |
"nodeTypes": [], |
| 356 |
} |
| 357 |
if self.parameters.get("node_type") is not None: |
| 358 |
self.data_gateway["pinTargets"]["nodeTypes"] = [self.parameters["node_type"]] |
| 359 |
if self.parameters["binding_mode"] == "ha-groups": |
| 360 |
self.data_gateway["pinTargets"]["haGroups"] = self.build_ha_group_list() |
| 361 |
elif self.parameters["binding_mode"] == "node-interfaces": |
| 362 |
self.data_gateway["pinTargets"]["nodeInterfaces"] = self.build_node_interface_list() |
| 363 |
|
| 364 |
# Parameters for configuring Management Interface |
| 365 |
if self.parameters["default_service_type"] == "management": |
| 366 |
self.data_gateway["managementInterfaces"] = { |
| 367 |
"enableTenantManager": self.parameters.get("enable_tenant_manager"), |
| 368 |
"enableGridManager": self.parameters.get("enable_grid_manager") |
| 369 |
} |
| 370 |
self.data_gateway["closedOnUntrustedClientNetwork"] = self.parameters.get("closed_on_untrusted_client_network") |
| 371 |
# If no binding targets are specified, default to adminNode |
| 372 |
if not any(self.data_gateway["pinTargets"].values()): |
| 373 |
self.data_gateway["pinTargets"]["nodeTypes"] = ["adminNode"] |
| 374 |
|
| 375 |
def build_ha_group_list(self): |
| 376 |
ha_group_ids = [] |
| 377 |
|
| 378 |
api = "api/%s/private/ha-groups" % self.api_version |
| 379 |
ha_groups, error = self.rest_api.get(api) |
| 380 |
if error: |
| 381 |
self.module.fail_json(msg=error) |
| 382 |
|
| 383 |
for param in self.parameters["ha_groups"]: |
| 384 |
ha_group = next( |
| 385 |
(item for item in ha_groups["data"] if (item["name"] == param or item["id"] == param)), None |
| 386 |
) |
| 387 |
if ha_group is not None: |
| 388 |
ha_group_ids.append(ha_group["id"]) |
| 389 |
else: |
| 390 |
self.module.fail_json(msg="HA Group '%s' is invalid" % param) |
| 391 |
|
| 392 |
return ha_group_ids |
| 393 |
|
| 394 |
def build_node_interface_list(self): |
| 395 |
node_interfaces = [] |
| 396 |
|
| 397 |
api = "api/%s/grid/node-health" % self.api_version |
| 398 |
nodes, error = self.rest_api.get(api) |
| 399 |
|
| 400 |
if error: |
| 401 |
self.module.fail_json(msg=error) |
| 402 |
|
| 403 |
for node_interface in self.parameters["node_interfaces"]: |
| 404 |
node_dict = {} |
| 405 |
node = next((item for item in nodes["data"] if item["name"] == node_interface["node"]), None) |
| 406 |
if node is not None: |
| 407 |
node_dict["nodeId"] = node["id"] |
| 408 |
node_dict["interface"] = node_interface["interface"] |
| 409 |
node_interfaces.append(node_dict) |
| 410 |
else: |
| 411 |
self.module.fail_json(msg="Node '%s' is invalid" % node_interface["node"]) |
| 412 |
|
| 413 |
return node_interfaces |
| 414 |
|
| 415 |
def get_grid_gateway_config(self, gateway_id): |
| 416 |
api = "api/%s/private/gateway-configs/%s" % (self.api_version, gateway_id) |
| 417 |
response, error = self.rest_api.get(api) |
| 418 |
|
| 419 |
if error: |
| 420 |
self.module.fail_json(msg=error) |
| 421 |
|
| 422 |
gateway = response["data"] |
| 423 |
gateway_config = self.get_grid_gateway_server_config(gateway["id"]) |
| 424 |
|
| 425 |
return gateway, gateway_config |
| 426 |
|
| 427 |
def get_grid_gateway_server_config(self, gateway_id): |
| 428 |
api = "api/%s/private/gateway-configs/%s/server-config" % (self.api_version, gateway_id) |
| 429 |
response, error = self.rest_api.get(api) |
| 430 |
|
| 431 |
if error: |
| 432 |
self.module.fail_json(msg=error) |
| 433 |
|
| 434 |
return response["data"] |
| 435 |
|
| 436 |
def get_grid_gateway_ports(self, target_port): |
| 437 |
|
| 438 |
configured_ports = [] |
| 439 |
gateway = {} |
| 440 |
gateway_config = {} |
| 441 |
|
| 442 |
api = "api/%s/private/gateway-configs" % self.api_version |
| 443 |
response, error = self.rest_api.get(api) |
| 444 |
|
| 445 |
if error: |
| 446 |
self.module.fail_json(msg=error) |
| 447 |
|
| 448 |
grid_gateway_ports = response["data"] |
| 449 |
|
| 450 |
# Get only a list of used ports |
| 451 |
configured_ports = [data["port"] for data in grid_gateway_ports] |
| 452 |
|
| 453 |
for index, port in enumerate(configured_ports): |
| 454 |
# if port already exists then get gateway ID and get the gateway port server configs |
| 455 |
if target_port == port and grid_gateway_ports[index]["displayName"] == self.parameters["display_name"]: |
| 456 |
gateway = grid_gateway_ports[index] |
| 457 |
gateway_config = self.get_grid_gateway_server_config(gateway["id"]) |
| 458 |
break |
| 459 |
|
| 460 |
return gateway, gateway_config |
| 461 |
|
| 462 |
def create_grid_gateway(self): |
| 463 |
api = "api/%s/private/gateway-configs" % self.api_version |
| 464 |
response, error = self.rest_api.post(api, self.data_gateway) |
| 465 |
|
| 466 |
if error: |
| 467 |
self.module.fail_json(msg=error) |
| 468 |
|
| 469 |
return response["data"] |
| 470 |
|
| 471 |
def delete_grid_gateway(self, gateway_id): |
| 472 |
api = "api/%s/private/gateway-configs/%s" % (self.api_version, gateway_id) |
| 473 |
self.data = None |
| 474 |
response, error = self.rest_api.delete(api, self.data) |
| 475 |
|
| 476 |
if error: |
| 477 |
self.module.fail_json(msg=error) |
| 478 |
|
| 479 |
def update_grid_gateway(self, gateway_id): |
| 480 |
api = "api/%s/private/gateway-configs/%s" % (self.api_version, gateway_id) |
| 481 |
response, error = self.rest_api.put(api, self.data_gateway) |
| 482 |
|
| 483 |
if error: |
| 484 |
self.module.fail_json(msg=error) |
| 485 |
|
| 486 |
return response["data"] |
| 487 |
|
| 488 |
def update_grid_gateway_server(self, gateway_id): |
| 489 |
api = "api/%s/private/gateway-configs/%s/server-config" % (self.api_version, gateway_id) |
| 490 |
response, error = self.rest_api.put(api, self.data_server) |
| 491 |
|
| 492 |
if error: |
| 493 |
self.module.fail_json(msg=error) |
| 494 |
|
| 495 |
return response["data"] |
| 496 |
|
| 497 |
def apply(self): |
| 498 |
gateway = None |
| 499 |
gateway_config = None |
| 500 |
|
| 501 |
update_gateway = False |
| 502 |
update_gateway_server = False |
| 503 |
|
| 504 |
if self.parameters.get("gateway_id"): |
| 505 |
gateway, gateway_config = self.get_grid_gateway_config(self.parameters["gateway_id"]) |
| 506 |
|
| 507 |
else: |
| 508 |
# Get list of all gateway port configurations |
| 509 |
gateway, gateway_config = self.get_grid_gateway_ports(self.data_gateway["port"]) |
| 510 |
|
| 511 |
cd_action = self.na_helper.get_cd_action(gateway.get("id"), self.parameters) |
| 512 |
|
| 513 |
if cd_action is None and self.parameters["state"] == "present": |
| 514 |
update = False |
| 515 |
|
| 516 |
if self.data_server.get("plaintextCertData"): |
| 517 |
if self.data_server["plaintextCertData"].get("privateKeyEncoded") is not None: |
| 518 |
update = True |
| 519 |
self.module.warn("This module is not idempotent when private_key is present.") |
| 520 |
|
| 521 |
if gateway_config.get("plaintextCertData"): |
| 522 |
# If certificate private key supplied, update |
| 523 |
if gateway_config["plaintextCertData"].get("metadata"): |
| 524 |
# remove metadata because we can't compare that |
| 525 |
del gateway_config["plaintextCertData"]["metadata"] |
| 526 |
|
| 527 |
# compare current and desired state |
| 528 |
# gateway config cannot be modified until StorageGRID 11.5 |
| 529 |
if self.rest_api.meets_sg_minimum_version(11, 5): |
| 530 |
update_gateway = self.na_helper.get_modified_attributes(gateway, self.data_gateway) |
| 531 |
update_gateway_server = self.na_helper.get_modified_attributes(gateway_config, self.data_server) |
| 532 |
|
| 533 |
if update: |
| 534 |
self.na_helper.changed = True |
| 535 |
|
| 536 |
result_message = "" |
| 537 |
resp_data = {} |
| 538 |
|
| 539 |
if self.na_helper.changed and not self.module.check_mode: |
| 540 |
if cd_action == "delete": |
| 541 |
self.delete_grid_gateway(gateway["id"]) |
| 542 |
result_message = "Load Balancer Gateway Port Deleted" |
| 543 |
|
| 544 |
elif cd_action == "create": |
| 545 |
resp_data = self.create_grid_gateway() |
| 546 |
gateway["id"] = resp_data["id"] |
| 547 |
resp_data_server = self.update_grid_gateway_server(gateway["id"]) |
| 548 |
resp_data.update(resp_data_server) |
| 549 |
result_message = "Load Balancer Gateway Port Created" |
| 550 |
|
| 551 |
else: |
| 552 |
resp_data = gateway |
| 553 |
if update_gateway: |
| 554 |
resp_data = self.update_grid_gateway(gateway["id"]) |
| 555 |
resp_data.update(gateway_config) |
| 556 |
|
| 557 |
if update_gateway_server: |
| 558 |
resp_data_server = self.update_grid_gateway_server(gateway["id"]) |
| 559 |
resp_data.update(resp_data_server) |
| 560 |
result_message = "Load Balancer Gateway Port Updated" |
| 561 |
|
| 562 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 563 |
|
| 564 |
|
| 565 |
def main(): |
| 566 |
""" |
| 567 |
Main function |
| 568 |
""" |
| 569 |
na_sg_grid_gateway = SgGridGateway() |
| 570 |
na_sg_grid_gateway.apply() |
| 571 |
|
| 572 |
|
| 573 |
if __name__ == "__main__": |
| 574 |
main() |