Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_schema_template_bd_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_bd_subnet
16 short_description: Manage BD subnets in schema templates
17 description:
18 - Manage BD 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 bd:
33 description:
34 - The name of the BD to manage.
35 type: str
36 required: true
37 subnet:
38 description:
39 - The IP range in CIDR notation.
40 type: str
41 aliases: [ ip ]
42 description:
43 description:
44 - The description of this subnet.
45 type: str
46 is_virtual_ip:
47 description:
48 - Treat as Virtual IP Address
49 type: bool
50 default: false
51 scope:
52 description:
53 - The scope of the subnet.
54 type: str
55 choices: [ private, public ]
56 shared:
57 description:
58 - Whether this subnet is shared between VRFs.
59 type: bool
60 default: false
61 no_default_gateway:
62 description:
63 - Whether this subnet has a default gateway.
64 type: bool
65 default: false
66 querier:
67 description:
68 - Whether this subnet is an IGMP querier.
69 type: bool
70 default: false
71 primary:
72 description:
73 - Treat as Primary Subnet.
74 - There can be only one primary subnet per address family under a BD.
75 - This option can only be used on versions of MSO that are 3.1.1h or greater.
76 type: bool
77 default: false
78 state:
79 description:
80 - Use C(present) or C(absent) for adding or removing.
81 - Use C(query) for listing an object or multiple objects.
82 type: str
83 choices: [ absent, present, query ]
84 default: present
85 notes:
86 - Due to restrictions of the MSO REST API concurrent modifications to BD subnets can be dangerous and corrupt data.
87 extends_documentation_fragment: cisco.mso.modules
88 """
89
90 EXAMPLES = r"""
91 - name: Add a new subnet to a BD
92 cisco.mso.mso_schema_template_bd_subnet:
93 host: mso_host
94 username: admin
95 password: SomeSecretPassword
96 schema: Schema 1
97 template: Template 1
98 bd: BD 1
99 subnet: 10.0.0.0/24
100 state: present
101
102 - name: Remove a subset from a BD
103 cisco.mso.mso_schema_template_bd_subnet:
104 host: mso_host
105 username: admin
106 password: SomeSecretPassword
107 schema: Schema 1
108 template: Template 1
109 bd: BD 1
110 subnet: 10.0.0.0/24
111 state: absent
112
113 - name: Query a specific BD subnet
114 cisco.mso.mso_schema_template_bd_subnet:
115 host: mso_host
116 username: admin
117 password: SomeSecretPassword
118 schema: Schema 1
119 template: Template 1
120 bd: BD 1
121 subnet: 10.0.0.0/24
122 state: query
123 register: query_result
124
125 - name: Query all BD subnets
126 cisco.mso.mso_schema_template_bd_subnet:
127 host: mso_host
128 username: admin
129 password: SomeSecretPassword
130 schema: Schema 1
131 template: Template 1
132 bd: BD 1
133 state: query
134 register: query_result
135 """
136
137 RETURN = r"""
138 """
139
140 from ansible.module_utils.basic import AnsibleModule
141 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
142
143
144 def main():
145 argument_spec = mso_argument_spec()
146 argument_spec.update(
147 schema=dict(type="str", required=True),
148 template=dict(type="str", required=True),
149 bd=dict(type="str", required=True),
150 subnet=dict(type="str", aliases=["ip"]),
151 description=dict(type="str"),
152 is_virtual_ip=dict(type="bool", default=False),
153 scope=dict(type="str", choices=["private", "public"]),
154 shared=dict(type="bool", default=False),
155 no_default_gateway=dict(type="bool", default=False),
156 querier=dict(type="bool", default=False),
157 primary=dict(type="bool", default=False),
158 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
159 )
160
161 module = AnsibleModule(
162 argument_spec=argument_spec,
163 supports_check_mode=True,
164 required_if=[
165 ["state", "absent", ["subnet"]],
166 ["state", "present", ["subnet"]],
167 ],
168 )
169
170 schema = module.params.get("schema")
171 template = module.params.get("template").replace(" ", "")
172 bd = module.params.get("bd")
173 subnet = module.params.get("subnet")
174 description = module.params.get("description")
175 is_virtual_ip = module.params.get("is_virtual_ip")
176 scope = module.params.get("scope")
177 shared = module.params.get("shared")
178 no_default_gateway = module.params.get("no_default_gateway")
179 querier = module.params.get("querier")
180 primary = module.params.get("primary")
181 state = module.params.get("state")
182
183 mso = MSOModule(module)
184
185 # Get schema
186 schema_id, schema_path, schema_obj = mso.query_schema(schema)
187
188 # Get template
189 templates = [t.get("name") for t in schema_obj.get("templates")]
190 if template not in templates:
191 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates)))
192 template_idx = templates.index(template)
193
194 # Get BD
195 bds = [b.get("name") for b in schema_obj.get("templates")[template_idx]["bds"]]
196 if bd not in bds:
197 mso.fail_json(msg="Provided BD '{0}' does not exist. Existing BDs: {1}".format(bd, ", ".join(bds)))
198 bd_idx = bds.index(bd)
199
200 # Get Subnet
201 subnets = [s.get("ip") for s in schema_obj.get("templates")[template_idx]["bds"][bd_idx]["subnets"]]
202 if subnet in subnets:
203 subnet_idx = subnets.index(subnet)
204 # FIXME: Changes based on index are DANGEROUS
205 subnet_path = "/templates/{0}/bds/{1}/subnets/{2}".format(template, bd, subnet_idx)
206 mso.existing = schema_obj.get("templates")[template_idx]["bds"][bd_idx]["subnets"][subnet_idx]
207
208 if state == "query":
209 if subnet is None:
210 mso.existing = schema_obj.get("templates")[template_idx]["bds"][bd_idx]["subnets"]
211 elif not mso.existing:
212 mso.fail_json(msg="Subnet IP '{subnet}' not found".format(subnet=subnet))
213 mso.exit_json()
214
215 subnets_path = "/templates/{0}/bds/{1}/subnets".format(template, bd)
216 ops = []
217
218 mso.previous = mso.existing
219 if state == "absent":
220 if mso.existing:
221 mso.sent = mso.existing = {}
222 ops.append(dict(op="remove", path=subnet_path))
223
224 elif state == "present":
225 if not mso.existing:
226 if description is None:
227 description = subnet
228 if scope is None:
229 scope = "private"
230
231 payload = dict(
232 ip=subnet,
233 description=description,
234 virtual=is_virtual_ip,
235 scope=scope,
236 shared=shared,
237 noDefaultGateway=no_default_gateway,
238 querier=querier,
239 primary=primary,
240 )
241
242 mso.sanitize(payload, collate=True)
243
244 if mso.existing:
245 ops.append(dict(op="replace", path=subnet_path, value=mso.sent))
246 else:
247 ops.append(dict(op="add", path=subnets_path + "/-", value=mso.sent))
248
249 mso.existing = mso.proposed
250
251 if not module.check_mode:
252 mso.request(schema_path, method="PATCH", data=ops)
253
254 mso.exit_json()
255
256
257 if __name__ == "__main__":
258 main()