Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, 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_template_anp_epg_subnet
16 short_description: Manage EPG subnets in schema templates
17 description:
18 - Manage EPG subnets in schema templates 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 template:
28 description:
29 - The name of the template to change.
30 type: str
31 required: true
32 anp:
33 description:
34 - The name of the ANP.
35 type: str
36 required: true
37 epg:
38 description:
39 - The name of the EPG to manage.
40 type: str
41 required: true
42 subnet:
43 description:
44 - The IP range in CIDR notation.
45 type: str
46 required: true
47 aliases: [ ip ]
48 description:
49 description:
50 - The description of this subnet.
51 type: str
52 scope:
53 description:
54 - The scope of the subnet.
55 type: str
56 default: private
57 choices: [ private, public ]
58 shared:
59 description:
60 - Whether this subnet is shared between VRFs.
61 type: bool
62 default: false
63 no_default_gateway:
64 description:
65 - Whether this subnet has a default gateway.
66 type: bool
67 default: false
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 EPG
82 cisco.mso.mso_schema_template_anp_epg_subnet:
83 host: mso_host
84 username: admin
85 password: SomeSecretPassword
86 schema: Schema 1
87 template: Template 1
88 anp: ANP 1
89 epg: EPG 1
90 subnet: 10.0.0.0/24
91 state: present
92
93 - name: Remove a subnet from an EPG
94 cisco.mso.mso_schema_template_anp_epg_subnet:
95 host: mso_host
96 username: admin
97 password: SomeSecretPassword
98 schema: Schema 1
99 template: Template 1
100 anp: ANP 1
101 epg: EPG 1
102 subnet: 10.0.0.0/24
103 state: absent
104
105 - name: Query a specific EPG subnet
106 cisco.mso.mso_schema_template_anp_epg_subnet:
107 host: mso_host
108 username: admin
109 password: SomeSecretPassword
110 schema: Schema 1
111 template: Template 1
112 anp: ANP 1
113 epg: EPG 1
114 subnet: 10.0.0.0/24
115 state: query
116 register: query_result
117
118 - name: Query all EPGs subnets
119 cisco.mso.mso_schema_template_anp_epg_subnet:
120 host: mso_host
121 username: admin
122 password: SomeSecretPassword
123 schema: Schema 1
124 template: Template 1
125 anp: ANP 1
126 state: query
127 register: query_result
128 """
129
130 RETURN = r"""
131 """
132
133 from ansible.module_utils.basic import AnsibleModule
134 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_epg_subnet_spec
135
136
137 def main():
138 argument_spec = mso_argument_spec()
139 argument_spec.update(
140 schema=dict(type="str", required=True),
141 template=dict(type="str", required=True),
142 anp=dict(type="str", required=True),
143 epg=dict(type="str", required=True),
144 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
145 )
146 argument_spec.update(mso_epg_subnet_spec())
147
148 module = AnsibleModule(
149 argument_spec=argument_spec,
150 supports_check_mode=True,
151 required_if=[
152 ["state", "absent", ["subnet"]],
153 ["state", "present", ["subnet"]],
154 ],
155 )
156
157 schema = module.params.get("schema")
158 template = module.params.get("template").replace(" ", "")
159 anp = module.params.get("anp")
160 epg = module.params.get("epg")
161 subnet = module.params.get("subnet")
162 description = module.params.get("description")
163 scope = module.params.get("scope")
164 shared = module.params.get("shared")
165 no_default_gateway = module.params.get("no_default_gateway")
166 state = module.params.get("state")
167
168 mso = MSOModule(module)
169
170 # Get schema
171 schema_id, schema_path, schema_obj = mso.query_schema(schema)
172
173 # Get template
174 templates = [t.get("name") for t in schema_obj.get("templates")]
175 if template not in templates:
176 mso.fail_json(
177 msg="Provided template '{template}' does not exist. Existing templates: {templates}".format(template=template, templates=", ".join(templates))
178 )
179 template_idx = templates.index(template)
180
181 # Get ANP
182 anps = [a.get("name") for a in schema_obj.get("templates")[template_idx]["anps"]]
183 if anp not in anps:
184 mso.fail_json(msg="Provided anp '{anp}' does not exist. Existing anps: {anps}".format(anp=anp, anps=", ".join(anps)))
185 anp_idx = anps.index(anp)
186
187 # Get EPG
188 epgs = [e.get("name") for e in schema_obj.get("templates")[template_idx]["anps"][anp_idx]["epgs"]]
189 if epg not in epgs:
190 mso.fail_json(msg="Provided epg '{epg}' does not exist. Existing epgs: {epgs}".format(epg=epg, epgs=", ".join(epgs)))
191 epg_idx = epgs.index(epg)
192
193 # Get Subnet
194 subnets = [s.get("ip") for s in schema_obj.get("templates")[template_idx]["anps"][anp_idx]["epgs"][epg_idx]["subnets"]]
195 if subnet in subnets:
196 subnet_idx = subnets.index(subnet)
197 # FIXME: Changes based on index are DANGEROUS
198 subnet_path = "/templates/{0}/anps/{1}/epgs/{2}/subnets/{3}".format(template, anp, epg, subnet_idx)
199 mso.existing = schema_obj.get("templates")[template_idx]["anps"][anp_idx]["epgs"][epg_idx]["subnets"][subnet_idx]
200
201 if state == "query":
202 if subnet is None:
203 mso.existing = schema_obj.get("templates")[template_idx]["anps"][anp_idx]["epgs"][epg_idx]["subnets"]
204 elif not mso.existing:
205 mso.fail_json(msg="Subnet '{subnet}' not found".format(subnet=subnet))
206 mso.exit_json()
207
208 subnets_path = "/templates/{0}/anps/{1}/epgs/{2}/subnets".format(template, anp, epg)
209 ops = []
210
211 mso.previous = mso.existing
212 if state == "absent":
213 if mso.existing:
214 mso.existing = {}
215 ops.append(dict(op="remove", path=subnet_path))
216
217 elif state == "present":
218 if not mso.existing:
219 if description is None:
220 description = subnet
221 if scope is None:
222 scope = "private"
223 if shared is None:
224 shared = False
225 if no_default_gateway is None:
226 no_default_gateway = False
227
228 payload = dict(
229 ip=subnet,
230 description=description,
231 scope=scope,
232 shared=shared,
233 noDefaultGateway=no_default_gateway,
234 )
235
236 mso.sanitize(payload, collate=True)
237
238 if mso.existing:
239 ops.append(dict(op="replace", path=subnet_path, value=mso.sent))
240 else:
241 ops.append(dict(op="add", path=subnets_path + "/-", value=mso.sent))
242
243 mso.existing = mso.proposed
244
245 if not module.check_mode:
246 mso.request(schema_path, method="PATCH", data=ops)
247
248 mso.exit_json()
249
250
251 if __name__ == "__main__":
252 main()