Debian Codegraph @

ansible_collections/netapp/storagegrid/plugins/modules/na_sg_org_info.py

raw
1 #!/usr/bin/python
2
3 # (c) 2020, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 """ NetApp StorageGRID Org Info using REST APIs """
7
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {'metadata_version': '1.1',
14 'status': ['preview'],
15 'supported_by': 'community'}
16
17 DOCUMENTATION = """
18 module: na_sg_org_info
19 author: NetApp Ansible Team (@jasonl4) <[email protected]>
20 extends_documentation_fragment:
21 - netapp.storagegrid.netapp.sg
22 short_description: NetApp StorageGRID Org information gatherer.
23 description:
24 - This module allows you to gather various information about StorageGRID Org configuration.
25 version_added: 20.11.0
26
27 options:
28 gather_subset:
29 type: list
30 elements: str
31 description:
32 - When supplied, this argument will restrict the information collected to a given subset.
33 - Either the info name or the Rest API can be given.
34 - Possible values for this argument include
35 - C(org_compliance_global_info) or C(org/compliance-global)
36 - C(org_config_info) or C(org/config)
37 - C(org_config_product_version_info) or C(org/config/product-version)
38 - C(org_containers_info) or C(org/containers)
39 - C(org_deactivated_features_info) or C(org/deactivated-features)
40 - C(org_endpoints_info) or C(org/endpoints)
41 - C(org_groups_info) or C(org/groups)
42 - C(org_identity_source_info) or C(org/identity-source)
43 - C(org_regions_info) or C(org/regions)
44 - C(org_users_current_user_s3_access_keys_info) or C(org/users/current-user/s3-access-keys)
45 - C(org_usage_info) or C(org/usage)
46 - C(org_users_info) or C(org/users)
47 - C(org_users_root_info) or C(org/users/root)
48 - C(org_users_grid_federation_connections_info) or C(org/grid-federation-connections)
49 - C(org_ilm_info) or C(org/ilm-policy-tags)
50 - C(versions_info) or C(versions)
51 - Can specify a list of values to include a larger subset.
52 default: "all"
53 parameters:
54 description:
55 - Allows for any rest option to be passed in.
56 type: dict
57 """
58
59 EXAMPLES = """
60 - name: Gather StorageGRID Org info
61 netapp.storagegrid.na_sg_org_info:
62 api_url: "https://1.2.3.4/"
63 auth_token: "storagegrid-auth-token"
64 validate_certs: false
65 register: sg_org_info
66
67 - name: Gather StorageGRID Org info for org/containers and org/config subsets
68 netapp.storagegrid.na_sg_org_info:
69 api_url: "https://1.2.3.4/"
70 auth_token: "storagegrid-auth-token"
71 validate_certs: false
72 gather_subset:
73 - org_containers_info
74 - org/config
75 register: sg_org_info
76
77 - name: Gather StorageGRID Org info for all subsets
78 netapp.storagegrid.na_sg_org_info:
79 api_url: "https://1.2.3.4/"
80 auth_token: "storagegrid-auth-token"
81 validate_certs: false
82 gather_subset:
83 - all
84 register: sg_org_info
85
86 - name: Gather StorageGRID Org info for org/containers and org/users subsets, limit to 5 results for each subset
87 netapp.storagegrid.na_sg_org_info:
88 api_url: "https://1.2.3.4/"
89 auth_token: "storagegrid-auth-token"
90 validate_certs: false
91 gather_subset:
92 - org/containers
93 - org/users
94 parameters:
95 limit: 5
96 register: sg_org_info
97 """
98
99 RETURN = """
100 sg_info:
101 description: Returns various information about the StorageGRID Grid configuration.
102 returned: always
103 type: dict
104 sample: {
105 "org/compliance-global": {...},
106 "org/config": {...},
107 "org/config/product-version": {...},
108 "org/containers": {...},
109 "org/deactivated-features": {...},
110 "org/endpoints": {...},
111 "org/groups": {...},
112 "org/identity-source": {...},
113 "org/regions": {...},
114 "org/users/current-user/s3-access-keys": {...},
115 "org/usage": {...},
116 "org/users": {...},
117 "org/users/root": {...},
118 "org/grid-federation-connections": {...},
119 "org/ilm-policy-tags": {...},
120 "org/versions": {...}
121 }
122 """
123
124 from ansible.module_utils.basic import AnsibleModule
125 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
126 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
127 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
128
129
130 class NetAppSgGatherInfo(object):
131 """ Class with gather info methods """
132
133 def __init__(self):
134 """
135 Parse arguments, setup variables, check parameters and ensure
136 request module is installed.
137 """
138 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
139 self.argument_spec.update(dict(
140 gather_subset=dict(default=['all'], type='list', elements='str', required=False),
141 parameters=dict(type='dict', required=False)
142 ))
143
144 self.module = AnsibleModule(
145 argument_spec=self.argument_spec,
146 supports_check_mode=True
147 )
148
149 # set up variables
150 self.na_helper = NetAppModule()
151 self.parameters = self.na_helper.set_parameters(self.module.params)
152 self.rest_api = SGRestAPI(self.module)
153 # Get API version
154 self.rest_api.get_sg_product_version(api_root="org")
155 self.api_version = self.rest_api.get_api_version()
156
157 def get_subset_info(self, gather_subset_info):
158 """
159 Gather StorageGRID information for the given subset using REST APIs
160 Input for REST APIs call : (api, data)
161 return gathered_sg_info
162 """
163
164 api = gather_subset_info['api_call']
165 data = {}
166 # allow for passing in any additional rest api parameters
167 if self.parameters.get('parameters'):
168 for each in self.parameters['parameters']:
169 data[each] = self.parameters['parameters'][each]
170
171 gathered_sg_info, error = self.rest_api.get(api, data)
172
173 if error:
174 self.module.fail_json(msg=error)
175 else:
176 return gathered_sg_info
177
178 return None
179
180 def convert_subsets(self):
181 """ Convert an info to the REST API """
182 info_to_rest_mapping = {
183 'org_compliance_global_info': 'org/compliance-global',
184 'org_config_info': 'org/config',
185 'org_config_product_version_info': 'org/config/product-version',
186 'org_containers_info': 'org/containers',
187 'org_deactivated_features_info': 'org/deactivated-features',
188 'org_endpoints_info': 'org/endpoints',
189 'org_groups_info': 'org/groups',
190 'org_identity_source_info': 'org/identity-source',
191 'org_regions_info': 'org/regions',
192 'org_users_current_user_s3_access_keys_info': 'org/users/current-user/s3-access-keys',
193 'org_usage_info': 'org/usage',
194 'org_users_info': 'org/users',
195 'org_users_root_info': 'org/users/root',
196 'org_grid_federation_connections_info': 'org/grid-federation-connections',
197 'org_ilm_info': 'org/ilm-policy-tags',
198 'versions_info': 'versions'
199 }
200 # Add rest API names as there info version, also make sure we don't add a duplicate
201 subsets = []
202 for subset in self.parameters['gather_subset']:
203 if subset in info_to_rest_mapping:
204 if info_to_rest_mapping[subset] not in subsets:
205 subsets.append(info_to_rest_mapping[subset])
206 else:
207 if subset not in subsets:
208 subsets.append(subset)
209 return subsets
210
211 def apply(self):
212 """ Perform pre-checks, call functions and exit """
213
214 result_message = dict()
215
216 # Defining gather_subset and appropriate api_call
217 get_sg_subset_info = {
218 'org/compliance-global': {
219 'api_call': 'api/%s/org/compliance-global' % self.api_version,
220 },
221 'org/config': {
222 'api_call': 'api/%s/org/config' % self.api_version,
223 },
224 'org/config/product-version': {
225 'api_call': 'api/%s/org/config/product-version' % self.api_version,
226 },
227 'org/containers': {
228 'api_call': 'api/%s/org/containers' % self.api_version,
229 },
230 'org/deactivated-features': {
231 'api_call': 'api/%s/org/deactivated-features' % self.api_version,
232 },
233 'org/endpoints': {
234 'api_call': 'api/%s/org/endpoints' % self.api_version,
235 },
236 'org/groups': {
237 'api_call': 'api/%s/org/groups' % self.api_version,
238 },
239 'org/identity-source': {
240 'api_call': 'api/%s/org/identity-source' % self.api_version,
241 },
242 'org/regions': {
243 'api_call': 'api/%s/org/regions' % self.api_version,
244 },
245 'org/users/current-user/s3-access-keys': {
246 'api_call': 'api/%s/org/users/current-user/s3-access-keys' % self.api_version,
247 },
248 'org/usage': {
249 'api_call': 'api/%s/org/usage' % self.api_version,
250 },
251 'org/users': {
252 'api_call': 'api/%s/org/users' % self.api_version,
253 },
254 'org/users/root': {
255 'api_call': 'api/%s/org/users/root' % self.api_version,
256 },
257 'versions': {
258 'api_call': 'api/%s/versions' % self.api_version,
259 },
260 'org/grid-federation-connections': {
261 'api_call': 'api/%s/org/grid-federation-connections' % self.api_version,
262 },
263 'org/ilm-policy-tags': {
264 'api_call': 'api/%s/org/ilm-policy-tags' % self.api_version,
265 },
266 }
267
268 if 'all' in self.parameters['gather_subset']:
269 # If all in subset list, get the information of all subsets
270 self.parameters['gather_subset'] = sorted(get_sg_subset_info.keys())
271
272 converted_subsets = self.convert_subsets()
273
274 for subset in converted_subsets:
275 try:
276 # Verify whether the supported subset passed
277 specified_subset = get_sg_subset_info[subset]
278 except KeyError:
279 self.module.fail_json(msg="Specified subset %s not found, supported subsets are %s" %
280 (subset, list(get_sg_subset_info.keys())))
281
282 result_message[subset] = self.get_subset_info(specified_subset)
283
284 self.module.exit_json(changed=False, sg_info=result_message)
285
286
287 def main():
288 """ Main function """
289 obj = NetAppSgGatherInfo()
290 obj.apply()
291
292
293 if __name__ == '__main__':
294 main()