| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, 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_migrate |
| 16 |
short_description: Migrate Bridge Domains (BDs) and EPGs between templates |
| 17 |
description: |
| 18 |
- Migrate BDs and EPGs between templates of same and different schemas. |
| 19 |
author: |
| 20 |
- Anvitha Jain (@anvitha-jain) |
| 21 |
options: |
| 22 |
schema: |
| 23 |
description: |
| 24 |
- The name of the schema. |
| 25 |
type: str |
| 26 |
required: true |
| 27 |
template: |
| 28 |
description: |
| 29 |
- The name of the template. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
bds: |
| 33 |
description: |
| 34 |
- The name of the BDs to migrate. |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
epgs: |
| 38 |
description: |
| 39 |
- The name of the EPGs and the ANP it is in to migrate. |
| 40 |
type: list |
| 41 |
elements: dict |
| 42 |
suboptions: |
| 43 |
epg: |
| 44 |
description: |
| 45 |
- The name of the EPG to migrate. |
| 46 |
type: str |
| 47 |
required: true |
| 48 |
anp: |
| 49 |
description: |
| 50 |
- The name of the anp to migrate. |
| 51 |
type: str |
| 52 |
required: true |
| 53 |
target_schema: |
| 54 |
description: |
| 55 |
- The name of the target_schema. |
| 56 |
type: str |
| 57 |
required: true |
| 58 |
target_template: |
| 59 |
description: |
| 60 |
- The name of the target_template. |
| 61 |
type: str |
| 62 |
required: true |
| 63 |
state: |
| 64 |
description: |
| 65 |
- Use C(present) for adding. |
| 66 |
type: str |
| 67 |
default: present |
| 68 |
extends_documentation_fragment: cisco.mso.modules |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
- name: Migration of objects between templates of same schema |
| 73 |
mso_schema_template_migrate: |
| 74 |
host: mso_host |
| 75 |
username: admin |
| 76 |
password: SomeSecretPassword |
| 77 |
schema: Schema 1 |
| 78 |
template: Template 1 |
| 79 |
target_schema: Schema 1 |
| 80 |
target_template: Template 2 |
| 81 |
bds: |
| 82 |
- BD |
| 83 |
epgs: |
| 84 |
- epg: EPG1 |
| 85 |
anp: ANP |
| 86 |
state: present |
| 87 |
|
| 88 |
- name: Migration of objects between templates of different schema |
| 89 |
mso_schema_template_migrate: |
| 90 |
host: mso_host |
| 91 |
username: admin |
| 92 |
password: SomeSecretPassword |
| 93 |
schema: Schema 1 |
| 94 |
template: Template 1 |
| 95 |
target_schema: Schema 2 |
| 96 |
target_template: Template 2 |
| 97 |
bds: |
| 98 |
- BD |
| 99 |
epgs: |
| 100 |
- epg: EPG1 |
| 101 |
anp: ANP |
| 102 |
state: present |
| 103 |
|
| 104 |
- name: Migration of BD object between templates of same schema |
| 105 |
mso_schema_template_migrate: |
| 106 |
host: mso_host |
| 107 |
username: admin |
| 108 |
password: SomeSecretPassword |
| 109 |
schema: Schema 1 |
| 110 |
template: Template 1 |
| 111 |
target_schema: Schema 1 |
| 112 |
target_template: Template 2 |
| 113 |
bds: |
| 114 |
- BD |
| 115 |
- BD1 |
| 116 |
state: present |
| 117 |
|
| 118 |
- name: Migration of BD object between templates of different schema |
| 119 |
mso_schema_template_migrate: |
| 120 |
host: mso_host |
| 121 |
username: admin |
| 122 |
password: SomeSecretPassword |
| 123 |
schema: Schema 1 |
| 124 |
template: Template 1 |
| 125 |
target_schema: Schema 2 |
| 126 |
target_template: Template 2 |
| 127 |
bds: |
| 128 |
- BD |
| 129 |
- BD1 |
| 130 |
state: present |
| 131 |
|
| 132 |
- name: Migration of EPG objects between templates of same schema |
| 133 |
mso_schema_template_migrate: |
| 134 |
host: mso_host |
| 135 |
username: admin |
| 136 |
password: SomeSecretPassword |
| 137 |
schema: Schema 1 |
| 138 |
template: Template 1 |
| 139 |
target_schema: Schema 2 |
| 140 |
target_template: Template 2 |
| 141 |
epgs: |
| 142 |
- epg: EPG1 |
| 143 |
anp: ANP |
| 144 |
- epg: EPG2 |
| 145 |
anp: ANP2 |
| 146 |
state: present |
| 147 |
|
| 148 |
- name: Migration of EPG objects between templates of different schema |
| 149 |
mso_schema_template_migrate: |
| 150 |
host: mso_host |
| 151 |
username: admin |
| 152 |
password: SomeSecretPassword |
| 153 |
schema: Schema 1 |
| 154 |
template: Template 1 |
| 155 |
target_schema: Schema 2 |
| 156 |
target_template: Template 2 |
| 157 |
epgs: |
| 158 |
- epg: EPG1 |
| 159 |
anp: ANP |
| 160 |
- epg: EPG2 |
| 161 |
anp: ANP2 |
| 162 |
state: present |
| 163 |
""" |
| 164 |
|
| 165 |
RETURN = r""" |
| 166 |
""" |
| 167 |
|
| 168 |
from ansible.module_utils.basic import AnsibleModule |
| 169 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_object_migrate_spec |
| 170 |
|
| 171 |
|
| 172 |
def main(): |
| 173 |
argument_spec = mso_argument_spec() |
| 174 |
argument_spec.update( |
| 175 |
schema=dict(type="str", required=True), |
| 176 |
template=dict(type="str", required=True), |
| 177 |
bds=dict(type="list", elements="str"), |
| 178 |
epgs=dict(type="list", elements="dict", options=mso_object_migrate_spec()), |
| 179 |
target_schema=dict(type="str", required=True), |
| 180 |
target_template=dict(type="str", required=True), |
| 181 |
state=dict(type="str", default="present"), |
| 182 |
) |
| 183 |
|
| 184 |
module = AnsibleModule( |
| 185 |
argument_spec=argument_spec, |
| 186 |
supports_check_mode=True, |
| 187 |
) |
| 188 |
|
| 189 |
schema = module.params.get("schema") |
| 190 |
template = module.params.get("template").replace(" ", "") |
| 191 |
target_schema = module.params.get("target_schema") |
| 192 |
target_template = module.params.get("target_template").replace(" ", "") |
| 193 |
bds = module.params.get("bds") |
| 194 |
epgs = module.params.get("epgs") |
| 195 |
state = module.params.get("state") |
| 196 |
|
| 197 |
mso = MSOModule(module) |
| 198 |
|
| 199 |
# Get schema_id |
| 200 |
schema_id = mso.lookup_schema(schema) |
| 201 |
|
| 202 |
target_schema_id = mso.lookup_schema(target_schema) |
| 203 |
|
| 204 |
if state == "present": |
| 205 |
if schema_id is not None: |
| 206 |
bds_payload = [] |
| 207 |
if bds is not None: |
| 208 |
for bd in bds: |
| 209 |
bds_payload.append(dict(name=bd)) |
| 210 |
|
| 211 |
anp_dict = {} |
| 212 |
if epgs is not None: |
| 213 |
for epg in epgs: |
| 214 |
if epg.get("anp") in anp_dict: |
| 215 |
anp_dict[epg.get("anp")].append(dict(name=epg.get("epg"))) |
| 216 |
else: |
| 217 |
anp_dict[epg.get("anp")] = [dict(name=epg.get("epg"))] |
| 218 |
|
| 219 |
anps_payload = [] |
| 220 |
for anp, epgs_payload in anp_dict.items(): |
| 221 |
anps_payload.append(dict(name=anp, epgs=epgs_payload)) |
| 222 |
|
| 223 |
payload = dict( |
| 224 |
targetSchemaId=target_schema_id, |
| 225 |
targetTemplateName=target_template, |
| 226 |
bds=bds_payload, |
| 227 |
anps=anps_payload, |
| 228 |
) |
| 229 |
|
| 230 |
template = template.replace(" ", "%20") |
| 231 |
|
| 232 |
target_template = target_template.replace(" ", "%20") # removes API error for extra space |
| 233 |
|
| 234 |
mso.existing = mso.request(path="migrate/schema/{0}/template/{1}".format(schema_id, template), method="POST", data=payload) |
| 235 |
|
| 236 |
mso.exit_json() |
| 237 |
|
| 238 |
|
| 239 |
if __name__ == "__main__": |
| 240 |
main() |