| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, 2023, Anvitha Jain (@anvitha-jain) <[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_external_epg |
| 16 |
short_description: Manage External EPG in schema of sites. |
| 17 |
description: |
| 18 |
- Manage External EPG in schema of sites on Cisco ACI Multi-Site. |
| 19 |
- This module can only be used on versions of MSO that are 3.3 or greater. |
| 20 |
author: |
| 21 |
- Anvitha Jain (@anvitha-jain) |
| 22 |
options: |
| 23 |
schema: |
| 24 |
description: |
| 25 |
- The name of the schema. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
template: |
| 29 |
description: |
| 30 |
- The name of the template to change. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
l3out: |
| 34 |
description: |
| 35 |
- The L3Out associated with the external epg. |
| 36 |
- Required when site is of type on-premise. |
| 37 |
- In NDO versions over 4.2, the parameter is accessible only when an external EPG is |
| 38 |
- linked to the current schema-template's VRF. |
| 39 |
type: str |
| 40 |
aliases: [ l3out_name ] |
| 41 |
l3out_schema: |
| 42 |
description: |
| 43 |
- The schema that defines the referenced L3Out. |
| 44 |
- If this parameter is unspecified, it defaults to the current schema. |
| 45 |
type: str |
| 46 |
l3out_template: |
| 47 |
description: |
| 48 |
- The template that defines the referenced L3Out. |
| 49 |
- If this parameter is unspecified, it defaults to the current template. |
| 50 |
type: str |
| 51 |
l3out_on_apic: |
| 52 |
description: |
| 53 |
- If this parameter is specified, the constructed l3out reference will refer to a distinguished name (DN) in APIC. |
| 54 |
type: bool |
| 55 |
external_epg: |
| 56 |
description: |
| 57 |
- The name of the External EPG to be managed. |
| 58 |
type: str |
| 59 |
aliases: [ name ] |
| 60 |
site: |
| 61 |
description: |
| 62 |
- The name of the site. |
| 63 |
type: str |
| 64 |
required: true |
| 65 |
route_reachability: |
| 66 |
description: |
| 67 |
- Configures if an external EPG route is pointing to the internet or to an external remote network. |
| 68 |
- Only available when associated with an azure site. |
| 69 |
type: str |
| 70 |
choices: [ internet, site-ext ] |
| 71 |
default: internet |
| 72 |
state: |
| 73 |
description: |
| 74 |
- Use C(present) or C(absent) for adding or removing. |
| 75 |
- Use C(query) for listing an object or multiple objects. |
| 76 |
type: str |
| 77 |
choices: [ absent, present, query ] |
| 78 |
default: present |
| 79 |
seealso: |
| 80 |
- module: cisco.mso.mso_schema_template_external_epg |
| 81 |
extends_documentation_fragment: cisco.mso.modules |
| 82 |
""" |
| 83 |
|
| 84 |
EXAMPLES = r""" |
| 85 |
- name: Add a Site External EPG |
| 86 |
cisco.mso.mso_schema_site_external_epg: |
| 87 |
host: mso_host |
| 88 |
username: admin |
| 89 |
password: SomeSecretPassword |
| 90 |
schema: Schema 1 |
| 91 |
template: Template 1 |
| 92 |
external_epg: External EPG 1 |
| 93 |
l3out: L3out1 |
| 94 |
state: present |
| 95 |
|
| 96 |
- name: Remove a Site External EPG |
| 97 |
cisco.mso.mso_schema_site_external_epg: |
| 98 |
host: mso_host |
| 99 |
username: admin |
| 100 |
password: SomeSecretPassword |
| 101 |
schema: Schema 1 |
| 102 |
template: Template 1 |
| 103 |
external_epg: External EPG 1 |
| 104 |
l3out: L3out1 |
| 105 |
state: absent |
| 106 |
|
| 107 |
- name: Query a Site External EPG |
| 108 |
cisco.mso.mso_schema_site_external_epg: |
| 109 |
host: mso_host |
| 110 |
username: admin |
| 111 |
password: SomeSecretPassword |
| 112 |
schema: Schema 1 |
| 113 |
template: Template 1 |
| 114 |
external_epg: External EPG 1 |
| 115 |
l3out: L3out1 |
| 116 |
state: query |
| 117 |
register: query_result |
| 118 |
|
| 119 |
- name: Query all Site External EPGs |
| 120 |
cisco.mso.mso_schema_site_external_epg: |
| 121 |
host: mso_host |
| 122 |
username: admin |
| 123 |
password: SomeSecretPassword |
| 124 |
schema: Schema 1 |
| 125 |
template: Template 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 |
| 135 |
from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema |
| 136 |
|
| 137 |
|
| 138 |
def main(): |
| 139 |
argument_spec = mso_argument_spec() |
| 140 |
argument_spec.update( |
| 141 |
schema=dict(type="str", required=True), |
| 142 |
template=dict(type="str", required=True), |
| 143 |
site=dict(type="str", required=True), |
| 144 |
l3out=dict(type="str", aliases=["l3out_name"]), |
| 145 |
l3out_schema=dict(type="str"), |
| 146 |
l3out_template=dict(type="str"), |
| 147 |
l3out_on_apic=dict(type="bool"), |
| 148 |
external_epg=dict(type="str", aliases=["name"]), |
| 149 |
route_reachability=dict(type="str", default="internet", choices=["internet", "site-ext"]), |
| 150 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 151 |
) |
| 152 |
|
| 153 |
module = AnsibleModule( |
| 154 |
argument_spec=argument_spec, |
| 155 |
supports_check_mode=True, |
| 156 |
required_if=[ |
| 157 |
["state", "absent", ["external_epg"]], |
| 158 |
["state", "present", ["external_epg"]], |
| 159 |
], |
| 160 |
) |
| 161 |
|
| 162 |
schema = module.params.get("schema") |
| 163 |
template = module.params.get("template").replace(" ", "") |
| 164 |
site = module.params.get("site") |
| 165 |
external_epg = module.params.get("external_epg") |
| 166 |
l3out = module.params.get("l3out") |
| 167 |
l3out_schema = module.params.get("l3out_schema") |
| 168 |
l3out_template = module.params.get("l3out_template") |
| 169 |
l3out_on_apic = module.params.get("l3out_on_apic") |
| 170 |
route_reachability = module.params.get("route_reachability") |
| 171 |
state = module.params.get("state") |
| 172 |
|
| 173 |
mso = MSOModule(module) |
| 174 |
|
| 175 |
l3out_template = template if l3out_template is None else l3out_template.replace(" ", "") |
| 176 |
l3out_schema = schema if l3out_schema is None else l3out_schema |
| 177 |
l3out_schema_id = mso.lookup_schema(l3out_schema) |
| 178 |
|
| 179 |
mso_schema = MSOSchema(mso, schema, template, site) |
| 180 |
mso_objects = mso_schema.schema_objects |
| 181 |
|
| 182 |
mso_schema.set_template_external_epg(external_epg, fail_module=False) |
| 183 |
|
| 184 |
# Path-based access uses site_id-template |
| 185 |
site_template = "{0}-{1}".format(mso_objects.get("site").details.get("siteId"), template) |
| 186 |
|
| 187 |
payload = dict() |
| 188 |
op_path = "/sites/{0}/externalEpgs/-".format(site_template) |
| 189 |
|
| 190 |
# Get template External EPG |
| 191 |
if mso_objects.get("template_external_epg") is not None: |
| 192 |
ext_epg_ref = mso_objects.get("template_external_epg").details.get("externalEpgRef") |
| 193 |
external_epgs = [e.get("externalEpgRef") for e in mso_objects.get("site").details.get("externalEpgs")] |
| 194 |
|
| 195 |
# Get Site External EPG |
| 196 |
if ext_epg_ref in external_epgs: |
| 197 |
external_epg_idx = external_epgs.index(ext_epg_ref) |
| 198 |
mso.existing = mso_objects.get("site").details.get("externalEpgs")[external_epg_idx] |
| 199 |
op_path = "/sites/{0}/externalEpgs/{1}".format(site_template, external_epg) |
| 200 |
|
| 201 |
ops = [] |
| 202 |
l3out_dn = "" |
| 203 |
if state == "query": |
| 204 |
if external_epg is None: |
| 205 |
mso.existing = mso_objects.get("site").details.get("externalEpgs") |
| 206 |
elif not mso.existing: |
| 207 |
mso.fail_json(msg="External EPG '{external_epg}' not found".format(external_epg=external_epg)) |
| 208 |
mso.exit_json() |
| 209 |
|
| 210 |
mso.previous = mso.existing |
| 211 |
|
| 212 |
if state == "absent": |
| 213 |
if mso.existing: |
| 214 |
mso.sent = mso.existing = {} |
| 215 |
ops.append(dict(op="remove", path=op_path)) |
| 216 |
|
| 217 |
elif state == "present": |
| 218 |
# Get external EPGs type from template level and verify template_external_epg type. |
| 219 |
if mso_objects.get("template_external_epg") is not None and mso_objects.get("template_external_epg").details.get("extEpgType") != "cloud": |
| 220 |
if l3out is not None: |
| 221 |
path = "tenants/{0}".format(mso_objects.get("template").details.get("tenantId")) |
| 222 |
tenant_name = mso.request(path, method="GET").get("name") |
| 223 |
l3out_dn = "uni/tn-{0}/out-{1}".format(tenant_name, l3out) |
| 224 |
else: |
| 225 |
mso.fail_json(msg="L3Out cannot be empty when template external EPG type is 'on-premise'.") |
| 226 |
|
| 227 |
payload = dict( |
| 228 |
externalEpgRef=dict( |
| 229 |
schemaId=mso_schema.id, |
| 230 |
templateName=template, |
| 231 |
externalEpgName=external_epg, |
| 232 |
), |
| 233 |
l3outDn=l3out_dn, |
| 234 |
routeReachabilityInternetType=route_reachability, |
| 235 |
) |
| 236 |
|
| 237 |
if not l3out_on_apic: |
| 238 |
payload.update( |
| 239 |
l3outRef=dict( |
| 240 |
schemaId=l3out_schema_id, |
| 241 |
templateName=l3out_template, |
| 242 |
l3outName=l3out, |
| 243 |
), |
| 244 |
) |
| 245 |
|
| 246 |
mso.sanitize(payload, collate=True) |
| 247 |
|
| 248 |
if mso.existing: |
| 249 |
ops.append(dict(op="replace", path=op_path, value=mso.sent)) |
| 250 |
else: |
| 251 |
ops.append(dict(op="add", path=op_path, value=mso.sent)) |
| 252 |
|
| 253 |
mso.existing = mso.proposed |
| 254 |
|
| 255 |
if not module.check_mode and mso.proposed != mso.previous: |
| 256 |
mso.request(mso_schema.path, method="PATCH", data=ops) |
| 257 |
|
| 258 |
mso.exit_json() |
| 259 |
|
| 260 |
|
| 261 |
if __name__ == "__main__": |
| 262 |
main() |