| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> |
| 5 |
# Copyright: (c) 2025, Gaspard Micol (@gmicol) <[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_anp_epg |
| 17 |
short_description: Manage site-local Endpoint Groups (EPGs) in schema template |
| 18 |
description: |
| 19 |
- Manage site-local EPGs in schema template on Cisco ACI Multi-Site. |
| 20 |
author: |
| 21 |
- Dag Wieers (@dagwieers) |
| 22 |
- Gaspard Micol (@gmicol) |
| 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 |
anp: |
| 40 |
description: |
| 41 |
- The name of the ANP. |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
epg: |
| 45 |
description: |
| 46 |
- The name of the EPG to manage. |
| 47 |
type: str |
| 48 |
aliases: [ name ] |
| 49 |
private_link_label: |
| 50 |
description: |
| 51 |
- The private link label used to represent this subnet. |
| 52 |
- This parameter is available for MSO version greater than 3.3. |
| 53 |
type: str |
| 54 |
admin_state: |
| 55 |
description: |
| 56 |
- The EPG admin state. |
| 57 |
- Defaults to C(admin_up) when unset during creation. |
| 58 |
type: str |
| 59 |
choices: [ admin_up, admin_shut ] |
| 60 |
state: |
| 61 |
description: |
| 62 |
- Use C(present) or C(absent) for adding or removing. |
| 63 |
- Use C(query) for listing an object or multiple objects. |
| 64 |
type: str |
| 65 |
choices: [ absent, present, query ] |
| 66 |
default: present |
| 67 |
seealso: |
| 68 |
- module: cisco.mso.mso_schema_site_anp |
| 69 |
- module: cisco.mso.mso_schema_site_anp_epg_subnet |
| 70 |
- module: cisco.mso.mso_schema_template_anp_epg |
| 71 |
extends_documentation_fragment: cisco.mso.modules |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = r""" |
| 75 |
- name: Add a new site EPG |
| 76 |
cisco.mso.mso_schema_site_anp_epg: |
| 77 |
host: mso_host |
| 78 |
username: admin |
| 79 |
password: SomeSecretPassword |
| 80 |
schema: Schema1 |
| 81 |
site: Site1 |
| 82 |
template: Template1 |
| 83 |
anp: ANP1 |
| 84 |
epg: EPG1 |
| 85 |
state: present |
| 86 |
|
| 87 |
- name: Remove a site EPG |
| 88 |
cisco.mso.mso_schema_site_anp_epg: |
| 89 |
host: mso_host |
| 90 |
username: admin |
| 91 |
password: SomeSecretPassword |
| 92 |
schema: Schema1 |
| 93 |
site: Site1 |
| 94 |
template: Template1 |
| 95 |
anp: ANP1 |
| 96 |
epg: EPG1 |
| 97 |
state: absent |
| 98 |
|
| 99 |
- name: Query a specific site EPGs |
| 100 |
cisco.mso.mso_schema_site_anp_epg: |
| 101 |
host: mso_host |
| 102 |
username: admin |
| 103 |
password: SomeSecretPassword |
| 104 |
schema: Schema1 |
| 105 |
site: Site1 |
| 106 |
template: Template1 |
| 107 |
anp: ANP1 |
| 108 |
epg: EPG1 |
| 109 |
state: query |
| 110 |
register: query_result |
| 111 |
|
| 112 |
- name: Query all site EPGs |
| 113 |
cisco.mso.mso_schema_site_anp_epg: |
| 114 |
host: mso_host |
| 115 |
username: admin |
| 116 |
password: SomeSecretPassword |
| 117 |
schema: Schema1 |
| 118 |
site: Site1 |
| 119 |
template: Template1 |
| 120 |
anp: ANP1 |
| 121 |
state: query |
| 122 |
register: query_result |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = r""" |
| 126 |
""" |
| 127 |
|
| 128 |
from ansible.module_utils.basic import AnsibleModule |
| 129 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 130 |
|
| 131 |
|
| 132 |
def main(): |
| 133 |
argument_spec = mso_argument_spec() |
| 134 |
argument_spec.update( |
| 135 |
schema=dict(type="str", required=True), |
| 136 |
site=dict(type="str", required=True), |
| 137 |
template=dict(type="str", required=True), |
| 138 |
anp=dict(type="str", required=True), |
| 139 |
epg=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects |
| 140 |
private_link_label=dict(type="str"), |
| 141 |
admin_state=dict(type="str", choices=["admin_up", "admin_shut"]), |
| 142 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 143 |
) |
| 144 |
|
| 145 |
module = AnsibleModule( |
| 146 |
argument_spec=argument_spec, |
| 147 |
supports_check_mode=True, |
| 148 |
required_if=[ |
| 149 |
["state", "absent", ["epg"]], |
| 150 |
["state", "present", ["epg"]], |
| 151 |
], |
| 152 |
) |
| 153 |
|
| 154 |
schema = module.params.get("schema") |
| 155 |
site = module.params.get("site") |
| 156 |
template = module.params.get("template").replace(" ", "") |
| 157 |
anp = module.params.get("anp") |
| 158 |
epg = module.params.get("epg") |
| 159 |
private_link_label = module.params.get("private_link_label") |
| 160 |
admin_state = module.params.get("admin_state") |
| 161 |
state = module.params.get("state") |
| 162 |
|
| 163 |
mso = MSOModule(module) |
| 164 |
|
| 165 |
# Get schema objects |
| 166 |
schema_id, schema_path, schema_obj = mso.query_schema(schema) |
| 167 |
|
| 168 |
# Get template |
| 169 |
templates = [t.get("name") for t in schema_obj.get("templates")] |
| 170 |
if template not in templates: |
| 171 |
mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates))) |
| 172 |
template_idx = templates.index(template) |
| 173 |
|
| 174 |
# Get site |
| 175 |
site_id = mso.lookup_site(site) |
| 176 |
|
| 177 |
# Get site_idx |
| 178 |
if not schema_obj.get("sites"): |
| 179 |
mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template)) |
| 180 |
sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")] |
| 181 |
sites_list = [s.get("siteId") + "/" + s.get("templateName") for s in schema_obj.get("sites")] |
| 182 |
if (site_id, template) not in sites: |
| 183 |
mso.fail_json( |
| 184 |
msg="Provided site/siteId/template '{0}/{1}/{2}' does not exist. " |
| 185 |
"Existing siteIds/templates: {3}".format(site, site_id, template, ", ".join(sites_list)) |
| 186 |
) |
| 187 |
|
| 188 |
# Schema-access uses indexes |
| 189 |
site_idx = sites.index((site_id, template)) |
| 190 |
# Path-based access uses site_id-template |
| 191 |
site_template = "{0}-{1}".format(site_id, template) |
| 192 |
|
| 193 |
payload = {} |
| 194 |
ops = [] |
| 195 |
op_path = "" |
| 196 |
epg_path = None |
| 197 |
|
| 198 |
# Get ANP |
| 199 |
anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp) |
| 200 |
anps = [a.get("anpRef") for a in schema_obj["sites"][site_idx]["anps"]] |
| 201 |
anps_in_temp = [a.get("name") for a in schema_obj["templates"][template_idx]["anps"]] |
| 202 |
if anp not in anps_in_temp: |
| 203 |
mso.fail_json(msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(anp, ", ".join(anps_in_temp))) |
| 204 |
else: |
| 205 |
# Get anp index at template level |
| 206 |
template_anp_idx = anps_in_temp.index(anp) |
| 207 |
|
| 208 |
# If anp not at site level but exists at template level |
| 209 |
if anp_ref not in anps: |
| 210 |
op_path = "/sites/{0}/anps".format(site_template) |
| 211 |
payload = dict( |
| 212 |
anpRef=dict( |
| 213 |
schemaId=schema_id, |
| 214 |
templateName=template, |
| 215 |
anpName=anp, |
| 216 |
), |
| 217 |
) |
| 218 |
else: |
| 219 |
# Get anp index at site level |
| 220 |
anp_idx = anps.index(anp_ref) |
| 221 |
|
| 222 |
if epg is not None: |
| 223 |
# Get EPG |
| 224 |
epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg) |
| 225 |
new_epg = dict( |
| 226 |
epgRef=dict( |
| 227 |
schemaId=schema_id, |
| 228 |
templateName=template, |
| 229 |
anpName=anp, |
| 230 |
epgName=epg, |
| 231 |
) |
| 232 |
) |
| 233 |
|
| 234 |
# If anp exists at site level |
| 235 |
if "anpRef" not in payload: |
| 236 |
epgs = [e.get("epgRef") for e in schema_obj["sites"][site_idx]["anps"][anp_idx]["epgs"]] |
| 237 |
|
| 238 |
# If anp already at site level AND if epg not at site level (or) anp not at site level? |
| 239 |
if ("anpRef" not in payload and epg_ref not in epgs) or "anpRef" in payload: |
| 240 |
epgs_in_temp = [e.get("name") for e in schema_obj["templates"][template_idx]["anps"][template_anp_idx]["epgs"]] |
| 241 |
|
| 242 |
# If EPG not at template level - Fail |
| 243 |
if epg not in epgs_in_temp: |
| 244 |
mso.fail_json(msg="Provided EPG '{0}' does not exist. Existing EPGs: {1}".format(epg, ", ".join(epgs_in_temp))) |
| 245 |
|
| 246 |
# EPG at template level but not at site level. Create payload at site level for EPG |
| 247 |
else: |
| 248 |
# If anp not in payload then, anp already exists at site level. New payload will only have new EPG payload |
| 249 |
if "anpRef" not in payload: |
| 250 |
op_path = "/sites/{0}/anps/{1}/epgs".format(site_template, anp) |
| 251 |
payload = new_epg |
| 252 |
else: |
| 253 |
# If anp in payload, anp exists at site level. Update payload with EPG payload |
| 254 |
payload["epgs"] = [new_epg] |
| 255 |
|
| 256 |
# Get index of EPG at site level |
| 257 |
else: |
| 258 |
epg_idx = epgs.index(epg_ref) |
| 259 |
epg_path = "/sites/{0}/anps/{1}/epgs/{2}".format(site_template, anp, epg) |
| 260 |
mso.existing = schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"][epg_idx] |
| 261 |
payload = new_epg |
| 262 |
|
| 263 |
ops = [] |
| 264 |
|
| 265 |
if state == "query": |
| 266 |
if anp_ref not in anps: |
| 267 |
mso.fail_json(msg="Provided anp '{0}' does not exist at site level.".format(anp)) |
| 268 |
if epg is None: |
| 269 |
mso.existing = schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"] |
| 270 |
elif not mso.existing: |
| 271 |
mso.fail_json(msg="EPG '{epg}' not found".format(epg=epg)) |
| 272 |
mso.exit_json() |
| 273 |
|
| 274 |
# Workaround due to inconsistency in attributes REQUEST/RESPONSE API |
| 275 |
# FIX for MSO Error 400: Bad Request: (0)(0)(0)(0)/deploymentImmediacy error.path.missing |
| 276 |
mso.replace_keys_in_dict("deployImmediacy", "deploymentImmediacy") |
| 277 |
if mso.existing.get("epgRef"): |
| 278 |
epg_ref = mso.dict_from_ref(mso.existing.get("epgRef")) |
| 279 |
mso.existing["epgRef"] = epg_ref |
| 280 |
|
| 281 |
mso.previous = mso.existing |
| 282 |
|
| 283 |
if state == "absent": |
| 284 |
if mso.existing and epg_path: |
| 285 |
mso.sent = mso.existing = {} |
| 286 |
ops.append(dict(op="remove", path=epg_path)) |
| 287 |
|
| 288 |
elif state == "present": |
| 289 |
if private_link_label is not None: |
| 290 |
payload["privateLinkLabel"] = dict(name=private_link_label) |
| 291 |
if admin_state is not None: |
| 292 |
payload["shutdown"] = True if admin_state == "admin_shut" else False |
| 293 |
|
| 294 |
mso.sanitize(payload, collate=True) |
| 295 |
|
| 296 |
if mso.existing and epg_path: |
| 297 |
ops.append(dict(op="replace", path=epg_path, value=mso.sent)) |
| 298 |
else: |
| 299 |
ops.append(dict(op="add", path=op_path + "/-", value=mso.sent)) |
| 300 |
|
| 301 |
mso.existing = mso.proposed |
| 302 |
|
| 303 |
if not module.check_mode and mso.existing != mso.previous: |
| 304 |
mso.request(schema_path, method="PATCH", data=ops) |
| 305 |
|
| 306 |
mso.exit_json() |
| 307 |
|
| 308 |
|
| 309 |
if __name__ == "__main__": |
| 310 |
main() |