Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_tenant_bgp_peer_prefix_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]>
5
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: ndo_tenant_bgp_peer_prefix_policy
17 short_description: Manage BGP Peer Prefix Policies in Tenant Policy Templates on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage BGP Peer Prefix Policies in Tenant Policy Templates on Cisco Nexus Dashboard Orchestrator (NDO).
20 - This module is only supported on ND v3.1 (NDO v4.3) and later.
21 author:
22 - Akini Ross (@akinross)
23 options:
24 template:
25 description:
26 - The name of the tenant template.
27 type: str
28 aliases: [ tenant_template ]
29 required: true
30 name:
31 description:
32 - The name of the BGP Peer Prefix Policy.
33 type: str
34 aliases: [ bgp_peer_prefix_policy ]
35 uuid:
36 description:
37 - The UUID of the BGP Peer Prefix Policy.
38 - This parameter is required when the BGP Peer Prefix Policy O(name) needs to be updated.
39 type: str
40 description:
41 description:
42 - The description of the BGP Peer Prefix Policy.
43 - Providing an empty string will remove the O(description="") from the BGP Peer Prefix Policy.
44 type: str
45 action:
46 description:
47 - The action of the BGP Peer Prefix Policy.
48 - The default value is C(reject).
49 type: str
50 choices: [ log, reject, restart, shutdown ]
51 max_number_of_prefixes:
52 description:
53 - The maximum number of prefixes for the BGP Peer Prefix Policy.
54 - The value must be between 1 and 300000.
55 - The default value is 20000 prefixes.
56 type: int
57 aliases: [ max_prefix, max ]
58 threshold_percentage:
59 description:
60 - The threshold percentage of the BGP Peer Prefix Policy.
61 - The value must be between 1 and 100.
62 - The default value is 75%.
63 type: int
64 aliases: [ threshold ]
65 restart_time:
66 description:
67 - The restart time of the BGP Peer Prefix Policy in seconds.
68 - The value must be between 1 and 65535.
69 - The default value is 1 second.
70 type: int
71 aliases: [ restart ]
72 state:
73 description:
74 - Use C(absent) for removing.
75 - Use C(query) for listing an object or multiple objects.
76 - Use C(present) for creating or updating.
77 type: str
78 choices: [ absent, query, present ]
79 default: query
80 notes:
81 - The O(template) must exist before using this module in your playbook.
82 Use M(cisco.mso.ndo_template) to create the Tenant template.
83 seealso:
84 - module: cisco.mso.ndo_template
85 extends_documentation_fragment: cisco.mso.modules
86 """
87
88 EXAMPLES = r"""
89 - name: Create a new BGP Peer Prefix Policy object
90 cisco.mso.ndo_tenant_bgp_peer_prefix_policy:
91 host: mso_host
92 username: admin
93 password: SomeSecretPassword
94 template: tenant_template
95 name: bgp_peer_prefix_policy_1
96 action: restart
97 max_number_of_prefixes: 1000
98 threshold_percentage: 50
99 restart_time: 60
100 state: present
101 register: bgp_peer_prefix_policy_1
102
103 - name: Update a BGP Peer Prefix Policy object name with UUID
104 cisco.mso.ndo_tenant_bgp_peer_prefix_policy:
105 host: mso_host
106 username: admin
107 password: SomeSecretPassword
108 template: tenant_template
109 name: bgp_peer_prefix_policy_2
110 uuid: "{{ bgp_peer_prefix_policy_1.current.uuid }}"
111 state: present
112
113 - name: Query a BGP Peer Prefix Policy object with name
114 cisco.mso.ndo_tenant_bgp_peer_prefix_policy:
115 host: mso_host
116 username: admin
117 password: SomeSecretPassword
118 template: tenant_template
119 name: bgp_peer_prefix_policy_1
120 state: query
121 register: query_uuid
122
123 - name: Query a BGP Peer Prefix Policy object with UUID
124 cisco.mso.ndo_tenant_bgp_peer_prefix_policy:
125 host: mso_host
126 username: admin
127 password: SomeSecretPassword
128 template: tenant_template
129 uuid: "{{ bgp_peer_prefix_policy_1.current.uuid }}"
130 state: query
131 register: query_uuid
132
133 - name: Query all BGP Peer Prefix Policy objects in a Tenant Template
134 cisco.mso.ndo_tenant_bgp_peer_prefix_policy:
135 host: mso_host
136 username: admin
137 password: SomeSecretPassword
138 template: tenant_template
139 state: query
140 register: query_all
141
142 - name: Delete a BGP Peer Prefix Policy object with name
143 cisco.mso.ndo_tenant_bgp_peer_prefix_policy:
144 host: mso_host
145 username: admin
146 password: SomeSecretPassword
147 template: tenant_template
148 name: bgp_peer_prefix_policy_1
149 state: absent
150
151 - name: Delete a BGP Peer Prefix Policy object with UUID
152 cisco.mso.ndo_tenant_bgp_peer_prefix_policy:
153 host: mso_host
154 username: admin
155 password: SomeSecretPassword
156 template: tenant_template
157 uuid: "{{ bgp_peer_prefix_policy_1.current.uuid }}"
158 state: absent
159 """
160
161 RETURN = r"""
162 """
163
164 import copy
165 from ansible.module_utils.basic import AnsibleModule
166 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
167 from ansible_collections.cisco.mso.plugins.module_utils.template import MSOTemplate, KVPair
168
169
170 def main():
171 argument_spec = mso_argument_spec()
172 argument_spec.update(
173 template=dict(type="str", required=True, aliases=["tenant_template"]),
174 name=dict(type="str", aliases=["bgp_peer_prefix_policy"]),
175 uuid=dict(type="str"),
176 description=dict(type="str"),
177 action=dict(type="str", choices=["log", "reject", "restart", "shutdown"]),
178 max_number_of_prefixes=dict(type="int", aliases=["max_prefix", "max"]),
179 threshold_percentage=dict(type="int", aliases=["threshold"]),
180 restart_time=dict(type="int", aliases=["restart"]),
181 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
182 )
183
184 module = AnsibleModule(
185 argument_spec=argument_spec,
186 supports_check_mode=True,
187 required_if=[
188 ["state", "absent", ["name", "uuid"], True],
189 ["state", "present", ["name", "uuid"], True],
190 ],
191 )
192
193 mso = MSOModule(module)
194
195 template = module.params.get("template")
196 name = module.params.get("name")
197 uuid = module.params.get("uuid")
198 description = module.params.get("description")
199 action = module.params.get("action")
200 max_number_of_prefixes = module.params.get("max_number_of_prefixes")
201 threshold_percentage = module.params.get("threshold_percentage")
202 restart_time = module.params.get("restart_time")
203
204 state = module.params.get("state")
205
206 template_object = MSOTemplate(mso, "tenant", template)
207 template_object.validate_template("tenantPolicy")
208
209 bgp_peer_prefix_policies = template_object.template.get("tenantPolicyTemplate", {}).get("template", {}).get("bgpPeerPrefixPolicies", [])
210 object_description = "BGP Peer Prefix Policy"
211 bgp_peer_prefix_policy_attrs_path = None
212 match = None
213
214 if state in ["query", "absent"] and bgp_peer_prefix_policies == []:
215 mso.exit_json()
216 elif state == "query" and not (name or uuid):
217 mso.existing = bgp_peer_prefix_policies
218 elif bgp_peer_prefix_policies and (name or uuid):
219 match = template_object.get_object_by_key_value_pairs(
220 object_description, bgp_peer_prefix_policies, [KVPair("uuid", uuid) if uuid else KVPair("name", name)]
221 )
222 if match:
223 bgp_peer_prefix_policy_attrs_path = "/tenantPolicyTemplate/template/bgpPeerPrefixPolicies/{0}".format(match.index)
224 mso.existing = mso.previous = copy.deepcopy(match.details)
225
226 ops = []
227
228 if state == "present":
229 if uuid and not mso.existing:
230 mso.fail_json(msg="{0} with the UUID: '{1}' not found".format(object_description, uuid))
231
232 if mso.existing and match:
233 proposed_payload = copy.deepcopy(match.details)
234
235 if name and mso.existing.get("name") != name:
236 ops.append(dict(op="replace", path=bgp_peer_prefix_policy_attrs_path + "/name", value=name))
237 proposed_payload["name"] = name
238
239 if description is not None and mso.existing.get("description") != description:
240 ops.append(dict(op="replace", path=bgp_peer_prefix_policy_attrs_path + "/description", value=description))
241 proposed_payload["description"] = description
242
243 if action and mso.existing.get("action") != action:
244 ops.append(dict(op="replace", path=bgp_peer_prefix_policy_attrs_path + "/action", value=action))
245 proposed_payload["action"] = action
246
247 if max_number_of_prefixes and mso.existing.get("maxPrefixes") != max_number_of_prefixes:
248 ops.append(dict(op="replace", path=bgp_peer_prefix_policy_attrs_path + "/maxPrefixes", value=max_number_of_prefixes))
249 proposed_payload["maxPrefixes"] = max_number_of_prefixes
250
251 if threshold_percentage and mso.existing.get("threshold") != threshold_percentage:
252 ops.append(dict(op="replace", path=bgp_peer_prefix_policy_attrs_path + "/threshold", value=threshold_percentage))
253 proposed_payload["threshold"] = threshold_percentage
254
255 if restart_time and mso.existing.get("restartTime") != restart_time:
256 ops.append(dict(op="replace", path=bgp_peer_prefix_policy_attrs_path + "/restartTime", value=restart_time))
257 proposed_payload["restartTime"] = restart_time
258
259 mso.sanitize(proposed_payload, collate=True)
260 else:
261 payload = dict(name=name)
262 if description:
263 payload["description"] = description
264 if action:
265 payload["action"] = action
266 if max_number_of_prefixes:
267 payload["maxPrefixes"] = max_number_of_prefixes
268 if threshold_percentage:
269 payload["threshold"] = threshold_percentage
270 if restart_time:
271 payload["restartTime"] = restart_time
272
273 mso.sanitize(payload)
274 ops.append(dict(op="add", path="/tenantPolicyTemplate/template/bgpPeerPrefixPolicies/-", value=payload))
275
276 elif state == "absent":
277 if mso.existing and match:
278 ops.append(dict(op="remove", path=bgp_peer_prefix_policy_attrs_path))
279
280 if not module.check_mode and ops:
281 response_object = mso.request(template_object.template_path, method="PATCH", data=ops)
282 bgp_peer_prefix_policies = response_object.get("tenantPolicyTemplate", {}).get("template", {}).get("bgpPeerPrefixPolicies", [])
283 match = template_object.get_object_by_key_value_pairs(
284 object_description, bgp_peer_prefix_policies, [KVPair("uuid", uuid) if uuid else KVPair("name", name)]
285 )
286 if match:
287 mso.existing = match.details # When the state is present
288 else:
289 mso.existing = {} # When the state is absent
290 elif module.check_mode and state != "query": # When the state is present/absent with check mode
291 mso.existing = mso.proposed if state == "present" else {}
292
293 mso.exit_json()
294
295
296 if __name__ == "__main__":
297 main()