| 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_clone |
| 16 |
short_description: Clone schemas |
| 17 |
description: |
| 18 |
- Clone schemas on Cisco ACI Multi-Site. |
| 19 |
- Clones only template objects and not site objects. |
| 20 |
- This module can only be used on versions of MSO that are 3.3 or greater. |
| 21 |
author: |
| 22 |
- Anvitha Jain (@anvitha-jain) |
| 23 |
options: |
| 24 |
source_schema: |
| 25 |
description: |
| 26 |
- The name of the source_schema. |
| 27 |
type: str |
| 28 |
destination_schema: |
| 29 |
description: |
| 30 |
- The name of the destination_schema. |
| 31 |
type: str |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Use C(clone) for adding. |
| 35 |
type: str |
| 36 |
choices: [ clone ] |
| 37 |
default: clone |
| 38 |
seealso: |
| 39 |
- module: cisco.mso.mso_schema |
| 40 |
extends_documentation_fragment: cisco.mso.modules |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
- name: Clone schema |
| 45 |
cisco.mso.mso_schema_clone: |
| 46 |
host: mso_host |
| 47 |
username: admin |
| 48 |
password: SomeSecretPassword |
| 49 |
source_schema: Source_Schema |
| 50 |
destination_schema: Destination_Schema |
| 51 |
state: clone |
| 52 |
""" |
| 53 |
|
| 54 |
RETURN = r""" |
| 55 |
""" |
| 56 |
|
| 57 |
from ansible.module_utils.basic import AnsibleModule |
| 58 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 59 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import NDO_4_UNIQUE_IDENTIFIERS |
| 60 |
import json |
| 61 |
|
| 62 |
|
| 63 |
def main(): |
| 64 |
argument_spec = mso_argument_spec() |
| 65 |
argument_spec.update( |
| 66 |
source_schema=dict(type="str"), |
| 67 |
destination_schema=dict(type="str"), |
| 68 |
state=dict(type="str", default="clone", choices=["clone"]), |
| 69 |
) |
| 70 |
|
| 71 |
module = AnsibleModule( |
| 72 |
argument_spec=argument_spec, |
| 73 |
supports_check_mode=True, |
| 74 |
required_if=[ |
| 75 |
["state", "clone", ["destination_schema"]], |
| 76 |
], |
| 77 |
) |
| 78 |
|
| 79 |
source_schema = module.params.get("source_schema") |
| 80 |
destination_schema = module.params.get("destination_schema") |
| 81 |
state = module.params.get("state") |
| 82 |
|
| 83 |
mso = MSOModule(module) |
| 84 |
|
| 85 |
# Get source schema details |
| 86 |
source_schema_path = "schemas/{0}".format(mso.lookup_schema(source_schema)) |
| 87 |
source_schema_obj = mso.query_obj(source_schema_path, displayName=source_schema) |
| 88 |
|
| 89 |
source_data = source_schema_obj.get("templates") |
| 90 |
source_data = json.loads(json.dumps(source_data).replace("/{0}".format(source_schema_path), "")) |
| 91 |
# certain unique identifiers are present in NDO4.0> source which need to be deleted from source_data prior to POST |
| 92 |
for template in source_data: |
| 93 |
mso.delete_keys_from_dict(template, NDO_4_UNIQUE_IDENTIFIERS) |
| 94 |
|
| 95 |
path = "schemas" |
| 96 |
|
| 97 |
# Check if source and destination schema are named differently |
| 98 |
if source_schema == destination_schema: |
| 99 |
mso.fail_json(msg="Source and Destination schema cannot have same names.") |
| 100 |
# Query for existing object(s) |
| 101 |
if destination_schema: |
| 102 |
mso.existing = mso.get_obj(path, displayName=destination_schema) |
| 103 |
if mso.existing: |
| 104 |
mso.fail_json(msg="Schema with the name '{0}' already exists. Please use another name.".format(destination_schema)) |
| 105 |
|
| 106 |
if state == "clone": |
| 107 |
mso.previous = mso.existing |
| 108 |
payload = dict( |
| 109 |
displayName=destination_schema, |
| 110 |
templates=source_data, |
| 111 |
) |
| 112 |
mso.sanitize(payload, collate=True) |
| 113 |
|
| 114 |
if not mso.existing: |
| 115 |
if module.check_mode: |
| 116 |
mso.existing = mso.proposed |
| 117 |
else: |
| 118 |
mso.existing = mso.request(path, method="POST", data=mso.sent) |
| 119 |
|
| 120 |
mso.exit_json() |
| 121 |
|
| 122 |
|
| 123 |
if __name__ == "__main__": |
| 124 |
main() |