| 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 endpoint domain name""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
ANSIBLE_METADATA = { |
| 14 |
"metadata_version": "1.0", |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
module: na_sg_grid_domain_name |
| 22 |
short_description: Configure endpoint domain name on StorageGRID. |
| 23 |
extends_documentation_fragment: |
| 24 |
- netapp.storagegrid.netapp.sg |
| 25 |
version_added: '21.15.0' |
| 26 |
author: NetApp Ansible Team (@vinaykus) <[email protected]> |
| 27 |
description: |
| 28 |
- Configure endpoint domain name on NetApp StorageGRID. |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- The endpoint domain name should be present. |
| 33 |
choices: ['present'] |
| 34 |
default: 'present' |
| 35 |
type: str |
| 36 |
domain_name: |
| 37 |
description: |
| 38 |
- List of domain names to be configured. |
| 39 |
required: true |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = """ |
| 45 |
- name: Configure endpoint domain name |
| 46 |
netapp.storagegrid.na_sg_grid_domain_name: |
| 47 |
api_url: "https://<storagegrid-endpoint-url>" |
| 48 |
auth_token: "storagegrid-auth-token" |
| 49 |
state: present |
| 50 |
validate_certs: false |
| 51 |
domain_name: |
| 52 |
- example1.com |
| 53 |
- example2.com |
| 54 |
""" |
| 55 |
|
| 56 |
RETURN = """ |
| 57 |
resp: |
| 58 |
description: Returns information about the StorageGRID domain name. |
| 59 |
returned: If state is 'present'. |
| 60 |
type: dict |
| 61 |
sample: [ |
| 62 |
"example1.com", |
| 63 |
"example2.com" |
| 64 |
] |
| 65 |
""" |
| 66 |
|
| 67 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 68 |
from ansible.module_utils.basic import AnsibleModule |
| 69 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 70 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 71 |
|
| 72 |
|
| 73 |
class SgDomainName: |
| 74 |
""" |
| 75 |
Configure endpoint domain name for StorageGRID |
| 76 |
""" |
| 77 |
|
| 78 |
def __init__(self): |
| 79 |
""" |
| 80 |
Parse arguments, setup state variables, |
| 81 |
check parameters and ensure request module is installed |
| 82 |
""" |
| 83 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 84 |
self.argument_spec.update( |
| 85 |
dict( |
| 86 |
state=dict(type="str", choices=["present"], default="present"), |
| 87 |
domain_name=dict(required=True, type="list", elements="str"), |
| 88 |
) |
| 89 |
) |
| 90 |
self.module = AnsibleModule( |
| 91 |
argument_spec=self.argument_spec, |
| 92 |
supports_check_mode=True |
| 93 |
) |
| 94 |
self.na_helper = NetAppModule() |
| 95 |
|
| 96 |
# set up state variables |
| 97 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 98 |
# Calling generic SG rest_api class |
| 99 |
self.rest_api = SGRestAPI(self.module) |
| 100 |
# Get API version |
| 101 |
self.rest_api.get_sg_product_version() |
| 102 |
self.api_version = self.rest_api.get_api_version() |
| 103 |
|
| 104 |
# Checking for the parameters passed and create new parameters list |
| 105 |
self.data = [] |
| 106 |
self.data = self.parameters["domain_name"] |
| 107 |
|
| 108 |
def get_domain_name(self): |
| 109 |
""" Get endpoint domain name """ |
| 110 |
api = "api/%s/grid/domain-names" % self.api_version |
| 111 |
response, error = self.rest_api.get(api) |
| 112 |
|
| 113 |
if not response or 'data' not in response: |
| 114 |
self.module.fail_json(msg="Invalid response from API") |
| 115 |
else: |
| 116 |
return response["data"] |
| 117 |
|
| 118 |
def update_domain_name(self): |
| 119 |
""" Update endpoint domain name """ |
| 120 |
api = "api/%s/grid/domain-names" % self.api_version |
| 121 |
response, error = self.rest_api.put(api, self.data) |
| 122 |
|
| 123 |
if error: |
| 124 |
self.module.fail_json(msg=error) |
| 125 |
else: |
| 126 |
return response["data"] |
| 127 |
|
| 128 |
def apply(self): |
| 129 |
''' Apply endpoint domain name ''' |
| 130 |
|
| 131 |
current_domain_name = self.get_domain_name() |
| 132 |
|
| 133 |
if self.parameters["state"] == "present": |
| 134 |
modify = False |
| 135 |
# Check if domain name is not present in the current domain name list |
| 136 |
if not current_domain_name and self.data: |
| 137 |
modify = True |
| 138 |
else: |
| 139 |
if set(self.data) != set(current_domain_name): |
| 140 |
modify = True |
| 141 |
|
| 142 |
if modify: |
| 143 |
self.na_helper.changed = True |
| 144 |
|
| 145 |
result_message = "" |
| 146 |
resp_data = current_domain_name |
| 147 |
if self.na_helper.changed: |
| 148 |
if self.module.check_mode: |
| 149 |
pass |
| 150 |
elif modify: |
| 151 |
resp_data = self.update_domain_name() |
| 152 |
result_message = "Endpoint domain name updated successfully." |
| 153 |
|
| 154 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 155 |
|
| 156 |
|
| 157 |
def main(): |
| 158 |
""" |
| 159 |
Main function |
| 160 |
""" |
| 161 |
na_sg_grid_domain_name = SgDomainName() |
| 162 |
na_sg_grid_domain_name.apply() |
| 163 |
|
| 164 |
|
| 165 |
if __name__ == "__main__": |
| 166 |
main() |