Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Dag Wieers (@dagwieers) <[email protected]>
5 # Copyright: (c) 2020, Shreyas Srish (@shrsr) <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: mso_tenant_site
17 short_description: Manage tenants with cloud sites.
18 description:
19 - Manage tenants with cloud sites on Cisco ACI Multi-Site.
20 author:
21 - Shreyas Srish (@shrsr)
22 options:
23 tenant:
24 description:
25 - The name of the tenant.
26 type: str
27 required: true
28 site:
29 description:
30 - The name of the site.
31 - This can either be cloud site or non-cloud site.
32 type: str
33 aliases: [ name ]
34 cloud_account:
35 description:
36 - Required for cloud site.
37 - Account id of AWS in the form '000000000000'.
38 - Account id of Azure in the form 'uni/tn-(tenant_name)/act-[(subscription_id)]-azure_vendor-azure'.
39 - Example values inside account id of Azure '(tenant_name)=tenant_test and (subscription_id)=10'.
40 type: str
41 security_domains:
42 description:
43 - List of security domains for sites.
44 type: list
45 elements: str
46 default: []
47 aws_account_org:
48 description:
49 - AWS account for organization.
50 default: false
51 type: bool
52 aws_trusted:
53 description:
54 - AWS account's access in trusted mode. Credentials are required, when set to false.
55 type: bool
56 aws_access_key:
57 description:
58 - AWS account's access key id. This is required when aws_trusted is set to false.
59 type: str
60 azure_access_type:
61 description:
62 - Managed mode for Azure.
63 - Unmanaged mode for Azure.
64 - Shared mode if the attribute is not specified.
65 choices: [ managed, unmanaged, shared ]
66 default: shared
67 type: str
68 azure_active_directory_id:
69 description:
70 - Azure account's active directory id.
71 - This attribute is required when azure_access_type is in unmanaged mode.
72 type: str
73 azure_active_directory_name:
74 description:
75 - Azure account's active directory name. Example being 'CiscoINSBUAd' as active directory name.
76 - This attribute is required when azure_access_type is in unmanaged mode.
77 type: str
78 azure_subscription_id:
79 description:
80 - Azure account's subscription id.
81 - This attribute is required when azure_access_type is either in managed mode or unmanaged mode.
82 type: str
83 azure_application_id:
84 description:
85 - Azure account's application id.
86 - This attribute is required when azure_access_type is either in managed mode or unmanaged mode.
87 type: str
88 azure_credential_name:
89 description:
90 - Azure account's credential name.
91 - This attribute is required when azure_access_type is in unmanaged mode.
92 type: str
93 secret_key:
94 description:
95 - secret key of AWS for untrusted account. Required when aws_trusted is set to false.
96 - secret key of Azure account for unmanaged identity. Required in unmanaged mode of Azure account.
97 type: str
98 state:
99 description:
100 - Use C(present) or C(absent) for adding or removing.
101 - Use C(query) for listing an object or multiple objects.
102 type: str
103 choices: [ absent, present, query ]
104 default: present
105 extends_documentation_fragment: cisco.mso.modules
106 """
107
108 EXAMPLES = r"""
109 - name: Associate a non-cloud site with a tenant
110 cisco.mso.mso_tenant_site:
111 host: mso_host
112 username: admin
113 password: SomeSecretPassword
114 tenant: tenant_name
115 site: site_name
116 state: present
117
118 - name: Associate AWS site with a tenant, with aws_trusted set to true
119 cisco.mso.mso_tenant_site:
120 host: mso_host
121 username: admin
122 password: SomeSecretPassword
123 tenant: tenant_name
124 site: site_name
125 cloud_account: '000000000000'
126 aws_trusted: true
127 state: present
128
129 - name: Associate AWS site with a tenant, with aws_trusted set to false
130 cisco.mso.mso_tenant_site:
131 host: mso_host
132 username: admin
133 password: SomeSecretPassword
134 tenant: tenant_name
135 site: AWS
136 cloud_account: '000000000000'
137 aws_trusted: false
138 aws_access_key: '1'
139 secret_key: '0'
140 aws_account_org: false
141 state: present
142
143 - name: Associate Azure site in managed mode
144 mso.cisco.mso_tenant_site:
145 host: mso_host
146 username: admin
147 password: SomeSecretPassword
148 tenant: tenant_name
149 site: site_name
150 cloud_account: uni/tn-ansible_test/act-[9]-azure_vendor-azure
151 azure_access_type: managed
152 azure_subscription_id: '9'
153 azure_application_id: '100'
154 state: present
155
156 - name: Associate Azure site in unmanaged mode
157 mso.cisco.mso_tenant_site:
158 host: mso_host
159 username: admin
160 password: SomeSecretPassword
161 tenant: tenant_name
162 site: site_name
163 cloud_account: uni/tn-ansible_test/act-[9]-azure_vendor-azure
164 azure_access_type: unmanaged
165 azure_subscription_id: '9'
166 azure_application_id: '100'
167 azure_credential_name: cApicApp
168 secret_key: iins
169 azure_active_directory_id: '32'
170 azure_active_directory_name: CiscoINSBUAd
171 state: present
172
173 - name: Dissociate a site
174 cisco.mso.mso_tenant_site:
175 host: mso_host
176 username: admin
177 password: SomeSecretPassword
178 tenant: tenant_name
179 site: site_name
180 state: absent
181
182 - name: Query a site
183 cisco.mso.mso_tenant_site:
184 host: mso_host
185 username: admin
186 password: SomeSecretPassword
187 tenant: tenant_name
188 site: site_name
189 state: query
190 register: query_result
191
192 - name: Query all sites of a tenant
193 cisco.mso.mso_tenant_site:
194 host: mso_host
195 username: admin
196 password: SomeSecretPassword
197 tenant: tenant_name
198 state: query
199 register: query_result
200 """
201
202 RETURN = r"""
203 """
204
205 from ansible.module_utils.basic import AnsibleModule
206 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
207
208
209 def main():
210 argument_spec = mso_argument_spec()
211 argument_spec.update(
212 tenant=dict(type="str", required=True),
213 site=dict(type="str", aliases=["name"]),
214 cloud_account=dict(type="str"),
215 security_domains=dict(type="list", elements="str", default=[]),
216 aws_trusted=dict(type="bool"),
217 azure_access_type=dict(type="str", default="shared", choices=["managed", "unmanaged", "shared"]),
218 azure_active_directory_id=dict(type="str"),
219 aws_access_key=dict(type="str", no_log=True),
220 aws_account_org=dict(type="bool", default="false"),
221 azure_active_directory_name=dict(type="str"),
222 azure_subscription_id=dict(type="str"),
223 azure_application_id=dict(type="str"),
224 azure_credential_name=dict(type="str"),
225 secret_key=dict(type="str", no_log=True),
226 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
227 )
228
229 module = AnsibleModule(
230 argument_spec=argument_spec,
231 supports_check_mode=True,
232 required_if=[
233 ["state", "absent", ["tenant", "site"]],
234 ["state", "present", ["tenant", "site"]],
235 ],
236 )
237
238 state = module.params.get("state")
239 security_domains = module.params.get("security_domains")
240 cloud_account = module.params.get("cloud_account")
241 azure_access_type = module.params.get("azure_access_type")
242 azure_credential_name = module.params.get("azure_credential_name")
243 azure_application_id = module.params.get("azure_application_id")
244 azure_active_directory_id = module.params.get("azure_active_directory_id")
245 azure_active_directory_name = module.params.get("azure_active_directory_name")
246 azure_subscription_id = module.params.get("azure_subscription_id")
247 secret_key = module.params.get("secret_key")
248 aws_account_org = module.params.get("aws_account_org")
249 aws_access_key = module.params.get("aws_access_key")
250 aws_trusted = module.params.get("aws_trusted")
251
252 mso = MSOModule(module)
253
254 # Get tenant_id and site_id
255 tenant_id = mso.lookup_tenant(module.params.get("tenant"))
256
257 # To ignore the object not found issue for the lookup methods
258 site_id = mso.lookup_site(module.params.get("site"), True)
259
260 if state == "absent" and not site_id:
261 mso.exit_json()
262
263 tenants = [(t.get("id")) for t in mso.query_objs("tenants")]
264 tenant_idx = tenants.index((tenant_id))
265
266 # set tenent and port paths
267 tenant_path = "tenants/{0}".format(tenant_id)
268 ops = []
269 ports_path = "/siteAssociations/-"
270 port_path = "/siteAssociations/{0}".format(site_id)
271
272 payload = dict(
273 siteId=site_id,
274 securityDomains=security_domains,
275 cloudAccount=cloud_account,
276 )
277
278 if cloud_account:
279 if "azure" in cloud_account:
280 azure_account = dict(
281 accessType=azure_access_type,
282 securityDomains=security_domains,
283 vendor="azure",
284 )
285
286 payload["azureAccount"] = [azure_account]
287
288 cloudSubscription = dict(
289 cloudSubscriptionId=azure_subscription_id,
290 cloudApplicationId=azure_application_id,
291 )
292
293 payload["azureAccount"][0]["cloudSubscription"] = cloudSubscription
294
295 if azure_access_type == "shared":
296 payload["azureAccount"] = []
297
298 if azure_access_type == "managed":
299 if not azure_subscription_id:
300 mso.fail_json(msg="azure_susbscription_id is required when in managed mode.")
301 if not azure_application_id:
302 mso.fail_json(msg="azure_application_id is required when in managed mode.")
303 payload["azureAccount"][0]["cloudApplication"] = []
304 payload["azureAccount"][0]["cloudActiveDirectory"] = []
305
306 if azure_access_type == "unmanaged":
307 if not azure_subscription_id:
308 mso.fail_json(msg="azure_subscription_id is required when in unmanaged mode.")
309 if not azure_application_id:
310 mso.fail_json(msg="azure_application_id is required when in unmanaged mode.")
311 if not secret_key:
312 mso.fail_json(msg="secret_key is required when in unmanaged mode.")
313 if not azure_active_directory_id:
314 mso.fail_json(msg="azure_active_directory_id is required when in unmanaged mode.")
315 if not azure_active_directory_name:
316 mso.fail_json(msg="azure_active_directory_name is required when in unmanaged mode.")
317 if not azure_credential_name:
318 mso.fail_json(msg="azure_credential_name is required when in unmanaged mode.")
319 azure_account.update(
320 accessType="credentials",
321 )
322 cloudApplication = dict(
323 cloudApplicationId=azure_application_id,
324 cloudCredentialName=azure_credential_name,
325 secretKey=secret_key,
326 cloudActiveDirectoryId=azure_active_directory_id,
327 )
328 cloudActiveDirectory = dict(cloudActiveDirectoryId=azure_active_directory_id, cloudActiveDirectoryName=azure_active_directory_name)
329 payload["azureAccount"][0]["cloudApplication"] = [cloudApplication]
330 payload["azureAccount"][0]["cloudActiveDirectory"] = [cloudActiveDirectory]
331
332 else:
333 aws_account = dict(
334 accountId=cloud_account,
335 isTrusted=aws_trusted,
336 accessKeyId=aws_access_key,
337 secretKey=secret_key,
338 isAccountInOrg=aws_account_org,
339 )
340
341 if not aws_trusted:
342 if not aws_access_key:
343 mso.fail_json(msg="aws_access_key is a required field in untrusted mode.")
344 if not secret_key:
345 mso.fail_json(msg="secret_key is a required field in untrusted mode.")
346 payload["awsAccount"] = [aws_account]
347
348 sites = [(s.get("siteId")) for s in mso.query_objs("tenants")[tenant_idx]["siteAssociations"]]
349
350 if site_id in sites:
351 site_idx = sites.index((site_id))
352 mso.existing = mso.query_objs("tenants")[tenant_idx]["siteAssociations"][site_idx]
353
354 if state == "query":
355 if len(sites) == 0:
356 mso.fail_json(msg="No site associated with tenant Id {0}".format(tenant_id))
357 elif site_id not in sites and site_id is not None:
358 mso.fail_json(msg="Site Id {0} not associated with tenant Id {1}".format(site_id, tenant_id))
359 elif site_id is None:
360 mso.existing = mso.query_objs("tenants")[tenant_idx]["siteAssociations"]
361 mso.exit_json()
362
363 mso.previous = mso.existing
364
365 if state == "absent":
366 if mso.existing:
367 mso.sent = mso.existing = {}
368 ops.append(dict(op="remove", path=port_path))
369 if state == "present":
370 mso.sanitize(payload, collate=True)
371
372 if mso.existing:
373 ops.append(dict(op="replace", path=port_path, value=mso.sent))
374 else:
375 ops.append(dict(op="add", path=ports_path, value=mso.sent))
376
377 mso.existing = mso.proposed
378
379 if not module.check_mode and mso.proposed != mso.previous:
380 mso.request(tenant_path, method="PATCH", data=ops)
381
382 mso.exit_json()
383
384
385 if __name__ == "__main__":
386 main()