Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_snmp_client.py

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