| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Dag Wieers (@dagwieers) <[email protected]> |
| 5 |
# Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]> |
| 6 |
|
| 7 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 14 |
|
| 15 |
DOCUMENTATION = r""" |
| 16 |
--- |
| 17 |
module: mso_schema_template |
| 18 |
short_description: Manage templates in schemas |
| 19 |
description: |
| 20 |
- Manage templates on Cisco ACI Multi-Site. |
| 21 |
author: |
| 22 |
- Dag Wieers (@dagwieers) |
| 23 |
- Akini Ross (@akinross) |
| 24 |
options: |
| 25 |
tenant: |
| 26 |
description: |
| 27 |
- The tenant used for this template. |
| 28 |
type: str |
| 29 |
schema: |
| 30 |
description: |
| 31 |
- The name of the schema. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
schema_description: |
| 35 |
description: |
| 36 |
- The description of Schema is supported on versions of MSO that are 3.3 or greater. |
| 37 |
type: str |
| 38 |
template_description: |
| 39 |
description: |
| 40 |
- The description of template is supported on versions of MSO that are 3.3 or greater. |
| 41 |
type: str |
| 42 |
template: |
| 43 |
description: |
| 44 |
- The name of the template. |
| 45 |
type: str |
| 46 |
aliases: [ name ] |
| 47 |
display_name: |
| 48 |
description: |
| 49 |
- The name as displayed on the MSO web interface. |
| 50 |
type: str |
| 51 |
state: |
| 52 |
description: |
| 53 |
- Use C(present) or C(absent) for adding or removing. |
| 54 |
- Use C(query) for listing an object or multiple objects. |
| 55 |
- Using C(present) on empty schemas M(cisco.mso.mso_schema) is supported on versions of MSO that are 4.2 or greater. |
| 56 |
type: str |
| 57 |
choices: [ absent, present, query ] |
| 58 |
default: present |
| 59 |
notes: |
| 60 |
- Due to restrictions of the MSO REST API this module creates schemas when needed, and removes them when the last template has been removed. |
| 61 |
seealso: |
| 62 |
- module: cisco.mso.mso_schema |
| 63 |
- module: cisco.mso.mso_schema_site |
| 64 |
extends_documentation_fragment: cisco.mso.modules |
| 65 |
""" |
| 66 |
|
| 67 |
EXAMPLES = r""" |
| 68 |
- name: Add a new template to a schema |
| 69 |
cisco.mso.mso_schema_template: |
| 70 |
host: mso_host |
| 71 |
username: admin |
| 72 |
password: SomeSecretPassword |
| 73 |
tenant: Tenant 1 |
| 74 |
schema: Schema 1 |
| 75 |
template: Template 1 |
| 76 |
state: present |
| 77 |
|
| 78 |
- name: Remove a template from a schema |
| 79 |
cisco.mso.mso_schema_template: |
| 80 |
host: mso_host |
| 81 |
username: admin |
| 82 |
password: SomeSecretPassword |
| 83 |
tenant: Tenant 1 |
| 84 |
schema: Schema 1 |
| 85 |
template: Template 1 |
| 86 |
state: absent |
| 87 |
|
| 88 |
- name: Query a template |
| 89 |
cisco.mso.mso_schema_template: |
| 90 |
host: mso_host |
| 91 |
username: admin |
| 92 |
password: SomeSecretPassword |
| 93 |
tenant: Tenant 1 |
| 94 |
schema: Schema 1 |
| 95 |
template: Template 1 |
| 96 |
state: query |
| 97 |
register: query_result |
| 98 |
|
| 99 |
- name: Query all templates |
| 100 |
cisco.mso.mso_schema_template: |
| 101 |
host: mso_host |
| 102 |
username: admin |
| 103 |
password: SomeSecretPassword |
| 104 |
tenant: Tenant 1 |
| 105 |
schema: Schema 1 |
| 106 |
state: query |
| 107 |
register: query_result |
| 108 |
""" |
| 109 |
|
| 110 |
RETURN = r""" |
| 111 |
""" |
| 112 |
|
| 113 |
from ansible.module_utils.basic import AnsibleModule |
| 114 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 115 |
|
| 116 |
|
| 117 |
def main(): |
| 118 |
argument_spec = mso_argument_spec() |
| 119 |
argument_spec.update( |
| 120 |
tenant=dict(type="str"), |
| 121 |
schema=dict(type="str", required=True), |
| 122 |
schema_description=dict(type="str"), |
| 123 |
template_description=dict(type="str"), |
| 124 |
template=dict(type="str", aliases=["name"]), |
| 125 |
display_name=dict(type="str"), |
| 126 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 127 |
) |
| 128 |
|
| 129 |
module = AnsibleModule( |
| 130 |
argument_spec=argument_spec, |
| 131 |
supports_check_mode=True, |
| 132 |
required_if=[ |
| 133 |
["state", "absent", ["template"]], |
| 134 |
["state", "present", ["template", "tenant"]], |
| 135 |
], |
| 136 |
) |
| 137 |
|
| 138 |
tenant = module.params.get("tenant") |
| 139 |
schema = module.params.get("schema") |
| 140 |
schema_description = module.params.get("schema_description") |
| 141 |
template_description = module.params.get("template_description") |
| 142 |
template = module.params.get("template") |
| 143 |
if template is not None: |
| 144 |
template = template.replace(" ", "") |
| 145 |
display_name = module.params.get("display_name") |
| 146 |
state = module.params.get("state") |
| 147 |
|
| 148 |
mso = MSOModule(module) |
| 149 |
|
| 150 |
# Get schema |
| 151 |
schema_obj = mso.get_obj("schemas", displayName=schema) |
| 152 |
|
| 153 |
mso.existing = {} |
| 154 |
if schema_obj: |
| 155 |
# Schema exists |
| 156 |
schema_path = "schemas/{id}".format(**schema_obj) |
| 157 |
|
| 158 |
# Get template |
| 159 |
templates = schema_obj.get("templates") if schema_obj.get("templates") is not None else [] |
| 160 |
if template: |
| 161 |
mso.existing = next((item for item in templates if item.get("name") == template), {}) |
| 162 |
else: |
| 163 |
mso.existing = templates |
| 164 |
else: |
| 165 |
schema_path = "schemas" |
| 166 |
|
| 167 |
if state == "query": |
| 168 |
if not mso.existing: |
| 169 |
if template: |
| 170 |
mso.fail_json(msg="Template '{0}' not found".format(template)) |
| 171 |
else: |
| 172 |
mso.existing = [] |
| 173 |
mso.exit_json() |
| 174 |
|
| 175 |
template_path = "/templates/{0}".format(template) |
| 176 |
ops = [] |
| 177 |
|
| 178 |
mso.previous = mso.existing |
| 179 |
if state == "absent": |
| 180 |
mso.proposed = mso.sent = {} |
| 181 |
if not schema_obj: |
| 182 |
# There was no schema to begin with |
| 183 |
pass |
| 184 |
elif len(templates) == 1 and mso.existing: |
| 185 |
# There is only one tenant, remove schema |
| 186 |
mso.existing = {} |
| 187 |
if not module.check_mode: |
| 188 |
mso.request(schema_path, method="DELETE") |
| 189 |
elif mso.existing: |
| 190 |
# Remove existing template |
| 191 |
mso.existing = {} |
| 192 |
ops.append(dict(op="remove", path=template_path)) |
| 193 |
|
| 194 |
elif state == "present": |
| 195 |
tenant_id = mso.lookup_tenant(tenant) |
| 196 |
|
| 197 |
if display_name is None: |
| 198 |
display_name = mso.existing.get("displayName", template) |
| 199 |
|
| 200 |
if not schema_obj: |
| 201 |
# Schema does not exist, so we have to create it |
| 202 |
payload = dict( |
| 203 |
displayName=schema, |
| 204 |
templates=[ |
| 205 |
dict( |
| 206 |
name=template, |
| 207 |
displayName=display_name, |
| 208 |
tenantId=tenant_id, |
| 209 |
) |
| 210 |
], |
| 211 |
sites=[], |
| 212 |
) |
| 213 |
|
| 214 |
if schema_description is not None: |
| 215 |
payload.update(description=schema_description) |
| 216 |
if template_description is not None: |
| 217 |
payload["templates"][0].update(description=template_description) |
| 218 |
|
| 219 |
mso.existing = payload.get("templates")[0] |
| 220 |
|
| 221 |
if not module.check_mode: |
| 222 |
mso.request(schema_path, method="POST", data=payload) |
| 223 |
|
| 224 |
elif mso.existing: |
| 225 |
# Template exists, so we have to update it |
| 226 |
payload = dict( |
| 227 |
name=template, |
| 228 |
displayName=display_name, |
| 229 |
description=template_description, |
| 230 |
tenantId=tenant_id, |
| 231 |
) |
| 232 |
|
| 233 |
mso.sanitize(payload, collate=True) |
| 234 |
|
| 235 |
ops.append(dict(op="replace", path=template_path + "/displayName", value=display_name)) |
| 236 |
ops.append(dict(op="replace", path=template_path + "/tenantId", value=tenant_id)) |
| 237 |
|
| 238 |
mso.existing = mso.proposed |
| 239 |
else: |
| 240 |
# Template does not exist, so we have to add it |
| 241 |
payload = dict( |
| 242 |
name=template, |
| 243 |
displayName=display_name, |
| 244 |
tenantId=tenant_id, |
| 245 |
) |
| 246 |
|
| 247 |
mso.sanitize(payload, collate=True) |
| 248 |
|
| 249 |
ops.append(dict(op="add", path="/templates/-", value=payload)) |
| 250 |
|
| 251 |
mso.existing = mso.proposed |
| 252 |
|
| 253 |
if not module.check_mode: |
| 254 |
mso.request(schema_path, method="PATCH", data=ops) |
| 255 |
|
| 256 |
mso.exit_json() |
| 257 |
|
| 258 |
|
| 259 |
if __name__ == "__main__": |
| 260 |
main() |