summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/storage/netapp/na_ontap_snmp.py
blob: caeadb9da3586e99e5bd37a517c5884d01b6b869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
"""
create SNMP module to add/delete/modify SNMP user
"""

# (c) 2018-2019, NetApp, Inc
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'certified'}


DOCUMENTATION = '''
author: NetApp Ansible Team (@carchi8py) <ng-ansibleteam@netapp.com>
description:
  - "Create/Delete SNMP community"
extends_documentation_fragment:
  - netapp.na_ontap
module: na_ontap_snmp
options:
  access_control:
    description:
      - "Access control for the community. The only supported value is 'ro' (read-only)"
    required: true
  community_name:
    description:
      - "The name of the SNMP community to manage."
    required: true
  state:
    choices: ['present', 'absent']
    description:
      - "Whether the specified SNMP community should exist or not."
    default: 'present'
short_description: NetApp ONTAP SNMP community
version_added: "2.6"
'''

EXAMPLES = """
    - name: Create SNMP community
      na_ontap_snmp:
        state: present
        community_name: communityName
        access_control: 'ro'
        hostname: "{{ netapp_hostname }}"
        username: "{{ netapp_username }}"
        password: "{{ netapp_password }}"
    - name: Delete SNMP community
      na_ontap_snmp:
        state: absent
        community_name: communityName
        access_control: 'ro'
        hostname: "{{ netapp_hostname }}"
        username: "{{ netapp_username }}"
        password: "{{ netapp_password }}"
"""

RETURN = """
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
import ansible.module_utils.netapp as netapp_utils

HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()


class NetAppONTAPSnmp(object):
    '''Class with SNMP methods, doesn't support check mode'''

    def __init__(self):

        self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
        self.argument_spec.update(dict(
            state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
            community_name=dict(required=True, type='str'),
            access_control=dict(required=True, type='str'),
        ))

        self.module = AnsibleModule(
            argument_spec=self.argument_spec,
            supports_check_mode=False
        )

        parameters = self.module.params
        # set up state variables
        self.state = parameters['state']
        self.community_name = parameters['community_name']
        self.access_control = parameters['access_control']

        if HAS_NETAPP_LIB is False:
            self.module.fail_json(msg="the python NetApp-Lib module is required")
        else:
            self.server = netapp_utils.setup_na_ontap_zapi(module=self.module)

    def invoke_snmp_community(self, zapi):
        """
        Invoke zapi - add/delete take the same NaElement structure
        @return: SUCCESS / FAILURE with an error_message
        """
        snmp_community = netapp_utils.zapi.NaElement.create_node_with_children(
            zapi, **{'community': self.community_name,
                     'access-control': self.access_control})
        try:
            self.server.invoke_successfully(snmp_community, enable_tunneling=True)
        except netapp_utils.zapi.NaApiError:  # return False for duplicate entry
            return False
        return True

    def add_snmp_community(self):
        """
        Adds a SNMP community
        """
        return self.invoke_snmp_community('snmp-community-add')

    def delete_snmp_community(self):
        """
        Delete a SNMP community
        """
        return self.invoke_snmp_community('snmp-community-delete')

    def apply(self):
        """
        Apply action to SNMP community
        This module is not idempotent:
        Add doesn't fail the playbook if user is trying
        to add an already existing snmp community
        """
        changed = False
        results = netapp_utils.get_cserver(self.server)
        cserver = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=results)
        netapp_utils.ems_log_event("na_ontap_snmp", cserver)
        if self.state == 'present':  # add
            if self.add_snmp_community():
                changed = True
        elif self.state == 'absent':  # delete
            if self.delete_snmp_community():
                changed = True

        self.module.exit_json(changed=changed)


def main():
    '''Execute action'''
    community_obj = NetAppONTAPSnmp()
    community_obj.apply()


if __name__ == '__main__':
    main()