Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Anvitha Jain (@anvitha-jain) <[email protected]>
5 # Copyright: (c) 2024, Akini Ross (@akinross) <[email protected]>
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: mso_schema_template_external_epg_subnet
17 short_description: Manage External EPG subnets in schema templates
18 description:
19 - Manage External EPG subnets in schema templates on Cisco ACI Multi-Site.
20 author:
21 - Devarshi Shah (@devarshishah3)
22 - Anvitha Jain (@anvitha-jain)
23 - Akini Ross (@akinross)
24 version_added: '0.0.8'
25 options:
26 schema:
27 description:
28 - The name of the schema.
29 type: str
30 required: true
31 template:
32 description:
33 - The name of the template to change.
34 type: str
35 required: true
36 external_epg:
37 description:
38 - The name of the External EPG to manage.
39 type: str
40 required: true
41 subnet:
42 description:
43 - The IP range in CIDR notation.
44 type: str
45 name:
46 description:
47 - The name of the External EPG Subnet.
48 type: str
49 scope:
50 description:
51 - The scope parameter contains two sections 1. Route Control and 2. External EPG Classification.
52 - The existing Route Control parameters are C(export-rtctrl) for Export Route Control, C(import-rtctrl) for Import Route Control
53 - and C(shared-rtctrl) for Shared Route Control
54 - The existing External EPG Classification parameters are C(import-security) for External Subnets for External EPG
55 - and C(shared-security) for Shared Security Import
56 - The C(shared-security) for Shared Security Import can only be used when External Subnets for External EPG is present
57 type: list
58 elements: str
59 default: []
60 aggregate:
61 description:
62 - The aggregate option aggregates shared routes for the subnet.
63 - Use C(shared-rtctrl) to add Aggregate Shared Routes
64 - The C(shared-rtctrl) option can only be used when scope parameter Shared Route Control in the Route Control section is selected.
65 type: list
66 elements: str
67 default: []
68 state:
69 description:
70 - Use C(present) or C(absent) for adding or removing.
71 - Use C(query) for listing an object or multiple objects.
72 type: str
73 choices: [ absent, present, query ]
74 default: present
75 notes:
76 - Due to restrictions of the MSO REST API concurrent modifications to EPG subnets can be dangerous and corrupt data.
77 extends_documentation_fragment: cisco.mso.modules
78 """
79
80 EXAMPLES = r"""
81 - name: Add a new subnet to an External EPG
82 cisco.mso.mso_schema_template_external_epg_subnet:
83 host: mso_host
84 username: admin
85 password: SomeSecretPassword
86 schema: Schema 1
87 template: Template 1
88 external_epg: EPG 1
89 subnet: 10.0.0.0/24
90 state: present
91
92 - name: Remove a subnet from an External EPG
93 cisco.mso.mso_schema_template_external_epg_subnet:
94 host: mso_host
95 username: admin
96 password: SomeSecretPassword
97 schema: Schema 1
98 template: Template 1
99 external_epg: EPG 1
100 subnet: 10.0.0.0/24
101 state: absent
102
103 - name: Query a specific External EPG subnet
104 cisco.mso.mso_schema_template_external_epg_subnet:
105 host: mso_host
106 username: admin
107 password: SomeSecretPassword
108 schema: Schema 1
109 template: Template 1
110 external_epg: EPG 1
111 subnet: 10.0.0.0/24
112 state: query
113 register: query_result
114
115 - name: Query all External EPGs subnets
116 cisco.mso.mso_schema_template_external_epg_subnet:
117 host: mso_host
118 username: admin
119 password: SomeSecretPassword
120 schema: Schema 1
121 template: Template 1
122 state: query
123 register: query_result
124 """
125
126 RETURN = r"""
127 """
128
129 from ansible.module_utils.basic import AnsibleModule
130 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
131
132
133 def main():
134 argument_spec = mso_argument_spec()
135 argument_spec.update(
136 schema=dict(type="str", required=True),
137 template=dict(type="str", required=True),
138 external_epg=dict(type="str", required=True),
139 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
140 subnet=dict(type="str"),
141 name=dict(type="str"),
142 scope=dict(type="list", elements="str", default=[]),
143 aggregate=dict(type="list", elements="str", default=[]),
144 )
145
146 module = AnsibleModule(
147 argument_spec=argument_spec,
148 supports_check_mode=True,
149 required_if=[
150 ["state", "absent", ["subnet"]],
151 ["state", "present", ["subnet"]],
152 ],
153 )
154
155 schema = module.params.get("schema")
156 template = module.params.get("template").replace(" ", "")
157 external_epg = module.params.get("external_epg")
158 subnet = module.params.get("subnet")
159 name = module.params.get("name")
160 scope = module.params.get("scope")
161 aggregate = module.params.get("aggregate")
162 state = module.params.get("state")
163
164 mso = MSOModule(module)
165
166 # Get schema
167 schema_id, schema_path, schema_obj = mso.query_schema(schema)
168
169 # Get template
170 templates = [t.get("name") for t in schema_obj.get("templates")]
171 if template not in templates:
172 mso.fail_json(
173 msg="Provided template '{template}' does not exist. Existing templates: {templates}".format(template=template, templates=", ".join(templates))
174 )
175 template_idx = templates.index(template)
176
177 # Get EPG
178 external_epgs = [e.get("name") for e in schema_obj.get("templates")[template_idx]["externalEpgs"]]
179 if external_epg not in external_epgs:
180 mso.fail_json(msg="Provided External EPG '{epg}' does not exist. Existing epgs: {epgs}".format(epg=external_epg, epgs=", ".join(external_epgs)))
181 epg_idx = external_epgs.index(external_epg)
182
183 # Get Subnet
184 subnets = [s.get("ip") for s in schema_obj.get("templates")[template_idx]["externalEpgs"][epg_idx]["subnets"]]
185 if subnet in subnets:
186 subnet_idx = subnets.index(subnet)
187 # FIXME: Changes based on index are DANGEROUS
188 subnet_path = "/templates/{0}/externalEpgs/{1}/subnets/{2}".format(template, external_epg, subnet_idx)
189 mso.existing = schema_obj.get("templates")[template_idx]["externalEpgs"][epg_idx]["subnets"][subnet_idx]
190
191 if state == "query":
192 if subnet is None:
193 mso.existing = schema_obj.get("templates")[template_idx]["externalEpgs"][epg_idx]["subnets"]
194 elif not mso.existing:
195 mso.fail_json(msg="Subnet '{subnet}' not found".format(subnet=subnet))
196 mso.exit_json()
197
198 subnets_path = "/templates/{0}/externalEpgs/{1}/subnets".format(template, external_epg)
199 ops = []
200
201 mso.previous = mso.existing
202 if state == "absent":
203 if mso.existing:
204 mso.existing = {}
205 ops.append(dict(op="remove", path=subnet_path))
206
207 elif state == "present":
208 payload = dict(
209 ip=subnet,
210 scope=scope,
211 aggregate=aggregate,
212 name=name,
213 )
214
215 mso.sanitize(payload, collate=True)
216
217 if mso.existing:
218 ops.append(dict(op="replace", path=subnet_path, value=mso.sent))
219 else:
220 ops.append(dict(op="add", path=subnets_path + "/-", value=mso.sent))
221
222 mso.existing = mso.proposed
223
224 if not module.check_mode:
225 mso.request(schema_path, method="PATCH", data=ops)
226
227 mso.exit_json()
228
229
230 if __name__ == "__main__":
231 main()