| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Tim Cragg (@timcragg) |
| 5 |
# Copyright: (c) 2025, Shreyas Srish (@shrsr) |
| 6 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 13 |
|
| 14 |
DOCUMENTATION = r""" |
| 15 |
--- |
| 16 |
module: aci_l4l7_service_graph_template_connection |
| 17 |
version_added: "2.12.0" |
| 18 |
short_description: Manage L4-L7 Service Graph Template Abs Connections (vns:AbsConnection) |
| 19 |
description: |
| 20 |
- Manage Layer 4 to Layer 7 (L4-L7) Service Graph Template Connections. |
| 21 |
options: |
| 22 |
tenant: |
| 23 |
description: |
| 24 |
- The name of an existing tenant. |
| 25 |
type: str |
| 26 |
aliases: [ tenant_name ] |
| 27 |
service_graph: |
| 28 |
description: |
| 29 |
- The name of an existing Service Graph. |
| 30 |
type: str |
| 31 |
connection_name: |
| 32 |
description: |
| 33 |
- The name of the connection. |
| 34 |
type: str |
| 35 |
direct_connect: |
| 36 |
description: |
| 37 |
- Whether to enable direct connections. |
| 38 |
- The APIC defaults to C(false) when unset during creation. |
| 39 |
type: bool |
| 40 |
unicast_route: |
| 41 |
description: |
| 42 |
- Whether to enable unicast routing. |
| 43 |
- The APIC defaults to C(true) when unset during creation. |
| 44 |
type: bool |
| 45 |
adjacency_type: |
| 46 |
description: |
| 47 |
- Whether the adjacency is Layer2 or Layer3. |
| 48 |
- The APIC defaults to C(l2) when unset during creation. |
| 49 |
type: str |
| 50 |
choices: [ l2, l3 ] |
| 51 |
connector_direction: |
| 52 |
description: |
| 53 |
- The connector direction. |
| 54 |
- The APIC defaults to C(unknown) when unset during creation. |
| 55 |
type: str |
| 56 |
choices: [ provider, consumer, unknown ] |
| 57 |
connection_type: |
| 58 |
description: |
| 59 |
- The connection type. |
| 60 |
- The APIC defaults to C(external) when unset during creation. |
| 61 |
type: str |
| 62 |
choices: [ internal, external ] |
| 63 |
description: |
| 64 |
description: |
| 65 |
- The description for the Service Graph Template connection. |
| 66 |
type: str |
| 67 |
state: |
| 68 |
description: |
| 69 |
- Use C(present) or C(absent) for adding or removing. |
| 70 |
- Use C(query) for listing an object or multiple objects. |
| 71 |
type: str |
| 72 |
choices: [ absent, present, query ] |
| 73 |
default: present |
| 74 |
extends_documentation_fragment: |
| 75 |
- cisco.aci.aci |
| 76 |
- cisco.aci.annotation |
| 77 |
- cisco.aci.owner |
| 78 |
notes: |
| 79 |
- The I(tenant) and I(service_graph) must exist before using this module in your playbook. |
| 80 |
The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_l4l7_service_graph_template) modules can be used for this. |
| 81 |
seealso: |
| 82 |
- module: cisco.aci.aci_tenant |
| 83 |
- module: cisco.aci.aci_l4l7_service_graph_template |
| 84 |
- name: APIC Management Information Model reference |
| 85 |
description: More information about the internal APIC class, B(vns:AbsConnection) |
| 86 |
link: https://developer.cisco.com/docs/apic-mim-ref/ |
| 87 |
author: |
| 88 |
- Tim Cragg (@timcragg) |
| 89 |
- Shreyas Srish (@shrsr) |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = r""" |
| 93 |
- name: Add a new connection |
| 94 |
cisco.aci.aci_l4l7_service_graph_template_connection: |
| 95 |
host: apic |
| 96 |
username: admin |
| 97 |
password: SomeSecretPassword |
| 98 |
tenant: my_tenant |
| 99 |
service_graph: my_graph |
| 100 |
connection_name: C1 |
| 101 |
state: present |
| 102 |
delegate_to: localhost |
| 103 |
|
| 104 |
- name: Query a connection |
| 105 |
cisco.aci.aci_l4l7_service_graph_template_connection: |
| 106 |
host: apic |
| 107 |
username: admin |
| 108 |
password: SomeSecretPassword |
| 109 |
tenant: my_tenant |
| 110 |
service_graph: my_graph |
| 111 |
connection_name: C1 |
| 112 |
state: query |
| 113 |
delegate_to: localhost |
| 114 |
register: query_result |
| 115 |
|
| 116 |
- name: Query all connections |
| 117 |
cisco.aci.aci_l4l7_service_graph_template_connection: |
| 118 |
host: apic |
| 119 |
username: admin |
| 120 |
password: SomeSecretPassword |
| 121 |
state: query |
| 122 |
delegate_to: localhost |
| 123 |
register: query_result |
| 124 |
|
| 125 |
- name: Delete a connection |
| 126 |
cisco.aci.aci_l4l7_service_graph_template_connection: |
| 127 |
host: apic |
| 128 |
username: admin |
| 129 |
password: SomeSecretPassword |
| 130 |
tenant: my_tenant |
| 131 |
service_graph: my_graph |
| 132 |
connection_name: C1 |
| 133 |
state: absent |
| 134 |
delegate_to: localhost |
| 135 |
""" |
| 136 |
|
| 137 |
RETURN = r""" |
| 138 |
current: |
| 139 |
description: The existing configuration from the APIC after the module has finished |
| 140 |
returned: success |
| 141 |
type: list |
| 142 |
sample: |
| 143 |
[ |
| 144 |
{ |
| 145 |
"fvTenant": { |
| 146 |
"attributes": { |
| 147 |
"descr": "Production environment", |
| 148 |
"dn": "uni/tn-production", |
| 149 |
"name": "production", |
| 150 |
"nameAlias": "", |
| 151 |
"ownerKey": "", |
| 152 |
"ownerTag": "" |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
] |
| 157 |
error: |
| 158 |
description: The error information as returned from the APIC |
| 159 |
returned: failure |
| 160 |
type: dict |
| 161 |
sample: |
| 162 |
{ |
| 163 |
"code": "122", |
| 164 |
"text": "unknown managed object class foo" |
| 165 |
} |
| 166 |
raw: |
| 167 |
description: The raw output returned by the APIC REST API (xml or json) |
| 168 |
returned: parse error |
| 169 |
type: str |
| 170 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 171 |
sent: |
| 172 |
description: The actual/minimal configuration pushed to the APIC |
| 173 |
returned: info |
| 174 |
type: list |
| 175 |
sample: |
| 176 |
{ |
| 177 |
"fvTenant": { |
| 178 |
"attributes": { |
| 179 |
"descr": "Production environment" |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
previous: |
| 184 |
description: The original configuration from the APIC before the module has started |
| 185 |
returned: info |
| 186 |
type: list |
| 187 |
sample: |
| 188 |
[ |
| 189 |
{ |
| 190 |
"fvTenant": { |
| 191 |
"attributes": { |
| 192 |
"descr": "Production", |
| 193 |
"dn": "uni/tn-production", |
| 194 |
"name": "production", |
| 195 |
"nameAlias": "", |
| 196 |
"ownerKey": "", |
| 197 |
"ownerTag": "" |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
] |
| 202 |
proposed: |
| 203 |
description: The assembled configuration from the user-provided parameters |
| 204 |
returned: info |
| 205 |
type: dict |
| 206 |
sample: |
| 207 |
{ |
| 208 |
"fvTenant": { |
| 209 |
"attributes": { |
| 210 |
"descr": "Production environment", |
| 211 |
"name": "production" |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
filter_string: |
| 216 |
description: The filter string used for the request |
| 217 |
returned: failure or debug |
| 218 |
type: str |
| 219 |
sample: ?rsp-prop-include=config-only |
| 220 |
method: |
| 221 |
description: The HTTP method used for the request to the APIC |
| 222 |
returned: failure or debug |
| 223 |
type: str |
| 224 |
sample: POST |
| 225 |
response: |
| 226 |
description: The HTTP response from the APIC |
| 227 |
returned: failure or debug |
| 228 |
type: str |
| 229 |
sample: OK (30 bytes) |
| 230 |
status: |
| 231 |
description: The HTTP status from the APIC |
| 232 |
returned: failure or debug |
| 233 |
type: int |
| 234 |
sample: 200 |
| 235 |
url: |
| 236 |
description: The HTTP url used for the request to the APIC |
| 237 |
returned: failure or debug |
| 238 |
type: str |
| 239 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 240 |
""" |
| 241 |
|
| 242 |
|
| 243 |
from ansible.module_utils.basic import AnsibleModule |
| 244 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 245 |
|
| 246 |
|
| 247 |
def main(): |
| 248 |
argument_spec = aci_argument_spec() |
| 249 |
argument_spec.update(aci_annotation_spec()) |
| 250 |
argument_spec.update(aci_owner_spec()) |
| 251 |
argument_spec.update( |
| 252 |
tenant=dict(type="str", aliases=["tenant_name"]), |
| 253 |
service_graph=dict(type="str"), |
| 254 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 255 |
connection_name=dict(type="str"), |
| 256 |
direct_connect=dict(type="bool"), |
| 257 |
unicast_route=dict(type="bool"), |
| 258 |
adjacency_type=dict(type="str", choices=["l2", "l3"]), |
| 259 |
connector_direction=dict(type="str", choices=["provider", "consumer", "unknown"]), |
| 260 |
connection_type=dict(type="str", choices=["internal", "external"]), |
| 261 |
description=dict(type="str"), |
| 262 |
) |
| 263 |
|
| 264 |
module = AnsibleModule( |
| 265 |
argument_spec=argument_spec, |
| 266 |
supports_check_mode=True, |
| 267 |
required_if=[ |
| 268 |
["state", "absent", ["tenant", "service_graph", "connection_name"]], |
| 269 |
["state", "present", ["tenant", "service_graph", "connection_name"]], |
| 270 |
], |
| 271 |
) |
| 272 |
|
| 273 |
aci = ACIModule(module) |
| 274 |
|
| 275 |
tenant = module.params.get("tenant") |
| 276 |
service_graph = module.params.get("service_graph") |
| 277 |
state = module.params.get("state") |
| 278 |
connection_name = module.params.get("connection_name") |
| 279 |
unicast_route = aci.boolean(module.params.get("unicast_route")) |
| 280 |
adjacency_type = module.params.get("adjacency_type").upper() if module.params.get("adjacency_type") is not None else None |
| 281 |
direct_connect = aci.boolean(module.params.get("direct_connect")) |
| 282 |
connector_direction = module.params.get("connector_direction") |
| 283 |
connection_type = module.params.get("connection_type") |
| 284 |
description = module.params.get("description") |
| 285 |
|
| 286 |
aci.construct_url( |
| 287 |
root_class=dict( |
| 288 |
aci_class="fvTenant", |
| 289 |
aci_rn="tn-{0}".format(tenant), |
| 290 |
module_object=tenant, |
| 291 |
target_filter={"name": tenant}, |
| 292 |
), |
| 293 |
subclass_1=dict( |
| 294 |
aci_class="vnsAbsGraph", |
| 295 |
aci_rn="AbsGraph-{0}".format(service_graph), |
| 296 |
module_object=service_graph, |
| 297 |
target_filter={"name": service_graph}, |
| 298 |
), |
| 299 |
subclass_2=dict( |
| 300 |
aci_class="vnsAbsConnection", |
| 301 |
aci_rn="AbsConnection-{0}".format(connection_name), |
| 302 |
module_object=connection_name, |
| 303 |
target_filter={"name": connection_name}, |
| 304 |
), |
| 305 |
) |
| 306 |
|
| 307 |
aci.get_existing() |
| 308 |
|
| 309 |
if state == "present": |
| 310 |
aci.payload( |
| 311 |
aci_class="vnsAbsConnection", |
| 312 |
class_config=dict( |
| 313 |
name=connection_name, |
| 314 |
adjType=adjacency_type, |
| 315 |
directConnect=direct_connect, |
| 316 |
unicastRoute=unicast_route, |
| 317 |
connType=connection_type, |
| 318 |
connDir=connector_direction, |
| 319 |
descr=description, |
| 320 |
), |
| 321 |
) |
| 322 |
aci.get_diff(aci_class="vnsAbsConnection") |
| 323 |
|
| 324 |
aci.post_config() |
| 325 |
|
| 326 |
elif state == "absent": |
| 327 |
aci.delete_config() |
| 328 |
|
| 329 |
aci.exit_json() |
| 330 |
|
| 331 |
|
| 332 |
if __name__ == "__main__": |
| 333 |
main() |