ansible  2.9.27
About: Ansible is an IT Configuration Management, Deployment \
About: Ansible (2.x) is an IT Configuration Management, Deployment & Orchestration tool.
ansible download page.
  Fossies Dox: ansible-2.9.27.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

mso_tenant.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# Copyright: (c) 2018, Dag Wieers (@dagwieers) <dag@wieers.com>
5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7from __future__ import absolute_import, division, print_function
8__metaclass__ = type
9
10ANSIBLE_METADATA = {'metadata_version': '1.1',
11 'status': ['preview'],
12 'supported_by': 'community'}
13
14DOCUMENTATION = r'''
15---
16module: mso_tenant
17short_description: Manage tenants
18description:
19- Manage tenants on Cisco ACI Multi-Site.
20author:
21- Dag Wieers (@dagwieers)
22version_added: '2.8'
23options:
24 tenant:
25 description:
26 - The name of the tenant.
27 type: str
28 required: yes
29 aliases: [ name ]
30 display_name:
31 description:
32 - The name of the tenant to be displayed in the web UI.
33 type: str
34 required: yes
35 description:
36 description:
37 - The description for this tenant.
38 type: str
39 users:
40 description:
41 - A list of associated users for this tenant.
42 - Using this property will replace any existing associated users.
43 type: list
44 sites:
45 description:
46 - A list of associated sites for this tenant.
47 - Using this property will replace any existing associated sites.
48 type: list
49 state:
50 description:
51 - Use C(present) or C(absent) for adding or removing.
52 - Use C(query) for listing an object or multiple objects.
53 type: str
54 choices: [ absent, present, query ]
55 default: present
56extends_documentation_fragment: mso
57'''
58
59EXAMPLES = r'''
60- name: Add a new tenant
61 mso_tenant:
62 host: mso_host
63 username: admin
64 password: SomeSecretPassword
65 tenant: north_europe
66 display_name: North European Datacenter
67 description: This tenant manages the NEDC environment.
68 state: present
69 delegate_to: localhost
70
71- name: Remove a tenant
72 mso_tenant:
73 host: mso_host
74 username: admin
75 password: SomeSecretPassword
76 tenant: north_europe
77 state: absent
78 delegate_to: localhost
79
80- name: Query a tenant
81 mso_tenant:
82 host: mso_host
83 username: admin
84 password: SomeSecretPassword
85 tenant: north_europe
86 state: query
87 delegate_to: localhost
88 register: query_result
89
90- name: Query all tenants
91 mso_tenant:
92 host: mso_host
93 username: admin
94 password: SomeSecretPassword
95 state: query
96 delegate_to: localhost
97 register: query_result
98'''
99
100RETURN = r'''
101'''
102
103from ansible.module_utils.basic import AnsibleModule
104from ansible.module_utils.network.aci.mso import MSOModule, mso_argument_spec, issubset
105
106
107def main():
108 argument_spec = mso_argument_spec()
109 argument_spec.update(
110 description=dict(type='str'),
111 display_name=dict(type='str'),
112 tenant=dict(type='str', aliases=['name']),
113 users=dict(type='list'),
114 sites=dict(type='list'),
115 state=dict(type='str', default='present', choices=['absent', 'present', 'query']),
116 )
117
118 module = AnsibleModule(
119 argument_spec=argument_spec,
120 supports_check_mode=True,
121 required_if=[
122 ['state', 'absent', ['tenant']],
123 ['state', 'present', ['tenant']],
124 ],
125 )
126
127 description = module.params['description']
128 display_name = module.params['display_name']
129 tenant = module.params['tenant']
130 state = module.params['state']
131
132 mso = MSOModule(module)
133
134 # Convert sites and users
135 sites = mso.lookup_sites(module.params['sites'])
136 users = mso.lookup_users(module.params['users'])
137
138 tenant_id = None
139 path = 'tenants'
140
141 # Query for existing object(s)
142 if tenant:
143 mso.existing = mso.get_obj(path, name=tenant)
144 if mso.existing:
145 tenant_id = mso.existing['id']
146 # If we found an existing object, continue with it
147 path = 'tenants/{id}'.format(id=tenant_id)
148 else:
149 mso.existing = mso.query_objs(path)
150
151 if state == 'query':
152 pass
153
154 elif state == 'absent':
155 mso.previous = mso.existing
156 if mso.existing:
157 if module.check_mode:
158 mso.existing = {}
159 else:
160 mso.existing = mso.request(path, method='DELETE')
161
162 elif state == 'present':
163 mso.previous = mso.existing
164
165 payload = dict(
166 description=description,
167 id=tenant_id,
168 name=tenant,
169 displayName=display_name,
170 siteAssociations=sites,
171 userAssociations=users,
172 )
173
174 mso.sanitize(payload, collate=True)
175
176 # Ensure displayName is not undefined
177 if mso.sent.get('displayName') is None:
178 mso.sent['displayName'] = tenant
179
180 # Ensure tenant has at least admin user
181 if mso.sent.get('userAssociations') is None:
182 mso.sent['userAssociations'] = [dict(userId="0000ffff0000000000000020")]
183
184 if mso.existing:
185 if not issubset(mso.sent, mso.existing):
186 if module.check_mode:
187 mso.existing = mso.proposed
188 else:
189 mso.existing = mso.request(path, method='PUT', data=mso.sent)
190 else:
191 if module.check_mode:
192 mso.existing = mso.proposed
193 else:
194 mso.existing = mso.request(path, method='POST', data=mso.sent)
195
196 mso.exit_json()
197
198
199if __name__ == "__main__":
200 main()
def issubset(subset, superset)
Definition: mso.py:19