| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Anvitha Jain (@anvitha-jain) <[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_external_epg_selector |
| 14 |
short_description: Manage Cloud Endpoint Selector for External EPGs (cloud:ExtEPSelector) |
| 15 |
description: |
| 16 |
- Decides which endpoints belong to the EPGs based on several parameters. |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- The name of the Cloud Endpoint selector. |
| 21 |
aliases: [ selector, cloud_external_epg_selector, external_epg_selector, extepg_selector, selector_name ] |
| 22 |
type: str |
| 23 |
subnet: |
| 24 |
description: |
| 25 |
- IP address of the Cloud Subnet. |
| 26 |
aliases: [ ip ] |
| 27 |
type: str |
| 28 |
tenant: |
| 29 |
description: |
| 30 |
- The name of tenant. |
| 31 |
type: str |
| 32 |
ap: |
| 33 |
description: |
| 34 |
- The name of the cloud application profile. |
| 35 |
aliases: [ app_profile, app_profile_name ] |
| 36 |
type: str |
| 37 |
cloud_external_epg: |
| 38 |
description: |
| 39 |
- Name of Object cloud_external_epg. |
| 40 |
type: str |
| 41 |
state: |
| 42 |
description: |
| 43 |
- Use C(present) or C(absent) for adding or removing. |
| 44 |
- Use C(query) for listing an object or multiple objects. |
| 45 |
choices: [ absent, present, query ] |
| 46 |
default: present |
| 47 |
type: str |
| 48 |
extends_documentation_fragment: |
| 49 |
- cisco.aci.aci |
| 50 |
- cisco.aci.annotation |
| 51 |
- cisco.aci.owner |
| 52 |
|
| 53 |
notes: |
| 54 |
- More information about the internal APIC class B(cloud:ExtEPSelector) from |
| 55 |
L(the APIC Management Information Model reference,https://developer.cisco.com/docs/apic-mim-ref/). |
| 56 |
author: |
| 57 |
- Anvitha Jain (@anvitha-jain) |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = r""" |
| 61 |
- name: Add a new cloud external EPG selector |
| 62 |
cisco.aci.aci_cloud_external_epg_selector: |
| 63 |
host: apic |
| 64 |
username: admin |
| 65 |
password: SomeSecretPassword |
| 66 |
tenant: tenant1 |
| 67 |
ap: ap1 |
| 68 |
cloud_external_epg: ext_epg |
| 69 |
name: subnet_name |
| 70 |
subnet: 10.0.0.0/16 |
| 71 |
state: present |
| 72 |
delegate_to: localhost |
| 73 |
|
| 74 |
- name: Remove a cloud external EPG selector |
| 75 |
cisco.aci.aci_cloud_external_epg_selector: |
| 76 |
host: apic |
| 77 |
username: admin |
| 78 |
password: SomeSecretPassword |
| 79 |
validate_certs: false |
| 80 |
tenant: tenant1 |
| 81 |
ap: ap1 |
| 82 |
cloud_external_epg: ext_epg |
| 83 |
name: subnet_name |
| 84 |
subnet: 10.0.0.0/16 |
| 85 |
state: absent |
| 86 |
delegate_to: localhost |
| 87 |
|
| 88 |
- name: Query all cloud external EPG selectors |
| 89 |
cisco.aci.aci_cloud_external_epg_selector: |
| 90 |
host: apic |
| 91 |
username: admin |
| 92 |
password: SomeSecretPassword |
| 93 |
tenant: tenant1 |
| 94 |
ap: ap1 |
| 95 |
cloud_external_epg: ext_epg |
| 96 |
state: query |
| 97 |
delegate_to: localhost |
| 98 |
""" |
| 99 |
|
| 100 |
from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec |
| 101 |
from ansible.module_utils.basic import AnsibleModule |
| 102 |
|
| 103 |
|
| 104 |
def main(): |
| 105 |
argument_spec = aci_argument_spec() |
| 106 |
argument_spec.update(aci_annotation_spec()) |
| 107 |
argument_spec.update(aci_owner_spec()) |
| 108 |
argument_spec.update( |
| 109 |
{ |
| 110 |
"name": dict(type="str", aliases=["selector", "cloud_external_epg_selector", "external_epg_selector", "extepg_selector", "selector_name"]), |
| 111 |
"subnet": dict(type="str", aliases=["ip"]), |
| 112 |
"tenant": dict(type="str"), |
| 113 |
"cloud_external_epg": dict(type="str"), |
| 114 |
"ap": dict(type="str", aliases=["app_profile", "app_profile_name"]), |
| 115 |
"state": dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 116 |
} |
| 117 |
) |
| 118 |
|
| 119 |
module = AnsibleModule( |
| 120 |
argument_spec=argument_spec, |
| 121 |
supports_check_mode=True, |
| 122 |
required_if=[ |
| 123 |
["state", "absent", ["subnet", "tenant", "ap", "cloud_external_epg"]], |
| 124 |
["state", "present", ["subnet", "tenant", "ap", "cloud_external_epg"]], |
| 125 |
], |
| 126 |
) |
| 127 |
|
| 128 |
name = module.params.get("name") |
| 129 |
subnet = module.params.get("subnet") |
| 130 |
tenant = module.params.get("tenant") |
| 131 |
ap = module.params.get("ap") |
| 132 |
cloud_external_epg = module.params.get("cloud_external_epg") |
| 133 |
state = module.params.get("state") |
| 134 |
child_configs = [] |
| 135 |
|
| 136 |
aci = ACIModule(module) |
| 137 |
aci.construct_url( |
| 138 |
root_class={ |
| 139 |
"aci_class": "fvTenant", |
| 140 |
"aci_rn": "tn-{0}".format(tenant), |
| 141 |
"target_filter": 'eq(fvTenant.name, "{0}")'.format(tenant), |
| 142 |
"module_object": tenant, |
| 143 |
}, |
| 144 |
subclass_1={"aci_class": "cloudApp", "aci_rn": "cloudapp-{0}".format(ap), "target_filter": 'eq(cloudApp.name, "{0}")'.format(ap), "module_object": ap}, |
| 145 |
subclass_2={ |
| 146 |
"aci_class": "cloudExtEPg", |
| 147 |
"aci_rn": "cloudextepg-{0}".format(cloud_external_epg), |
| 148 |
"target_filter": 'eq(cloudExtEPg.name, "{0}")'.format(cloud_external_epg), |
| 149 |
"module_object": cloud_external_epg, |
| 150 |
}, |
| 151 |
subclass_3={ |
| 152 |
"aci_class": "cloudExtEPSelector", |
| 153 |
"aci_rn": "extepselector-[{0}]".format(subnet), |
| 154 |
"target_filter": 'eq(cloudExtEPSelector.name, "{0}")'.format(subnet), |
| 155 |
"module_object": subnet, |
| 156 |
}, |
| 157 |
child_classes=[], |
| 158 |
) |
| 159 |
|
| 160 |
aci.get_existing() |
| 161 |
|
| 162 |
if state == "present": |
| 163 |
aci.payload( |
| 164 |
aci_class="cloudExtEPSelector", |
| 165 |
class_config={ |
| 166 |
"name": name, |
| 167 |
"subnet": subnet, |
| 168 |
}, |
| 169 |
child_configs=child_configs, |
| 170 |
) |
| 171 |
|
| 172 |
aci.get_diff(aci_class="cloudExtEPSelector") |
| 173 |
|
| 174 |
aci.post_config() |
| 175 |
|
| 176 |
elif state == "absent": |
| 177 |
aci.delete_config() |
| 178 |
|
| 179 |
aci.exit_json() |
| 180 |
|
| 181 |
|
| 182 |
if __name__ == "__main__": |
| 183 |
main() |