Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_s3_policies.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_policies
13 short_description: NetApp ONTAP S3 Policies
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 Policies on NetApp ONTAP.
20
21 options:
22 state:
23 description:
24 - Whether the specified S3 policy 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 policy.
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 policy
44 type: str
45
46 statements:
47 description:
48 - Policy statements are built using this structure to specify permissions
49 - Grant <Effect> to allow/deny <Principal> to perform <Action> on <Resource> when <Condition> applies
50 type: list
51 elements: dict
52 suboptions:
53 sid:
54 description: Statement ID
55 type: str
56 required: true
57 resources:
58 description:
59 - The bucket and any object it contains.
60 - The wildcard characters * and ? can be used to form a regular expression for specifying a resource.
61 type: list
62 elements: str
63 required: true
64 actions:
65 description:
66 - You can specify * to mean all actions, or a list of one or more of the following
67 - GetObject
68 - PutObject
69 - DeleteObject
70 - ListBucket
71 - GetBucketAcl
72 - GetObjectAcl
73 - ListBucketMultipartUploads
74 - ListMultipartUploadParts
75 type: list
76 elements: str
77 required: true
78 effect:
79 description: The statement may allow or deny access
80 type: str
81 choices:
82 - allow
83 - deny
84 required: true
85
86 lambda_config:
87 description:
88 - Configuration parameters for AWS Lambda proxy functionality.
89 - These option and suboptions are only supported with REST.
90 type: dict
91 version_added: 23.4.0
92 suboptions:
93 function_name:
94 description:
95 - The name of the AWS Lambda function to invoke.
96 type: str
97 required: true
98 aws_region:
99 description:
100 - The name of the AWS region.
101 type: str
102 required: true
103 aws_profile:
104 description:
105 - The name of the AWS profile to use for authentication.
106 type: str
107
108 notes:
109 - Supports AWS Lambda proxy functionality. See README for example usage.
110 '''
111 EXAMPLES = """
112 - name: Create and modify a S3 policy
113 netapp.ontap.na_ontap_s3_policies:
114 state: present
115 name: carchi-s3-policy
116 comment: carchi8py was here
117 vserver: ansibleSVM
118 statements:
119 - sid: 1
120 resources:
121 - "bucket1"
122 - "bucket1/*"
123 actions:
124 - "*"
125 effect:
126 allow
127 - sid: 2
128 resources:
129 - "bucket2"
130 - "bucket2/*"
131 actions:
132 - "*"
133 effect:
134 allow
135 hostname: "{{ netapp_hostname }}"
136 username: "{{ netapp_username }}"
137 password: "{{ netapp_password }}"
138 https: true
139 validate_certs: false
140 use_rest: always
141
142 - name: Delete S3 policy
143 netapp.ontap.na_ontap_s3_policies:
144 state: absent
145 name: carchi-s3-policy
146 vserver: ansibleSVM
147 hostname: "{{ netapp_hostname }}"
148 username: "{{ netapp_username }}"
149 password: "{{ netapp_password }}"
150 https: true
151 validate_certs: false
152 use_rest: always
153 """
154 RETURN = """
155 """
156
157 import traceback
158 from ansible.module_utils.basic import AnsibleModule
159 from ansible.module_utils._text import to_native
160 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
161 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
162 from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI
163 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
164 from ansible_collections.netapp.ontap.plugins.module_utils import rest_vserver
165
166
167 class NetAppOntapS3Policies:
168 def __init__(self):
169 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
170 self.argument_spec.update(dict(
171 state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
172 vserver=dict(required=True, type='str'),
173 name=dict(required=True, type='str'),
174 comment=dict(required=False, type='str'),
175 statements=dict(type='list', elements='dict', options=dict(
176 sid=dict(required=True, type='str'),
177 resources=dict(required=True, type='list', elements='str'),
178 actions=dict(required=True, type='list', elements='str'),
179 effect=dict(required=True, type='str', choices=['allow', 'deny']),
180 ))))
181 self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec())
182
183 self.module = AnsibleModule(
184 argument_spec=self.argument_spec,
185 required_if=[
186 ['use_lambda', True, ('lambda_config',)]
187 ],
188 supports_check_mode=True
189 )
190 self.svm_uuid = None
191 self.uuid = None
192 self.na_helper = NetAppModule(self.module)
193 self.parameters = self.na_helper.check_and_set_parameters(self.module)
194 self.rest_api = OntapRestAPI(self.module)
195 self.use_rest = self.rest_api.is_rest()
196 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_s3_policies', 9, 8)
197
198 def get_s3_policies(self):
199 self.get_svm_uuid()
200 api = 'protocols/s3/services/%s/policies' % self.svm_uuid
201 fields = ','.join(('name',
202 'comment',
203 'statements'))
204 params = {'name': self.parameters['name'],
205 'fields': fields}
206 record, error = rest_generic.get_one_record(self.rest_api, api, params)
207 if error:
208 self.module.fail_json(msg='Error fetching S3 policy %s: %s' % (self.parameters['name'], to_native(error)),
209 exception=traceback.format_exc())
210 # sid is an Str or a number, it will return a string back unless you pass a number then it returns a int
211 if record:
212 for each in record.get('statements', []):
213 each['sid'] = str(each['sid'])
214 return record
215
216 def get_svm_uuid(self):
217 uuid, dummy = rest_vserver.get_vserver_uuid(self.rest_api, self.parameters['vserver'], self.module, True)
218 self.svm_uuid = uuid
219
220 def create_s3_policies(self):
221 api = 'protocols/s3/services/%s/policies' % self.svm_uuid
222 body = {'name': self.parameters['name']}
223 if self.parameters.get('comment'):
224 body['comment'] = self.parameters['comment']
225 if self.parameters.get('statements'):
226 body['statements'] = self.parameters['statements']
227 dummy, error = rest_generic.post_async(self.rest_api, api, body)
228 if error:
229 self.module.fail_json(msg='Error creating S3 policy %s: %s' % (self.parameters['name'], to_native(error)),
230 exception=traceback.format_exc())
231
232 def delete_s3_policies(self):
233 api = 'protocols/s3/services/%s/policies' % self.svm_uuid
234 dummy, error = rest_generic.delete_async(self.rest_api, api, self.parameters['name'])
235 if error:
236 self.module.fail_json(msg='Error deleting S3 policy %s: %s' % (self.parameters['name'], to_native(error)),
237 exception=traceback.format_exc())
238
239 def modify_s3_policies(self, modify):
240 api = 'protocols/s3/services/%s/policies' % self.svm_uuid
241 body = {}
242 if modify.get('comment'):
243 body['comment'] = self.parameters['comment']
244 if self.parameters.get('statements'):
245 body['statements'] = self.parameters['statements']
246 dummy, error = rest_generic.patch_async(self.rest_api, api, self.parameters['name'], body)
247 if error:
248 self.module.fail_json(msg='Error modifying S3 policy %s: %s' % (self.parameters['name'], to_native(error)),
249 exception=traceback.format_exc())
250
251 def apply(self):
252 current = self.get_s3_policies()
253 modify = None
254 cd_action = self.na_helper.get_cd_action(current, self.parameters)
255 if cd_action is None:
256 modify = self.na_helper.get_modified_attributes(current, self.parameters)
257 if self.na_helper.changed and not self.module.check_mode:
258 if cd_action == 'create':
259 self.create_s3_policies()
260 if cd_action == 'delete':
261 self.delete_s3_policies()
262 if modify:
263 self.modify_s3_policies(modify)
264 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
265 self.module.exit_json(**result)
266
267
268 def main():
269 '''Apply volume operations from playbook'''
270 obj = NetAppOntapS3Policies()
271 obj.apply()
272
273
274 if __name__ == '__main__':
275 main()