Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]>
5 # Copyright: (c) 2021, Anvitha Jain (@anvitha-jain) <[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_site_vrf_region
17 short_description: Manage site-local VRF regions in schema template
18 description:
19 - Manage site-local VRF regions in schema template on Cisco ACI Multi-Site.
20 author:
21 - Anvitha Jain (@anvitha-jain)
22 - Dag Wieers (@dagwieers)
23 options:
24 schema:
25 description:
26 - The name of the schema.
27 type: str
28 required: true
29 site:
30 description:
31 - The name of the site.
32 type: str
33 required: true
34 template:
35 description:
36 - The name of the template.
37 type: str
38 required: true
39 vrf:
40 description:
41 - The name of the VRF.
42 type: str
43 required: true
44 region:
45 description:
46 - The name of the region to manage.
47 type: str
48 aliases: [ name ]
49 vpn_gateway_router:
50 description:
51 - Whether VPN Gateway Router is enabled or not.
52 type: bool
53 container_overlay:
54 description:
55 - The name of the context profile type.
56 - This is supported on versions of MSO that are 3.3 or greater.
57 type: bool
58 underlay_context_profile:
59 description:
60 - The name of the context profile type.
61 - This parameter can only be added when container_overlay is True.
62 - This is supported on versions of MSO that are 3.3 or greater.
63 type: dict
64 suboptions:
65 vrf:
66 description:
67 - The name of the VRF to associate with underlay context profile.
68 type: str
69 required: true
70 region:
71 description:
72 - The name of the region associated with underlay context profile VRF.
73 type: str
74 required: true
75 state:
76 description:
77 - Use C(present) or C(absent) for adding or removing.
78 - Use C(query) for listing an object or multiple objects.
79 type: str
80 choices: [ absent, present, query ]
81 default: present
82 notes:
83 - Due to restrictions of the MSO REST API, this module cannot create empty region (i.e. regions without cidrs)
84 Use the M(cisco.mso.mso_schema_site_vrf_region_cidr) to automatically create regions with cidrs.
85 seealso:
86 - module: cisco.mso.mso_schema_site_vrf
87 - module: cisco.mso.mso_schema_template_vrf
88 extends_documentation_fragment: cisco.mso.modules
89 """
90
91 EXAMPLES = r"""
92 - name: Remove VPN Gateway Router at site VRF Region
93 cisco.mso.mso_schema_site_vrf_region:
94 host: mso_host
95 username: admin
96 password: SomeSecretPassword
97 schema: Schema1
98 site: Site1
99 template: Template1
100 vrf: VRF1
101 region: us-west-1
102 vpn_gateway_router: false
103 state: present
104
105 - name: Remove a site VRF region
106 cisco.mso.mso_schema_site_vrf_region:
107 host: mso_host
108 username: admin
109 password: SomeSecretPassword
110 schema: Schema1
111 site: Site1
112 template: Template1
113 vrf: VRF1
114 region: us-west-1
115 state: absent
116
117 - name: Query a specific site VRF region
118 cisco.mso.mso_schema_site_vrf_region:
119 host: mso_host
120 username: admin
121 password: SomeSecretPassword
122 schema: Schema1
123 site: Site1
124 template: Template1
125 vrf: VRF1
126 region: us-west-1
127 state: query
128 register: query_result
129
130 - name: Query all site VRF regions
131 cisco.mso.mso_schema_site_vrf_region:
132 host: mso_host
133 username: admin
134 password: SomeSecretPassword
135 schema: Schema1
136 site: Site1
137 template: Template1
138 vrf: VRF1
139 state: query
140 register: query_result
141 """
142
143 RETURN = r"""
144 """
145
146 from ansible.module_utils.basic import AnsibleModule
147 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
148
149
150 def main():
151 argument_spec = mso_argument_spec()
152 argument_spec.update(
153 schema=dict(type="str", required=True),
154 site=dict(type="str", required=True),
155 template=dict(type="str", required=True),
156 vrf=dict(type="str", required=True),
157 region=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects
158 vpn_gateway_router=dict(type="bool"),
159 container_overlay=dict(type="bool"),
160 underlay_context_profile=dict(
161 type="dict",
162 options=dict(
163 vrf=dict(type="str", required=True),
164 region=dict(type="str", required=True),
165 ),
166 ),
167 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
168 )
169
170 module = AnsibleModule(
171 argument_spec=argument_spec,
172 supports_check_mode=True,
173 required_if=[
174 ["state", "absent", ["region"]],
175 ["state", "present", ["region"]],
176 ],
177 )
178
179 schema = module.params.get("schema")
180 site = module.params.get("site")
181 template = module.params.get("template").replace(" ", "")
182 vrf = module.params.get("vrf")
183 region = module.params.get("region")
184 vpn_gateway_router = module.params.get("vpn_gateway_router")
185 container_overlay = module.params.get("container_overlay")
186 underlay_context_profile = module.params.get("underlay_context_profile")
187 state = module.params.get("state")
188
189 mso = MSOModule(module)
190
191 # Get schema objects
192 schema_id, schema_path, schema_obj = mso.query_schema(schema)
193
194 # Get site
195 site_id = mso.lookup_site(site)
196
197 # Get site_idx
198 if not schema_obj.get("sites"):
199 mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template))
200 sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")]
201 if (site_id, template) not in sites:
202 mso.fail_json(msg="Provided site-template association '{0}-{1}' does not exist.".format(site, template))
203
204 # Schema-access uses indexes
205 site_idx = sites.index((site_id, template))
206 # Path-based access uses site_id-template
207 site_template = "{0}-{1}".format(site_id, template)
208
209 # Get VRF
210 vrf_ref = mso.vrf_ref(schema_id=schema_id, template=template, vrf=vrf)
211 vrfs = [v.get("vrfRef") for v in schema_obj.get("sites")[site_idx]["vrfs"]]
212 vrfs_name = [mso.dict_from_ref(v).get("vrfName") for v in vrfs]
213 if vrf_ref not in vrfs:
214 mso.fail_json(msg="Provided vrf '{0}' does not exist. Existing vrfs: {1}".format(vrf, ", ".join(vrfs_name)))
215 vrf_idx = vrfs.index(vrf_ref)
216
217 # Get Region
218 regions = [r.get("name") for r in schema_obj.get("sites")[site_idx]["vrfs"][vrf_idx]["regions"]]
219 if region is not None and region in regions:
220 region_idx = regions.index(region)
221 region_path = "/sites/{0}/vrfs/{1}/regions/{2}".format(site_template, vrf, region)
222 mso.existing = schema_obj.get("sites")[site_idx]["vrfs"][vrf_idx]["regions"][region_idx]
223
224 if state == "query":
225 if region is None:
226 mso.existing = schema_obj.get("sites")[site_idx]["vrfs"][vrf_idx]["regions"]
227 elif not mso.existing:
228 mso.fail_json(msg="Region '{region}' not found".format(region=region))
229 mso.exit_json()
230
231 regions_path = "/sites/{0}/vrfs/{1}/regions".format(site_template, vrf)
232 ops = []
233
234 mso.previous = mso.existing
235 if state == "absent":
236 if mso.existing:
237 mso.sent = mso.existing = {}
238 ops.append(dict(op="remove", path=region_path))
239
240 elif state == "present":
241 payload = dict(
242 name=region,
243 isVpnGatewayRouter=vpn_gateway_router,
244 )
245
246 if container_overlay:
247 payload["contextProfileType"] = "container-overlay"
248 if mso.existing:
249 underlay_dict = dict(
250 vrfRef=dict(schemaId=schema_id, templateName=template, vrfName=underlay_context_profile["vrf"]),
251 regionName=underlay_context_profile["region"],
252 )
253 payload["underlayCtxProfile"] = underlay_dict
254
255 mso.sanitize(payload, collate=True)
256 if mso.existing:
257 ops.append(dict(op="replace", path=region_path, value=mso.sent))
258 else:
259 ops.append(dict(op="add", path=regions_path + "/-", value=mso.sent))
260
261 mso.existing = mso.proposed
262
263 if not module.check_mode:
264 mso.request(schema_path, method="PATCH", data=ops)
265
266 mso.exit_json()
267
268
269 if __name__ == "__main__":
270 main()