| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2021-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ |
| 5 |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
|
| 12 |
module: na_ontap_snaplock_clock |
| 13 |
|
| 14 |
short_description: NetApp ONTAP Sets the snaplock compliance clock. |
| 15 |
extends_documentation_fragment: |
| 16 |
- netapp.ontap.netapp.na_ontap |
| 17 |
version_added: '21.4.0' |
| 18 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 19 |
description: |
| 20 |
- Sets the Snaplock compliance clock on NetApp ONTAP. |
| 21 |
|
| 22 |
options: |
| 23 |
node: |
| 24 |
description: |
| 25 |
- Name of the node to set compliance clock on. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
|
| 29 |
''' |
| 30 |
|
| 31 |
EXAMPLES = """ |
| 32 |
- name: Set node compliance clock |
| 33 |
netapp.ontap.na_ontap_snaplock_clock: |
| 34 |
node: cluster1-01 |
| 35 |
hostname: "{{ netapp_hostname }}" |
| 36 |
username: "{{ netapp_username }}" |
| 37 |
password: "{{ netapp_password }}" |
| 38 |
""" |
| 39 |
|
| 40 |
RETURN = """ |
| 41 |
""" |
| 42 |
|
| 43 |
import traceback |
| 44 |
|
| 45 |
from ansible.module_utils.basic import AnsibleModule |
| 46 |
from ansible.module_utils._text import to_native |
| 47 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 48 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 49 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 50 |
import ansible_collections.netapp.ontap.plugins.module_utils.rest_response_helpers as rrh |
| 51 |
|
| 52 |
|
| 53 |
class NetAppOntapSnaplockClock: |
| 54 |
'''Class with SnapLock clock operations''' |
| 55 |
|
| 56 |
def __init__(self): |
| 57 |
'''Initialize module parameters''' |
| 58 |
|
| 59 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 60 |
self.argument_spec.update(dict( |
| 61 |
node=dict(required=True, type='str'), |
| 62 |
)) |
| 63 |
self.module = AnsibleModule( |
| 64 |
argument_spec=self.argument_spec, |
| 65 |
supports_check_mode=True |
| 66 |
) |
| 67 |
self.na_helper = NetAppModule() |
| 68 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 69 |
|
| 70 |
self.rest_api = OntapRestAPI(self.module) |
| 71 |
self.use_rest = self.rest_api.is_rest() |
| 72 |
|
| 73 |
if not self.use_rest: |
| 74 |
if not netapp_utils.has_netapp_lib(): |
| 75 |
self.module.fail_json(msg=netapp_utils.netapp) |
| 76 |
else: |
| 77 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 78 |
|
| 79 |
def get_snaplock_node_compliance_clock(self): |
| 80 |
|
| 81 |
if self.use_rest: |
| 82 |
""" |
| 83 |
Return snaplock-node-compliance-clock query results |
| 84 |
:return: dict of clock info |
| 85 |
""" |
| 86 |
api = "private/cli/snaplock/compliance-clock" |
| 87 |
query = { |
| 88 |
'fields': 'node,time', |
| 89 |
'node': self.parameters['node'], |
| 90 |
} |
| 91 |
message, error = self.rest_api.get(api, query) |
| 92 |
records, error = rrh.check_for_0_or_1_records(api, message, error) |
| 93 |
|
| 94 |
if error is None and records is not None: |
| 95 |
return_value = { |
| 96 |
'node': message['records'][0]['node'], |
| 97 |
'compliance_clock_time': message['records'][0]['time'] |
| 98 |
} |
| 99 |
|
| 100 |
if error: |
| 101 |
self.module.fail_json(msg=error) |
| 102 |
|
| 103 |
if not records: |
| 104 |
error = "REST API did not return snaplock compliance clock for node %s" % (self.parameters['node']) |
| 105 |
self.module.fail_json(msg=error) |
| 106 |
|
| 107 |
else: |
| 108 |
""" |
| 109 |
Return snaplock-node-compliance-clock query results |
| 110 |
:param node_name: name of the cluster node |
| 111 |
:return: NaElement |
| 112 |
""" |
| 113 |
|
| 114 |
node_snaplock_clock = netapp_utils.zapi.NaElement('snaplock-get-node-compliance-clock') |
| 115 |
node_snaplock_clock.add_new_child('node', self.parameters['node']) |
| 116 |
|
| 117 |
try: |
| 118 |
result = self.server.invoke_successfully(node_snaplock_clock, True) |
| 119 |
except netapp_utils.zapi.NaApiError as error: |
| 120 |
self.module.fail_json(msg='Error fetching snaplock compliance clock for node %s : %s' |
| 121 |
% (self.parameters['node'], to_native(error)), |
| 122 |
exception=traceback.format_exc()) |
| 123 |
|
| 124 |
return_value = None |
| 125 |
|
| 126 |
if result.get_child_by_name('snaplock-node-compliance-clock'): |
| 127 |
node_snaplock_clock_attributes = result['snaplock-node-compliance-clock']['compliance-clock-info'] |
| 128 |
return_value = { |
| 129 |
'compliance_clock_time': node_snaplock_clock_attributes['formatted-snaplock-compliance-clock'], |
| 130 |
} |
| 131 |
return return_value |
| 132 |
|
| 133 |
def set_snaplock_node_compliance_clock(self): |
| 134 |
'''Set ONTAP snaplock compliance clock for each node''' |
| 135 |
if self.use_rest: |
| 136 |
api = "private/cli/snaplock/compliance-clock/initialize" |
| 137 |
query = { |
| 138 |
"node": self.parameters['node'] |
| 139 |
} |
| 140 |
|
| 141 |
body = {} |
| 142 |
dummy, error = self.rest_api.patch(api, body, query) |
| 143 |
if error: |
| 144 |
self.module.fail_json(msg=error) |
| 145 |
else: |
| 146 |
node_snaplock_clock_obj = netapp_utils.zapi.NaElement('snaplock-set-node-compliance-clock') |
| 147 |
node_snaplock_clock_obj.add_new_child('node', self.parameters['node']) |
| 148 |
|
| 149 |
try: |
| 150 |
result = self.server.invoke_successfully(node_snaplock_clock_obj, True) |
| 151 |
except netapp_utils.zapi.NaApiError as error: |
| 152 |
self.module.fail_json(msg='Error setting snaplock compliance clock for node %s : %s' |
| 153 |
% (self.parameters['node'], to_native(error)), |
| 154 |
exception=traceback.format_exc()) |
| 155 |
return result |
| 156 |
|
| 157 |
def apply(self): |
| 158 |
current = self.get_snaplock_node_compliance_clock() |
| 159 |
|
| 160 |
if current['compliance_clock_time'] == "ComplianceClock is not configured.": |
| 161 |
self.na_helper.changed = True |
| 162 |
|
| 163 |
if self.na_helper.changed and not self.module.check_mode: |
| 164 |
self.set_snaplock_node_compliance_clock() |
| 165 |
|
| 166 |
self.module.exit_json(changed=self.na_helper.changed) |
| 167 |
|
| 168 |
|
| 169 |
def main(): |
| 170 |
'''Set snaplock compliance clock''' |
| 171 |
obj = NetAppOntapSnaplockClock() |
| 172 |
obj.apply() |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == '__main__': |
| 176 |
main() |