Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2019, Dag Wieers (@dagwieers) <[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 ---
15 module: mso_schema_site_anp_epg_subnet
16 short_description: Manage site-local EPG subnets in schema template
17 description:
18 - Manage site-local EPG subnets in schema template on Cisco ACI Multi-Site.
19 author:
20 - Dag Wieers (@dagwieers)
21 options:
22 schema:
23 description:
24 - The name of the schema.
25 type: str
26 required: true
27 site:
28 description:
29 - The name of the site.
30 type: str
31 required: true
32 template:
33 description:
34 - The name of the template.
35 type: str
36 required: true
37 anp:
38 description:
39 - The name of the ANP.
40 type: str
41 required: true
42 epg:
43 description:
44 - The name of the EPG.
45 type: str
46 required: true
47 subnet:
48 description:
49 - The IP range in CIDR notation.
50 type: str
51 required: true
52 aliases: [ ip ]
53 description:
54 description:
55 - The description of this subnet.
56 type: str
57 scope:
58 description:
59 - The scope of the subnet.
60 type: str
61 default: private
62 choices: [ private, public ]
63 shared:
64 description:
65 - Whether this subnet is shared between VRFs.
66 type: bool
67 default: false
68 no_default_gateway:
69 description:
70 - Whether this subnet has a default gateway.
71 type: bool
72 default: false
73 state:
74 description:
75 - Use C(present) or C(absent) for adding or removing.
76 - Use C(query) for listing an object or multiple objects.
77 type: str
78 choices: [ absent, present, query ]
79 default: present
80 notes:
81 - The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index.
82 This can cause silent corruption on concurrent access when changing/removing on object as
83 the wrong object may be referenced. This module is affected by this deficiency.
84 seealso:
85 - module: cisco.mso.mso_schema_site_anp_epg
86 - module: cisco.mso.mso_schema_template_anp_epg_subnet
87 extends_documentation_fragment: cisco.mso.modules
88 """
89
90 EXAMPLES = r"""
91 - name: Add a new subnet to a site EPG
92 cisco.mso.mso_schema_site_anp_epg_subnet:
93 host: mso_host
94 username: admin
95 password: SomeSecretPassword
96 schema: Schema1
97 site: Site1
98 template: Template1
99 anp: ANP1
100 epg: EPG1
101 subnet: 10.0.0.0/24
102 state: present
103
104 - name: Remove a subnet from a site EPG
105 cisco.mso.mso_schema_site_anp_epg_subnet:
106 host: mso_host
107 username: admin
108 password: SomeSecretPassword
109 schema: Schema1
110 site: Site1
111 template: Template1
112 anp: ANP1
113 epg: EPG1
114 subnet: 10.0.0.0/24
115 state: absent
116
117 - name: Query a specific site EPG subnet
118 cisco.mso.mso_schema_site_anp_epg_subnet:
119 host: mso_host
120 username: admin
121 password: SomeSecretPassword
122 schema: Schema1
123 site: Site1
124 template: Template1
125 anp: ANP1
126 epg: EPG1
127 subnet: 10.0.0.0/24
128 state: query
129 register: query_result
130
131 - name: Query all site EPG subnets
132 cisco.mso.mso_schema_site_anp_epg_subnet:
133 host: mso_host
134 username: admin
135 password: SomeSecretPassword
136 schema: Schema1
137 site: Site1
138 template: Template1
139 anp: ANP1
140 state: query
141 register: query_result
142 """
143
144 RETURN = r"""
145 """
146
147 from ansible.module_utils.basic import AnsibleModule
148 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_epg_subnet_spec
149
150
151 def main():
152 argument_spec = mso_argument_spec()
153 argument_spec.update(
154 schema=dict(type="str", required=True),
155 site=dict(type="str", required=True),
156 template=dict(type="str", required=True),
157 anp=dict(type="str", required=True),
158 epg=dict(type="str", required=True),
159 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
160 )
161 argument_spec.update(mso_epg_subnet_spec())
162
163 module = AnsibleModule(
164 argument_spec=argument_spec,
165 supports_check_mode=True,
166 required_if=[
167 ["state", "absent", ["subnet"]],
168 ["state", "present", ["subnet"]],
169 ],
170 )
171
172 schema = module.params.get("schema")
173 site = module.params.get("site")
174 template = module.params.get("template").replace(" ", "")
175 anp = module.params.get("anp")
176 epg = module.params.get("epg")
177 subnet = module.params.get("subnet")
178 description = module.params.get("description")
179 scope = module.params.get("scope")
180 shared = module.params.get("shared")
181 no_default_gateway = module.params.get("no_default_gateway")
182 state = module.params.get("state")
183
184 mso = MSOModule(module)
185
186 # Get schema objects
187 schema_id, schema_path, schema_obj = mso.query_schema(schema)
188
189 # Get site
190 site_id = mso.lookup_site(site)
191
192 # Get site_idx
193 if not schema_obj.get("sites"):
194 mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template))
195 sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")]
196 if (site_id, template) not in sites:
197 mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ", ".join(sites)))
198
199 # Schema-access uses indexes
200 site_idx = sites.index((site_id, template))
201 # Path-based access uses site_id-template
202 site_template = "{0}-{1}".format(site_id, template)
203
204 # Get ANP
205 anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp)
206 anps = [a.get("anpRef") for a in schema_obj.get("sites")[site_idx]["anps"]]
207 if anp_ref not in anps:
208 mso.fail_json(msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(anp, ", ".join(anps)))
209 anp_idx = anps.index(anp_ref)
210
211 # Get EPG
212 epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg)
213 epgs = [e.get("epgRef") for e in schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"]]
214 if epg_ref not in epgs:
215 mso.fail_json(msg="Provided epg '{0}' does not exist. Existing epgs: {1}".format(epg, ", ".join(epgs)))
216 epg_idx = epgs.index(epg_ref)
217
218 # Get Subnet
219 subnets = [s.get("ip") for s in schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"][epg_idx]["subnets"]]
220 if subnet in subnets:
221 subnet_idx = subnets.index(subnet)
222 # FIXME: Changes based on index are DANGEROUS
223 subnet_path = "/sites/{0}/anps/{1}/epgs/{2}/subnets/{3}".format(site_template, anp, epg, subnet_idx)
224 mso.existing = schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"][epg_idx]["subnets"][subnet_idx]
225
226 if state == "query":
227 if subnet is None:
228 mso.existing = schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"][epg_idx]["subnets"]
229 elif not mso.existing:
230 mso.fail_json(msg="Subnet '{subnet}' not found".format(subnet=subnet))
231 mso.exit_json()
232
233 subnets_path = "/sites/{0}/anps/{1}/epgs/{2}/subnets".format(site_template, anp, epg)
234 ops = []
235
236 mso.previous = mso.existing
237 if state == "absent":
238 if mso.existing:
239 mso.sent = mso.existing = {}
240 ops.append(dict(op="remove", path=subnet_path))
241
242 elif state == "present":
243 if not mso.existing:
244 if description is None:
245 description = subnet
246 if scope is None:
247 scope = "private"
248 if shared is None:
249 shared = False
250 if no_default_gateway is None:
251 no_default_gateway = False
252
253 payload = dict(
254 ip=subnet,
255 description=description,
256 scope=scope,
257 shared=shared,
258 noDefaultGateway=no_default_gateway,
259 )
260
261 mso.sanitize(payload, collate=True)
262
263 if mso.existing:
264 ops.append(dict(op="replace", path=subnet_path, value=mso.sent))
265 else:
266 ops.append(dict(op="add", path=subnets_path + "/-", value=mso.sent))
267
268 mso.existing = mso.proposed
269
270 if not module.check_mode:
271 mso.request(schema_path, method="PATCH", data=ops)
272
273 mso.exit_json()
274
275
276 if __name__ == "__main__":
277 main()