Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2019, Tim Knipper <[email protected]>
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": "community"}
11
12 DOCUMENTATION = r"""
13 ---
14 module: aci_interface_policy_cdp
15 short_description: Manage CDP interface policies (cdp:IfPol)
16 description:
17 - Manage CDP interface policies on Cisco ACI fabrics.
18 options:
19 cdp_policy:
20 description:
21 - The CDP interface policy name.
22 type: str
23 aliases: [ cdp_interface, name ]
24 description:
25 description:
26 - The description for the CDP interface policy name.
27 type: str
28 aliases: [ descr ]
29 admin_state:
30 description:
31 - Enable or Disable CDP state.
32 - The APIC defaults to C(true) when unset during creation.
33 type: bool
34 state:
35 description:
36 - Use C(present) or C(absent) for adding or removing.
37 - Use C(query) for listing an object or multiple objects.
38 type: str
39 choices: [ absent, present, query ]
40 default: present
41 name_alias:
42 description:
43 - The alias for the current object. This relates to the nameAlias field in ACI.
44 type: str
45 extends_documentation_fragment:
46 - cisco.aci.aci
47 - cisco.aci.annotation
48 - cisco.aci.owner
49
50 seealso:
51 - name: APIC Management Information Model reference
52 description: More information about the internal APIC class B(cdp:IfPol).
53 link: https://developer.cisco.com/docs/apic-mim-ref/
54 author:
55 - Tim Knipper (@tknipper11)
56 """
57
58 EXAMPLES = r"""
59 - name: Create CDP Interface Policy to enable CDP
60 cisco.aci.aci_interface_policy_cdp:
61 name: Ansible_CDP_Interface_Policy
62 host: apic.example.com
63 username: admin
64 password: adminpass
65 admin_state: true
66 state: present
67
68 - name: Create CDP Interface Policy to disable CDP
69 cisco.aci.aci_interface_policy_cdp:
70 name: Ansible_CDP_Interface_Policy
71 host: apic.example.com
72 username: admin
73 password: adminpass
74 admin_state: false
75 state: present
76
77 - name: Remove CDP Interface Policy
78 cisco.aci.aci_interface_policy_cdp:
79 name: Ansible_CDP_Interface_Policy
80 host: apic.example.com
81 username: admin
82 password: adminpass
83 output_level: debug
84 state: absent
85
86 - name: Query CDP Policy
87 cisco.aci.aci_interface_policy_cdp:
88 host: apic.example.com
89 username: admin
90 password: adminpass
91 state: query
92 """
93
94 RETURN = r"""
95 current:
96 description: The existing configuration from the APIC after the module has finished
97 returned: success
98 type: list
99 sample:
100 [
101 {
102 "cdpIfPol": {
103 "attributes": {
104 "adminSt": "disabled",
105 "annotation": "",
106 "descr": "Ansible Created CDP Test Policy",
107 "dn": "uni/infra/cdpIfP-Ansible_CDP_Test_Policy",
108 "name": "Ansible_CDP_Test_Policy",
109 "nameAlias": "",
110 "ownerKey": "",
111 "ownerTag": ""
112 }
113 }
114 }
115 ]
116 error:
117 description: The error information as returned from the APIC
118 returned: failure
119 type: dict
120 sample:
121 {
122 "code": "122",
123 "text": "unknown managed object class foo"
124 }
125 raw:
126 description: The raw output returned by the APIC REST API (xml or json)
127 returned: parse error
128 type: str
129 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
130 sent:
131 description: The actual/minimal configuration pushed to the APIC
132 returned: info
133 type: list
134 sample:
135 {
136 "fvTenant": {
137 "attributes": {
138 "descr": "Production environment"
139 }
140 }
141 }
142 previous:
143 description: The original configuration from the APIC before the module has started
144 returned: info
145 type: list
146 sample:
147 [
148 {
149 "fvTenant": {
150 "attributes": {
151 "descr": "Production",
152 "dn": "uni/tn-production",
153 "name": "production",
154 "nameAlias": "",
155 "ownerKey": "",
156 "ownerTag": ""
157 }
158 }
159 }
160 ]
161 proposed:
162 description: The assembled configuration from the user-provided parameters
163 returned: info
164 type: dict
165 sample:
166 {
167 "fvTenant": {
168 "attributes": {
169 "descr": "Production environment",
170 "name": "production"
171 }
172 }
173 }
174 filter_string:
175 description: The filter string used for the request
176 returned: failure or debug
177 type: str
178 sample: ?rsp-prop-include=config-only
179 method:
180 description: The HTTP method used for the request to the APIC
181 returned: failure or debug
182 type: str
183 sample: POST
184 response:
185 description: The HTTP response from the APIC
186 returned: failure or debug
187 type: str
188 sample: OK (30 bytes)
189 status:
190 description: The HTTP status from the APIC
191 returned: failure or debug
192 type: int
193 sample: 200
194 url:
195 description: The HTTP url used for the request to the APIC
196 returned: failure or debug
197 type: str
198 sample: https://10.11.12.13/api/mo/uni/tn-production.json
199 """
200 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
201
202 from ansible.module_utils.basic import AnsibleModule
203
204
205 def main():
206 argument_spec = aci_argument_spec()
207 argument_spec.update(aci_annotation_spec())
208 argument_spec.update(aci_owner_spec())
209 argument_spec.update(
210 cdp_policy=dict(type="str", required=False, aliases=["cdp_interface", "name"]), # Not required for querying all objects
211 description=dict(type="str", aliases=["descr"]),
212 admin_state=dict(type="bool"),
213 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
214 name_alias=dict(type="str"),
215 )
216
217 module = AnsibleModule(
218 argument_spec=argument_spec,
219 supports_check_mode=True,
220 required_if=[
221 ["state", "absent", ["cdp_policy"]],
222 ["state", "present", ["cdp_policy"]],
223 ],
224 )
225
226 aci = ACIModule(module)
227
228 cdp_policy = module.params.get("cdp_policy")
229 description = module.params.get("description")
230 admin_state = aci.boolean(module.params.get("admin_state"), "enabled", "disabled")
231 state = module.params.get("state")
232 name_alias = module.params.get("name_alias")
233
234 aci.construct_url(
235 root_class=dict(
236 aci_class="cdpIfPol",
237 aci_rn="infra/cdpIfP-{0}".format(cdp_policy),
238 module_object=cdp_policy,
239 target_filter={"name": cdp_policy},
240 ),
241 )
242
243 aci.get_existing()
244
245 if state == "present":
246 aci.payload(
247 aci_class="cdpIfPol",
248 class_config=dict(
249 name=cdp_policy,
250 descr=description,
251 adminSt=admin_state,
252 nameAlias=name_alias,
253 ),
254 )
255
256 aci.get_diff(aci_class="cdpIfPol")
257
258 aci.post_config()
259
260 elif state == "absent":
261 aci.delete_config()
262
263 aci.exit_json()
264
265
266 if __name__ == "__main__":
267 main()