| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> |
| 5 |
# Copyright: (c) 2020, Lionel Hercot (@lhercot) <[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_cidr |
| 17 |
short_description: Manage site-local VRF region CIDRs in schema template |
| 18 |
description: |
| 19 |
- Manage site-local VRF region CIDRs in schema template on Cisco ACI Multi-Site. |
| 20 |
author: |
| 21 |
- Dag Wieers (@dagwieers) |
| 22 |
- Lionel Hercot (@lhercot) |
| 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. |
| 47 |
type: str |
| 48 |
required: true |
| 49 |
cidr: |
| 50 |
description: |
| 51 |
- The name of the region CIDR to manage. |
| 52 |
type: str |
| 53 |
aliases: [ ip ] |
| 54 |
primary: |
| 55 |
description: |
| 56 |
- Whether this is the primary CIDR. |
| 57 |
type: bool |
| 58 |
default: true |
| 59 |
state: |
| 60 |
description: |
| 61 |
- Use C(present) or C(absent) for adding or removing. |
| 62 |
- Use C(query) for listing an object or multiple objects. |
| 63 |
type: str |
| 64 |
choices: [ absent, present, query ] |
| 65 |
default: present |
| 66 |
notes: |
| 67 |
- The ACI MultiSite PATCH API has a deficiency requiring some objects to be referenced by index. |
| 68 |
This can cause silent corruption on concurrent access when changing/removing on object as |
| 69 |
the wrong object may be referenced. This module is affected by this deficiency. |
| 70 |
seealso: |
| 71 |
- module: cisco.mso.mso_schema_site_vrf_region |
| 72 |
- module: cisco.mso.mso_schema_site_vrf_region_cidr_subnet |
| 73 |
- module: cisco.mso.mso_schema_template_vrf |
| 74 |
extends_documentation_fragment: cisco.mso.modules |
| 75 |
""" |
| 76 |
|
| 77 |
EXAMPLES = r""" |
| 78 |
- name: Add a new site VRF region CIDR |
| 79 |
cisco.mso.mso_schema_site_vrf_region_cidr: |
| 80 |
host: mso_host |
| 81 |
username: admin |
| 82 |
password: SomeSecretPassword |
| 83 |
schema: Schema1 |
| 84 |
site: Site1 |
| 85 |
template: Template1 |
| 86 |
vrf: VRF1 |
| 87 |
region: us-west-1 |
| 88 |
cidr: 14.14.14.1/24 |
| 89 |
state: present |
| 90 |
|
| 91 |
- name: Remove a site VRF region CIDR |
| 92 |
cisco.mso.mso_schema_site_vrf_region_cidr: |
| 93 |
host: mso_host |
| 94 |
username: admin |
| 95 |
password: SomeSecretPassword |
| 96 |
schema: Schema1 |
| 97 |
site: Site1 |
| 98 |
template: Template1 |
| 99 |
vrf: VRF1 |
| 100 |
region: us-west-1 |
| 101 |
cidr: 14.14.14.1/24 |
| 102 |
state: absent |
| 103 |
|
| 104 |
- name: Query a specific site VRF region CIDR |
| 105 |
cisco.mso.mso_schema_site_vrf_region_cidr: |
| 106 |
host: mso_host |
| 107 |
username: admin |
| 108 |
password: SomeSecretPassword |
| 109 |
schema: Schema1 |
| 110 |
site: Site1 |
| 111 |
template: Template1 |
| 112 |
vrf: VRF1 |
| 113 |
region: us-west-1 |
| 114 |
cidr: 14.14.14.1/24 |
| 115 |
state: query |
| 116 |
register: query_result |
| 117 |
|
| 118 |
- name: Query all site VRF region CIDR |
| 119 |
cisco.mso.mso_schema_site_vrf_region_cidr: |
| 120 |
host: mso_host |
| 121 |
username: admin |
| 122 |
password: SomeSecretPassword |
| 123 |
schema: Schema1 |
| 124 |
site: Site1 |
| 125 |
template: Template1 |
| 126 |
vrf: VRF1 |
| 127 |
region: us-west-1 |
| 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 |
vrf=dict(type="str", required=True), |
| 146 |
region=dict(type="str", required=True), |
| 147 |
cidr=dict(type="str", aliases=["ip"]), # This parameter is not required for querying all objects |
| 148 |
primary=dict(type="bool", default=True), |
| 149 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 150 |
) |
| 151 |
|
| 152 |
module = AnsibleModule( |
| 153 |
argument_spec=argument_spec, |
| 154 |
supports_check_mode=True, |
| 155 |
required_if=[ |
| 156 |
["state", "absent", ["cidr"]], |
| 157 |
["state", "present", ["cidr"]], |
| 158 |
], |
| 159 |
) |
| 160 |
|
| 161 |
schema = module.params.get("schema") |
| 162 |
site = module.params.get("site") |
| 163 |
template = module.params.get("template").replace(" ", "") |
| 164 |
vrf = module.params.get("vrf") |
| 165 |
region = module.params.get("region") |
| 166 |
cidr = module.params.get("cidr") |
| 167 |
primary = module.params.get("primary") |
| 168 |
state = module.params.get("state") |
| 169 |
|
| 170 |
mso = MSOModule(module) |
| 171 |
|
| 172 |
# Get schema objects |
| 173 |
schema_id, schema_path, schema_obj = mso.query_schema(schema) |
| 174 |
|
| 175 |
# Get template |
| 176 |
templates = [t.get("name") for t in schema_obj.get("templates")] |
| 177 |
if template not in templates: |
| 178 |
mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates))) |
| 179 |
template_idx = templates.index(template) |
| 180 |
|
| 181 |
payload = dict() |
| 182 |
op_path = "" |
| 183 |
cidr_path = None |
| 184 |
new_cidr = dict( |
| 185 |
ip=cidr, |
| 186 |
primary=primary, |
| 187 |
) |
| 188 |
|
| 189 |
# Get site |
| 190 |
site_id = mso.lookup_site(site) |
| 191 |
|
| 192 |
# Get site_idx |
| 193 |
all_sites = schema_obj.get("sites") |
| 194 |
sites = [] |
| 195 |
if all_sites is not None: |
| 196 |
sites = [(s.get("siteId"), s.get("templateName")) for s in all_sites] |
| 197 |
|
| 198 |
# Get VRF |
| 199 |
vrf_ref = mso.vrf_ref(schema_id=schema_id, template=template, vrf=vrf) |
| 200 |
template_vrfs = [a.get("name") for a in schema_obj["templates"][template_idx]["vrfs"]] |
| 201 |
if vrf not in template_vrfs: |
| 202 |
mso.fail_json(msg="Provided vrf '{0}' does not exist. Existing vrfs: {1}".format(vrf, ", ".join(template_vrfs))) |
| 203 |
|
| 204 |
# if site-template does not exist, create it |
| 205 |
if (site_id, template) not in sites: |
| 206 |
op_path = "/sites/-" |
| 207 |
payload.update( |
| 208 |
siteId=site_id, |
| 209 |
templateName=template, |
| 210 |
vrfs=[ |
| 211 |
dict( |
| 212 |
vrfRef=dict( |
| 213 |
schemaId=schema_id, |
| 214 |
templateName=template, |
| 215 |
vrfName=vrf, |
| 216 |
), |
| 217 |
regions=[dict(name=region, cidrs=[new_cidr])], |
| 218 |
) |
| 219 |
], |
| 220 |
) |
| 221 |
|
| 222 |
else: |
| 223 |
# Schema-access uses indexes |
| 224 |
site_idx = sites.index((site_id, template)) |
| 225 |
# Path-based access uses site_id-template |
| 226 |
site_template = "{0}-{1}".format(site_id, template) |
| 227 |
|
| 228 |
# If vrf not at site level but exists at template level |
| 229 |
vrfs = [v.get("vrfRef") for v in schema_obj.get("sites")[site_idx]["vrfs"]] |
| 230 |
if vrf_ref not in vrfs: |
| 231 |
op_path = "/sites/{0}/vrfs/-".format(site_template) |
| 232 |
payload.update( |
| 233 |
vrfRef=dict( |
| 234 |
schemaId=schema_id, |
| 235 |
templateName=template, |
| 236 |
vrfName=vrf, |
| 237 |
), |
| 238 |
regions=[dict(name=region, cidrs=[new_cidr])], |
| 239 |
) |
| 240 |
else: |
| 241 |
# Update vrf index at site level |
| 242 |
vrf_idx = vrfs.index(vrf_ref) |
| 243 |
|
| 244 |
# Get Region |
| 245 |
regions = [r.get("name") for r in schema_obj.get("sites")[site_idx]["vrfs"][vrf_idx]["regions"]] |
| 246 |
if region not in regions: |
| 247 |
op_path = "/sites/{0}/vrfs/{1}/regions/-".format(site_template, vrf) |
| 248 |
payload.update(name=region, cidrs=[new_cidr]) |
| 249 |
else: |
| 250 |
region_idx = regions.index(region) |
| 251 |
|
| 252 |
# Get CIDR |
| 253 |
cidrs = [c.get("ip") for c in schema_obj.get("sites")[site_idx]["vrfs"][vrf_idx]["regions"][region_idx]["cidrs"]] |
| 254 |
if cidr is not None: |
| 255 |
if cidr in cidrs: |
| 256 |
cidr_idx = cidrs.index(cidr) |
| 257 |
# FIXME: Changes based on index are DANGEROUS |
| 258 |
cidr_path = "/sites/{0}/vrfs/{1}/regions/{2}/cidrs/{3}".format(site_template, vrf, region, cidr_idx) |
| 259 |
mso.existing = schema_obj.get("sites")[site_idx]["vrfs"][vrf_idx]["regions"][region_idx]["cidrs"][cidr_idx] |
| 260 |
op_path = "/sites/{0}/vrfs/{1}/regions/{2}/cidrs/-".format(site_template, vrf, region) |
| 261 |
payload = new_cidr |
| 262 |
|
| 263 |
if state == "query": |
| 264 |
if (site_id, template) not in sites: |
| 265 |
mso.fail_json(msg="Provided site-template association '{0}-{1}' does not exist.".format(site, template)) |
| 266 |
elif vrf_ref not in vrfs: |
| 267 |
mso.fail_json(msg="Provided vrf '{0}' does not exist at site level.".format(vrf)) |
| 268 |
elif not regions or region not in regions: |
| 269 |
mso.fail_json(msg="Provided region '{0}' does not exist. Existing regions: {1}".format(region, ", ".join(regions))) |
| 270 |
elif cidr is None and not payload: |
| 271 |
mso.existing = schema_obj.get("sites")[site_idx]["vrfs"][vrf_idx]["regions"][region_idx]["cidrs"] |
| 272 |
elif not mso.existing: |
| 273 |
mso.fail_json(msg="CIDR IP '{cidr}' not found".format(cidr=cidr)) |
| 274 |
mso.exit_json() |
| 275 |
|
| 276 |
ops = [] |
| 277 |
|
| 278 |
mso.previous = mso.existing |
| 279 |
if state == "absent": |
| 280 |
if mso.existing and cidr_path: |
| 281 |
mso.sent = mso.existing = {} |
| 282 |
ops.append(dict(op="remove", path=cidr_path)) |
| 283 |
|
| 284 |
elif state == "present": |
| 285 |
mso.sanitize(payload, collate=True) |
| 286 |
|
| 287 |
if mso.existing and cidr_path: |
| 288 |
ops.append(dict(op="replace", path=cidr_path, value=mso.sent)) |
| 289 |
else: |
| 290 |
ops.append(dict(op="add", path=op_path, value=mso.sent)) |
| 291 |
|
| 292 |
mso.existing = new_cidr |
| 293 |
|
| 294 |
if not module.check_mode and mso.previous != mso.existing: |
| 295 |
mso.request(schema_path, method="PATCH", data=ops) |
| 296 |
|
| 297 |
mso.exit_json() |
| 298 |
|
| 299 |
|
| 300 |
if __name__ == "__main__": |
| 301 |
main() |