| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Shreyas Srish (@shrsr) <[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_service_graph |
| 16 |
short_description: Manage Service Graph in schema templates |
| 17 |
description: |
| 18 |
- Manage Service Graph in schema templates on Cisco ACI Multi-Site. |
| 19 |
author: |
| 20 |
- Shreyas Srish (@shrsr) |
| 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 |
service_graph: |
| 33 |
description: |
| 34 |
- The name of the Service Graph to manage. |
| 35 |
type: str |
| 36 |
aliases: [ name ] |
| 37 |
description: |
| 38 |
description: |
| 39 |
- The description of Service Graph. |
| 40 |
type: str |
| 41 |
default: '' |
| 42 |
display_name: |
| 43 |
description: |
| 44 |
- The name as displayed on the MSO web interface. |
| 45 |
type: str |
| 46 |
service_nodes: |
| 47 |
description: |
| 48 |
- A list of node types to be associated with the Service Graph. |
| 49 |
type: list |
| 50 |
elements: dict |
| 51 |
suboptions: |
| 52 |
type: |
| 53 |
description: |
| 54 |
- The type of node |
| 55 |
required: true |
| 56 |
type: str |
| 57 |
filter_after_first_node: |
| 58 |
description: |
| 59 |
- The filter applied after the first node. |
| 60 |
type: str |
| 61 |
choices: [ allow_all, filters_from_contract ] |
| 62 |
state: |
| 63 |
description: |
| 64 |
- Use C(present) or C(absent) for adding or removing. |
| 65 |
- Use C(query) for listing an object or multiple objects. |
| 66 |
type: str |
| 67 |
choices: [ absent, present, query ] |
| 68 |
default: present |
| 69 |
extends_documentation_fragment: cisco.mso.modules |
| 70 |
""" |
| 71 |
|
| 72 |
EXAMPLES = r""" |
| 73 |
- name: Add a new Service Graph |
| 74 |
cisco.mso.mso_schema_template_service_graph: |
| 75 |
host: mso_host |
| 76 |
username: admin |
| 77 |
password: SomeSecretPassword |
| 78 |
schema: Schema1 |
| 79 |
template: Template1 |
| 80 |
service_graph: graph1 |
| 81 |
service_nodes: |
| 82 |
- type: firewall |
| 83 |
- type: other |
| 84 |
- type: load-balancer |
| 85 |
state: present |
| 86 |
|
| 87 |
- name: Remove a Service Graph |
| 88 |
cisco.mso.mso_schema_template_service_graph: |
| 89 |
host: mso_host |
| 90 |
username: admin |
| 91 |
password: SomeSecretPassword |
| 92 |
schema: Schema1 |
| 93 |
template: Template1 |
| 94 |
service_graph: graph1 |
| 95 |
state: absent |
| 96 |
|
| 97 |
- name: Query a specific Service Graph |
| 98 |
cisco.mso.mso_schema_template_service_graph: |
| 99 |
host: mso_host |
| 100 |
username: admin |
| 101 |
password: SomeSecretPassword |
| 102 |
schema: Schema1 |
| 103 |
template: Template1 |
| 104 |
service_graph: graph1 |
| 105 |
state: query |
| 106 |
register: query_result |
| 107 |
|
| 108 |
- name: Query all Service Graphs |
| 109 |
cisco.mso.mso_schema_template_service_graph: |
| 110 |
host: mso_host |
| 111 |
username: admin |
| 112 |
password: SomeSecretPassword |
| 113 |
schema: Schema1 |
| 114 |
template: Template1 |
| 115 |
state: query |
| 116 |
register: query_result |
| 117 |
""" |
| 118 |
|
| 119 |
RETURN = r""" |
| 120 |
""" |
| 121 |
|
| 122 |
from ansible.module_utils.basic import AnsibleModule |
| 123 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_service_graph_node_spec |
| 124 |
|
| 125 |
|
| 126 |
def main(): |
| 127 |
argument_spec = mso_argument_spec() |
| 128 |
argument_spec.update( |
| 129 |
schema=dict(type="str", required=True), |
| 130 |
template=dict(type="str", required=True), |
| 131 |
service_graph=dict(type="str", aliases=["name"]), |
| 132 |
description=dict(type="str", default=""), |
| 133 |
display_name=dict(type="str"), |
| 134 |
service_nodes=dict(type="list", elements="dict", options=mso_service_graph_node_spec()), |
| 135 |
filter_after_first_node=dict(type="str", choices=["allow_all", "filters_from_contract"]), |
| 136 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 137 |
) |
| 138 |
|
| 139 |
module = AnsibleModule( |
| 140 |
argument_spec=argument_spec, |
| 141 |
supports_check_mode=True, |
| 142 |
required_if=[ |
| 143 |
["state", "absent", ["service_graph"]], |
| 144 |
["state", "present", ["service_graph", "service_nodes"]], |
| 145 |
], |
| 146 |
) |
| 147 |
|
| 148 |
schema = module.params.get("schema") |
| 149 |
template = module.params.get("template").replace(" ", "") |
| 150 |
service_graph = module.params.get("service_graph") |
| 151 |
display_name = module.params.get("display_name") |
| 152 |
description = module.params.get("description") |
| 153 |
service_nodes = module.params.get("service_nodes") |
| 154 |
filter_after_first_node = module.params.get("filter_after_first_node") |
| 155 |
state = module.params.get("state") |
| 156 |
|
| 157 |
mso = MSOModule(module) |
| 158 |
|
| 159 |
# Get schema |
| 160 |
schema_id, schema_path, schema_obj = mso.query_schema(schema) |
| 161 |
|
| 162 |
# Get template |
| 163 |
templates = [t.get("name") for t in schema_obj.get("templates")] |
| 164 |
if template not in templates: |
| 165 |
mso.fail_json( |
| 166 |
msg="Provided template '{template}' does not exist. Existing templates: {templates}".format(template=template, templates=", ".join(templates)) |
| 167 |
) |
| 168 |
template_idx = templates.index(template) |
| 169 |
|
| 170 |
mso.existing = {} |
| 171 |
service_graph_idx = None |
| 172 |
|
| 173 |
# Get Service Graphs |
| 174 |
service_graphs = [f.get("name") for f in schema_obj.get("templates")[template_idx]["serviceGraphs"]] |
| 175 |
if service_graph in service_graphs: |
| 176 |
service_graph_idx = service_graphs.index(service_graph) |
| 177 |
mso.existing = schema_obj.get("templates")[template_idx]["serviceGraphs"][service_graph_idx] |
| 178 |
|
| 179 |
if state == "query": |
| 180 |
if service_graph is None: |
| 181 |
mso.existing = schema_obj.get("templates")[template_idx]["serviceGraphs"] |
| 182 |
if service_graph is not None and service_graph_idx is None: |
| 183 |
mso.fail_json(msg="Service Graph '{service_graph}' not found".format(service_graph=service_graph)) |
| 184 |
mso.exit_json() |
| 185 |
|
| 186 |
service_graphs_path = "/templates/{0}/serviceGraphs/-".format(template) |
| 187 |
service_graph_path = "/templates/{0}/serviceGraphs/{1}".format(template, service_graph) |
| 188 |
ops = [] |
| 189 |
|
| 190 |
mso.previous = mso.existing |
| 191 |
if state == "absent": |
| 192 |
if mso.existing: |
| 193 |
mso.sent = mso.existing = {} |
| 194 |
ops.append(dict(op="remove", path=service_graph_path)) |
| 195 |
|
| 196 |
elif state == "present": |
| 197 |
nodes_payload = [] |
| 198 |
service_node_index = 0 |
| 199 |
|
| 200 |
if filter_after_first_node == "allow_all": |
| 201 |
filter_after_first_node = "allow-all" |
| 202 |
elif filter_after_first_node == "filters_from_contract": |
| 203 |
filter_after_first_node = "filters-from-contract" |
| 204 |
|
| 205 |
if display_name is None: |
| 206 |
display_name = service_graph |
| 207 |
|
| 208 |
# Get service nodes |
| 209 |
query_node_data = mso.query_service_node_types() |
| 210 |
service_node_types = [f.get("name") for f in query_node_data] |
| 211 |
if service_nodes is not None: |
| 212 |
for node in service_nodes: |
| 213 |
node_name = node.get("type") |
| 214 |
if node_name in service_node_types: |
| 215 |
service_node_index = service_node_index + 1 |
| 216 |
service_node_name = "node{0}".format(service_node_index) |
| 217 |
for node_data in query_node_data: |
| 218 |
if node_data["name"] == node_name: |
| 219 |
payload = dict( |
| 220 |
name=service_node_name, |
| 221 |
serviceNodeTypeId=node_data.get("id"), |
| 222 |
index=service_node_index, |
| 223 |
serviceNodeRef=dict( |
| 224 |
serviceNodeName=service_node_name, |
| 225 |
serviceGraphName=service_graph, |
| 226 |
templateName=template, |
| 227 |
schemaId=schema_id, |
| 228 |
), |
| 229 |
) |
| 230 |
if node_data.get("uuid"): |
| 231 |
payload.update(uuid=node_data.get("uuid")) |
| 232 |
nodes_payload.append(payload) |
| 233 |
else: |
| 234 |
mso.fail_json( |
| 235 |
"Provided service node type '{node_name}' does not exist. Existing service node types include: {node_types}".format( |
| 236 |
node_name=node_name, node_types=", ".join(service_node_types) |
| 237 |
) |
| 238 |
) |
| 239 |
|
| 240 |
payload = dict( |
| 241 |
name=service_graph, |
| 242 |
displayName=display_name, |
| 243 |
description=description, |
| 244 |
nodeFilter=filter_after_first_node, |
| 245 |
serviceGraphRef=dict( |
| 246 |
serviceGraphName=service_graph, |
| 247 |
templateName=template, |
| 248 |
schemaId=schema_id, |
| 249 |
), |
| 250 |
serviceNodes=nodes_payload, |
| 251 |
) |
| 252 |
|
| 253 |
mso.sanitize(payload, collate=True) |
| 254 |
|
| 255 |
if not mso.existing: |
| 256 |
ops.append(dict(op="add", path=service_graphs_path, value=payload)) |
| 257 |
else: |
| 258 |
ops.append(dict(op="replace", path=service_graph_path, value=mso.sent)) |
| 259 |
|
| 260 |
mso.existing = mso.proposed |
| 261 |
|
| 262 |
if not module.check_mode: |
| 263 |
mso.request(schema_path, method="PATCH", data=ops) |
| 264 |
|
| 265 |
mso.exit_json() |
| 266 |
|
| 267 |
|
| 268 |
if __name__ == "__main__": |
| 269 |
main() |