Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/mso_dhcp_option_policy.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, Lionel Hercot (@lhercot) <[email protected]>
5 # Copyright: (c) 2020, Jorge Gomez (@jgomezve) <[email protected]> (based on mso_dhcp_relay_policy module)
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: mso_dhcp_option_policy
20 short_description: Manage DHCP Option policies.
21 description:
22 - Manage DHCP Option policies on Cisco Multi-Site Orchestrator.
23 - This module is only supported on NDO version prior to v4.0.
24 author:
25 - Lionel Hercot (@lhercot)
26 options:
27 dhcp_option_policy:
28 description:
29 - Name of the DHCP Option Policy
30 type: str
31 aliases: [ name ]
32 description:
33 description:
34 - Description of the DHCP Option Policy
35 type: str
36 tenant:
37 description:
38 - Tenant where the DHCP Option Policy is located.
39 type: str
40 state:
41 description:
42 - Use C(present) or C(absent) for adding or removing.
43 - Use C(query) for listing an object or multiple objects.
44 type: str
45 choices: [ absent, present, query ]
46 default: present
47 extends_documentation_fragment: cisco.mso.modules
48 """
49
50 EXAMPLES = r"""
51 - name: Add a new DHCP Option Policy
52 cisco.mso.mso_dhcp_option_policy:
53 host: mso_host
54 username: admin
55 password: SomeSecretPassword
56 dhcp_option_policy: my_test_dhcp_policy
57 description: "My Test DHCP Policy"
58 tenant: ansible_test
59 state: present
60
61 - name: Remove DHCP Option Policy
62 cisco.mso.mso_dhcp_option_policy:
63 host: mso_host
64 username: admin
65 password: SomeSecretPassword
66 dhcp_option_policy: my_test_dhcp_policy
67 state: absent
68
69 - name: Query a DHCP Option Policy
70 cisco.mso.mso_dhcp_option_policy:
71 host: mso_host
72 username: admin
73 password: SomeSecretPassword
74 dhcp_option_policy: my_test_dhcp_policy
75 state: query
76 register: query_result
77
78 - name: Query all DHCP Option Policies
79 cisco.mso.mso_dhcp_option_policy:
80 host: mso_host
81 username: admin
82 password: SomeSecretPassword
83 state: query
84 register: query_result
85 """
86
87 RETURN = r"""
88 """
89
90 from ansible.module_utils.basic import AnsibleModule
91 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
92
93
94 def main():
95 argument_spec = mso_argument_spec()
96 argument_spec.update(
97 dhcp_option_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_option_policy"]],
107 ["state", "present", ["dhcp_option_policy", "tenant"]],
108 ],
109 )
110
111 dhcp_option_policy = module.params.get("dhcp_option_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/option"
119
120 # Query for existing object(s)
121 if dhcp_option_policy:
122 mso.existing = mso.get_obj(path, name=dhcp_option_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_option_policy,
143 desc=description,
144 policyType="dhcp",
145 policySubtype="option",
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
165 if __name__ == "__main__":
166 main()