| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, 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: ndo_schema_template_bd_dhcp_policy |
| 16 |
short_description: Manage BD DHCP Policies in schema templates |
| 17 |
description: |
| 18 |
- Manage BD DHCP policies in schema templates on Cisco ACI Multi-Site. |
| 19 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 20 |
author: |
| 21 |
- Akini Ross (@akinross) |
| 22 |
options: |
| 23 |
schema: |
| 24 |
description: |
| 25 |
- The name of the schema. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
template: |
| 29 |
description: |
| 30 |
- The name of the template to change. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
bd: |
| 34 |
description: |
| 35 |
- The name of the BD to manage. |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
dhcp_relay_policy: |
| 39 |
description: |
| 40 |
- The name of the DHCP Relay Policy. |
| 41 |
type: str |
| 42 |
dhcp_option_policy: |
| 43 |
description: |
| 44 |
- The name of the DHCP Option Policy. |
| 45 |
- When the O(dhcp_option_policy) is provided, the O(dhcp_relay_policy) must also be provided. |
| 46 |
type: str |
| 47 |
state: |
| 48 |
description: |
| 49 |
- Use C(present) or C(absent) for adding or removing. |
| 50 |
- Use C(query) for listing an object or multiple objects. |
| 51 |
type: str |
| 52 |
choices: [ absent, present, query ] |
| 53 |
default: present |
| 54 |
notes: |
| 55 |
- This module can only be used on versions of NDO that are 4.1 or greater. |
| 56 |
extends_documentation_fragment: cisco.mso.modules |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
- name: Add a new DHCP policy to a BD |
| 61 |
cisco.mso.ndo_schema_template_bd_dhcp_policy: |
| 62 |
host: mso_host |
| 63 |
username: admin |
| 64 |
password: SomeSecretPassword |
| 65 |
schema: Schema1 |
| 66 |
template: Template1 |
| 67 |
bd: BD1 |
| 68 |
dhcp_relay_policy: ansible_test_relay |
| 69 |
dhcp_option_policy: ansible_test_option |
| 70 |
state: present |
| 71 |
|
| 72 |
- name: Query a specific BD DHCP Policy |
| 73 |
cisco.mso.ndo_schema_template_bd_dhcp_policy: |
| 74 |
host: mso_host |
| 75 |
username: admin |
| 76 |
password: SomeSecretPassword |
| 77 |
schema: Schema1 |
| 78 |
template: Template1 |
| 79 |
bd: BD1 |
| 80 |
dhcp_relay_policy: ansible_test_relay |
| 81 |
state: query |
| 82 |
register: query_result |
| 83 |
|
| 84 |
- name: Query all BD DHCP Policies |
| 85 |
cisco.mso.ndo_schema_template_bd_dhcp_policy: |
| 86 |
host: mso_host |
| 87 |
username: admin |
| 88 |
password: SomeSecretPassword |
| 89 |
schema: Schema1 |
| 90 |
template: Template1 |
| 91 |
bd: BD1 |
| 92 |
state: query |
| 93 |
register: query_result |
| 94 |
|
| 95 |
- name: Remove a DHCP policy from a BD |
| 96 |
cisco.mso.ndo_schema_template_bd_dhcp_policy: |
| 97 |
host: mso_host |
| 98 |
username: admin |
| 99 |
password: SomeSecretPassword |
| 100 |
schema: Schema1 |
| 101 |
template: Template1 |
| 102 |
bd: BD1 |
| 103 |
dhcp_relay_policy: ansible_test_relay |
| 104 |
state: absent |
| 105 |
""" |
| 106 |
|
| 107 |
RETURN = r""" |
| 108 |
""" |
| 109 |
|
| 110 |
from ansible.module_utils.basic import AnsibleModule |
| 111 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 112 |
from ansible_collections.cisco.mso.plugins.module_utils.schema import MSOSchema |
| 113 |
|
| 114 |
|
| 115 |
def main(): |
| 116 |
argument_spec = mso_argument_spec() |
| 117 |
argument_spec.update( |
| 118 |
schema=dict(type="str", required=True), |
| 119 |
template=dict(type="str", required=True), |
| 120 |
bd=dict(type="str", required=True), |
| 121 |
dhcp_relay_policy=dict(type="str"), |
| 122 |
dhcp_option_policy=dict(type="str"), |
| 123 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 124 |
) |
| 125 |
|
| 126 |
module = AnsibleModule( |
| 127 |
argument_spec=argument_spec, |
| 128 |
supports_check_mode=True, |
| 129 |
required_if=[ |
| 130 |
["state", "absent", ["dhcp_relay_policy"]], |
| 131 |
["state", "present", ["dhcp_relay_policy"]], |
| 132 |
], |
| 133 |
) |
| 134 |
|
| 135 |
schema = module.params.get("schema") |
| 136 |
template = module.params.get("template").replace(" ", "") |
| 137 |
bd = module.params.get("bd") |
| 138 |
dhcp_relay_policy = module.params.get("dhcp_relay_policy") |
| 139 |
dhcp_option_policy = module.params.get("dhcp_option_policy") |
| 140 |
state = module.params.get("state") |
| 141 |
|
| 142 |
dhcp_labels_path = "/templates/{0}/bds/{1}/dhcpLabels".format(template, bd) |
| 143 |
|
| 144 |
mso = MSOModule(module) |
| 145 |
|
| 146 |
mso_schema = MSOSchema(mso, schema, template) |
| 147 |
mso_schema.set_template_bd(bd) |
| 148 |
|
| 149 |
tenant_id = mso_schema.schema_objects["template"].details.get("tenantId") |
| 150 |
|
| 151 |
if dhcp_relay_policy: |
| 152 |
existing_dhcp_relay_policy = {} |
| 153 |
dhcp_relay_policy_uuid = get_dhcp_relay_policy_uuid(mso, tenant_id, dhcp_relay_policy) |
| 154 |
mso_schema.set_template_bd_dhcp_relay_policy(dhcp_relay_policy_uuid, False) |
| 155 |
if mso_schema.schema_objects.get("template_bd_dhcp_relay_policy") is not None: |
| 156 |
dhcp_labels_path = "{0}/{1}".format(dhcp_labels_path, mso_schema.schema_objects["template_bd_dhcp_relay_policy"].index) |
| 157 |
mso_schema.schema_objects["template_bd_dhcp_relay_policy"].details["name"] = dhcp_relay_policy |
| 158 |
dhcp_option_label_ref = mso_schema.schema_objects["template_bd_dhcp_relay_policy"].details.get("dhcpOptionLabel", {}).get("ref") |
| 159 |
if dhcp_option_label_ref: |
| 160 |
mso_schema.schema_objects["template_bd_dhcp_relay_policy"].details["dhcpOptionLabel"]["name"] = get_dhcp_option_label_name( |
| 161 |
mso, dhcp_option_label_ref |
| 162 |
) |
| 163 |
existing_dhcp_relay_policy = mso_schema.schema_objects["template_bd_dhcp_relay_policy"].details |
| 164 |
else: |
| 165 |
existing_dhcp_relay_policy = mso_schema.schema_objects["template_bd"].details.get("dhcpLabels", []) |
| 166 |
for dhcp_relay_policy in existing_dhcp_relay_policy: |
| 167 |
dhcp_relay_policy["name"] = get_dhcp_relay_label_name(mso, dhcp_relay_policy.get("ref")) |
| 168 |
dhcp_option_label_ref = dhcp_relay_policy.get("dhcpOptionLabel", {}).get("ref") |
| 169 |
if dhcp_option_label_ref: |
| 170 |
dhcp_relay_policy["dhcpOptionLabel"]["name"] = get_dhcp_option_label_name(mso, dhcp_option_label_ref) |
| 171 |
|
| 172 |
if state == "query": |
| 173 |
mso.existing = existing_dhcp_relay_policy |
| 174 |
mso.exit_json() |
| 175 |
|
| 176 |
ops = [] |
| 177 |
mso.previous = existing_dhcp_relay_policy |
| 178 |
|
| 179 |
if state == "absent" and existing_dhcp_relay_policy: |
| 180 |
ops.append(dict(op="remove", path=dhcp_labels_path)) |
| 181 |
|
| 182 |
elif state == "present": |
| 183 |
|
| 184 |
payload = dict(ref=dhcp_relay_policy_uuid, name=dhcp_relay_policy) |
| 185 |
|
| 186 |
if dhcp_option_policy: |
| 187 |
dhcp_option_policy_uuid = get_dhcp_option_policy_uuid(mso, tenant_id, dhcp_option_policy) |
| 188 |
payload.update(dhcpOptionLabel=dict(ref=dhcp_option_policy_uuid, name=dhcp_option_policy)) |
| 189 |
|
| 190 |
mso.sanitize(payload, collate=True) |
| 191 |
|
| 192 |
if existing_dhcp_relay_policy: |
| 193 |
ops.append(dict(op="replace", path=dhcp_labels_path, value=mso.sent)) |
| 194 |
else: |
| 195 |
ops.append(dict(op="add", path="{0}/{1}".format(dhcp_labels_path, "-"), value=mso.sent)) |
| 196 |
|
| 197 |
mso.existing = mso.proposed |
| 198 |
|
| 199 |
if not module.check_mode and mso.existing != mso.previous: |
| 200 |
mso.request(mso_schema.path, method="PATCH", data=ops) |
| 201 |
|
| 202 |
mso.exit_json() |
| 203 |
|
| 204 |
|
| 205 |
def get_dhcp_relay_label_name(mso, dhcp_relay_uuid): |
| 206 |
dhcp_relay = mso.request("templates/objects?type=dhcpRelay&uuid={0}".format(dhcp_relay_uuid), "GET") |
| 207 |
if dhcp_relay: |
| 208 |
return dhcp_relay.get("name") |
| 209 |
|
| 210 |
|
| 211 |
def get_dhcp_option_label_name(mso, dhcp_option_uuid): |
| 212 |
dhcp_option = mso.request("templates/objects?type=dhcpOption&uuid={0}".format(dhcp_option_uuid), "GET") |
| 213 |
if dhcp_option: |
| 214 |
return dhcp_option.get("name") |
| 215 |
|
| 216 |
|
| 217 |
def get_dhcp_option_policy_uuid(mso, tenant_id, dhcp_option_label): |
| 218 |
dhcp_options = mso.request("templates/objects?type=dhcpOption&name={0}".format(dhcp_option_label), "GET") |
| 219 |
for dhcp_option in dhcp_options: |
| 220 |
if dhcp_option.get("tenantId") == tenant_id and dhcp_option.get("name") == dhcp_option_label: |
| 221 |
return dhcp_option.get("uuid") |
| 222 |
mso.fail_json("Provided DHCP Option Policy with '{0}' not found.".format(dhcp_option_label)) |
| 223 |
|
| 224 |
|
| 225 |
def get_dhcp_relay_policy_uuid(mso, tenant_id, dhcp_relay_label): |
| 226 |
dhcp_relays = mso.request("templates/objects?type=dhcpRelay&name={0}".format(dhcp_relay_label), "GET") |
| 227 |
for dhcp_relay in dhcp_relays: |
| 228 |
if dhcp_relay.get("tenantId") == tenant_id and dhcp_relay.get("name") == dhcp_relay_label: |
| 229 |
return dhcp_relay.get("uuid") |
| 230 |
mso.fail_json("Provided DHCP Relay Policy with '{0}' not found.".format(dhcp_relay_label)) |
| 231 |
|
| 232 |
|
| 233 |
if __name__ == "__main__": |
| 234 |
main() |