| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Akini Ross (@akinross) <[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_contract_service_graph |
| 16 |
short_description: Manage the service graph association with a contract in schema template |
| 17 |
description: |
| 18 |
- Manage the service graph association with a contract in schema template on Cisco ACI Multi-Site. |
| 19 |
- The Contract Service Graph parameter is supported on versions of MSO/NDO that are 3.3 or greater. |
| 20 |
- The recommended approach is to use M(cisco.mso.ndo_schema_template_contract_service_chain) rather than the Contract Service Graph. |
| 21 |
- Service Chaining is supported only on ND v3.1 (NDO v4.2.3) and later versions. |
| 22 |
|
| 23 |
author: |
| 24 |
- Akini Ross (@akinross) |
| 25 |
options: |
| 26 |
schema: |
| 27 |
description: |
| 28 |
- The name of the schema. |
| 29 |
type: str |
| 30 |
required: true |
| 31 |
template: |
| 32 |
description: |
| 33 |
- The name of the template. |
| 34 |
type: str |
| 35 |
required: true |
| 36 |
contract: |
| 37 |
description: |
| 38 |
- The name of the contract. |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
service_graph: |
| 42 |
description: |
| 43 |
- The service graph to associate with this contract. |
| 44 |
type: str |
| 45 |
service_graph_template: |
| 46 |
description: |
| 47 |
- The template name in which the service graph is located. |
| 48 |
type: str |
| 49 |
service_graph_schema: |
| 50 |
description: |
| 51 |
- The schema name in which the service graph is located. |
| 52 |
type: str |
| 53 |
service_nodes: |
| 54 |
description: |
| 55 |
- A list of nodes and their connector details associated with the Service Graph. |
| 56 |
- The order of the list matches the node id ordering in GUI, so first entry in list will be match node 1. |
| 57 |
type: list |
| 58 |
elements: dict |
| 59 |
suboptions: |
| 60 |
provider: |
| 61 |
description: |
| 62 |
- The name of the Bridge Domain. |
| 63 |
required: true |
| 64 |
type: str |
| 65 |
consumer: |
| 66 |
description: |
| 67 |
- The name of the Bridge Domain. |
| 68 |
required: true |
| 69 |
type: str |
| 70 |
connector_object_type: |
| 71 |
description: |
| 72 |
- The connector ACI object type of the node. |
| 73 |
type: str |
| 74 |
default: bd |
| 75 |
choices: [ bd ] |
| 76 |
provider_schema: |
| 77 |
description: |
| 78 |
- The schema name in which the provider is located. |
| 79 |
type: str |
| 80 |
provider_template: |
| 81 |
description: |
| 82 |
- The template name in which the provider is located. |
| 83 |
type: str |
| 84 |
consumer_schema: |
| 85 |
description: |
| 86 |
- The schema name in which the consumer is located. |
| 87 |
type: str |
| 88 |
consumer_template: |
| 89 |
description: |
| 90 |
- The template name in which the consumer is located. |
| 91 |
type: str |
| 92 |
state: |
| 93 |
description: |
| 94 |
- Use C(present) or C(absent) for adding or removing. |
| 95 |
- Use C(query) for listing an object or multiple objects. |
| 96 |
type: str |
| 97 |
choices: [ absent, present, query ] |
| 98 |
default: present |
| 99 |
seealso: |
| 100 |
- module: cisco.mso.mso_schema_template_contract_filter |
| 101 |
extends_documentation_fragment: cisco.mso.modules |
| 102 |
""" |
| 103 |
|
| 104 |
EXAMPLES = r""" |
| 105 |
- name: Add a new contract service graph |
| 106 |
cisco.mso.mso_schema_template_contract_service_graph: |
| 107 |
host: mso_host |
| 108 |
username: admin |
| 109 |
password: SomeSecretPassword |
| 110 |
schema: Schema 1 |
| 111 |
template: Template 1 |
| 112 |
contract: Contract 1 |
| 113 |
service_graph: SG1 |
| 114 |
service_graph_nodes: |
| 115 |
- provider: b1 |
| 116 |
consumer: b2 |
| 117 |
filter: Filter 1 |
| 118 |
state: present |
| 119 |
|
| 120 |
- name: Remove a contract service graph |
| 121 |
cisco.mso.mso_schema_template_contract_service_graph: |
| 122 |
host: mso_host |
| 123 |
username: admin |
| 124 |
password: SomeSecretPassword |
| 125 |
schema: Schema 1 |
| 126 |
template: Template 1 |
| 127 |
contract: Contract 1 |
| 128 |
service_graph: SG1 |
| 129 |
state: absent |
| 130 |
|
| 131 |
- name: Query a contract service graph |
| 132 |
cisco.mso.mso_schema_template_contract_service_graph: |
| 133 |
host: mso_host |
| 134 |
username: admin |
| 135 |
password: SomeSecretPassword |
| 136 |
schema: Schema 1 |
| 137 |
template: Template 1 |
| 138 |
contract: Contract 1 |
| 139 |
state: query |
| 140 |
register: query_result |
| 141 |
""" |
| 142 |
|
| 143 |
RETURN = r""" |
| 144 |
""" |
| 145 |
|
| 146 |
from ansible.module_utils.basic import AnsibleModule |
| 147 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, mso_service_graph_connector_spec |
| 148 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import SERVICE_NODE_CONNECTOR_MAP |
| 149 |
|
| 150 |
|
| 151 |
def main(): |
| 152 |
argument_spec = mso_argument_spec() |
| 153 |
argument_spec.update( |
| 154 |
schema=dict(type="str", required=True), |
| 155 |
template=dict(type="str", required=True), |
| 156 |
contract=dict(type="str", required=True), |
| 157 |
service_graph=dict(type="str"), |
| 158 |
service_graph_template=dict(type="str"), |
| 159 |
service_graph_schema=dict(type="str"), |
| 160 |
service_nodes=dict(type="list", elements="dict", options=mso_service_graph_connector_spec()), |
| 161 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 162 |
) |
| 163 |
|
| 164 |
module = AnsibleModule( |
| 165 |
argument_spec=argument_spec, |
| 166 |
supports_check_mode=True, |
| 167 |
required_if=[ |
| 168 |
["state", "absent", ["service_graph"]], |
| 169 |
["state", "present", ["service_graph", "service_nodes"]], |
| 170 |
], |
| 171 |
) |
| 172 |
|
| 173 |
schema = module.params.get("schema") |
| 174 |
template_name = module.params.get("template").replace(" ", "") |
| 175 |
contract_name = module.params.get("contract") |
| 176 |
service_graph_name = module.params.get("service_graph") |
| 177 |
service_graph_template = module.params.get("service_graph_template") |
| 178 |
service_graph_schema = module.params.get("service_graph_schema") |
| 179 |
service_nodes = module.params.get("service_nodes") |
| 180 |
|
| 181 |
state = module.params.get("state") |
| 182 |
|
| 183 |
mso = MSOModule(module) |
| 184 |
|
| 185 |
# Initialize variables |
| 186 |
ops = [] |
| 187 |
service_graph_obj = None |
| 188 |
|
| 189 |
# Set path defaults for create logic, if object (contract or filter) is found replace the "-" for specific value |
| 190 |
base_contract_path = "/templates/{0}/contracts".format(template_name) |
| 191 |
service_graph_path = "{0}/{1}/serviceGraphRelationship".format(base_contract_path, contract_name) |
| 192 |
|
| 193 |
# Get schema |
| 194 |
schema_id, schema_path, schema_obj = mso.query_schema(schema) |
| 195 |
|
| 196 |
# Get template |
| 197 |
template_obj = next((item for item in schema_obj.get("templates") if item.get("name") == template_name), None) |
| 198 |
if not template_obj: |
| 199 |
mso.fail_json( |
| 200 |
msg="Provided template '{0}' does not exist. Existing templates: {1}".format( |
| 201 |
template_name, ", ".join([t.get("name") for t in schema_obj.get("templates")]) |
| 202 |
) |
| 203 |
) |
| 204 |
|
| 205 |
# Get contract |
| 206 |
contract_obj = next((item for item in template_obj.get("contracts") if item.get("name") == contract_name), None) |
| 207 |
if contract_obj: |
| 208 |
# Get service graph if it exists in contract |
| 209 |
if contract_obj.get("serviceGraphRelationship"): |
| 210 |
service_graph_obj = contract_obj.get("serviceGraphRelationship") |
| 211 |
mso.update_service_graph_obj(service_graph_obj) |
| 212 |
mso.existing = service_graph_obj |
| 213 |
else: |
| 214 |
mso.fail_json( |
| 215 |
msg="Provided contract '{0}' does not exist. Existing contracts: {1}".format( |
| 216 |
contract_name, ", ".join([c.get("name") for c in template_obj.get("contracts")]) |
| 217 |
) |
| 218 |
) |
| 219 |
|
| 220 |
if state == "query": |
| 221 |
mso.exit_json() |
| 222 |
|
| 223 |
mso.previous = mso.existing |
| 224 |
|
| 225 |
if state == "absent": |
| 226 |
if contract_obj.get("serviceGraphRelationship"): |
| 227 |
mso.existing = {} |
| 228 |
ops.append(dict(op="remove", path=service_graph_path)) |
| 229 |
|
| 230 |
elif state == "present": |
| 231 |
service_nodes_relationship = [] |
| 232 |
service_graph_template = service_graph_template.replace(" ", "") if service_graph_template else template_name |
| 233 |
service_graph_schema = service_graph_schema if service_graph_schema else schema |
| 234 |
service_graph_schema_id, service_graph_schema_path, service_graph_schema_obj = mso.query_schema(service_graph_schema) |
| 235 |
|
| 236 |
# Validation to check if amount of service graph nodes provided is matching the service graph template. |
| 237 |
# The API allows providing more or less service graph nodes behaviour but the GUI does not. |
| 238 |
service_graph_template_obj = next((item for item in service_graph_schema_obj.get("templates") if item.get("name") == service_graph_template), None) |
| 239 |
if not service_graph_template_obj: |
| 240 |
mso.fail_json( |
| 241 |
msg="Provided template '{0}' does not exist. Existing templates: {1}".format( |
| 242 |
template_name, ", ".join([t.get("name") for t in service_graph_schema_obj.get("templates")]) |
| 243 |
) |
| 244 |
) |
| 245 |
service_graph_schema_obj = next((item for item in service_graph_template_obj.get("serviceGraphs") if item.get("name") == service_graph_name), None) |
| 246 |
if service_graph_schema_obj: |
| 247 |
if len(service_nodes) < len(service_graph_schema_obj.get("serviceNodes")): |
| 248 |
mso.fail_json( |
| 249 |
msg="Not enough service nodes defined, {0} service node(s) provided when {1} needed.".format( |
| 250 |
len(service_nodes), len(service_graph_schema_obj.get("serviceNodes")) |
| 251 |
) |
| 252 |
) |
| 253 |
elif len(service_nodes) > len(service_graph_schema_obj.get("serviceNodes")): |
| 254 |
mso.fail_json( |
| 255 |
msg="Too many service nodes defined, {0} service nodes provided when {1} needed.".format( |
| 256 |
len(service_nodes), len(service_graph_schema_obj.get("serviceNodes")) |
| 257 |
) |
| 258 |
) |
| 259 |
else: |
| 260 |
mso.fail_json(msg="Provided service graph '{0}' does not exist.".format(service_graph_name)) |
| 261 |
|
| 262 |
for node_id, service_node in enumerate(service_nodes, 0): |
| 263 |
# Consumer and provider share connector details (so provider/consumer could have separate details in future) |
| 264 |
connector_details = SERVICE_NODE_CONNECTOR_MAP.get(service_node.get("connector_object_type")) |
| 265 |
provider_schema = mso.lookup_schema(service_node.get("provider_schema")) if service_node.get("provider_schema") else schema_id |
| 266 |
provider_template = service_node.get("provider_template").replace(" ", "") if service_node.get("provider_template") else template_name |
| 267 |
consumer_schema = mso.lookup_schema(service_node.get("consumer_schema")) if service_node.get("consumer_schema") else schema_id |
| 268 |
consumer_template = service_node.get("consumer_template").replace(" ", "") if service_node.get("consumer_template") else template_name |
| 269 |
|
| 270 |
service_nodes_relationship.append( |
| 271 |
{ |
| 272 |
"serviceNodeRef": dict( |
| 273 |
schemaId=service_graph_schema_id, |
| 274 |
templateName=service_graph_template, |
| 275 |
serviceGraphName=service_graph_name, |
| 276 |
serviceNodeName=service_graph_schema_obj.get("serviceNodes")[node_id].get("name"), |
| 277 |
), |
| 278 |
"providerConnector": { |
| 279 |
"connectorType": connector_details.get("connector_type"), |
| 280 |
"{0}Ref".format(connector_details.get("id")): { |
| 281 |
"schemaId": provider_schema, |
| 282 |
"templateName": provider_template, |
| 283 |
"{0}Name".format(connector_details.get("id")): service_node.get("provider"), |
| 284 |
}, |
| 285 |
}, |
| 286 |
"consumerConnector": { |
| 287 |
"connectorType": connector_details.get("connector_type"), |
| 288 |
"{0}Ref".format(connector_details.get("id")): { |
| 289 |
"schemaId": consumer_schema, |
| 290 |
"templateName": consumer_template, |
| 291 |
"{0}Name".format(connector_details.get("id")): service_node.get("consumer"), |
| 292 |
}, |
| 293 |
}, |
| 294 |
} |
| 295 |
) |
| 296 |
|
| 297 |
service_graph_payload = dict( |
| 298 |
serviceGraphRef=dict(serviceGraphName=service_graph_name, templateName=service_graph_template, schemaId=service_graph_schema_id), |
| 299 |
serviceNodesRelationship=service_nodes_relationship, |
| 300 |
) |
| 301 |
|
| 302 |
# If service graph exist the operation should be set to "replace" else operation is "add" to create new |
| 303 |
if service_graph_obj: |
| 304 |
ops.append(dict(op="replace", path=service_graph_path, value=service_graph_payload)) |
| 305 |
else: |
| 306 |
ops.append(dict(op="add", path=service_graph_path, value=service_graph_payload)) |
| 307 |
|
| 308 |
mso.existing = mso.sent = service_graph_payload |
| 309 |
|
| 310 |
if not module.check_mode and mso.existing != mso.previous: |
| 311 |
mso.request(schema_path, method="PATCH", data=ops) |
| 312 |
|
| 313 |
mso.exit_json() |
| 314 |
|
| 315 |
|
| 316 |
if __name__ == "__main__": |
| 317 |
main() |