| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Dag Wieers (@dagwieers) <[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_anp |
| 16 |
short_description: Manage Application Network Profiles (ANPs) in schema templates |
| 17 |
description: |
| 18 |
- Manage ANPs in schema templates on Cisco ACI Multi-Site. |
| 19 |
author: |
| 20 |
- Dag Wieers (@dagwieers) |
| 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 |
anp: |
| 33 |
description: |
| 34 |
- The name of the ANP to manage. |
| 35 |
type: str |
| 36 |
aliases: [ name ] |
| 37 |
description: |
| 38 |
description: |
| 39 |
- The description of ANP is supported on versions of MSO that are 3.3 or greater. |
| 40 |
type: str |
| 41 |
display_name: |
| 42 |
description: |
| 43 |
- The name as displayed on the MSO web interface. |
| 44 |
type: str |
| 45 |
state: |
| 46 |
description: |
| 47 |
- Use C(present) or C(absent) for adding or removing. |
| 48 |
- Use C(query) for listing an object or multiple objects. |
| 49 |
type: str |
| 50 |
choices: [ absent, present, query ] |
| 51 |
default: present |
| 52 |
seealso: |
| 53 |
- module: cisco.mso.mso_schema_template |
| 54 |
- module: cisco.mso.mso_schema_template_anp_epg |
| 55 |
extends_documentation_fragment: cisco.mso.modules |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = r""" |
| 59 |
- name: Add a new ANP |
| 60 |
cisco.mso.mso_schema_template_anp: |
| 61 |
host: mso_host |
| 62 |
username: admin |
| 63 |
password: SomeSecretPassword |
| 64 |
schema: Schema 1 |
| 65 |
template: Template 1 |
| 66 |
anp: ANP 1 |
| 67 |
state: present |
| 68 |
|
| 69 |
- name: Remove an ANP |
| 70 |
cisco.mso.mso_schema_template_anp: |
| 71 |
host: mso_host |
| 72 |
username: admin |
| 73 |
password: SomeSecretPassword |
| 74 |
schema: Schema 1 |
| 75 |
template: Template 1 |
| 76 |
anp: ANP 1 |
| 77 |
state: absent |
| 78 |
|
| 79 |
- name: Query a specific ANPs |
| 80 |
cisco.mso.mso_schema_template_anp: |
| 81 |
host: mso_host |
| 82 |
username: admin |
| 83 |
password: SomeSecretPassword |
| 84 |
schema: Schema 1 |
| 85 |
template: Template 1 |
| 86 |
state: query |
| 87 |
register: query_result |
| 88 |
|
| 89 |
- name: Query all ANPs |
| 90 |
cisco.mso.mso_schema_template_anp: |
| 91 |
host: mso_host |
| 92 |
username: admin |
| 93 |
password: SomeSecretPassword |
| 94 |
schema: Schema 1 |
| 95 |
template: Template 1 |
| 96 |
state: query |
| 97 |
register: query_result |
| 98 |
""" |
| 99 |
|
| 100 |
RETURN = r""" |
| 101 |
""" |
| 102 |
|
| 103 |
from ansible.module_utils.basic import AnsibleModule |
| 104 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 105 |
|
| 106 |
|
| 107 |
def main(): |
| 108 |
argument_spec = mso_argument_spec() |
| 109 |
argument_spec.update( |
| 110 |
schema=dict(type="str", required=True), |
| 111 |
template=dict(type="str", required=True), |
| 112 |
anp=dict(type="str", aliases=["name"]), # This parameter is not required for querying all objects |
| 113 |
description=dict(type="str"), |
| 114 |
display_name=dict(type="str"), |
| 115 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 116 |
) |
| 117 |
|
| 118 |
module = AnsibleModule( |
| 119 |
argument_spec=argument_spec, |
| 120 |
supports_check_mode=True, |
| 121 |
required_if=[ |
| 122 |
["state", "absent", ["anp"]], |
| 123 |
["state", "present", ["anp"]], |
| 124 |
], |
| 125 |
) |
| 126 |
|
| 127 |
schema = module.params.get("schema") |
| 128 |
template = module.params.get("template").replace(" ", "") |
| 129 |
anp = module.params.get("anp") |
| 130 |
description = module.params.get("description") |
| 131 |
display_name = module.params.get("display_name") |
| 132 |
state = module.params.get("state") |
| 133 |
|
| 134 |
mso = MSOModule(module) |
| 135 |
|
| 136 |
# Get schema |
| 137 |
schema_id, schema_path, schema_obj = mso.query_schema(schema) |
| 138 |
|
| 139 |
# Get template |
| 140 |
templates = [t.get("name") for t in schema_obj.get("templates")] |
| 141 |
if template not in templates: |
| 142 |
mso.fail_json(msg="Provided template '{0}' does not exist. Existing templates: {1}".format(template, ", ".join(templates))) |
| 143 |
template_idx = templates.index(template) |
| 144 |
|
| 145 |
# Get ANP |
| 146 |
anps = [a.get("name") for a in schema_obj.get("templates")[template_idx]["anps"]] |
| 147 |
|
| 148 |
if anp is not None and anp in anps: |
| 149 |
anp_idx = anps.index(anp) |
| 150 |
mso.existing = schema_obj.get("templates")[template_idx]["anps"][anp_idx] |
| 151 |
|
| 152 |
if state == "query": |
| 153 |
if anp is None: |
| 154 |
mso.existing = schema_obj.get("templates")[template_idx]["anps"] |
| 155 |
elif not mso.existing: |
| 156 |
mso.fail_json(msg="ANP '{anp}' not found".format(anp=anp)) |
| 157 |
mso.exit_json() |
| 158 |
|
| 159 |
anps_path = "/templates/{0}/anps".format(template) |
| 160 |
anp_path = "/templates/{0}/anps/{1}".format(template, anp) |
| 161 |
ops = [] |
| 162 |
|
| 163 |
mso.previous = mso.existing |
| 164 |
if state == "absent": |
| 165 |
if mso.existing: |
| 166 |
mso.sent = mso.existing = {} |
| 167 |
ops.append(dict(op="remove", path=anp_path)) |
| 168 |
|
| 169 |
elif state == "present": |
| 170 |
if display_name is None and not mso.existing: |
| 171 |
display_name = anp |
| 172 |
|
| 173 |
epgs = [] |
| 174 |
if mso.existing: |
| 175 |
epgs = None |
| 176 |
|
| 177 |
payload = dict( |
| 178 |
name=anp, |
| 179 |
displayName=display_name, |
| 180 |
epgs=epgs, |
| 181 |
) |
| 182 |
|
| 183 |
if description is not None: |
| 184 |
payload.update(description=description) |
| 185 |
|
| 186 |
mso.sanitize(payload, collate=True) |
| 187 |
|
| 188 |
if mso.existing: |
| 189 |
if display_name is not None: |
| 190 |
ops.append(dict(op="replace", path=anp_path + "/displayName", value=display_name)) |
| 191 |
else: |
| 192 |
ops.append(dict(op="add", path=anps_path + "/-", value=mso.sent)) |
| 193 |
|
| 194 |
mso.existing = mso.proposed |
| 195 |
|
| 196 |
if "anpRef" in mso.previous: |
| 197 |
del mso.previous["anpRef"] |
| 198 |
|
| 199 |
if not module.check_mode: |
| 200 |
mso.request(schema_path, method="PATCH", data=ops) |
| 201 |
|
| 202 |
mso.exit_json() |
| 203 |
|
| 204 |
|
| 205 |
if __name__ == "__main__": |
| 206 |
main() |