Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2022, 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 - Manage Traffic Classification Policies"""
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 DOCUMENTATION = """
13 module: na_sg_grid_traffic_classes
14 short_description: Manage Traffic Classification Policy configuration on StorageGRID.
15 extends_documentation_fragment:
16 - netapp.storagegrid.netapp.sg
17 version_added: '21.10.0'
18 author: NetApp Ansible Team (@joshedmonds) <[email protected]>
19 description:
20 - Create, Update, Delete Traffic Classification Policies on NetApp StorageGRID.
21 options:
22 state:
23 description:
24 - Whether the specified Traffic Classification Policy should exist.
25 type: str
26 choices: ['present', 'absent']
27 default: present
28 name:
29 description:
30 - Name of the Traffic Classification Policy.
31 type: str
32 policy_id:
33 description:
34 - Traffic Classification Policy ID.
35 - May be used for modify or delete operation.
36 type: str
37 description:
38 description:
39 - Description of the Traffic Classification Policy.
40 type: str
41 matchers:
42 description:
43 - A set of parameters to match.
44 - The traffic class will match requests where any of these matchers match.
45 type: list
46 elements: dict
47 suboptions:
48 type:
49 description:
50 - The attribute of the request to match.
51 - C(bucket) - The S3 bucket (or Swift container) being accessed.
52 - C(bucket-regex) - A regular expression to evaluate against the S3 bucket (or Swift container) being accessed.
53 - C(cidr) - Matches if the client request source IP is in the specified IPv4 CIDR (RFC4632).
54 - C(tenant) - Matches if the S3 bucket (or Swift container) is owned by the tenant account with this ID.
55 choices: ['bucket', 'bucket-regex', 'cidr', 'endpoint', 'tenant']
56 type: str
57 required: true
58 inverse:
59 description:
60 - If I(true), entities that match the value are excluded.
61 type: bool
62 default: false
63 members:
64 description:
65 - A list of members to match on.
66 type: list
67 elements: str
68 required: true
69 limits:
70 description:
71 - Optional limits to impose on client requests matched by this traffic class.
72 - Only one of each limit type can be specified.
73 type: list
74 elements: dict
75 suboptions:
76 type:
77 description:
78 - The type of limit to apply.
79 - C(aggregateBandwidthIn) - The maximum combined upload bandwidth in bytes/second of all concurrent requests that match this policy.
80 - C(aggregateBandwidthOut) - The maximum combined download bandwidth in bytes/second of all concurrent requests that match this policy.
81 - C(concurrentReadRequests) - The maximum number of download requests that can be in progress at the same time.
82 - C(concurrentWriteRequests) - The maximum number of upload requests that can be in progress at the same time.
83 - C(readRequestRate) - The maximum number of download requests that can be started each second.
84 - C(writeRequestRate) - The maximum number of download requests that can be started each second.
85 - C(perRequestBandwidthIn) - The maximum upload bandwidth in bytes/second allowed for each request that matches this policy.
86 - C(perRequestBandwidthOut) - The maximum download bandwidth in bytes/second allowed for each request that matches this policy.
87 choices: [
88 'aggregateBandwidthIn',
89 'aggregateBandwidthOut',
90 'concurrentReadRequests',
91 'concurrentWriteRequests',
92 'readRequestRate',
93 'writeRequestRate',
94 'perRequestBandwidthIn',
95 'perRequestBandwidthOut'
96 ]
97 type: str
98 required: true
99 value:
100 description:
101 - The limit to apply.
102 - Limit values are type specific.
103 type: int
104 required: true
105 """
106
107 EXAMPLES = """
108 - name: create Traffic Classification Policy with bandwidth limit on buckets
109 netapp.storagegrid.na_sg_grid_traffic_classes:
110 api_url: "https://<storagegrid-endpoint-url>"
111 auth_token: "storagegrid-auth-token"
112 validate_certs: false
113 state: present
114 name: Traffic-Policy1
115 matchers:
116 - type: bucket
117 members: bucket1,anotherbucket
118 limits:
119 - type: aggregateBandwidthOut
120 value: 100000000
121
122 - name: create Traffic Classification Policy with bandwidth limits except for specific tenant account
123 netapp.storagegrid.na_sg_grid_traffic_classes:
124 api_url: "https://<storagegrid-endpoint-url>"
125 auth_token: "storagegrid-auth-token"
126 validate_certs: false
127 state: present
128 name: Fabricpool-Policy
129 description: "Limit all to 500MB/s except FabricPool tenant"
130 matchers:
131 - type: tenant
132 inverse: true
133 members: 12345678901234567890
134 limits:
135 - type: aggregateBandwidthIn
136 value: 50000000
137 - type: aggregateBandwidthOut
138 value: 50000000
139
140 - name: rename Traffic Classification Policy
141 netapp.storagegrid.na_sg_grid_traffic_classes:
142 api_url: "https://<storagegrid-endpoint-url>"
143 auth_token: "storagegrid-auth-token"
144 validate_certs: false
145 state: present
146 policy_id: 00000000-0000-0000-0000-000000000000
147 name: Traffic-Policy1-New-Name
148 matchers:
149 - type: bucket
150 members: bucket1,anotherbucket
151 limits:
152 - type: aggregateBandwidthOut
153 value: 100000000
154
155 - name: delete Traffic Classification Policy
156 netapp.storagegrid.na_sg_grid_traffic_classes:
157 api_url: "https://<storagegrid-endpoint-url>"
158 auth_token: "storagegrid-auth-token"
159 validate_certs: false
160 state: absent
161 name: Traffic-Policy1
162 """
163
164 RETURN = """
165 resp:
166 description: Returns information about the StorageGRID Traffic Classification Policy.
167 returned: success
168 type: dict
169 sample: {
170 "id": "6b2946e6-7fed-40d0-9262-8e922580aba7",
171 "name": "Traffic-Policy1",
172 "description": "Traffic Classification Policy 1",
173 "matchers": [
174 {
175 "type": "cidr",
176 "inverse": False,
177 "members": [
178 "192.168.50.0/24"
179 ]
180 },
181 {
182 "type": "bucket",
183 "inverse": False,
184 "members": [
185 "mybucket1",
186 "mybucket2"
187 ]
188 },
189 ],
190 "limits": [
191 {
192 "type": "aggregateBandwidthOut",
193 "value": 100000000
194 }
195 ],
196 }
197 """
198
199 import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils
200 from ansible.module_utils.basic import AnsibleModule
201 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule
202 from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI
203
204
205 class SgGridTrafficClasses:
206 """
207 Create, modify and delete Traffic Classification Policies for StorageGRID
208 """
209
210 def __init__(self):
211 """
212 Parse arguments, setup state variables,
213 check parameters and ensure request module is installed
214 """
215 self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec()
216 self.argument_spec.update(
217 dict(
218 state=dict(required=False, type="str", choices=["present", "absent"], default="present"),
219 name=dict(required=False, type="str"),
220 policy_id=dict(required=False, type="str"),
221 description=dict(required=False, type="str"),
222 matchers=dict(
223 required=False,
224 type="list",
225 elements="dict",
226 options=dict(
227 type=dict(
228 required=True,
229 type="str",
230 choices=["bucket", "bucket-regex", "cidr", "endpoint", "tenant"],
231 ),
232 inverse=dict(required=False, type="bool", default="false"),
233 members=dict(required=True, type="list", elements="str"),
234 ),
235 ),
236 limits=dict(
237 required=False,
238 type="list",
239 elements="dict",
240 options=dict(
241 type=dict(
242 required=True,
243 type="str",
244 choices=[
245 "aggregateBandwidthIn",
246 "aggregateBandwidthOut",
247 "concurrentReadRequests",
248 "concurrentWriteRequests",
249 "readRequestRate",
250 "writeRequestRate",
251 "perRequestBandwidthIn",
252 "perRequestBandwidthOut",
253 ],
254 ),
255 value=dict(required=True, type="int"),
256 ),
257 ),
258 )
259 )
260
261 self.module = AnsibleModule(
262 argument_spec=self.argument_spec,
263 required_if=[("state", "present", ["name"])],
264 required_one_of=[("name", "policy_id")],
265 )
266
267 self.na_helper = NetAppModule()
268
269 # set up state variables
270 self.parameters = self.na_helper.set_parameters(self.module.params)
271 # Calling generic SG rest_api class
272 self.rest_api = SGRestAPI(self.module)
273 # Get API version
274 self.rest_api.get_sg_product_version()
275 self.api_version = self.rest_api.get_api_version()
276
277 # Checking for the parameters passed and create new parameters list
278 self.data = {}
279
280 if self.parameters["state"] == "present":
281 for k in ["name", "description", "matchers", "limits"]:
282 if self.parameters.get(k) is not None:
283 self.data[k] = self.parameters[k]
284
285 def get_traffic_class_policy_id(self):
286 # Check if Traffic Classification Policy exists
287 # Return policy ID if found, or None
288 api = "api/%s/grid/traffic-classes/policies" % self.api_version
289 response, error = self.rest_api.get(api)
290
291 if error:
292 self.module.fail_json(msg=error)
293
294 return next((item["id"] for item in response.get("data") if item["name"] == self.parameters["name"]), None)
295
296 def get_traffic_class_policy(self, policy_id):
297 api = "api/%s/grid/traffic-classes/policies/%s" % (self.api_version, policy_id)
298 response, error = self.rest_api.get(api)
299
300 if error:
301 self.module.fail_json(msg=error)
302
303 return response["data"]
304
305 def create_traffic_class_policy(self):
306 api = "api/%s/grid/traffic-classes/policies" % self.api_version
307 # self.module.fail_json(msg=self.data)
308 response, error = self.rest_api.post(api, self.data)
309
310 if error:
311 self.module.fail_json(msg=error)
312
313 return response["data"]
314
315 def delete_traffic_class_policy(self, policy_id):
316 api = "api/%s/grid/traffic-classes/policies/%s" % (self.api_version, policy_id)
317 dummy, error = self.rest_api.delete(api, self.data)
318
319 if error:
320 self.module.fail_json(msg=error)
321
322 def update_traffic_class_policy(self, policy_id):
323 api = "api/%s/grid/traffic-classes/policies/%s" % (self.api_version, policy_id)
324 response, error = self.rest_api.put(api, self.data)
325
326 if error:
327 self.module.fail_json(msg=error)
328
329 return response["data"]
330
331 def apply(self):
332 """
333 Perform pre-checks, call functions and exit
334 """
335
336 traffic_class_policy = None
337
338 if self.parameters.get("policy_id"):
339 traffic_class_policy = self.get_traffic_class_policy(self.parameters["policy_id"])
340 else:
341 policy_id = self.get_traffic_class_policy_id()
342 if policy_id:
343 traffic_class_policy = self.get_traffic_class_policy(policy_id)
344
345 cd_action = self.na_helper.get_cd_action(traffic_class_policy, self.parameters)
346
347 if cd_action is None and self.parameters["state"] == "present":
348 # let's see if we need to update parameters
349 modify = self.na_helper.get_modified_attributes(traffic_class_policy, self.data)
350
351 result_message = ""
352 resp_data = {}
353
354 if self.na_helper.changed and not self.module.check_mode:
355 if cd_action == "delete":
356 self.delete_traffic_class_policy(traffic_class_policy["id"])
357 result_message = "Traffic Classification Policy deleted"
358 elif cd_action == "create":
359 resp_data = self.create_traffic_class_policy()
360 result_message = "Traffic Classification Policy created"
361 elif modify:
362 resp_data = self.update_traffic_class_policy(traffic_class_policy["id"])
363 result_message = "Traffic Classification Policy updated"
364
365 self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data)
366
367
368 def main():
369 """
370 Main function
371 """
372 na_sg_grid_traffic_classes = SgGridTrafficClasses()
373 na_sg_grid_traffic_classes.apply()
374
375
376 if __name__ == "__main__":
377 main()