Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_s3_groups.py

raw
1 #!/usr/bin/python
2
3 # (c) 2022-2026, NetApp, Inc
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 DOCUMENTATION = '''
12 module: na_ontap_s3_groups
13 short_description: NetApp ONTAP S3 groups
14 extends_documentation_fragment:
15 - netapp.ontap.netapp.na_ontap_rest
16 version_added: 21.21.0
17 author: NetApp Ansible Team (@carchi8py) <[email protected]>
18 description:
19 - Create, delete, or modify S3 groups on NetApp ONTAP.
20
21 options:
22 state:
23 description:
24 - Whether the specified S3 group should exist or not.
25 choices: ['present', 'absent']
26 type: str
27 default: 'present'
28
29 name:
30 description:
31 - The name of the S3 group.
32 type: str
33 required: true
34
35 vserver:
36 description:
37 - Name of the vserver to use.
38 type: str
39 required: true
40
41 comment:
42 description:
43 - comment about the group
44 type: str
45
46 users:
47 description: List of users to to add the the group
48 type: list
49 elements: dict
50 suboptions:
51 name:
52 description: username
53 type: str
54
55 policies:
56 description: Policies to add the the group
57 type: list
58 elements: dict
59 suboptions:
60 name:
61 description: policy name
62 type: str
63
64 lambda_config:
65 description:
66 - Configuration parameters for AWS Lambda proxy functionality.
67 - These option and suboptions are only supported with REST.
68 type: dict
69 version_added: 23.4.0
70 suboptions:
71 function_name:
72 description:
73 - The name of the AWS Lambda function to invoke.
74 type: str
75 required: true
76 aws_region:
77 description:
78 - The name of the AWS region.
79 type: str
80 required: true
81 aws_profile:
82 description:
83 - The name of the AWS profile to use for authentication.
84 type: str
85
86 notes:
87 - Supports AWS Lambda proxy functionality. See README for example usage.
88 '''
89
90 EXAMPLES = """
91 - name: Create and modify a S3 Group
92 netapp.ontap.na_ontap_s3_groups:
93 state: present
94 name: dev-group
95 comment: group for devs
96 vserver: ansibleSVM
97 users:
98 - name: carchi8py
99 - name: carchi8py2
100 policies:
101 - name: allow_policy
102 - name: deny_policy
103 hostname: "{{ netapp_hostname }}"
104 username: "{{ netapp_username }}"
105 password: "{{ netapp_password }}"
106 https: true
107 validate_certs: false
108 use_rest: always
109
110 - name: Delete a S3 Group
111 netapp.ontap.na_ontap_s3_groups:
112 state: absent
113 name: dev-group
114 vserver: ansibleSVM
115 hostname: "{{ netapp_hostname }}"
116 username: "{{ netapp_username }}"
117 password: "{{ netapp_password }}"
118 https: true
119 validate_certs: false
120 use_rest: always
121 """
122
123 RETURN = """
124 """
125
126 import traceback
127 from ansible.module_utils.basic import AnsibleModule
128 from ansible.module_utils._text import to_native
129 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
130 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
131 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
132 from ansible_collections.netapp.ontap.plugins.module_utils import rest_vserver
133
134
135 class NetAppOntapS3Groups:
136 def __init__(self):
137 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
138 self.argument_spec.update(dict(
139 state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
140 vserver=dict(required=True, type='str'),
141 name=dict(required=True, type='str'),
142 comment=dict(required=False, type='str'),
143 users=dict(required=False, type='list', elements='dict', options=dict(
144 name=dict(required=False, type='str'))),
145 policies=dict(required=False, type='list', elements='dict', options=dict(
146 name=dict(required=False, type='str')))))
147 self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec())
148 self.module = AnsibleModule(
149 argument_spec=self.argument_spec,
150 required_if=[
151 ['use_lambda', True, ('lambda_config',)]
152 ],
153 supports_check_mode=True,
154 )
155 self.svm_uuid = None
156 self.group_id = None
157 self.na_helper = NetAppModule(self.module)
158 self.parameters = self.na_helper.check_and_set_parameters(self.module)
159 self.rest_api = netapp_utils.OntapRestAPI(self.module)
160 self.use_rest = self.rest_api.is_rest()
161 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_s3_groups', 9, 8)
162
163 def get_s3_groups(self):
164 self.get_svm_uuid()
165 api = 'protocols/s3/services/%s/groups' % self.svm_uuid
166 fields = ','.join(('name',
167 'comment',
168 'users.name',
169 'policies.name'))
170 params = {'name': self.parameters['name'],
171 'fields': fields}
172 record, error = rest_generic.get_one_record(self.rest_api, api, params)
173 if error:
174 self.module.fail_json(msg='Error fetching S3 groups %s: %s' % (self.parameters['name'], to_native(error)),
175 exception=traceback.format_exc())
176 if record:
177 self.group_id = record.get('id')
178 return self.form_current(record)
179 return record
180
181 @staticmethod
182 def form_current(record):
183 current = {
184 'comment': record.get('comment'),
185 'users': [],
186 'policies': [],
187 }
188 # the APi Returning _link in each user and policy record which is causing modify to get called
189 if record.get('users'):
190 for user in record['users']:
191 current['users'].append({'name': user['name']})
192 if record.get('policies'):
193 for policy in record['policies']:
194 current['policies'].append({'name': policy['name']})
195 return current
196
197 def create_s3_groups(self):
198 api = 'protocols/s3/services/%s/groups' % self.svm_uuid
199 body = {'name': self.parameters['name'],
200 'users': self.parameters['users'],
201 'policies': self.parameters['policies']}
202 if self.parameters.get('comment'):
203 body['comment'] = self.parameters['comment']
204 dummy, error = rest_generic.post_async(self.rest_api, api, body)
205 if error:
206 self.module.fail_json(msg='Error creating S3 groups %s: %s' % (self.parameters['name'], to_native(error)),
207 exception=traceback.format_exc())
208
209 def delete_s3_groups(self):
210 api = 'protocols/s3/services/%s/groups' % self.svm_uuid
211 dummy, error = rest_generic.delete_async(self.rest_api, api, self.group_id)
212 if error:
213 self.module.fail_json(msg='Error deleting S3 group %s: %s' % (self.parameters['name'], to_native(error)),
214 exception=traceback.format_exc())
215
216 def modify_s3_groups(self, modify):
217 api = 'protocols/s3/services/%s/groups' % self.svm_uuid
218 body = {}
219 if modify.get('comment') is not None:
220 body['comment'] = self.parameters['comment']
221 if modify.get('users') is not None:
222 body['users'] = self.parameters['users']
223 if modify.get('policies') is not None:
224 body['policies'] = self.parameters['policies']
225 dummy, error = rest_generic.patch_async(self.rest_api, api, self.group_id, body)
226 if error:
227 self.module.fail_json(msg='Error modifying S3 group %s: %s' % (self.parameters['name'], to_native(error)),
228 exception=traceback.format_exc())
229
230 def get_svm_uuid(self):
231 record, error = rest_vserver.get_vserver_uuid(self.rest_api, self.parameters['vserver'], self.module, True)
232 self.svm_uuid = record
233
234 def apply(self):
235 current = self.get_s3_groups()
236 cd_action, modify = None, None
237 cd_action = self.na_helper.get_cd_action(current, self.parameters)
238 if cd_action is None:
239 modify = self.na_helper.get_modified_attributes(current, self.parameters)
240 if cd_action == 'create' and (self.na_helper.safe_get(self.parameters, ['users']) is None
241 or self.na_helper.safe_get(self.parameters, ['policies']) is None):
242 self.module.fail_json(msg='policies and users are required for a creating a group.')
243 if modify and (self.na_helper.safe_get(self.parameters, ['users']) is None
244 or self.na_helper.safe_get(self.parameters, ['policies']) is None):
245 self.module.fail_json(msg='policies and users can not be empty when modifying a group.')
246 if self.na_helper.changed and not self.module.check_mode:
247 if cd_action == 'create':
248 self.create_s3_groups()
249 if cd_action == 'delete':
250 self.delete_s3_groups()
251 if modify:
252 self.modify_s3_groups(modify)
253 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
254 self.module.exit_json(**result)
255
256
257 def main():
258 obj = NetAppOntapS3Groups()
259 obj.apply()
260
261
262 if __name__ == '__main__':
263 main()