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