Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_schema_site_bd.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_bd
16 short_description: Manage site-local Bridge Domains (BDs) in schema template
17 description:
18 - Manage site-local BDs 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 bd:
38 description:
39 - The name of the BD to manage.
40 type: str
41 aliases: [ name ]
42 host_route:
43 description:
44 - Whether host-based routing is enabled.
45 type: bool
46 svi_mac:
47 description:
48 - SVI MAC Address
49 type: str
50 state:
51 description:
52 - Use C(present) or C(absent) for adding or removing.
53 - Use C(query) for listing an object or multiple objects.
54 type: str
55 choices: [ absent, present, query ]
56 default: present
57 seealso:
58 - module: cisco.mso.mso_schema_site
59 - module: cisco.mso.mso_schema_site_bd_l3out
60 - module: cisco.mso.mso_schema_site_bd_subnet
61 - module: cisco.mso.mso_schema_template_bd
62 extends_documentation_fragment: cisco.mso.modules
63 """
64
65 EXAMPLES = r"""
66 - name: Add a new site BD
67 cisco.mso.mso_schema_site_bd:
68 host: mso_host
69 username: admin
70 password: SomeSecretPassword
71 schema: Schema1
72 site: Site1
73 template: Template1
74 bd: BD1
75 state: present
76
77 - name: Remove a site BD
78 cisco.mso.mso_schema_site_bd:
79 host: mso_host
80 username: admin
81 password: SomeSecretPassword
82 schema: Schema1
83 site: Site1
84 template: Template1
85 bd: BD1
86 state: absent
87
88 - name: Query a specific site BD
89 cisco.mso.mso_schema_site_bd:
90 host: mso_host
91 username: admin
92 password: SomeSecretPassword
93 schema: Schema1
94 site: Site1
95 template: Template1
96 bd: BD1
97 state: query
98 register: query_result
99
100 - name: Query all site BDs
101 cisco.mso.mso_schema_site_bd:
102 host: mso_host
103 username: admin
104 password: SomeSecretPassword
105 schema: Schema1
106 site: Site1
107 template: Template1
108 state: query
109 register: query_result
110 """
111
112 RETURN = r"""
113 """
114
115 from ansible.module_utils.basic import AnsibleModule
116 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
117
118
119 def main():
120 argument_spec = mso_argument_spec()
121 argument_spec.update(
122 schema=dict(type="str", required=True),
123 site=dict(type="str", required=True),
124 template=dict(type="str", required=True),
125 bd=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects
126 host_route=dict(type="bool"),
127 svi_mac=dict(type="str"),
128 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
129 )
130
131 module = AnsibleModule(
132 argument_spec=argument_spec,
133 supports_check_mode=True,
134 required_if=[
135 ["state", "absent", ["bd"]],
136 ["state", "present", ["bd"]],
137 ],
138 )
139
140 schema = module.params.get("schema")
141 site = module.params.get("site")
142 template = module.params.get("template").replace(" ", "")
143 bd = module.params.get("bd")
144 host_route = module.params.get("host_route")
145 svi_mac = module.params.get("svi_mac")
146 state = module.params.get("state")
147
148 mso = MSOModule(module)
149
150 # Get schema objects
151 schema_id, schema_path, schema_obj = mso.query_schema(schema)
152
153 # Get template
154 templates = [t.get("name") for t in schema_obj.get("templates")]
155 if template not in templates:
156 mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates)))
157
158 # Get site
159 site_id = mso.lookup_site(site)
160
161 # Get site_idx
162 if not schema_obj.get("sites"):
163 mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template))
164 sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")]
165 if (site_id, template) not in sites:
166 mso.fail_json(msg="Provided site-template association '{0}-{1}' does not exist.".format(site, template))
167
168 # Schema-access uses indexes
169 site_idx = sites.index((site_id, template))
170 # Path-based access uses site_id-template
171 site_template = "{0}-{1}".format(site_id, template)
172
173 # Get BD
174 bd_ref = mso.bd_ref(schema_id=schema_id, template=template, bd=bd)
175 bds = [v.get("bdRef") for v in schema_obj.get("sites")[site_idx]["bds"]]
176 if bd is not None and bd_ref in bds:
177 bd_idx = bds.index(bd_ref)
178 bd_path = "/sites/{0}/bds/{1}".format(site_template, bd)
179 mso.existing = schema_obj.get("sites")[site_idx]["bds"][bd_idx]
180 mso.existing["bdRef"] = mso.dict_from_ref(mso.existing.get("bdRef"))
181
182 if state == "query":
183 if bd is None:
184 mso.existing = schema_obj.get("sites")[site_idx]["bds"]
185 for bd in mso.existing:
186 bd["bdRef"] = mso.dict_from_ref(bd.get("bdRef"))
187 elif not mso.existing:
188 mso.fail_json(msg="BD '{bd}' not found".format(bd=bd))
189 mso.exit_json()
190
191 bds_path = "/sites/{0}/bds".format(site_template)
192 ops = []
193
194 mso.previous = mso.existing
195 if state == "absent":
196 if mso.existing:
197 mso.sent = mso.existing = {}
198 ops.append(dict(op="remove", path=bd_path))
199
200 elif state == "present":
201 if not mso.existing:
202 if host_route is None:
203 host_route = False
204
205 payload = dict(
206 bdRef=dict(
207 schemaId=schema_id,
208 templateName=template,
209 bdName=bd,
210 ),
211 hostBasedRouting=host_route,
212 )
213 if svi_mac is not None:
214 payload.update(mac=svi_mac)
215
216 mso.sanitize(payload, collate=True)
217
218 if mso.existing:
219 ops.append(dict(op="replace", path=bd_path, value=mso.sent))
220 else:
221 ops.append(dict(op="add", path=bds_path + "/-", value=mso.sent))
222
223 mso.existing = mso.proposed
224
225 if not module.check_mode and mso.existing != mso.previous:
226 mso.request(schema_path, method="PATCH", data=ops)
227
228 mso.exit_json()
229
230
231 if __name__ == "__main__":
232 main()