Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vcenter_domain_user_group_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2020, sky-joker
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11 DOCUMENTATION = r'''
12 module: vcenter_domain_user_group_info
13 short_description: Gather user or group information of a domain
14 author:
15 - sky-joker (@sky-joker)
16 description:
17 - This module can be used to gather information about user or group of a domain.
18 options:
19 domain:
20 description:
21 - The I(domain) to be specified searching.
22 type: str
23 default: vsphere.local
24 search_string:
25 description:
26 - The I(search_string) is a string to be specified searching.
27 - Specify the domain user or group name to be searched.
28 type: str
29 required: true
30 belongs_to_group:
31 description:
32 - If a group existing, returned contains only users or groups that directly belong to the specified group.
33 type: str
34 belongs_to_user:
35 description:
36 - If a user existing, returned contains only groups that directly contain the specified user.
37 type: str
38 exact_match:
39 description:
40 - If V(true), it indicates the O(search_string) passed should match a user or group name exactly.
41 type: bool
42 default: false
43 find_users:
44 description:
45 - If V(true), domain users will be included in the result.
46 type: bool
47 default: true
48 find_groups:
49 description:
50 - If V(true), domain groups will be included in the result.
51 type: bool
52 default: true
53 extends_documentation_fragment:
54 - vmware.vmware.base_options
55 '''
56
57 EXAMPLES = r'''
58 - name: Gather all domain user and group of vsphere.local
59 community.vmware.vcenter_domain_user_group_info:
60 hostname: "{{ vcenter_hostname }}"
61 username: "{{ vcenter_username }}"
62 password: "{{ vcenter_password }}"
63 validate_certs: false
64 domain: vsphere.local
65 search_string: ''
66 register: gather_all_domain_user_group_result
67
68 - name: Gather all domain user and group included the administrator string
69 community.vmware.vcenter_domain_user_group_info:
70 hostname: "{{ vcenter_hostname }}"
71 username: "{{ vcenter_username }}"
72 password: "{{ vcenter_password }}"
73 validate_certs: false
74 domain: vsphere.local
75 search_string: administrator
76 register: gather_domain_user_group_result
77
78 - name: Gather all domain user of vsphere.local
79 community.vmware.vcenter_domain_user_group_info:
80 hostname: "{{ vcenter_hostname }}"
81 username: "{{ vcenter_username }}"
82 password: "{{ vcenter_password }}"
83 validate_certs: false
84 domain: vsphere.local
85 search_string: ''
86 find_users: true
87 find_groups: false
88 register: gather_all_domain_user_result
89
90 - name: Gather administrator user by exact match condition
91 community.vmware.vcenter_domain_user_group_info:
92 hostname: "{{ vcenter_hostname }}"
93 username: "{{ vcenter_username }}"
94 password: "{{ vcenter_password }}"
95 validate_certs: false
96 domain: vsphere.local
97 search_string: "vsphere.local\\administrator"
98 exact_match: true
99 register: gather_administrator_user_exact_match_result
100 '''
101
102 RETURN = r'''
103 domain_user_groups:
104 description: list of domain user and group information
105 returned: success
106 type: list
107 sample: >-
108 [
109 {
110 "fullName": "Administrator vsphere.local",
111 "group": false,
112 "principal": "Administrator"
113 }
114 ]
115 '''
116
117 try:
118 from pyVmomi import vim
119 except ImportError:
120 pass
121
122 from ansible.module_utils.basic import AnsibleModule
123 from ansible.module_utils.common.text.converters import to_native
124 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
125 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
126
127
128 class VcenterDomainUserGroupInfo(PyVmomi):
129 def __init__(self, module):
130 super(VcenterDomainUserGroupInfo, self).__init__(module)
131 self.domain = self.params['domain']
132 self.search_string = self.params['search_string']
133 self.belongs_to_group = self.params['belongs_to_group']
134 self.belongs_to_user = self.params['belongs_to_user']
135 self.exact_match = self.params['exact_match']
136 self.find_users = self.params['find_users']
137 self.find_groups = self.params['find_groups']
138
139 def execute(self):
140 user_directory_manager = self.content.userDirectory
141
142 if not self.domain.upper() in user_directory_manager.domainList:
143 self.module.fail_json(msg="domain not found: %s" % self.domain)
144
145 try:
146 user_search_result = user_directory_manager.RetrieveUserGroups(
147 domain=self.domain,
148 searchStr=self.search_string,
149 belongsToGroup=self.belongs_to_group,
150 belongsToUser=self.belongs_to_user,
151 exactMatch=self.exact_match,
152 findUsers=self.find_users,
153 findGroups=self.find_groups
154 )
155 except vim.fault.NotFound as e:
156 self.module.fail_json(msg="%s" % to_native(e.msg))
157 except Exception as e:
158 self.module.fail_json(msg="Couldn't gather domain user or group information: %s" % to_native(e))
159
160 user_search_result_normalization = []
161 if user_search_result:
162 for object in user_search_result:
163 user_search_result_normalization.append({
164 'fullName': object.fullName,
165 'principal': object.principal,
166 'group': object.group
167 })
168
169 self.module.exit_json(changed=False, domain_user_groups=user_search_result_normalization)
170
171
172 def main():
173 argument_spec = base_argument_spec()
174 argument_spec.update(
175 domain=dict(type='str', default='vsphere.local'),
176 search_string=dict(type='str', required=True),
177 belongs_to_group=dict(type='str', default=None),
178 belongs_to_user=dict(type='str', default=None),
179 exact_match=dict(type='bool', default=False),
180 find_users=dict(type='bool', default=True),
181 find_groups=dict(type='bool', default=True)
182 )
183
184 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
185
186 vcenter_domain_user_info = VcenterDomainUserGroupInfo(module)
187 vcenter_domain_user_info.execute()
188
189
190 if __name__ == "__main__":
191 main()