| 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_anp_epg_staticleaf |
| 16 |
short_description: Manage site-local EPG static leafs in schema template |
| 17 |
description: |
| 18 |
- Manage site-local EPG static leafs 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 |
anp: |
| 38 |
description: |
| 39 |
- The name of the ANP. |
| 40 |
type: str |
| 41 |
required: true |
| 42 |
epg: |
| 43 |
description: |
| 44 |
- The name of the EPG. |
| 45 |
type: str |
| 46 |
required: true |
| 47 |
pod: |
| 48 |
description: |
| 49 |
- The pod of the static leaf. |
| 50 |
type: str |
| 51 |
leaf: |
| 52 |
description: |
| 53 |
- The path of the static leaf. |
| 54 |
type: str |
| 55 |
aliases: [ name ] |
| 56 |
vlan: |
| 57 |
description: |
| 58 |
- The VLAN id of the static leaf. |
| 59 |
type: int |
| 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 |
notes: |
| 68 |
- The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index. |
| 69 |
This can cause silent corruption on concurrent access when changing/removing on object as |
| 70 |
the wrong object may be referenced. This module is affected by this deficiency. |
| 71 |
seealso: |
| 72 |
- module: cisco.mso.mso_schema_site_anp_epg |
| 73 |
- module: cisco.mso.mso_schema_template_anp_epg |
| 74 |
extends_documentation_fragment: cisco.mso.modules |
| 75 |
""" |
| 76 |
|
| 77 |
EXAMPLES = r""" |
| 78 |
- name: Add a new static leaf to a site EPG |
| 79 |
cisco.mso.mso_schema_site_anp_epg_staticleaf: |
| 80 |
host: mso_host |
| 81 |
username: admin |
| 82 |
password: SomeSecretPassword |
| 83 |
schema: Schema1 |
| 84 |
site: Site1 |
| 85 |
template: Template1 |
| 86 |
anp: ANP1 |
| 87 |
epg: EPG1 |
| 88 |
leaf: Leaf1 |
| 89 |
vlan: 123 |
| 90 |
state: present |
| 91 |
|
| 92 |
- name: Remove a static leaf from a site EPG |
| 93 |
cisco.mso.mso_schema_site_anp_epg_staticleaf: |
| 94 |
host: mso_host |
| 95 |
username: admin |
| 96 |
password: SomeSecretPassword |
| 97 |
schema: Schema1 |
| 98 |
site: Site1 |
| 99 |
template: Template1 |
| 100 |
anp: ANP1 |
| 101 |
epg: EPG1 |
| 102 |
leaf: Leaf1 |
| 103 |
state: absent |
| 104 |
|
| 105 |
- name: Query a specific site EPG static leaf |
| 106 |
cisco.mso.mso_schema_site_anp_epg_staticleaf: |
| 107 |
host: mso_host |
| 108 |
username: admin |
| 109 |
password: SomeSecretPassword |
| 110 |
schema: Schema1 |
| 111 |
site: Site1 |
| 112 |
template: Template1 |
| 113 |
anp: ANP1 |
| 114 |
epg: EPG1 |
| 115 |
leaf: Leaf1 |
| 116 |
state: query |
| 117 |
register: query_result |
| 118 |
|
| 119 |
- name: Query all site EPG static leafs |
| 120 |
cisco.mso.mso_schema_site_anp_epg_staticleaf: |
| 121 |
host: mso_host |
| 122 |
username: admin |
| 123 |
password: SomeSecretPassword |
| 124 |
schema: Schema1 |
| 125 |
site: Site1 |
| 126 |
template: Template1 |
| 127 |
anp: ANP1 |
| 128 |
state: query |
| 129 |
register: query_result |
| 130 |
""" |
| 131 |
|
| 132 |
RETURN = r""" |
| 133 |
""" |
| 134 |
|
| 135 |
from ansible.module_utils.basic import AnsibleModule |
| 136 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 137 |
|
| 138 |
|
| 139 |
def main(): |
| 140 |
argument_spec = mso_argument_spec() |
| 141 |
argument_spec.update( |
| 142 |
schema=dict(type="str", required=True), |
| 143 |
site=dict(type="str", required=True), |
| 144 |
template=dict(type="str", required=True), |
| 145 |
anp=dict(type="str", required=True), |
| 146 |
epg=dict(type="str", required=True), |
| 147 |
pod=dict(type="str"), # This parameter is not required for querying all objects |
| 148 |
leaf=dict(type="str", aliases=["name"]), |
| 149 |
vlan=dict(type="int"), |
| 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", ["pod", "leaf", "vlan"]], |
| 158 |
["state", "present", ["pod", "leaf", "vlan"]], |
| 159 |
], |
| 160 |
) |
| 161 |
|
| 162 |
schema = module.params.get("schema") |
| 163 |
site = module.params.get("site") |
| 164 |
template = module.params.get("template").replace(" ", "") |
| 165 |
anp = module.params.get("anp") |
| 166 |
epg = module.params.get("epg") |
| 167 |
pod = module.params.get("pod") |
| 168 |
leaf = module.params.get("leaf") |
| 169 |
vlan = module.params.get("vlan") |
| 170 |
state = module.params.get("state") |
| 171 |
|
| 172 |
leafpath = "topology/{0}/node-{1}".format(pod, leaf) |
| 173 |
|
| 174 |
mso = MSOModule(module) |
| 175 |
|
| 176 |
# Get schema objects |
| 177 |
schema_id, schema_path, schema_obj = mso.query_schema(schema) |
| 178 |
|
| 179 |
# Get site |
| 180 |
site_id = mso.lookup_site(site) |
| 181 |
|
| 182 |
# Get site_idx |
| 183 |
if not schema_obj.get("sites"): |
| 184 |
mso.fail_json(msg="No site associated with template '{0}'. Associate the site with the template using mso_schema_site.".format(template)) |
| 185 |
sites = [(s.get("siteId"), s.get("templateName")) for s in schema_obj.get("sites")] |
| 186 |
if (site_id, template) not in sites: |
| 187 |
mso.fail_json(msg="Provided site/template '{0}-{1}' does not exist. Existing sites/templates: {2}".format(site, template, ", ".join(sites))) |
| 188 |
|
| 189 |
# Schema-access uses indexes |
| 190 |
site_idx = sites.index((site_id, template)) |
| 191 |
# Path-based access uses site_id-template |
| 192 |
site_template = "{0}-{1}".format(site_id, template) |
| 193 |
|
| 194 |
# Get ANP |
| 195 |
anp_ref = mso.anp_ref(schema_id=schema_id, template=template, anp=anp) |
| 196 |
anps = [a.get("anpRef") for a in schema_obj.get("sites")[site_idx]["anps"]] |
| 197 |
if anp_ref not in anps: |
| 198 |
mso.fail_json(msg="Provided anp '{0}' does not exist. Existing anps: {1}".format(anp, ", ".join(anps))) |
| 199 |
anp_idx = anps.index(anp_ref) |
| 200 |
|
| 201 |
# Get EPG |
| 202 |
epg_ref = mso.epg_ref(schema_id=schema_id, template=template, anp=anp, epg=epg) |
| 203 |
epgs = [e.get("epgRef") for e in schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"]] |
| 204 |
if epg_ref not in epgs: |
| 205 |
mso.fail_json(msg="Provided epg '{0}' does not exist. Existing epgs: {1}".format(epg, ", ".join(epgs))) |
| 206 |
epg_idx = epgs.index(epg_ref) |
| 207 |
|
| 208 |
# Get Leaf |
| 209 |
leafs = [(leaf.get("path"), leaf.get("portEncapVlan")) for leaf in schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"][epg_idx]["staticLeafs"]] |
| 210 |
if (leafpath, vlan) in leafs: |
| 211 |
leaf_idx = leafs.index((leafpath, vlan)) |
| 212 |
# FIXME: Changes based on index are DANGEROUS |
| 213 |
leaf_path = "/sites/{0}/anps/{1}/epgs/{2}/staticLeafs/{3}".format(site_template, anp, epg, leaf_idx) |
| 214 |
mso.existing = schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"][epg_idx]["staticLeafs"][leaf_idx] |
| 215 |
|
| 216 |
if state == "query": |
| 217 |
if leaf is None or vlan is None: |
| 218 |
mso.existing = schema_obj.get("sites")[site_idx]["anps"][anp_idx]["epgs"][epg_idx]["staticLeafs"] |
| 219 |
elif not mso.existing: |
| 220 |
mso.fail_json(msg="Static leaf '{leaf}/{vlan}' not found".format(leaf=leaf, vlan=vlan)) |
| 221 |
mso.exit_json() |
| 222 |
|
| 223 |
leafs_path = "/sites/{0}/anps/{1}/epgs/{2}/staticLeafs".format(site_template, anp, epg) |
| 224 |
ops = [] |
| 225 |
|
| 226 |
mso.previous = mso.existing |
| 227 |
if state == "absent": |
| 228 |
if mso.existing: |
| 229 |
mso.sent = mso.existing = {} |
| 230 |
ops.append(dict(op="remove", path=leaf_path)) |
| 231 |
|
| 232 |
elif state == "present": |
| 233 |
payload = dict( |
| 234 |
path=leafpath, |
| 235 |
portEncapVlan=vlan, |
| 236 |
) |
| 237 |
|
| 238 |
mso.sanitize(payload, collate=True) |
| 239 |
|
| 240 |
if mso.existing: |
| 241 |
ops.append(dict(op="replace", path=leaf_path, value=mso.sent)) |
| 242 |
else: |
| 243 |
ops.append(dict(op="add", path=leafs_path + "/-", value=mso.sent)) |
| 244 |
|
| 245 |
mso.existing = mso.proposed |
| 246 |
|
| 247 |
if not module.check_mode: |
| 248 |
mso.request(schema_path, method="PATCH", data=ops) |
| 249 |
|
| 250 |
mso.exit_json() |
| 251 |
|
| 252 |
|
| 253 |
if __name__ == "__main__": |
| 254 |
main() |