| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2020-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
# This module implements the operations for ONTAP MCC Mediator. |
| 7 |
# The Mediator is supported for MCC IP configs from ONTAP 9.7 or later. |
| 8 |
# This module requires REST APIs for Mediator which is supported from |
| 9 |
# ONTAP 9.8 (DW) or later |
| 10 |
|
| 11 |
''' |
| 12 |
na_ontap_mcc_mediator |
| 13 |
''' |
| 14 |
|
| 15 |
from __future__ import absolute_import, division, print_function |
| 16 |
__metaclass__ = type |
| 17 |
|
| 18 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 19 |
'status': ['preview'], |
| 20 |
'supported_by': 'certified'} |
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
module: na_ontap_mcc_mediator |
| 24 |
short_description: NetApp ONTAP Add and Remove MetroCluster Mediator |
| 25 |
extends_documentation_fragment: |
| 26 |
- netapp.ontap.netapp.na_ontap_rest |
| 27 |
version_added: 20.9.0 |
| 28 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 29 |
description: |
| 30 |
- Add and remove ONTAP MCC Mediator |
| 31 |
options: |
| 32 |
state: |
| 33 |
choices: ['present', 'absent'] |
| 34 |
description: |
| 35 |
- "Whether MCCIP Mediator is present or not." |
| 36 |
default: present |
| 37 |
type: str |
| 38 |
|
| 39 |
mediator_address: |
| 40 |
description: |
| 41 |
- ip address of the mediator |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
|
| 45 |
mediator_user: |
| 46 |
description: |
| 47 |
- username of the mediator |
| 48 |
type: str |
| 49 |
required: true |
| 50 |
|
| 51 |
mediator_password: |
| 52 |
description: |
| 53 |
- password of the mediator |
| 54 |
type: str |
| 55 |
required: true |
| 56 |
|
| 57 |
''' |
| 58 |
|
| 59 |
EXAMPLES = """ |
| 60 |
- name: Add ONTAP MCCIP Mediator |
| 61 |
netapp.ontap.na_ontap_mcc_mediator: |
| 62 |
state: present |
| 63 |
hostname: "{{ netapp_hostname }}" |
| 64 |
username: "{{ netapp_username }}" |
| 65 |
password: "{{ netapp_password }}" |
| 66 |
mediator_address: mediator_ip |
| 67 |
mediator_user: metrocluster_admin |
| 68 |
mediator_password: metrocluster_password |
| 69 |
|
| 70 |
- name: Delete ONTAP MCCIP Mediator |
| 71 |
netapp.ontap.na_ontap_mcc_mediator: |
| 72 |
state: absent |
| 73 |
hostname: "{{ netapp_hostname }}" |
| 74 |
username: "{{ netapp_username }}" |
| 75 |
password: "{{ netapp_password }}" |
| 76 |
mediator_user: metrocluster_admin |
| 77 |
mediator_password: metrocluster_password |
| 78 |
""" |
| 79 |
|
| 80 |
RETURN = """ |
| 81 |
""" |
| 82 |
|
| 83 |
from ansible.module_utils.basic import AnsibleModule |
| 84 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 85 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 86 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 87 |
|
| 88 |
|
| 89 |
class NetAppOntapMccipMediator(object): |
| 90 |
""" |
| 91 |
Mediator object for Add/Remove/Display |
| 92 |
""" |
| 93 |
|
| 94 |
def __init__(self): |
| 95 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 96 |
self.argument_spec.update(dict( |
| 97 |
state=dict(required=False, choices=['present', 'absent'], default='present'), |
| 98 |
mediator_address=dict(required=True, type='str'), |
| 99 |
mediator_user=dict(required=True, type='str'), |
| 100 |
mediator_password=dict(required=True, type='str', no_log=True), |
| 101 |
)) |
| 102 |
|
| 103 |
self.module = AnsibleModule( |
| 104 |
argument_spec=self.argument_spec, |
| 105 |
supports_check_mode=True |
| 106 |
) |
| 107 |
|
| 108 |
self.na_helper = NetAppModule() |
| 109 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 110 |
self.rest_api = OntapRestAPI(self.module) |
| 111 |
self.use_rest = self.rest_api.is_rest() |
| 112 |
|
| 113 |
if not self.use_rest: |
| 114 |
self.module.fail_json(msg=self.rest_api.requires_ontap_9_6('na_ontap_mcc_mediator')) |
| 115 |
|
| 116 |
def add_mediator(self): |
| 117 |
""" |
| 118 |
Adds an ONTAP Mediator to MCC configuration |
| 119 |
""" |
| 120 |
api = 'cluster/mediators' |
| 121 |
params = { |
| 122 |
'ip_address': self.parameters['mediator_address'], |
| 123 |
'password': self.parameters['mediator_password'], |
| 124 |
'user': self.parameters['mediator_user'] |
| 125 |
} |
| 126 |
dummy, error = self.rest_api.post(api, params) |
| 127 |
if error: |
| 128 |
self.module.fail_json(msg=error) |
| 129 |
|
| 130 |
def remove_mediator(self, current_uuid): |
| 131 |
""" |
| 132 |
Removes the ONTAP Mediator from MCC configuration |
| 133 |
""" |
| 134 |
api = 'cluster/mediators/%s' % current_uuid |
| 135 |
params = { |
| 136 |
'ip_address': self.parameters['mediator_address'], |
| 137 |
'password': self.parameters['mediator_password'], |
| 138 |
'user': self.parameters['mediator_user'] |
| 139 |
} |
| 140 |
dummy, error = self.rest_api.delete(api, params) |
| 141 |
if error: |
| 142 |
self.module.fail_json(msg=error) |
| 143 |
|
| 144 |
def get_mediator(self): |
| 145 |
""" |
| 146 |
Determine if the MCC configuration has added an ONTAP Mediator |
| 147 |
""" |
| 148 |
api = "cluster/mediators" |
| 149 |
message, error = self.rest_api.get(api, None) |
| 150 |
if error: |
| 151 |
self.module.fail_json(msg=error) |
| 152 |
if message['num_records'] > 0: |
| 153 |
return message['records'][0]['uuid'] |
| 154 |
return None |
| 155 |
|
| 156 |
def apply(self): |
| 157 |
""" |
| 158 |
Apply action to MCC Mediator |
| 159 |
""" |
| 160 |
current = self.get_mediator() |
| 161 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 162 |
|
| 163 |
if self.na_helper.changed: |
| 164 |
if self.module.check_mode: |
| 165 |
pass |
| 166 |
else: |
| 167 |
if cd_action == 'create': |
| 168 |
self.add_mediator() |
| 169 |
elif cd_action == 'delete': |
| 170 |
self.remove_mediator(current) |
| 171 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action) |
| 172 |
self.module.exit_json(**result) |
| 173 |
|
| 174 |
|
| 175 |
def main(): |
| 176 |
""" |
| 177 |
Add, Remove and display ONTAP MCC Mediator |
| 178 |
""" |
| 179 |
mediator_obj = NetAppOntapMccipMediator() |
| 180 |
mediator_obj.apply() |
| 181 |
|
| 182 |
|
| 183 |
if __name__ == '__main__': |
| 184 |
main() |