| 1 |
#!/usr/bin/python |
| 2 |
""" |
| 3 |
create SNMP module to add/delete/modify SNMP user |
| 4 |
""" |
| 5 |
|
| 6 |
# (c) 2020-2025, NetApp, Inc |
| 7 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
module: na_ontap_snmp_traphosts |
| 14 |
short_description: NetApp ONTAP SNMP traphosts. |
| 15 |
extends_documentation_fragment: |
| 16 |
- netapp.ontap.netapp.na_ontap_rest |
| 17 |
version_added: '20.3.0' |
| 18 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 19 |
description: |
| 20 |
- Whether the specified SNMP traphost should exist or not. Requires REST with 9.7 or higher |
| 21 |
options: |
| 22 |
state: |
| 23 |
choices: ['present', 'absent'] |
| 24 |
description: |
| 25 |
- "Whether the specified SNMP traphost should exist or not." |
| 26 |
default: 'present' |
| 27 |
type: str |
| 28 |
host: |
| 29 |
description: |
| 30 |
- "Fully qualified domain name (FQDN), IPv4 address or IPv6 address of SNMP traphost." |
| 31 |
aliases: ['ip_address'] |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
version_added: 21.24.0 |
| 35 |
''' |
| 36 |
|
| 37 |
EXAMPLES = """ |
| 38 |
- name: Create SNMP traphost |
| 39 |
netapp.ontap.na_ontap_snmp_traphosts: |
| 40 |
state: present |
| 41 |
host: example1.com |
| 42 |
hostname: "{{ netapp_hostname }}" |
| 43 |
username: "{{ netapp_username }}" |
| 44 |
password: "{{ netapp_password }}" |
| 45 |
- name: Delete SNMP traphost |
| 46 |
netapp.ontap.na_ontap_snmp_traphosts: |
| 47 |
state: absent |
| 48 |
host: example1.com |
| 49 |
hostname: "{{ netapp_hostname }}" |
| 50 |
username: "{{ netapp_username }}" |
| 51 |
password: "{{ netapp_password }}" |
| 52 |
""" |
| 53 |
|
| 54 |
RETURN = """ |
| 55 |
""" |
| 56 |
from ansible.module_utils.basic import AnsibleModule |
| 57 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 58 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 59 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 60 |
|
| 61 |
|
| 62 |
class NetAppONTAPSnmpTraphosts: |
| 63 |
"""Class with SNMP methods""" |
| 64 |
|
| 65 |
def __init__(self): |
| 66 |
self.use_rest = False |
| 67 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 68 |
self.argument_spec.update(dict( |
| 69 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 70 |
host=dict(required=True, type='str', aliases=['ip_address']), |
| 71 |
)) |
| 72 |
self.module = AnsibleModule( |
| 73 |
argument_spec=self.argument_spec, |
| 74 |
supports_check_mode=True |
| 75 |
) |
| 76 |
self.na_helper = NetAppModule() |
| 77 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 78 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 79 |
self.use_rest = self.rest_api.is_rest() |
| 80 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_snmp_traphosts', 9, 7) |
| 81 |
|
| 82 |
def get_snmp_traphosts(self): |
| 83 |
query = {'host': self.parameters.get('host'), |
| 84 |
'fields': 'host'} |
| 85 |
api = 'support/snmp/traphosts' |
| 86 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 87 |
if error: |
| 88 |
self.module.fail_json(msg="Error on fetching snmp traphosts info: %s" % error) |
| 89 |
return record |
| 90 |
|
| 91 |
def create_snmp_traphost(self): |
| 92 |
api = 'support/snmp/traphosts' |
| 93 |
params = {'host': self.parameters.get('host')} |
| 94 |
dummy, error = rest_generic.post_async(self.rest_api, api, params) |
| 95 |
if error: |
| 96 |
self.module.fail_json(msg="Error creating traphost: %s" % error) |
| 97 |
|
| 98 |
def delete_snmp_traphost(self): |
| 99 |
api = 'support/snmp/traphosts' |
| 100 |
dummy, error = rest_generic.delete_async(self.rest_api, api, self.parameters.get('host')) |
| 101 |
if error is not None: |
| 102 |
self.module.fail_json(msg="Error deleting traphost: %s" % error) |
| 103 |
|
| 104 |
def apply(self): |
| 105 |
""" |
| 106 |
Apply action to SNMP traphost |
| 107 |
""" |
| 108 |
current = self.get_snmp_traphosts() |
| 109 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 110 |
if self.na_helper.changed and not self.module.check_mode: |
| 111 |
if cd_action == 'create': |
| 112 |
self.create_snmp_traphost() |
| 113 |
elif cd_action == 'delete': |
| 114 |
self.delete_snmp_traphost() |
| 115 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action) |
| 116 |
self.module.exit_json(**result) |
| 117 |
|
| 118 |
|
| 119 |
def main(): |
| 120 |
"""Execute action""" |
| 121 |
community_obj = NetAppONTAPSnmpTraphosts() |
| 122 |
community_obj.apply() |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == '__main__': |
| 126 |
main() |