1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3
4# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6from __future__ import absolute_import, division, print_function
7__metaclass__ = type
8
9ANSIBLE_METADATA = {'metadata_version': '1.1',
10                    'status': ['preview'],
11                    'supported_by': 'certified'}
12
13DOCUMENTATION = r'''
14---
15module: aci_snmp_community_policy
16short_description: Manage SNMP community policies (snmp:CommunityP).
17description:
18- Manage SNMP community policies
19options:
20  community:
21    description:
22    - Name of the SNMP community policy
23    type: str
24  description:
25    description:
26    - Description of the SNMP policy
27    type: str
28  policy:
29    description:
30    - Name of an existing SNMP policy
31    type: str
32    aliases: [ snmp_policy, snmp_policy_name ]
33  state:
34    description:
35    - Use C(present) or C(absent) for adding or removing.
36    - Use C(query) for listing an object or multiple objects.
37    type: str
38    choices: [ absent, present, query ]
39    default: present
40extends_documentation_fragment:
41- cisco.aci.aci
42
43seealso:
44- name: APIC Management Information Model reference
45  description: More information about the internal APIC class B(snmp:CommunityP).
46  link: https://developer.cisco.com/docs/apic-mim-ref/
47author:
48- Tim Cragg (@timcragg)
49'''
50
51EXAMPLES = r'''
52- name: Create an SNMP community policy
53  cisco.aci.aci_snmp_community_policy:
54    host: apic
55    username: admin
56    password: SomeSecretPassword
57    policy: my_snmp_policy
58    community: my_snmp_community
59    state: present
60  delegate_to: localhost
61
62- name: Remove an SNMP community policy
63  cisco.aci.aci_snmp_community_policy:
64    host: apic
65    username: admin
66    password: SomeSecretPassword
67    policy: my_snmp_policy
68    community: my_snmp_community
69    state: absent
70  delegate_to: localhost
71
72- name: Query an SNMP community policy
73  cisco.aci.aci_snmp_community_policy:
74    host: apic
75    username: admin
76    password: SomeSecretPassword
77    policy: my_snmp_policy
78    community: my_snmp_community
79    state: query
80  delegate_to: localhost
81  register: query_result
82
83- name: Query all SNMP community policies
84  cisco.aci.aci_snmp_community_policy:
85    host: apic
86    username: admin
87    password: SomeSecretPassword
88    state: query
89  delegate_to: localhost
90  register: query_result
91'''
92
93RETURN = r'''
94current:
95  description: The existing configuration from the APIC after the module has finished
96  returned: success
97  type: list
98  sample:
99    [
100        {
101            "fvTenant": {
102                "attributes": {
103                    "descr": "Production environment",
104                    "dn": "uni/tn-production",
105                    "name": "production",
106                    "nameAlias": "",
107                    "ownerKey": "",
108                    "ownerTag": ""
109                }
110            }
111        }
112    ]
113error:
114  description: The error information as returned from the APIC
115  returned: failure
116  type: dict
117  sample:
118    {
119        "code": "122",
120        "text": "unknown managed object class foo"
121    }
122raw:
123  description: The raw output returned by the APIC REST API (xml or json)
124  returned: parse error
125  type: str
126  sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
127sent:
128  description: The actual/minimal configuration pushed to the APIC
129  returned: info
130  type: list
131  sample:
132    {
133        "fvTenant": {
134            "attributes": {
135                "descr": "Production environment"
136            }
137        }
138    }
139previous:
140  description: The original configuration from the APIC before the module has started
141  returned: info
142  type: list
143  sample:
144    [
145        {
146            "fvTenant": {
147                "attributes": {
148                    "descr": "Production",
149                    "dn": "uni/tn-production",
150                    "name": "production",
151                    "nameAlias": "",
152                    "ownerKey": "",
153                    "ownerTag": ""
154                }
155            }
156        }
157    ]
158proposed:
159  description: The assembled configuration from the user-provided parameters
160  returned: info
161  type: dict
162  sample:
163    {
164        "fvTenant": {
165            "attributes": {
166                "descr": "Production environment",
167                "name": "production"
168            }
169        }
170    }
171filter_string:
172  description: The filter string used for the request
173  returned: failure or debug
174  type: str
175  sample: ?rsp-prop-include=config-only
176method:
177  description: The HTTP method used for the request to the APIC
178  returned: failure or debug
179  type: str
180  sample: POST
181response:
182  description: The HTTP response from the APIC
183  returned: failure or debug
184  type: str
185  sample: OK (30 bytes)
186status:
187  description: The HTTP status from the APIC
188  returned: failure or debug
189  type: int
190  sample: 200
191url:
192  description: The HTTP url used for the request to the APIC
193  returned: failure or debug
194  type: str
195  sample: https://10.11.12.13/api/mo/uni/tn-production.json
196'''
197
198
199from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec
200from ansible.module_utils.basic import AnsibleModule
201
202
203def main():
204    argument_spec = aci_argument_spec()
205    argument_spec.update(
206        community=dict(type='str'),
207        policy=dict(type='str', aliases=['snmp_policy', 'snmp_policy_name']),
208        description=dict(type='str'),
209        state=dict(type='str', default='present',
210                   choices=['absent', 'present', 'query'])
211    )
212
213    module = AnsibleModule(
214        argument_spec=argument_spec,
215        supports_check_mode=True,
216        required_if=[
217            ['state', 'absent', ['policy', 'community']],
218            ['state', 'present', ['policy', 'community']],
219        ]
220    )
221
222    aci = ACIModule(module)
223
224    community = module.params.get('community')
225    policy = module.params.get('policy')
226    description = module.params.get('description')
227    state = module.params.get('state')
228
229    aci.construct_url(
230        root_class=dict(
231            aci_class='snmpPol',
232            aci_rn='fabric/snmppol-{0}'.format(policy),
233            module_object=policy,
234            target_filter={'name': policy},
235        ),
236        subclass_1=dict(
237            aci_class='snmpCommunityP',
238            aci_rn='community-{0}'.format(community),
239            module_object=community,
240            target_filter={'name': community},
241        )
242    )
243
244    aci.get_existing()
245
246    if state == 'present':
247        aci.payload(
248            aci_class='snmpCommunityP',
249            class_config=dict(
250                name=community,
251                descr=description
252            ),
253        )
254
255        aci.get_diff(aci_class='snmpCommunityP')
256
257        aci.post_config()
258
259    elif state == 'absent':
260        aci.delete_config()
261
262    aci.exit_json()
263
264
265if __name__ == "__main__":
266    main()
267