| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: NetApp, Inc |
| 5 |
# GNU General Public License v3.0+ (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 |
module: na_ontap_autoupdate_config |
| 12 |
short_description: NetApp ONTAP module to manage configurations for automatic updates. |
| 13 |
extends_documentation_fragment: |
| 14 |
- netapp.ontap.netapp.na_ontap_rest |
| 15 |
version_added: 23.4.0 |
| 16 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 17 |
description: |
| 18 |
- Modify the configuration for a specified automatic update. |
| 19 |
options: |
| 20 |
state: |
| 21 |
description: |
| 22 |
- Update the configuration, only present is supported. |
| 23 |
type: str |
| 24 |
choices: ['present'] |
| 25 |
default: present |
| 26 |
category: |
| 27 |
description: |
| 28 |
- Specifies the category for the configuration row. |
| 29 |
- Examples - firmware, sp_fw, system, security. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
action: |
| 33 |
description: |
| 34 |
- Specifies the action to be taken by the alert source as specified by the user. |
| 35 |
type: str |
| 36 |
choices: ['confirm', 'dismiss', 'automatic'] |
| 37 |
default: 'confirm' |
| 38 |
required: false |
| 39 |
|
| 40 |
notes: |
| 41 |
- Only supported with REST and requires ONTAP 9.10.1 or later. |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = """ |
| 45 |
- name: Modify the automatic update setting for security category |
| 46 |
netapp.ontap.na_ontap_autoupdate_config: |
| 47 |
state: present |
| 48 |
category: security |
| 49 |
action: automatic |
| 50 |
hostname: "{{ netapp_hostname }}" |
| 51 |
username: "{{ netapp_username }}" |
| 52 |
password: "{{ netapp_password }}" |
| 53 |
https: true |
| 54 |
validate_certs: "{{ validate_certs }}" |
| 55 |
""" |
| 56 |
|
| 57 |
RETURN = """ |
| 58 |
""" |
| 59 |
|
| 60 |
import traceback |
| 61 |
from ansible.module_utils.basic import AnsibleModule |
| 62 |
from ansible.module_utils._text import to_native |
| 63 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 64 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 65 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 66 |
|
| 67 |
|
| 68 |
class NetAppOntapAutoUpdateConfig: |
| 69 |
def __init__(self): |
| 70 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 71 |
self.argument_spec.update(dict( |
| 72 |
state=dict(required=False, type='str', choices=['present'], default='present'), |
| 73 |
category=dict(required=True, type='str'), |
| 74 |
action=dict(required=False, type='str', choices=['confirm', 'dismiss', 'automatic'], default='confirm'), |
| 75 |
)) |
| 76 |
self.module = AnsibleModule( |
| 77 |
argument_spec=self.argument_spec, |
| 78 |
supports_check_mode=True |
| 79 |
) |
| 80 |
self.uuid = None |
| 81 |
self.na_helper = NetAppModule(self.module) |
| 82 |
self.parameters = self.na_helper.check_and_set_parameters(self.module) |
| 83 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 84 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_autoupdate_config:', 9, 10, 1) |
| 85 |
|
| 86 |
def get_autoupdate_config_rest(self): |
| 87 |
""" Retrieves the configuration for the specified automatic update """ |
| 88 |
api = "support/auto-update/configurations" |
| 89 |
fields = 'category,action' |
| 90 |
records, error = rest_generic.get_0_or_more_records(self.rest_api, api, query=None, fields=fields) |
| 91 |
if error: |
| 92 |
self.module.fail_json(msg="Error retrieving auto-update settings for %s: %s" % (self.parameters['category'], to_native(error)), |
| 93 |
exception=traceback.format_exc()) |
| 94 |
if records: |
| 95 |
for record in records: |
| 96 |
if record.get('category') == self.parameters['category']: |
| 97 |
self.uuid = record.get('uuid') |
| 98 |
return { |
| 99 |
'category': record.get('category'), |
| 100 |
'action': record.get('action') |
| 101 |
} |
| 102 |
return None |
| 103 |
|
| 104 |
def modify_autoupdate_config_rest(self, modify): |
| 105 |
""" Updates the configuration for the specified automatic update """ |
| 106 |
api = "support/auto-update/configurations" |
| 107 |
body = {'action': modify['action']} |
| 108 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid_or_name=self.uuid, body=body) |
| 109 |
if error: |
| 110 |
self.module.fail_json(msg='Error modifying auto-update settings for %s: %s.' % (self.parameters['category'], to_native(error)), |
| 111 |
exception=traceback.format_exc()) |
| 112 |
|
| 113 |
def apply(self): |
| 114 |
current = self.get_autoupdate_config_rest() |
| 115 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 116 |
|
| 117 |
if self.na_helper.changed and not self.module.check_mode: |
| 118 |
self.modify_autoupdate_config_rest(modify) |
| 119 |
result = netapp_utils.generate_result(changed=self.na_helper.changed, modify=modify) |
| 120 |
self.module.exit_json(**result) |
| 121 |
|
| 122 |
|
| 123 |
def main(): |
| 124 |
autoupdate_config = NetAppOntapAutoUpdateConfig() |
| 125 |
autoupdate_config.apply() |
| 126 |
|
| 127 |
|
| 128 |
if __name__ == '__main__': |
| 129 |
main() |