| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, 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_template_clone |
| 16 |
short_description: Clone templates |
| 17 |
description: |
| 18 |
- Clone templates on Cisco ACI Multi-Site. |
| 19 |
- Clones only template objects and not site objects. |
| 20 |
author: |
| 21 |
- Anvitha Jain (@anvitha-jain) |
| 22 |
options: |
| 23 |
source_schema: |
| 24 |
description: |
| 25 |
- The name of the source_schema. |
| 26 |
type: str |
| 27 |
destination_schema: |
| 28 |
description: |
| 29 |
- The name of the destination_schema. |
| 30 |
type: str |
| 31 |
destination_tenant: |
| 32 |
description: |
| 33 |
- The name of the destination_schema. |
| 34 |
type: str |
| 35 |
source_template_name: |
| 36 |
description: |
| 37 |
- The name of the source template. |
| 38 |
type: str |
| 39 |
destination_template_name: |
| 40 |
description: |
| 41 |
- The name of the destination template. |
| 42 |
type: str |
| 43 |
destination_template_display_name: |
| 44 |
description: |
| 45 |
- The display name of the destination template. |
| 46 |
type: str |
| 47 |
state: |
| 48 |
description: |
| 49 |
- Use C(clone) for adding. |
| 50 |
type: str |
| 51 |
choices: [ clone ] |
| 52 |
default: clone |
| 53 |
seealso: |
| 54 |
- module: cisco.mso.mso_schema |
| 55 |
- module: cisco.mso.mso_schema_clone |
| 56 |
extends_documentation_fragment: cisco.mso.modules |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
- name: Clone template in the same schema |
| 61 |
cisco.mso.mso_schema_template_clone: |
| 62 |
host: mso_host |
| 63 |
username: admin |
| 64 |
password: SomeSecretPassword |
| 65 |
source_schema: Schema1 |
| 66 |
destination_schema: Schema1 |
| 67 |
destination_tenant: ansible_test |
| 68 |
source_template_name: Template1 |
| 69 |
destination_template_name: Template1_clone |
| 70 |
destination_template_display_name: Template1_clone |
| 71 |
state: clone |
| 72 |
|
| 73 |
- name: Clone template to different schema |
| 74 |
cisco.mso.mso_schema_template_clone: |
| 75 |
host: mso_host |
| 76 |
username: admin |
| 77 |
password: SomeSecretPassword |
| 78 |
source_schema: Schema1 |
| 79 |
destination_schema: Schema2 |
| 80 |
destination_tenant: ansible_test |
| 81 |
source_template_name: Template2 |
| 82 |
destination_template_name: Cloned_template_1 |
| 83 |
destination_template_display_name: Cloned_template_1 |
| 84 |
state: clone |
| 85 |
|
| 86 |
- name: Clone template in the same schema but different tenant attached |
| 87 |
cisco.mso.mso_schema_template_clone: |
| 88 |
host: mso_host |
| 89 |
username: admin |
| 90 |
password: SomeSecretPassword |
| 91 |
source_schema: Schema1 |
| 92 |
destination_schema: Schema1 |
| 93 |
destination_tenant: common |
| 94 |
source_template_name: Template1_clone |
| 95 |
destination_template_name: Template1_clone_2 |
| 96 |
state: clone |
| 97 |
""" |
| 98 |
|
| 99 |
RETURN = r""" |
| 100 |
""" |
| 101 |
|
| 102 |
from ansible.module_utils.basic import AnsibleModule |
| 103 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 104 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import NDO_4_UNIQUE_IDENTIFIERS |
| 105 |
import json |
| 106 |
|
| 107 |
|
| 108 |
def main(): |
| 109 |
argument_spec = mso_argument_spec() |
| 110 |
argument_spec.update( |
| 111 |
source_schema=dict(type="str"), |
| 112 |
destination_schema=dict(type="str"), |
| 113 |
destination_tenant=dict(type="str"), |
| 114 |
source_template_name=dict(type="str"), |
| 115 |
destination_template_name=dict(type="str"), |
| 116 |
destination_template_display_name=dict(type="str"), |
| 117 |
state=dict(type="str", default="clone", choices=["clone"]), |
| 118 |
) |
| 119 |
|
| 120 |
module = AnsibleModule( |
| 121 |
argument_spec=argument_spec, |
| 122 |
supports_check_mode=True, |
| 123 |
required_if=[ |
| 124 |
["state", "clone", ["source_schema", "source_template_name"]], |
| 125 |
], |
| 126 |
) |
| 127 |
|
| 128 |
source_schema = module.params.get("source_schema") |
| 129 |
destination_schema = module.params.get("destination_schema") |
| 130 |
destination_tenant = module.params.get("destination_tenant") |
| 131 |
source_template_name = module.params.get("source_template_name") |
| 132 |
destination_template_name = module.params.get("destination_template_name") |
| 133 |
destination_template_display_name = module.params.get("destination_template_display_name") |
| 134 |
state = module.params.get("state") |
| 135 |
|
| 136 |
mso = MSOModule(module) |
| 137 |
|
| 138 |
source_schema_id = None |
| 139 |
destination_schema_id = None |
| 140 |
destination_tenant_id = None |
| 141 |
ops = [] |
| 142 |
|
| 143 |
if destination_schema is None: |
| 144 |
destination_schema = source_schema |
| 145 |
|
| 146 |
if destination_template_name is None: |
| 147 |
destination_template_name = source_template_name |
| 148 |
|
| 149 |
if destination_template_display_name is None: |
| 150 |
destination_template_display_name = destination_template_name |
| 151 |
|
| 152 |
# Check if source and destination template are named differently if in same schema |
| 153 |
if source_schema == destination_schema: |
| 154 |
if source_template_name == destination_template_name: |
| 155 |
mso.fail_json(msg="Source and destination templates in the same schema cannot have same names.") |
| 156 |
|
| 157 |
# Get source schema id and destination schema id |
| 158 |
schema_summary = mso.query_objs("schemas/list-identity", key="schemas") |
| 159 |
|
| 160 |
for schema in schema_summary: |
| 161 |
if schema.get("displayName") == source_schema: |
| 162 |
source_schema_id = schema.get("id") |
| 163 |
|
| 164 |
if schema.get("displayName") == destination_schema: |
| 165 |
destination_schema_id = schema.get("id") |
| 166 |
for template in schema.get("templates"): |
| 167 |
if template.get("name") == destination_template_name: |
| 168 |
mso.fail_json(msg="Template with the name '{0}' already exists. Please use another name.".format(destination_template_name)) |
| 169 |
|
| 170 |
if source_schema_id is None: |
| 171 |
mso.fail_json(msg="Schema with the name '{0}' does not exist.".format(source_schema)) |
| 172 |
elif destination_schema_id is None: |
| 173 |
mso.fail_json(msg="Schema with the name '{0}' does not exist.".format(destination_schema)) |
| 174 |
|
| 175 |
# Get destination schema details before change |
| 176 |
destination_schema_path = "schemas/{0}".format(destination_schema_id) |
| 177 |
mso.existing = mso.query_obj(destination_schema_path, displayName=destination_schema) |
| 178 |
|
| 179 |
if state == "clone": |
| 180 |
# Get destination tenant id |
| 181 |
if destination_tenant is not None: |
| 182 |
destination_tenant_id = mso.lookup_tenant(destination_tenant) |
| 183 |
|
| 184 |
# Get source schema details |
| 185 |
source_schema_path = "schemas/{0}".format(source_schema_id) |
| 186 |
source_schema_obj = mso.query_obj(source_schema_path, displayName=source_schema) |
| 187 |
|
| 188 |
source_template_path = "/{0}/templates/{1}".format(source_schema_path, source_template_name) |
| 189 |
destination_template_path = "/{0}/templates/{1}".format(destination_schema_path, destination_template_name) |
| 190 |
|
| 191 |
source_templates = source_schema_obj.get("templates") |
| 192 |
new_template = None |
| 193 |
for template in source_templates: |
| 194 |
if template.get("name") == source_template_name: |
| 195 |
new_template = json.loads(json.dumps(template).replace(source_template_path, destination_template_path)) |
| 196 |
new_template["name"] = destination_template_name |
| 197 |
new_template["displayName"] = destination_template_display_name |
| 198 |
if destination_tenant_id is not None: |
| 199 |
new_template["tenantId"] = destination_tenant_id |
| 200 |
mso.delete_keys_from_dict(new_template, NDO_4_UNIQUE_IDENTIFIERS) |
| 201 |
break |
| 202 |
|
| 203 |
if new_template is None: |
| 204 |
mso.fail_json(msg="Source template with the name '{0}' does not exist.".format(source_template_name)) |
| 205 |
|
| 206 |
new_template = mso.recursive_dict_from_ref(new_template) |
| 207 |
mso.previous = mso.existing |
| 208 |
|
| 209 |
ops.append(dict(op="add", path="/templates/-", value=new_template)) |
| 210 |
if not module.check_mode: |
| 211 |
mso.request(destination_schema_path, method="PATCH", data=ops) |
| 212 |
|
| 213 |
mso.existing = mso.query_obj(destination_schema_path, displayName=destination_schema) |
| 214 |
|
| 215 |
mso.exit_json() |
| 216 |
|
| 217 |
|
| 218 |
if __name__ == "__main__": |
| 219 |
main() |