| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Cindy Zhao <[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 |
DOCUMENTATION = r""" |
| 12 |
--- |
| 13 |
module: aci_cloud_epg |
| 14 |
short_description: Manage Cloud EPG (cloud:EPg) |
| 15 |
description: |
| 16 |
- Manage Cloud EPG on Cisco Cloud ACI |
| 17 |
options: |
| 18 |
tenant: |
| 19 |
description: |
| 20 |
- The name of the existing tenant. |
| 21 |
type: str |
| 22 |
ap: |
| 23 |
description: |
| 24 |
- The name of the cloud application profile. |
| 25 |
aliases: [ app_profile, app_profile_name ] |
| 26 |
type: str |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the cloud EPG. |
| 30 |
aliases: [ cloud_epg, cloud_epg_name, epg, epg_name ] |
| 31 |
type: str |
| 32 |
description: |
| 33 |
description: |
| 34 |
- Description of the cloud EPG. |
| 35 |
aliases: [ descr ] |
| 36 |
type: str |
| 37 |
vrf: |
| 38 |
description: |
| 39 |
- The name of the VRF. |
| 40 |
type: str |
| 41 |
aliases: [ context, vrf_name ] |
| 42 |
state: |
| 43 |
description: |
| 44 |
- Use C(present) or C(absent) for adding or removing. |
| 45 |
- Use C(query) for listing an object or multiple objects. |
| 46 |
choices: [ absent, present, query ] |
| 47 |
default: present |
| 48 |
type: str |
| 49 |
extends_documentation_fragment: |
| 50 |
- cisco.aci.aci |
| 51 |
- cisco.aci.annotation |
| 52 |
|
| 53 |
notes: |
| 54 |
- More information about the internal APIC class B(cloud:EPg) from |
| 55 |
L(the APIC Management Information Model reference,https://developer.cisco.com/docs/apic-mim-ref/). |
| 56 |
author: |
| 57 |
- Nirav (@nirav) |
| 58 |
- Cindy Zhao (@cizhao) |
| 59 |
""" |
| 60 |
|
| 61 |
EXAMPLES = r""" |
| 62 |
- name: Create aci cloud epg (check_mode) |
| 63 |
cisco.aci.aci_cloud_epg: |
| 64 |
host: apic |
| 65 |
username: admin |
| 66 |
password: SomeSecretPassword |
| 67 |
tenant: tenantName |
| 68 |
ap: apName |
| 69 |
vrf: vrfName |
| 70 |
description: Aci Cloud EPG |
| 71 |
name: epgName |
| 72 |
state: present |
| 73 |
delegate_to: localhost |
| 74 |
|
| 75 |
- name: Remove cloud epg |
| 76 |
cisco.aci.aci_cloud_epg: |
| 77 |
host: apic |
| 78 |
username: admin |
| 79 |
password: SomeSecretPassword |
| 80 |
tenant: tenantName |
| 81 |
ap: apName |
| 82 |
name: cloudName |
| 83 |
state: absent |
| 84 |
delegate_to: localhost |
| 85 |
|
| 86 |
- name: query all |
| 87 |
cisco.aci.aci_cloud_epg: |
| 88 |
host: apic |
| 89 |
username: admin |
| 90 |
password: SomeSecretPassword |
| 91 |
tenant: tenantName |
| 92 |
ap: apName |
| 93 |
state: query |
| 94 |
delegate_to: localhost |
| 95 |
|
| 96 |
- name: query a specific cloud epg |
| 97 |
cisco.aci.aci_cloud_epg: |
| 98 |
host: apic |
| 99 |
username: admin |
| 100 |
password: SomeSecretPassword |
| 101 |
tenant: tenantName |
| 102 |
ap: apName |
| 103 |
name: epgName |
| 104 |
state: query |
| 105 |
delegate_to: localhost |
| 106 |
""" |
| 107 |
|
| 108 |
RETURN = r""" |
| 109 |
current: |
| 110 |
description: The existing configuration from the APIC after the module has finished |
| 111 |
returned: success |
| 112 |
type: list |
| 113 |
sample: |
| 114 |
[ |
| 115 |
{ |
| 116 |
"fvTenant": { |
| 117 |
"attributes": { |
| 118 |
"descr": "Production environment", |
| 119 |
"dn": "uni/tn-production", |
| 120 |
"name": "production", |
| 121 |
"nameAlias": "", |
| 122 |
"ownerKey": "", |
| 123 |
"ownerTag": "" |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
] |
| 128 |
error: |
| 129 |
description: The error information as returned from the APIC |
| 130 |
returned: failure |
| 131 |
type: dict |
| 132 |
sample: |
| 133 |
{ |
| 134 |
"code": "122", |
| 135 |
"text": "unknown managed object class foo" |
| 136 |
} |
| 137 |
raw: |
| 138 |
description: The raw output returned by the APIC REST API (xml or json) |
| 139 |
returned: parse error |
| 140 |
type: str |
| 141 |
sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>' |
| 142 |
sent: |
| 143 |
description: The actual/minimal configuration pushed to the APIC |
| 144 |
returned: info |
| 145 |
type: list |
| 146 |
sample: |
| 147 |
{ |
| 148 |
"fvTenant": { |
| 149 |
"attributes": { |
| 150 |
"descr": "Production environment" |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
previous: |
| 155 |
description: The original configuration from the APIC before the module has started |
| 156 |
returned: info |
| 157 |
type: list |
| 158 |
sample: |
| 159 |
[ |
| 160 |
{ |
| 161 |
"fvTenant": { |
| 162 |
"attributes": { |
| 163 |
"descr": "Production", |
| 164 |
"dn": "uni/tn-production", |
| 165 |
"name": "production", |
| 166 |
"nameAlias": "", |
| 167 |
"ownerKey": "", |
| 168 |
"ownerTag": "" |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
] |
| 173 |
proposed: |
| 174 |
description: The assembled configuration from the user-provided parameters |
| 175 |
returned: info |
| 176 |
type: dict |
| 177 |
sample: |
| 178 |
{ |
| 179 |
"fvTenant": { |
| 180 |
"attributes": { |
| 181 |
"descr": "Production environment", |
| 182 |
"name": "production" |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
filter_string: |
| 187 |
description: The filter string used for the request |
| 188 |
returned: failure or debug |
| 189 |
type: str |
| 190 |
sample: ?rsp-prop-include=config-only |
| 191 |
method: |
| 192 |
description: The HTTP method used for the request to the APIC |
| 193 |
returned: failure or debug |
| 194 |
type: str |
| 195 |
sample: POST |
| 196 |
response: |
| 197 |
description: The HTTP response from the APIC |
| 198 |
returned: failure or debug |
| 199 |
type: str |
| 200 |
sample: OK (30 bytes) |
| 201 |
status: |
| 202 |
description: The HTTP status from the APIC |
| 203 |
returned: failure or debug |
| 204 |
type: int |
| 205 |
sample: 200 |
| 206 |
url: |
| 207 |
description: The HTTP url used for the request to the APIC |
| 208 |
returned: failure or debug |
| 209 |
type: str |
| 210 |
sample: https://10.11.12.13/api/mo/uni/tn-production.json |
| 211 |
""" |
| 212 |
|
| 213 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 214 |
from ansible.module_utils.basic import AnsibleModule |
| 215 |
|
| 216 |
|
| 217 |
def main(): |
| 218 |
argument_spec = aci_argument_spec() |
| 219 |
argument_spec.update(aci_annotation_spec()) |
| 220 |
argument_spec.update( |
| 221 |
{ |
| 222 |
"description": dict(type="str", aliases=["descr"]), |
| 223 |
"name": dict(type="str", aliases=["cloud_epg", "cloud_epg_name", "epg", "epg_name"]), |
| 224 |
"tenant": dict( |
| 225 |
type="str", |
| 226 |
), |
| 227 |
"ap": dict(type="str", aliases=["app_profile", "app_profile_name"]), |
| 228 |
"state": dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 229 |
"vrf": dict(type="str", aliases=["context", "vrf_name"]), |
| 230 |
} |
| 231 |
) |
| 232 |
|
| 233 |
module = AnsibleModule( |
| 234 |
argument_spec=argument_spec, |
| 235 |
supports_check_mode=True, |
| 236 |
required_if=[ |
| 237 |
["state", "absent", ["name", "tenant", "ap"]], |
| 238 |
["state", "present", ["name", "tenant", "ap"]], |
| 239 |
], |
| 240 |
) |
| 241 |
|
| 242 |
description = module.params.get("description") |
| 243 |
name = module.params.get("name") |
| 244 |
tenant = module.params.get("tenant") |
| 245 |
ap = module.params.get("ap") |
| 246 |
state = module.params.get("state") |
| 247 |
child_configs = [] |
| 248 |
relation_vrf = module.params.get("vrf") |
| 249 |
|
| 250 |
if relation_vrf: |
| 251 |
child_configs.append({"cloudRsCloudEPgCtx": {"attributes": {"tnFvCtxName": relation_vrf}}}) |
| 252 |
|
| 253 |
aci = ACIModule(module) |
| 254 |
aci.construct_url( |
| 255 |
root_class={ |
| 256 |
"aci_class": "fvTenant", |
| 257 |
"aci_rn": "tn-{0}".format(tenant), |
| 258 |
"target_filter": 'eq(fvTenant.name, "{0}")'.format(tenant), |
| 259 |
"module_object": tenant, |
| 260 |
}, |
| 261 |
subclass_1={"aci_class": "cloudApp", "aci_rn": "cloudapp-{0}".format(ap), "target_filter": 'eq(cloudApp.name, "{0}")'.format(ap), "module_object": ap}, |
| 262 |
subclass_2={ |
| 263 |
"aci_class": "cloudEPg", |
| 264 |
"aci_rn": "cloudepg-{0}".format(name), |
| 265 |
"target_filter": 'eq(cloudEPg.name, "{0}")'.format(name), |
| 266 |
"module_object": name, |
| 267 |
}, |
| 268 |
child_classes=["cloudRsCloudEPgCtx"], |
| 269 |
) |
| 270 |
|
| 271 |
aci.get_existing() |
| 272 |
|
| 273 |
if state == "present": |
| 274 |
aci.payload( |
| 275 |
aci_class="cloudEPg", |
| 276 |
class_config={ |
| 277 |
"descr": description, |
| 278 |
"name": name, |
| 279 |
}, |
| 280 |
child_configs=child_configs, |
| 281 |
) |
| 282 |
|
| 283 |
aci.get_diff(aci_class="cloudEPg") |
| 284 |
|
| 285 |
aci.post_config() |
| 286 |
|
| 287 |
elif state == "absent": |
| 288 |
aci.delete_config() |
| 289 |
|
| 290 |
aci.exit_json() |
| 291 |
|
| 292 |
|
| 293 |
if __name__ == "__main__": |
| 294 |
main() |