| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2022-2025, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = """ |
| 11 |
module: na_ontap_ntp_key |
| 12 |
short_description: NetApp ONTAP NTP key |
| 13 |
extends_documentation_fragment: |
| 14 |
- netapp.ontap.netapp.na_ontap_rest |
| 15 |
version_added: 21.21.0 |
| 16 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 17 |
description: |
| 18 |
- Create or delete or modify NTP key in ONTAP |
| 19 |
options: |
| 20 |
state: |
| 21 |
description: |
| 22 |
- Whether the specified NTP key should exist or not. |
| 23 |
choices: ['present', 'absent'] |
| 24 |
type: str |
| 25 |
default: 'present' |
| 26 |
id: |
| 27 |
description: |
| 28 |
- NTP symmetric authentication key ID. The ID must be in the range 1 to 65535. |
| 29 |
required: True |
| 30 |
type: int |
| 31 |
digest_type: |
| 32 |
description: |
| 33 |
- NTP symmetric authentication key type. Only SHA1 currently supported. |
| 34 |
choices: ['sha1'] |
| 35 |
type: str |
| 36 |
required: True |
| 37 |
value: |
| 38 |
description: |
| 39 |
- NTP symmetric authentication key value. The value must be exactly 40 hexadecimal digits for SHA1 keys. |
| 40 |
type: str |
| 41 |
required: True |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = """ |
| 45 |
- name: Create NTP key |
| 46 |
netapp.ontap.na_ontap_ntp_key: |
| 47 |
state: present |
| 48 |
digest_type: sha1 |
| 49 |
value: "{{ key_value }}" |
| 50 |
id: 1 |
| 51 |
hostname: "{{ netapp_hostname }}" |
| 52 |
username: "{{ netapp_username }}" |
| 53 |
password: "{{ netapp_password }}" |
| 54 |
|
| 55 |
- name: Delete NTP key |
| 56 |
netapp.ontap.na_ontap_ntp_key: |
| 57 |
state: absent |
| 58 |
id: 1 |
| 59 |
hostname: "{{ netapp_hostname }}" |
| 60 |
username: "{{ netapp_username }}" |
| 61 |
password: "{{ netapp_password }}" |
| 62 |
""" |
| 63 |
|
| 64 |
RETURN = """ |
| 65 |
""" |
| 66 |
import traceback |
| 67 |
|
| 68 |
from ansible.module_utils.basic import AnsibleModule |
| 69 |
from ansible.module_utils._text import to_native |
| 70 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 71 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 72 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 73 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 74 |
|
| 75 |
|
| 76 |
class NetAppOntapNTPKey: |
| 77 |
""" object initialize and class methods """ |
| 78 |
def __init__(self): |
| 79 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 80 |
self.argument_spec.update(dict( |
| 81 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 82 |
id=dict(required=True, type='int'), |
| 83 |
digest_type=dict(required=True, type='str', choices=['sha1']), |
| 84 |
value=dict(required=True, type='str'), |
| 85 |
)) |
| 86 |
|
| 87 |
self.module = AnsibleModule( |
| 88 |
argument_spec=self.argument_spec, |
| 89 |
supports_check_mode=True |
| 90 |
) |
| 91 |
self.na_helper = NetAppModule() |
| 92 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 93 |
self.rest_api = OntapRestAPI(self.module) |
| 94 |
self.use_rest = self.rest_api.is_rest() |
| 95 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_ntp_key', 9, 7) |
| 96 |
|
| 97 |
def get_ntp_key(self): |
| 98 |
api = 'cluster/ntp/keys' |
| 99 |
options = {'id': self.parameters['id'], |
| 100 |
'fields': 'id,digest_type,value'} |
| 101 |
record, error = rest_generic.get_one_record(self.rest_api, api, options) |
| 102 |
if error: |
| 103 |
self.module.fail_json(msg='Error fetching key with id %s: %s' % (self.parameters['id'], to_native(error)), |
| 104 |
exception=traceback.format_exc()) |
| 105 |
return record |
| 106 |
|
| 107 |
def create_ntp_key(self): |
| 108 |
api = 'cluster/ntp/keys' |
| 109 |
params = { |
| 110 |
'id': self.parameters['id'], |
| 111 |
'digest_type': self.parameters['digest_type'], |
| 112 |
'value': self.parameters['value'] |
| 113 |
} |
| 114 |
dummy, error = rest_generic.post_async(self.rest_api, api, params) |
| 115 |
if error: |
| 116 |
self.module.fail_json(msg='Error creating key with id %s: %s' % (self.parameters['id'], to_native(error)), |
| 117 |
exception=traceback.format_exc()) |
| 118 |
|
| 119 |
def delete_ntp_key(self): |
| 120 |
dummy, error = rest_generic.delete_async(self.rest_api, 'cluster/ntp/keys', str(self.parameters['id'])) |
| 121 |
if error: |
| 122 |
self.module.fail_json(msg='Error deleting key with id %s: %s' % (self.parameters['id'], to_native(error)), |
| 123 |
exception=traceback.format_exc()) |
| 124 |
|
| 125 |
def modify_ntp_key(self, modify): |
| 126 |
body = {} |
| 127 |
if 'digest_type' in modify: |
| 128 |
body['digest_type'] = self.parameters['digest_type'] |
| 129 |
if 'value' in modify: |
| 130 |
body['value'] = self.parameters['value'] |
| 131 |
if body: |
| 132 |
dummy, error = rest_generic.patch_async(self.rest_api, 'cluster/ntp/keys', str(self.parameters['id']), body) |
| 133 |
if error: |
| 134 |
self.module.fail_json(msg='Error modifying key with id %s: %s' % (self.parameters['id'], to_native(error)), |
| 135 |
exception=traceback.format_exc()) |
| 136 |
|
| 137 |
def apply(self): |
| 138 |
cd_action = None |
| 139 |
current = self.get_ntp_key() |
| 140 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 141 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) if cd_action is None else None |
| 142 |
if self.na_helper.changed and not self.module.check_mode: |
| 143 |
if cd_action == 'create': |
| 144 |
self.create_ntp_key() |
| 145 |
elif cd_action == 'delete': |
| 146 |
self.delete_ntp_key() |
| 147 |
elif modify: |
| 148 |
self.modify_ntp_key(modify) |
| 149 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 150 |
self.module.exit_json(**result) |
| 151 |
|
| 152 |
|
| 153 |
def main(): |
| 154 |
""" Create object and call apply """ |
| 155 |
ntp_obj = NetAppOntapNTPKey() |
| 156 |
ntp_obj.apply() |
| 157 |
|
| 158 |
|
| 159 |
if __name__ == '__main__': |
| 160 |
main() |