Debian Codegraph @

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

raw
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_provider
19 short_description: Manage DHCP providers in a DHCP Relay policy.
20 description:
21 - Manage DHCP providers in a DHCP Relay policy 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 required: true
31 aliases: [ name ]
32 ip:
33 description:
34 - IP address of the DHCP Server
35 type: str
36 tenant:
37 description:
38 - Tenant where the DHCP provider is located.
39 type: str
40 schema:
41 description:
42 - Schema where the DHCP provider is configured
43 type: str
44 template:
45 description:
46 - template where the DHCP provider is configured
47 type: str
48 application_profile:
49 description:
50 - Application Profile where the DHCP provider is configured
51 type: str
52 aliases: [ anp ]
53 endpoint_group:
54 description:
55 - EPG where the DHCP provider is configured
56 type: str
57 aliases: [ epg ]
58 external_endpoint_group:
59 description:
60 - External EPG where the DHCP provider is configured
61 type: str
62 aliases: [ ext_epg, external_epg ]
63 state:
64 description:
65 - Use C(present) or C(absent) for adding or removing.
66 - Use C(query) for listing an object or multiple objects.
67 type: str
68 choices: [ absent, present, query ]
69 default: present
70 extends_documentation_fragment: cisco.mso.modules
71 """
72
73 EXAMPLES = r"""
74 - name: Add a new provider to a DHCP Relay Policy
75 cisco.mso.mso_dhcp_relay_policy_provider:
76 host: mso_host
77 username: admin
78 password: SomeSecretPassword
79 dhcp_relay_policy: my_test_dhcp_policy
80 tenant: ansible_test
81 schema: ansible_test
82 template: Template 1
83 application_profile: ansible_test
84 endpoint_group: ansible_test
85 state: present
86
87 - name: Remove a provider to a DHCP Relay Policy
88 cisco.mso.mso_dhcp_relay_policy_provider:
89 host: mso_host
90 username: admin
91 password: SomeSecretPassword
92 dhcp_relay_policy: my_test_dhcp_policy
93 tenant: ansible_test
94 schema: ansible_test
95 template: Template 1
96 application_profile: ansible_test
97 endpoint_group: ansible_test
98 state: absent
99
100 - name: Query a provider to a DHCP Relay Policy
101 cisco.mso.mso_dhcp_relay_policy_provider:
102 host: mso_host
103 username: admin
104 password: SomeSecretPassword
105 dhcp_relay_policy: my_test_dhcp_policy
106 tenant: ansible_test
107 schema: ansible_test
108 template: Template 1
109 application_profile: ansible_test
110 endpoint_group: ansible_test
111 state: query
112 register: query_result
113
114 - name: Query all provider of a DHCP Relay Policy
115 cisco.mso.mso_dhcp_relay_policy_provider:
116 host: mso_host
117 username: admin
118 password: SomeSecretPassword
119 dhcp_relay_policy: my_test_dhcp_policy
120 state: query
121 register: query_result
122 """
123
124 RETURN = r"""
125 """
126 from ansible.module_utils.basic import AnsibleModule
127 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
128 MSOModule,
129 mso_argument_spec,
130 )
131
132
133 def main():
134 argument_spec = mso_argument_spec()
135 argument_spec.update(
136 dhcp_relay_policy=dict(type="str", required=True, aliases=["name"]),
137 ip=dict(type="str"),
138 tenant=dict(type="str"),
139 schema=dict(type="str"),
140 template=dict(type="str"),
141 application_profile=dict(type="str", aliases=["anp"]),
142 endpoint_group=dict(type="str", aliases=["epg"]),
143 external_endpoint_group=dict(type="str", aliases=["ext_epg", "external_epg"]),
144 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
145 )
146 module = AnsibleModule(
147 argument_spec=argument_spec,
148 supports_check_mode=True,
149 required_if=[
150 ["state", "present", ["ip", "tenant", "schema", "template"]],
151 ["state", "absent", ["tenant", "schema", "template"]],
152 ],
153 )
154
155 dhcp_relay_policy = module.params.get("dhcp_relay_policy")
156 ip = module.params.get("ip")
157 tenant = module.params.get("tenant")
158 schema = module.params.get("schema")
159 template = module.params.get("template")
160 if template is not None:
161 template = template.replace(" ", "")
162 application_profile = module.params.get("application_profile")
163 endpoint_group = module.params.get("endpoint_group")
164 external_endpoint_group = module.params.get("external_endpoint_group")
165 state = module.params.get("state")
166
167 mso = MSOModule(module)
168
169 path = "policies/dhcp/relay"
170
171 tenant_id = mso.lookup_tenant(tenant)
172 # Get schema_id
173 schema_id = mso.lookup_schema(schema)
174
175 provider = dict(
176 addr=ip,
177 externalEpgRef="",
178 epgRef="",
179 l3Ref="",
180 tenantId=tenant_id,
181 )
182 provider_index = None
183 previous_provider = {}
184
185 if application_profile is not None and endpoint_group is not None:
186 provider["epgRef"] = "/schemas/{schemaId}/templates/{templateName}/anps/{app}/epgs/{epg}".format(
187 schemaId=schema_id,
188 templateName=template,
189 app=application_profile,
190 epg=endpoint_group,
191 )
192 elif external_endpoint_group is not None:
193 provider["externalEpgRef"] = "/schemas/{schemaId}/templates/{templateName}/externalEpgs/{ext_epg}".format(
194 schemaId=schema_id, templateName=template, ext_epg=external_endpoint_group
195 )
196
197 # Query for existing object(s)
198 dhcp_relay_obj = mso.get_obj(path, name=dhcp_relay_policy, key="DhcpRelayPolicies")
199 if "id" not in dhcp_relay_obj:
200 mso.fail_json(msg="DHCP Relay Policy '{0}' is not a valid DHCP Relay Policy name.".format(dhcp_relay_policy))
201 policy_id = dhcp_relay_obj.get("id")
202 providers = []
203 if "provider" in dhcp_relay_obj:
204 providers = dhcp_relay_obj.get("provider")
205 for index, prov in enumerate(providers):
206 if (provider.get("epgRef") != "" and prov.get("epgRef") == provider.get("epgRef")) or (
207 provider.get("externalEpgRef") != "" and prov.get("externalEpgRef") == provider.get("externalEpgRef")
208 ):
209 previous_provider = prov
210 provider_index = index
211
212 # If we found an existing object, continue with it
213 path = "{0}/{1}".format(path, policy_id)
214
215 if state == "query":
216 mso.existing = providers
217 if endpoint_group is not None or external_endpoint_group is not None:
218 mso.existing = previous_provider
219 mso.exit_json()
220
221 if endpoint_group is None and external_endpoint_group is None:
222 mso.fail_json(msg="Missing either endpoint_group or external_endpoint_group required attribute.")
223
224 mso.previous = previous_provider
225 if state == "absent":
226 provider = {}
227 if previous_provider:
228 if provider_index is not None:
229 providers.pop(provider_index)
230
231 elif state == "present":
232 if provider_index is not None:
233 providers[provider_index] = provider
234 else:
235 providers.append(provider)
236
237 if module.check_mode:
238 mso.existing = provider
239 else:
240 mso.existing = dhcp_relay_obj
241 dhcp_relay_obj["provider"] = providers
242 mso.sanitize(dhcp_relay_obj, collate=True)
243 new_dhcp_relay_obj = mso.request(path, method="PUT", data=mso.sent)
244 mso.existing = {}
245 for index, prov in enumerate(new_dhcp_relay_obj.get("provider")):
246 if (provider.get("epgRef") != "" and prov.get("epgRef") == provider.get("epgRef")) or (
247 provider.get("externalEpgRef") != "" and prov.get("externalEpgRef") == provider.get("externalEpgRef")
248 ):
249 mso.existing = prov
250
251 mso.exit_json()
252
253
254 if __name__ == "__main__":
255 main()