Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Gaspard Micol (@gmicol) <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
12
13 DOCUMENTATION = r"""
14 module: aci_maintenance_group
15 short_description: This creates an ACI maintenance group (maint:MaintGrp)
16 description:
17 - This modules creates an ACI maintenance group.
18 options:
19 group:
20 description:
21 - The name of the maintenance group.
22 type: str
23 policy:
24 description:
25 - The name of the maintenance policy.
26 type: str
27 aliases: [ maintenancepol ]
28 firmware_nodes_type:
29 description:
30 - The firmware type of nodes in the maintenance group.
31 - The APIC defaults to C(switch) when unset during creation.
32 type: str
33 choices: [ c_apic_patch, catalog, config, controller, controller_patch, plugin, plugin_package, switch, switch_patch, vpod ]
34 type_group:
35 description:
36 - The type of the maintenance group.
37 - The APIC defaults to C(range) when unset during creation.
38 type: str
39 choices: [ all, all_in_pod, range ]
40 description:
41 description:
42 - Description of the maintenance group.
43 type: str
44 aliases: [ descr ]
45 state:
46 description:
47 - Use C(present) or C(absent) for adding or removing.
48 - Use C(query) for listing an object or multiple objects.
49 type: str
50 choices: [absent, present, query]
51 default: present
52 name_alias:
53 description:
54 - The alias for the current object. This relates to the nameAlias field in ACI.
55 type: str
56 extends_documentation_fragment:
57 - cisco.aci.aci
58 - cisco.aci.annotation
59 - cisco.aci.owner
60
61 notes:
62 - The C(policy) used must exist before using this module in your playbook.
63 - The M(cisco.aci.aci_maintenance_policy) module can be used for this.
64 seealso:
65 - module: cisco.aci.aci_maintenance_policy
66 - name: APIC Management Information Model reference
67 description: More information about the internal APIC class B(maint:MaintGrp).
68 link: https://developer.cisco.com/docs/apic-mim-ref/
69 author:
70 - Steven Gerhart (@sgerhart)
71 - Gaspard Micol (@gmicol)
72 """
73
74 EXAMPLES = r"""
75 - name: Create a maintenance group
76 cisco.aci.aci_maintenance_group:
77 host: apic
78 username: admin
79 password: SomeSecretPassword
80 group: my_maintenance_group
81 policy: my_maintenance_policy
82 state: present
83 delegate_to: localhost
84
85 - name: Delete a maintenance group
86 cisco.aci.aci_maintenance_group:
87 host: apic
88 username: admin
89 password: SomeSecretPassword
90 group: my_maintenance_group
91 state: absent
92 delegate_to: localhost
93
94 - name: Query all maintenance groups
95 cisco.aci.aci_maintenance_group:
96 host: apic
97 username: admin
98 password: SomeSecretPassword
99 state: query
100 delegate_to: localhost
101 register: query_result
102
103 - name: Query a specific maintenance group
104 cisco.aci.aci_maintenance_group:
105 host: apic
106 username: admin
107 password: SomeSecretPassword
108 group: my_maintenance_group
109 state: query
110 delegate_to: localhost
111 register: query_result
112 """
113
114 RETURN = r"""
115 current:
116 description: The existing configuration from the APIC after the module has finished
117 returned: success
118 type: list
119 sample:
120 [
121 {
122 "fvTenant": {
123 "attributes": {
124 "descr": "Production environment",
125 "dn": "uni/tn-production",
126 "name": "production",
127 "nameAlias": "",
128 "ownerKey": "",
129 "ownerTag": ""
130 }
131 }
132 }
133 ]
134 error:
135 description: The error information as returned from the APIC
136 returned: failure
137 type: dict
138 sample:
139 {
140 "code": "122",
141 "text": "unknown managed object class foo"
142 }
143 raw:
144 description: The raw output returned by the APIC REST API (xml or json)
145 returned: parse error
146 type: str
147 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
148 sent:
149 description: The actual/minimal configuration pushed to the APIC
150 returned: info
151 type: list
152 sample:
153 {
154 "fvTenant": {
155 "attributes": {
156 "descr": "Production environment"
157 }
158 }
159 }
160 previous:
161 description: The original configuration from the APIC before the module has started
162 returned: info
163 type: list
164 sample:
165 [
166 {
167 "fvTenant": {
168 "attributes": {
169 "descr": "Production",
170 "dn": "uni/tn-production",
171 "name": "production",
172 "nameAlias": "",
173 "ownerKey": "",
174 "ownerTag": ""
175 }
176 }
177 }
178 ]
179 proposed:
180 description: The assembled configuration from the user-provided parameters
181 returned: info
182 type: dict
183 sample:
184 {
185 "fvTenant": {
186 "attributes": {
187 "descr": "Production environment",
188 "name": "production"
189 }
190 }
191 }
192 filter_string:
193 description: The filter string used for the request
194 returned: failure or debug
195 type: str
196 sample: ?rsp-prop-include=config-only
197 method:
198 description: The HTTP method used for the request to the APIC
199 returned: failure or debug
200 type: str
201 sample: POST
202 response:
203 description: The HTTP response from the APIC
204 returned: failure or debug
205 type: str
206 sample: OK (30 bytes)
207 status:
208 description: The HTTP status from the APIC
209 returned: failure or debug
210 type: int
211 sample: 200
212 url:
213 description: The HTTP url used for the request to the APIC
214 returned: failure or debug
215 type: str
216 sample: https://10.11.12.13/api/mo/uni/tn-production.json
217 """
218
219 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
220 from ansible_collections.cisco.aci.plugins.module_utils.constants import MATCH_TYPE_GROUP_MAPPING, MATCH_FIRMWARE_NODES_TYPE_MAPPING
221 from ansible.module_utils.basic import AnsibleModule
222
223
224 def main():
225 argument_spec = aci_argument_spec()
226 argument_spec.update(aci_annotation_spec())
227 argument_spec.update(aci_owner_spec())
228 argument_spec.update(
229 group=dict(type="str"), # Not required for querying all objects
230 policy=dict(type="str", aliases=["maintenancepol"]), # Not required for querying all objects
231 firmware_nodes_type=dict(type="str", choices=list(MATCH_FIRMWARE_NODES_TYPE_MAPPING.keys())),
232 type_group=dict(type="str", choices=list(MATCH_TYPE_GROUP_MAPPING.keys())),
233 description=dict(type="str", aliases=["descr"]),
234 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
235 name_alias=dict(type="str"),
236 )
237
238 module = AnsibleModule(
239 argument_spec=argument_spec,
240 supports_check_mode=True,
241 required_if=[
242 ["state", "absent", ["group"]],
243 ["state", "present", ["group"]],
244 ],
245 )
246
247 state = module.params.get("state")
248 group = module.params.get("group")
249 policy = module.params.get("policy")
250 firmware_nodes_type = MATCH_FIRMWARE_NODES_TYPE_MAPPING.get(module.params.get("firmware_nodes_type"))
251 type_group = MATCH_TYPE_GROUP_MAPPING.get(module.params.get("type_group"))
252 description = module.params.get("description")
253 name_alias = module.params.get("name_alias")
254 aci = ACIModule(module)
255 aci.construct_url(
256 root_class=dict(
257 aci_class="maintMaintGrp",
258 aci_rn="fabric/maintgrp-{0}".format(group),
259 target_filter={"name": group},
260 module_object=group,
261 ),
262 child_classes=["maintRsMgrpp"],
263 )
264
265 aci.get_existing()
266
267 if state == "present":
268 aci.payload(
269 aci_class="maintMaintGrp",
270 class_config=dict(
271 name=group,
272 fwtype=firmware_nodes_type,
273 type=type_group,
274 descr=description,
275 nameAlias=name_alias,
276 ),
277 child_configs=[
278 dict(
279 maintRsMgrpp=dict(
280 attributes=dict(
281 tnMaintMaintPName=policy,
282 ),
283 ),
284 ),
285 ],
286 )
287
288 aci.get_diff(aci_class="maintMaintGrp")
289
290 aci.post_config()
291
292 elif state == "absent":
293 aci.delete_config()
294
295 aci.exit_json()
296
297
298 if __name__ == "__main__":
299 main()