| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, Jorge Gomez Velasquez <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = { |
| 11 |
"metadata_version": "1.1", |
| 12 |
"status": ["preview"], |
| 13 |
"supported_by": "community", |
| 14 |
} |
| 15 |
|
| 16 |
DOCUMENTATION = r""" |
| 17 |
--- |
| 18 |
module: mso_dhcp_relay_policy |
| 19 |
short_description: Manage DHCP Relay policies. |
| 20 |
description: |
| 21 |
- Manage DHCP Relay policies on Cisco Multi-Site Orchestrator. |
| 22 |
- This module is only supported on NDO version prior to v4.0. |
| 23 |
author: |
| 24 |
- Jorge Gomez (@jorgegome2307) |
| 25 |
options: |
| 26 |
dhcp_relay_policy: |
| 27 |
description: |
| 28 |
- Name of the DHCP Relay Policy |
| 29 |
type: str |
| 30 |
aliases: [ name ] |
| 31 |
description: |
| 32 |
description: |
| 33 |
- Description of the DHCP Relay Policy |
| 34 |
type: str |
| 35 |
tenant: |
| 36 |
description: |
| 37 |
- Tenant where the DHCP Relay Policy is located. |
| 38 |
type: str |
| 39 |
state: |
| 40 |
description: |
| 41 |
- Use C(present) or C(absent) for adding or removing. |
| 42 |
- Use C(query) for listing an object or multiple objects. |
| 43 |
type: str |
| 44 |
choices: [ absent, present, query ] |
| 45 |
default: present |
| 46 |
extends_documentation_fragment: cisco.mso.modules |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = r""" |
| 50 |
- name: Add a new DHCP Relay Policy |
| 51 |
cisco.mso.mso_dhcp_relay_policy: |
| 52 |
host: mso_host |
| 53 |
username: admin |
| 54 |
password: SomeSecretPassword |
| 55 |
dhcp_relay_policy: my_test_dhcp_policy |
| 56 |
description: "My Test DHCP Policy" |
| 57 |
tenant: ansible_test |
| 58 |
state: present |
| 59 |
|
| 60 |
- name: Remove DHCP Relay Policy |
| 61 |
cisco.mso.mso_dhcp_relay_policy: |
| 62 |
host: mso_host |
| 63 |
username: admin |
| 64 |
password: SomeSecretPassword |
| 65 |
dhcp_relay_policy: my_test_dhcp_policy |
| 66 |
state: absent |
| 67 |
|
| 68 |
- name: Query a DHCP Relay Policy |
| 69 |
cisco.mso.mso_dhcp_relay_policy: |
| 70 |
host: mso_host |
| 71 |
username: admin |
| 72 |
password: SomeSecretPassword |
| 73 |
dhcp_relay_policy: my_test_dhcp_policy |
| 74 |
state: query |
| 75 |
register: query_result |
| 76 |
|
| 77 |
- name: Query all DHCP Relay Policies |
| 78 |
cisco.mso.mso_dhcp_relay_policy: |
| 79 |
host: mso_host |
| 80 |
username: admin |
| 81 |
password: SomeSecretPassword |
| 82 |
state: query |
| 83 |
register: query_result |
| 84 |
""" |
| 85 |
|
| 86 |
RETURN = r""" |
| 87 |
""" |
| 88 |
|
| 89 |
from ansible.module_utils.basic import AnsibleModule |
| 90 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 91 |
|
| 92 |
|
| 93 |
def main(): |
| 94 |
argument_spec = mso_argument_spec() |
| 95 |
argument_spec.update( |
| 96 |
dhcp_relay_policy=dict(type="str", aliases=["name"]), |
| 97 |
description=dict(type="str"), |
| 98 |
tenant=dict(type="str"), |
| 99 |
state=dict(type="str", default="present", choices=["absent", "present", "query"]), |
| 100 |
) |
| 101 |
module = AnsibleModule( |
| 102 |
argument_spec=argument_spec, |
| 103 |
supports_check_mode=True, |
| 104 |
required_if=[ |
| 105 |
["state", "absent", ["dhcp_relay_policy"]], |
| 106 |
["state", "present", ["dhcp_relay_policy", "tenant"]], |
| 107 |
], |
| 108 |
) |
| 109 |
|
| 110 |
dhcp_relay_policy = module.params.get("dhcp_relay_policy") |
| 111 |
description = module.params.get("description") |
| 112 |
tenant = module.params.get("tenant") |
| 113 |
state = module.params.get("state") |
| 114 |
|
| 115 |
mso = MSOModule(module) |
| 116 |
|
| 117 |
path = "policies/dhcp/relay" |
| 118 |
|
| 119 |
# Query for existing object(s) |
| 120 |
if dhcp_relay_policy: |
| 121 |
mso.existing = mso.get_obj(path, name=dhcp_relay_policy, key="DhcpRelayPolicies") |
| 122 |
if mso.existing: |
| 123 |
policy_id = mso.existing.get("id") |
| 124 |
# If we found an existing object, continue with it |
| 125 |
path = "{0}/{1}".format(path, policy_id) |
| 126 |
else: |
| 127 |
mso.existing = mso.query_objs(path, key="DhcpRelayPolicies") |
| 128 |
|
| 129 |
mso.previous = mso.existing |
| 130 |
|
| 131 |
if state == "absent": |
| 132 |
if mso.existing: |
| 133 |
if module.check_mode: |
| 134 |
mso.existing = {} |
| 135 |
else: |
| 136 |
mso.existing = mso.request(path, method="DELETE", data=mso.sent) |
| 137 |
|
| 138 |
elif state == "present": |
| 139 |
tenant_id = mso.lookup_tenant(tenant) |
| 140 |
payload = dict( |
| 141 |
name=dhcp_relay_policy, |
| 142 |
desc=description, |
| 143 |
policyType="dhcp", |
| 144 |
policySubtype="relay", |
| 145 |
tenantId=tenant_id, |
| 146 |
) |
| 147 |
mso.sanitize(payload, collate=True) |
| 148 |
|
| 149 |
if mso.existing: |
| 150 |
if mso.check_changed(): |
| 151 |
if module.check_mode: |
| 152 |
mso.existing = mso.proposed |
| 153 |
else: |
| 154 |
mso.existing = mso.request(path, method="PUT", data=mso.sent) |
| 155 |
else: |
| 156 |
if module.check_mode: |
| 157 |
mso.existing = mso.proposed |
| 158 |
else: |
| 159 |
mso.existing = mso.request(path, method="POST", data=mso.sent) |
| 160 |
|
| 161 |
mso.exit_json() |
| 162 |
|
| 163 |
|
| 164 |
if __name__ == "__main__": |
| 165 |
main() |