| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2020, 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 DNS Servers""" |
| 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_dns |
| 22 |
short_description: NetApp StorageGRID manage external DNS servers for the grid. |
| 23 |
extends_documentation_fragment: |
| 24 |
- netapp.storagegrid.netapp.sg |
| 25 |
version_added: '20.6.0' |
| 26 |
author: NetApp Ansible Team (@joshedmonds) <[email protected]> |
| 27 |
description: |
| 28 |
- Update NetApp StorageGRID DNS addresses. |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Whether the specified DNS address should exist or not. |
| 33 |
- Required for all operations. |
| 34 |
type: str |
| 35 |
choices: ['present'] |
| 36 |
default: present |
| 37 |
dns_servers: |
| 38 |
description: |
| 39 |
- List of comma separated DNS Addresses to be updated or delete. |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
required: true |
| 43 |
""" |
| 44 |
|
| 45 |
EXAMPLES = """ |
| 46 |
- name: update DNS servers on StorageGRID |
| 47 |
netapp.storagegrid.na_sg_grid_dns: |
| 48 |
api_url: "https://<storagegrid-endpoint-url>" |
| 49 |
auth_token: "storagegrid-auth-token" |
| 50 |
validate_certs: false |
| 51 |
state: present |
| 52 |
dns_servers: "x.x.x.x,xxx.xxx.xxx.xxx" |
| 53 |
""" |
| 54 |
|
| 55 |
RETURN = """ |
| 56 |
resp: |
| 57 |
description: Returns information about the configured DNS servers. |
| 58 |
returned: success |
| 59 |
type: list |
| 60 |
elements: str |
| 61 |
sample: ["8.8.8.8", "8.8.4.4"] |
| 62 |
""" |
| 63 |
|
| 64 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 65 |
from ansible.module_utils.basic import AnsibleModule |
| 66 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 67 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 68 |
|
| 69 |
|
| 70 |
class SgGridDns(object): |
| 71 |
""" |
| 72 |
Create, modify and delete DNS entries for StorageGRID |
| 73 |
""" |
| 74 |
|
| 75 |
def __init__(self): |
| 76 |
""" |
| 77 |
Parse arguments, setup state variables, |
| 78 |
check parameters and ensure request module is installed |
| 79 |
""" |
| 80 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 81 |
self.argument_spec.update( |
| 82 |
dict( |
| 83 |
state=dict(required=False, type="str", choices=["present"], default="present"), |
| 84 |
dns_servers=dict(required=True, type="list", elements="str"), |
| 85 |
) |
| 86 |
) |
| 87 |
|
| 88 |
self.module = AnsibleModule( |
| 89 |
argument_spec=self.argument_spec, |
| 90 |
# required_if=[("state", "present", ["state", "name", "protocol"])], |
| 91 |
supports_check_mode=True, |
| 92 |
) |
| 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 = self.parameters["dns_servers"] |
| 106 |
|
| 107 |
def get_grid_dns(self): |
| 108 |
# Check if tenant account exists |
| 109 |
# Return tenant account info if found, or None |
| 110 |
api = "api/%s/grid/dns-servers" % self.api_version |
| 111 |
|
| 112 |
response, error = self.rest_api.get(api) |
| 113 |
|
| 114 |
if error: |
| 115 |
self.module.fail_json(msg=error) |
| 116 |
|
| 117 |
return response["data"] |
| 118 |
|
| 119 |
def update_grid_dns(self): |
| 120 |
api = "api/%s/grid/dns-servers" % self.api_version |
| 121 |
|
| 122 |
response, error = self.rest_api.put(api, self.data) |
| 123 |
if error: |
| 124 |
self.module.fail_json(msg=error) |
| 125 |
|
| 126 |
return response["data"] |
| 127 |
|
| 128 |
def apply(self): |
| 129 |
""" |
| 130 |
Perform pre-checks, call functions and exit |
| 131 |
""" |
| 132 |
grid_dns = self.get_grid_dns() |
| 133 |
|
| 134 |
cd_action = self.na_helper.get_cd_action(grid_dns, self.parameters["dns_servers"]) |
| 135 |
|
| 136 |
if cd_action is None and self.parameters["state"] == "present": |
| 137 |
# let's see if we need to update parameters |
| 138 |
update = False |
| 139 |
|
| 140 |
dns_diff = [i for i in self.data + grid_dns if i not in self.data or i not in grid_dns] |
| 141 |
if dns_diff: |
| 142 |
update = True |
| 143 |
|
| 144 |
if update: |
| 145 |
self.na_helper.changed = True |
| 146 |
result_message = "" |
| 147 |
resp_data = grid_dns |
| 148 |
if self.na_helper.changed: |
| 149 |
if self.module.check_mode: |
| 150 |
pass |
| 151 |
else: |
| 152 |
resp_data = self.update_grid_dns() |
| 153 |
result_message = "Grid DNS updated" |
| 154 |
|
| 155 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 156 |
|
| 157 |
|
| 158 |
def main(): |
| 159 |
""" |
| 160 |
Main function |
| 161 |
""" |
| 162 |
na_sg_grid_dns = SgGridDns() |
| 163 |
na_sg_grid_dns.apply() |
| 164 |
|
| 165 |
|
| 166 |
if __name__ == "__main__": |
| 167 |
main() |