| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-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 |
''' |
| 8 |
na_ontap_qos_policy_group |
| 9 |
''' |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
|
| 13 |
__metaclass__ = type |
| 14 |
|
| 15 |
|
| 16 |
DOCUMENTATION = ''' |
| 17 |
module: na_ontap_qos_policy_group |
| 18 |
short_description: NetApp ONTAP manage policy group in Quality of Service. |
| 19 |
extends_documentation_fragment: |
| 20 |
- netapp.ontap.netapp.na_ontap |
| 21 |
version_added: 2.8.0 |
| 22 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 23 |
|
| 24 |
description: |
| 25 |
- Create, destroy, modify, or rename QoS policy group on NetApp ONTAP. |
| 26 |
- With ZAPI, only fixed QoS policy group is supported. |
| 27 |
- With REST, both fixed and adaptive QoS policy group are supported. |
| 28 |
|
| 29 |
options: |
| 30 |
state: |
| 31 |
choices: ['present', 'absent'] |
| 32 |
description: |
| 33 |
- Whether the specified policy group should exist or not. |
| 34 |
default: 'present' |
| 35 |
type: str |
| 36 |
|
| 37 |
name: |
| 38 |
description: |
| 39 |
- The name of the policy group to manage. |
| 40 |
required: true |
| 41 |
type: str |
| 42 |
|
| 43 |
vserver: |
| 44 |
description: |
| 45 |
- Name of the vserver to use. |
| 46 |
required: true |
| 47 |
type: str |
| 48 |
|
| 49 |
from_name: |
| 50 |
description: |
| 51 |
- Name of the existing policy group to be renamed to name. |
| 52 |
type: str |
| 53 |
|
| 54 |
max_throughput: |
| 55 |
description: |
| 56 |
- Maximum throughput defined by this policy. |
| 57 |
- Not supported with REST, use C(fixed_qos_options). |
| 58 |
type: str |
| 59 |
|
| 60 |
min_throughput: |
| 61 |
description: |
| 62 |
- Minimum throughput defined by this policy. |
| 63 |
- Not supported with REST, use C(fixed_qos_options). |
| 64 |
type: str |
| 65 |
|
| 66 |
is_shared: |
| 67 |
description: |
| 68 |
- Whether the SLOs of the policy group are shared between the workloads or if the SLOs are applied separately to each workload. |
| 69 |
- Not supported with REST, use C(fixed_qos_options). |
| 70 |
type: bool |
| 71 |
version_added: 20.12.0 |
| 72 |
|
| 73 |
force: |
| 74 |
type: bool |
| 75 |
description: |
| 76 |
- Setting to 'true' forces the deletion of the workloads associated with the policy group along with the policy group. |
| 77 |
- Not supported with REST. |
| 78 |
|
| 79 |
fixed_qos_options: |
| 80 |
version_added: 21.19.0 |
| 81 |
type: dict |
| 82 |
description: |
| 83 |
- Set Minimum and Maximum throughput defined by this policy. |
| 84 |
- Only supported with REST. |
| 85 |
- Required one of throughtput options when creating qos_policy. |
| 86 |
suboptions: |
| 87 |
capacity_shared: |
| 88 |
description: |
| 89 |
- Whether the SLOs of the policy group are shared between the workloads or if the SLOs are applied separately to each workload. |
| 90 |
- Default value is False if not used in creating qos policy. |
| 91 |
type: bool |
| 92 |
max_throughput_iops: |
| 93 |
description: |
| 94 |
- Maximum throughput defined by this policy. It is specified in terms of IOPS. |
| 95 |
- 0 means no maximum throughput is enforced. |
| 96 |
type: int |
| 97 |
max_throughput_mbps: |
| 98 |
description: |
| 99 |
- Maximum throughput defined by this policy. It is specified in terms of Mbps. |
| 100 |
- 0 means no maximum throughput is enforced. |
| 101 |
type: int |
| 102 |
min_throughput_iops: |
| 103 |
description: |
| 104 |
- Minimum throughput defined by this policy. It is specified in terms of IOPS. |
| 105 |
- 0 means no minimum throughput is enforced. |
| 106 |
- These floors are not guaranteed on non-AFF platforms or when FabricPool tiering policies are set. |
| 107 |
type: int |
| 108 |
min_throughput_mbps: |
| 109 |
description: |
| 110 |
- Minimum throughput defined by this policy. It is specified in terms of Mbps. |
| 111 |
- 0 means no minimum throughput is enforced. |
| 112 |
- Requires ONTAP 9.8 or later, and REST support. |
| 113 |
type: int |
| 114 |
|
| 115 |
adaptive_qos_options: |
| 116 |
version_added: 21.19.0 |
| 117 |
type: dict |
| 118 |
description: |
| 119 |
- Adaptive QoS policy-groups define measurable service level objectives (SLOs) that adjust based on the storage object used space |
| 120 |
and the storage object allocated space. |
| 121 |
- Only supported with REST. |
| 122 |
suboptions: |
| 123 |
absolute_min_iops: |
| 124 |
description: |
| 125 |
- Specifies the absolute minimum IOPS that is used as an override when the expected_iops is less than this value. |
| 126 |
- These floors are not guaranteed on non-AFF platforms or when FabricPool tiering policies are set. |
| 127 |
type: int |
| 128 |
required: true |
| 129 |
expected_iops: |
| 130 |
description: |
| 131 |
- Expected IOPS. Specifies the minimum expected IOPS per TB allocated based on the storage object allocated size. |
| 132 |
- These floors are not guaranteed on non-AFF platforms or when FabricPool tiering policies are set. |
| 133 |
type: int |
| 134 |
required: true |
| 135 |
peak_iops: |
| 136 |
description: |
| 137 |
- Peak IOPS. Specifies the maximum possible IOPS per TB allocated based on the storage object allocated size or |
| 138 |
the storage object used size. |
| 139 |
type: int |
| 140 |
required: true |
| 141 |
block_size: |
| 142 |
description: |
| 143 |
- Specifies the block size. |
| 144 |
- Requires ONTAP 9.10.1 or later. |
| 145 |
type: str |
| 146 |
required: false |
| 147 |
choices: ['any', '4k', '8k', '16k', '32k', '64k', '128k'] |
| 148 |
version_added: 22.6.0 |
| 149 |
expected_iops_allocation: |
| 150 |
description: |
| 151 |
- Specifies the size to be used to calculate expected IOPS per TB. |
| 152 |
- Supported only with REST; requires ONTAP 9.10.1 or later. |
| 153 |
type: str |
| 154 |
required: false |
| 155 |
choices: ['used_space', 'allocated_space'] |
| 156 |
version_added: 22.8.0 |
| 157 |
peak_iops_allocation: |
| 158 |
description: |
| 159 |
- Specifies the size to be used to calculate peak IOPS per TB. |
| 160 |
- Supported only with REST; requires ONTAP 9.10.1 or later. |
| 161 |
type: str |
| 162 |
required: false |
| 163 |
choices: ['used_space', 'allocated_space'] |
| 164 |
version_added: 22.8.0 |
| 165 |
|
| 166 |
lambda_config: |
| 167 |
description: |
| 168 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 169 |
- These option and suboptions are only supported with REST. |
| 170 |
type: dict |
| 171 |
version_added: 23.5.0 |
| 172 |
suboptions: |
| 173 |
function_name: |
| 174 |
description: |
| 175 |
- The name of the AWS Lambda function to invoke. |
| 176 |
type: str |
| 177 |
required: true |
| 178 |
aws_region: |
| 179 |
description: |
| 180 |
- The name of the AWS region. |
| 181 |
type: str |
| 182 |
required: true |
| 183 |
aws_profile: |
| 184 |
description: |
| 185 |
- The name of the AWS profile to use for authentication. |
| 186 |
type: str |
| 187 |
|
| 188 |
notes: |
| 189 |
- Supports AWS Lambda proxy functionality when using REST. See the README file for examples. |
| 190 |
''' |
| 191 |
|
| 192 |
EXAMPLES = """ |
| 193 |
- name: Create qos policy group in ZAPI. |
| 194 |
netapp.ontap.na_ontap_qos_policy_group: |
| 195 |
state: present |
| 196 |
name: policy_1 |
| 197 |
vserver: policy_vserver |
| 198 |
max_throughput: 800KB/s,800iops |
| 199 |
min_throughput: 100iops |
| 200 |
hostname: "{{ netapp_hostname }}" |
| 201 |
username: "{{ netapp_username }}" |
| 202 |
password: "{{ netapp_password }}" |
| 203 |
use_rest: never |
| 204 |
|
| 205 |
- name: Modify qos policy group max throughput in ZAPI. |
| 206 |
netapp.ontap.na_ontap_qos_policy_group: |
| 207 |
state: present |
| 208 |
name: policy_1 |
| 209 |
vserver: policy_vserver |
| 210 |
max_throughput: 900KB/s,800iops |
| 211 |
min_throughput: 100iops |
| 212 |
hostname: "{{ netapp_hostname }}" |
| 213 |
username: "{{ netapp_username }}" |
| 214 |
password: "{{ netapp_password }}" |
| 215 |
use_rest: never |
| 216 |
|
| 217 |
- name: Delete qos policy group |
| 218 |
netapp.ontap.na_ontap_qos_policy_group: |
| 219 |
state: absent |
| 220 |
name: policy_1 |
| 221 |
vserver: policy_vserver |
| 222 |
hostname: "{{ netapp_hostname }}" |
| 223 |
username: "{{ netapp_username }}" |
| 224 |
password: "{{ netapp_password }}" |
| 225 |
|
| 226 |
- name: Create qos policy group in REST. |
| 227 |
netapp.ontap.na_ontap_qos_policy_group: |
| 228 |
state: present |
| 229 |
name: policy_1 |
| 230 |
vserver: policy_vserver |
| 231 |
hostname: "{{ netapp_hostname }}" |
| 232 |
username: "{{ netapp_username }}" |
| 233 |
password: "{{ netapp_password }}" |
| 234 |
use_rest: always |
| 235 |
fixed_qos_options: |
| 236 |
max_throughput_iops: 800 |
| 237 |
max_throughput_mbps: 200 |
| 238 |
min_throughput_iops: 500 |
| 239 |
min_throughput_mbps: 100 |
| 240 |
capacity_shared: true |
| 241 |
|
| 242 |
- name: Modify qos policy max_throughput in REST. |
| 243 |
netapp.ontap.na_ontap_qos_policy_group: |
| 244 |
state: present |
| 245 |
name: policy_1 |
| 246 |
vserver: policy_vserver |
| 247 |
hostname: "{{ netapp_hostname }}" |
| 248 |
username: "{{ netapp_username }}" |
| 249 |
password: "{{ netapp_password }}" |
| 250 |
use_rest: always |
| 251 |
fixed_qos_options: |
| 252 |
max_throughput_iops: 1000 |
| 253 |
max_throughput_mbps: 300 |
| 254 |
|
| 255 |
- name: Create adaptive qos policy group in REST. |
| 256 |
netapp.ontap.na_ontap_qos_policy_group: |
| 257 |
state: present |
| 258 |
name: adaptive_policy |
| 259 |
vserver: policy_vserver |
| 260 |
hostname: "{{ netapp_hostname }}" |
| 261 |
username: "{{ netapp_username }}" |
| 262 |
password: "{{ netapp_password }}" |
| 263 |
use_rest: always |
| 264 |
adaptive_qos_options: |
| 265 |
absolute_min_iops: 100 |
| 266 |
expected_iops: 200 |
| 267 |
peak_iops: 500 |
| 268 |
|
| 269 |
- name: Modify adaptive qos policy group in REST. |
| 270 |
netapp.ontap.na_ontap_qos_policy_group: |
| 271 |
state: present |
| 272 |
name: adaptive_policy |
| 273 |
vserver: policy_vserver |
| 274 |
hostname: "{{ netapp_hostname }}" |
| 275 |
username: "{{ netapp_username }}" |
| 276 |
password: "{{ netapp_password }}" |
| 277 |
use_rest: always |
| 278 |
adaptive_qos_options: |
| 279 |
expected_iops_allocation: used_space |
| 280 |
peak_iops_allocation: allocated_space |
| 281 |
""" |
| 282 |
|
| 283 |
RETURN = """ |
| 284 |
""" |
| 285 |
|
| 286 |
import traceback |
| 287 |
|
| 288 |
from ansible.module_utils.basic import AnsibleModule |
| 289 |
from ansible.module_utils._text import to_native |
| 290 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 291 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 292 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 293 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 294 |
|
| 295 |
|
| 296 |
class NetAppOntapQosPolicyGroup: |
| 297 |
""" |
| 298 |
Create, delete, modify and rename a policy group. |
| 299 |
""" |
| 300 |
def __init__(self): |
| 301 |
""" |
| 302 |
Initialize the Ontap qos policy group class. |
| 303 |
""" |
| 304 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 305 |
self.argument_spec.update(dict( |
| 306 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 307 |
name=dict(required=True, type='str'), |
| 308 |
from_name=dict(required=False, type='str'), |
| 309 |
vserver=dict(required=True, type='str'), |
| 310 |
max_throughput=dict(required=False, type='str'), |
| 311 |
min_throughput=dict(required=False, type='str'), |
| 312 |
is_shared=dict(required=False, type='bool'), |
| 313 |
force=dict(required=False, type='bool'), |
| 314 |
fixed_qos_options=dict(required=False, type='dict', options=dict( |
| 315 |
capacity_shared=dict(required=False, type='bool'), |
| 316 |
max_throughput_iops=dict(required=False, type='int'), |
| 317 |
max_throughput_mbps=dict(required=False, type='int'), |
| 318 |
min_throughput_iops=dict(required=False, type='int'), |
| 319 |
min_throughput_mbps=dict(required=False, type='int') |
| 320 |
)), |
| 321 |
adaptive_qos_options=dict(required=False, type='dict', options=dict( |
| 322 |
absolute_min_iops=dict(required=True, type='int'), |
| 323 |
expected_iops=dict(required=True, type='int'), |
| 324 |
peak_iops=dict(required=True, type='int'), |
| 325 |
block_size=dict(required=False, type='str', choices=['any', '4k', '8k', '16k', '32k', '64k', '128k']), |
| 326 |
expected_iops_allocation=dict(required=False, type='str', choices=['used_space', 'allocated_space']), |
| 327 |
peak_iops_allocation=dict(required=False, type='str', choices=['used_space', 'allocated_space']) |
| 328 |
)) |
| 329 |
)) |
| 330 |
|
| 331 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 332 |
|
| 333 |
self.module = AnsibleModule( |
| 334 |
argument_spec=self.argument_spec, |
| 335 |
supports_check_mode=True, |
| 336 |
mutually_exclusive=[ |
| 337 |
['max_throughput', 'fixed_qos_options'], |
| 338 |
['min_throughput', 'fixed_qos_options'], |
| 339 |
['max_throughput', 'adaptive_qos_options'], |
| 340 |
['min_throughput', 'adaptive_qos_options'], |
| 341 |
['fixed_qos_options', 'adaptive_qos_options'], |
| 342 |
['is_shared', 'adaptive_qos_options'], |
| 343 |
['is_shared', 'fixed_qos_options'] |
| 344 |
], |
| 345 |
required_if=[ |
| 346 |
('use_lambda', True, ['lambda_config']) |
| 347 |
] |
| 348 |
) |
| 349 |
self.na_helper = NetAppModule() |
| 350 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 351 |
|
| 352 |
# Set up Rest API |
| 353 |
self.rest_api = OntapRestAPI(self.module) |
| 354 |
unsupported_rest_properties = ['is_shared', 'max_throughput', 'min_throughput', 'force'] |
| 355 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties) |
| 356 |
|
| 357 |
if self.use_rest and self.parameters['state'] == 'present': |
| 358 |
if not self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 8) and \ |
| 359 |
self.na_helper.safe_get(self.parameters, ['fixed_qos_options', 'min_throughput_mbps']): |
| 360 |
self.module.fail_json(msg="Minimum version of ONTAP for 'fixed_qos_options.min_throughput_mbps' is (9, 8, 0)") |
| 361 |
|
| 362 |
ontap_9_10_adaptive_options = ['block_size', 'expected_iops_allocation', 'peak_iops_allocation'] |
| 363 |
if not self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 10, 1) and \ |
| 364 |
any(self.na_helper.safe_get(self.parameters, ['adaptive_qos_options', option]) for option in ontap_9_10_adaptive_options): |
| 365 |
self.module.fail_json(msg='Error: %s' % self.rest_api.options_require_ontap_version(ontap_9_10_adaptive_options, version='9.10.1')) |
| 366 |
self.uuid = None |
| 367 |
|
| 368 |
if not self.use_rest: |
| 369 |
if self.parameters.get('use_lambda'): |
| 370 |
self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.") |
| 371 |
if not netapp_utils.has_netapp_lib(): |
| 372 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 373 |
if 'adaptive_qos_options' in self.parameters: |
| 374 |
self.module.fail_json(msg="Error: use 'na_ontap_qos_adaptive_policy_group' module for create/modify/delete adaptive policy with ZAPI") |
| 375 |
if 'fixed_qos_options' in self.parameters and self.parameters['state'] == 'present': |
| 376 |
self.module.fail_json(msg="Error: 'fixed_qos_options' not supported with ZAPI, use 'max_throughput' and 'min_throughput'") |
| 377 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 378 |
# default value for force is false in ZAPI. |
| 379 |
self.parameters['force'] = False |
| 380 |
|
| 381 |
def get_policy_group(self, policy_group_name=None): |
| 382 |
""" |
| 383 |
Return details of a policy group. |
| 384 |
:param policy_group_name: policy group name |
| 385 |
:return: policy group details. |
| 386 |
:rtype: dict. |
| 387 |
""" |
| 388 |
if policy_group_name is None: |
| 389 |
policy_group_name = self.parameters['name'] |
| 390 |
if self.use_rest: |
| 391 |
return self.get_policy_group_rest(policy_group_name) |
| 392 |
policy_group_get_iter = netapp_utils.zapi.NaElement('qos-policy-group-get-iter') |
| 393 |
policy_group_info = netapp_utils.zapi.NaElement('qos-policy-group-info') |
| 394 |
policy_group_info.add_new_child('policy-group', policy_group_name) |
| 395 |
policy_group_info.add_new_child('vserver', self.parameters['vserver']) |
| 396 |
query = netapp_utils.zapi.NaElement('query') |
| 397 |
query.add_child_elem(policy_group_info) |
| 398 |
policy_group_get_iter.add_child_elem(query) |
| 399 |
try: |
| 400 |
result = self.server.invoke_successfully(policy_group_get_iter, True) |
| 401 |
except netapp_utils.zapi.NaApiError as error: |
| 402 |
self.module.fail_json(msg='Error fetching qos policy group %s: %s' % |
| 403 |
(self.parameters['name'], to_native(error)), |
| 404 |
exception=traceback.format_exc()) |
| 405 |
policy_group_detail = None |
| 406 |
|
| 407 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) == 1: |
| 408 |
policy_info = result.get_child_by_name('attributes-list').get_child_by_name('qos-policy-group-info') |
| 409 |
|
| 410 |
policy_group_detail = { |
| 411 |
'name': policy_info.get_child_content('policy-group'), |
| 412 |
'vserver': policy_info.get_child_content('vserver'), |
| 413 |
'max_throughput': policy_info.get_child_content('max-throughput'), |
| 414 |
'min_throughput': policy_info.get_child_content('min-throughput'), |
| 415 |
'is_shared': self.na_helper.get_value_for_bool(True, policy_info.get_child_content('is-shared')) |
| 416 |
} |
| 417 |
return policy_group_detail |
| 418 |
|
| 419 |
def get_policy_group_rest(self, policy_group_name): |
| 420 |
api = 'storage/qos/policies' |
| 421 |
query = { |
| 422 |
'name': policy_group_name, |
| 423 |
'svm.name': self.parameters['vserver'] |
| 424 |
} |
| 425 |
fields = 'name,svm' |
| 426 |
if 'fixed_qos_options' in self.parameters: |
| 427 |
fields += ',fixed' |
| 428 |
elif 'adaptive_qos_options' in self.parameters: |
| 429 |
fields += ',adaptive' |
| 430 |
record, error = rest_generic.get_one_record(self.rest_api, api, query, fields) |
| 431 |
if error: |
| 432 |
self.module.fail_json(msg='Error fetching qos policy group %s: %s' % |
| 433 |
(self.parameters['name'], error)) |
| 434 |
current = None |
| 435 |
if record: |
| 436 |
self.uuid = record['uuid'] |
| 437 |
current = { |
| 438 |
'name': record['name'], |
| 439 |
'vserver': record['svm']['name'] |
| 440 |
} |
| 441 |
|
| 442 |
if 'fixed' in record: |
| 443 |
current['fixed_qos_options'] = {} |
| 444 |
for fixed_qos_option in ['capacity_shared', 'max_throughput_iops', 'max_throughput_mbps', 'min_throughput_iops']: |
| 445 |
current['fixed_qos_options'][fixed_qos_option] = record['fixed'].get(fixed_qos_option) |
| 446 |
if self.na_helper.safe_get(self.parameters, ['fixed_qos_options', 'min_throughput_mbps']): |
| 447 |
current['fixed_qos_options']['min_throughput_mbps'] = record['fixed'].get('min_throughput_mbps') |
| 448 |
|
| 449 |
if 'adaptive' in record: |
| 450 |
current['adaptive_qos_options'] = {} |
| 451 |
for adaptive_qos_option in ['absolute_min_iops', 'expected_iops', 'peak_iops', 'block_size', |
| 452 |
'expected_iops_allocation', 'peak_iops_allocation']: |
| 453 |
current['adaptive_qos_options'][adaptive_qos_option] = record['adaptive'].get(adaptive_qos_option) |
| 454 |
return current |
| 455 |
|
| 456 |
def create_policy_group(self): |
| 457 |
""" |
| 458 |
create a policy group name. |
| 459 |
""" |
| 460 |
if self.use_rest: |
| 461 |
return self.create_policy_group_rest() |
| 462 |
policy_group = netapp_utils.zapi.NaElement('qos-policy-group-create') |
| 463 |
policy_group.add_new_child('policy-group', self.parameters['name']) |
| 464 |
policy_group.add_new_child('vserver', self.parameters['vserver']) |
| 465 |
if self.parameters.get('max_throughput'): |
| 466 |
policy_group.add_new_child('max-throughput', self.parameters['max_throughput']) |
| 467 |
if self.parameters.get('min_throughput'): |
| 468 |
policy_group.add_new_child('min-throughput', self.parameters['min_throughput']) |
| 469 |
if self.parameters.get('is_shared') is not None: |
| 470 |
policy_group.add_new_child('is-shared', self.na_helper.get_value_for_bool(False, self.parameters['is_shared'])) |
| 471 |
try: |
| 472 |
self.server.invoke_successfully(policy_group, True) |
| 473 |
except netapp_utils.zapi.NaApiError as error: |
| 474 |
self.module.fail_json(msg='Error creating qos policy group %s: %s' % |
| 475 |
(self.parameters['name'], to_native(error)), |
| 476 |
exception=traceback.format_exc()) |
| 477 |
|
| 478 |
def create_policy_group_rest(self): |
| 479 |
api = 'storage/qos/policies' |
| 480 |
body = { |
| 481 |
'name': self.parameters['name'], |
| 482 |
'svm.name': self.parameters['vserver'] |
| 483 |
} |
| 484 |
if 'fixed_qos_options' in self.parameters: |
| 485 |
body['fixed'] = self.na_helper.filter_out_none_entries(self.parameters['fixed_qos_options']) |
| 486 |
# default value for capacity_shared is False in REST. |
| 487 |
if self.na_helper.safe_get(body, ['fixed', 'capacity_shared']) is None: |
| 488 |
body['fixed']['capacity_shared'] = False |
| 489 |
else: |
| 490 |
body['adaptive'] = self.na_helper.filter_out_none_entries(self.parameters['adaptive_qos_options']) |
| 491 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 492 |
if error: |
| 493 |
self.module.fail_json(msg='Error creating qos policy group %s: %s' % |
| 494 |
(self.parameters['name'], error)) |
| 495 |
|
| 496 |
def delete_policy_group(self, policy_group=None): |
| 497 |
""" |
| 498 |
delete an existing policy group. |
| 499 |
:param policy_group: policy group name. |
| 500 |
""" |
| 501 |
if self.use_rest: |
| 502 |
return self.delete_policy_group_rest() |
| 503 |
if policy_group is None: |
| 504 |
policy_group = self.parameters['name'] |
| 505 |
policy_group_obj = netapp_utils.zapi.NaElement('qos-policy-group-delete') |
| 506 |
policy_group_obj.add_new_child('policy-group', policy_group) |
| 507 |
if self.parameters.get('force'): |
| 508 |
policy_group_obj.add_new_child('force', str(self.parameters['force'])) |
| 509 |
try: |
| 510 |
self.server.invoke_successfully(policy_group_obj, True) |
| 511 |
except netapp_utils.zapi.NaApiError as error: |
| 512 |
self.module.fail_json(msg='Error deleting qos policy group %s: %s' % |
| 513 |
(policy_group, to_native(error)), |
| 514 |
exception=traceback.format_exc()) |
| 515 |
|
| 516 |
def delete_policy_group_rest(self): |
| 517 |
api = 'storage/qos/policies' |
| 518 |
dummy, error = rest_generic.delete_async(self.rest_api, api, self.uuid) |
| 519 |
if error: |
| 520 |
self.module.fail_json(msg='Error deleting qos policy group %s: %s' % |
| 521 |
(self.parameters['name'], error)) |
| 522 |
|
| 523 |
def modify_policy_group(self, modify): |
| 524 |
""" |
| 525 |
Modify policy group. |
| 526 |
""" |
| 527 |
if self.use_rest: |
| 528 |
return self.modify_policy_group_rest(modify) |
| 529 |
policy_group_obj = netapp_utils.zapi.NaElement('qos-policy-group-modify') |
| 530 |
policy_group_obj.add_new_child('policy-group', self.parameters['name']) |
| 531 |
if self.parameters.get('max_throughput'): |
| 532 |
policy_group_obj.add_new_child('max-throughput', self.parameters['max_throughput']) |
| 533 |
if self.parameters.get('min_throughput'): |
| 534 |
policy_group_obj.add_new_child('min-throughput', self.parameters['min_throughput']) |
| 535 |
try: |
| 536 |
self.server.invoke_successfully(policy_group_obj, True) |
| 537 |
except netapp_utils.zapi.NaApiError as error: |
| 538 |
self.module.fail_json(msg='Error modifying qos policy group %s: %s' % |
| 539 |
(self.parameters['name'], to_native(error)), |
| 540 |
exception=traceback.format_exc()) |
| 541 |
|
| 542 |
def modify_policy_group_rest(self, modify): |
| 543 |
api = 'storage/qos/policies' |
| 544 |
body = {} |
| 545 |
if 'fixed_qos_options' in modify: |
| 546 |
body['fixed'] = modify['fixed_qos_options'] |
| 547 |
else: |
| 548 |
if 'block_size' not in self.na_helper.safe_get(modify, ['adaptive_qos_options']) and \ |
| 549 |
self.na_helper.safe_get(self.parameters, ['adaptive_qos_options', 'block_size']) is None: |
| 550 |
# if block_size is not to be modified then remove it from the params |
| 551 |
# to avoid error with block_size option during modification of other adaptive qos options |
| 552 |
del self.parameters['adaptive_qos_options']['block_size'] |
| 553 |
body['adaptive'] = self.parameters['adaptive_qos_options'] |
| 554 |
dummy, error = rest_generic.patch_async(self.rest_api, api, self.uuid, body) |
| 555 |
if error: |
| 556 |
self.module.fail_json(msg='Error modifying qos policy group %s: %s' % |
| 557 |
(self.parameters['name'], error)) |
| 558 |
|
| 559 |
def rename_policy_group(self): |
| 560 |
""" |
| 561 |
Rename policy group name. |
| 562 |
""" |
| 563 |
if self.use_rest: |
| 564 |
return self.rename_policy_group_rest() |
| 565 |
rename_obj = netapp_utils.zapi.NaElement('qos-policy-group-rename') |
| 566 |
rename_obj.add_new_child('new-name', self.parameters['name']) |
| 567 |
rename_obj.add_new_child('policy-group-name', self.parameters['from_name']) |
| 568 |
try: |
| 569 |
self.server.invoke_successfully(rename_obj, True) |
| 570 |
except netapp_utils.zapi.NaApiError as error: |
| 571 |
self.module.fail_json(msg='Error renaming qos policy group %s: %s' % |
| 572 |
(self.parameters['from_name'], to_native(error)), |
| 573 |
exception=traceback.format_exc()) |
| 574 |
|
| 575 |
def rename_policy_group_rest(self): |
| 576 |
api = 'storage/qos/policies' |
| 577 |
body = {'name': self.parameters['name']} |
| 578 |
dummy, error = rest_generic.patch_async(self.rest_api, api, self.uuid, body) |
| 579 |
if error: |
| 580 |
self.module.fail_json(msg='Error renaming qos policy group %s: %s' % |
| 581 |
(self.parameters['from_name'], error)) |
| 582 |
|
| 583 |
def modify_helper(self, modify): |
| 584 |
""" |
| 585 |
helper method to modify policy group. |
| 586 |
:param modify: modified attributes. |
| 587 |
""" |
| 588 |
if any( |
| 589 |
attribute in modify |
| 590 |
for attribute in ['max_throughput', 'min_throughput', 'fixed_qos_options', 'adaptive_qos_options'] |
| 591 |
): |
| 592 |
self.modify_policy_group(modify) |
| 593 |
|
| 594 |
def validate_adaptive_or_fixed_qos_options(self): |
| 595 |
error = None |
| 596 |
# one of the fixed throughput option required in create qos_policy. |
| 597 |
if 'fixed_qos_options' in self.parameters: |
| 598 |
fixed_options = ['max_throughput_iops', 'max_throughput_mbps', 'min_throughput_iops', 'min_throughput_mbps'] |
| 599 |
if not any(x in self.na_helper.filter_out_none_entries(self.parameters['fixed_qos_options']) for x in fixed_options): |
| 600 |
error = True |
| 601 |
# error if both fixed_qos_options or adaptive_qos_options not present in creating qos policy. |
| 602 |
elif self.parameters.get('fixed_qos_options', self.parameters.get('adaptive_qos_options')) is None: |
| 603 |
error = True |
| 604 |
return error |
| 605 |
|
| 606 |
def apply(self): |
| 607 |
""" |
| 608 |
Run module based on playbook |
| 609 |
""" |
| 610 |
current = self.get_policy_group() |
| 611 |
rename, cd_action = None, None |
| 612 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 613 |
if cd_action == 'create' and self.parameters.get('from_name'): |
| 614 |
# create policy by renaming an existing one |
| 615 |
old_policy = self.get_policy_group(self.parameters['from_name']) |
| 616 |
rename = self.na_helper.is_rename_action(old_policy, current) |
| 617 |
if rename: |
| 618 |
current = old_policy |
| 619 |
cd_action = None |
| 620 |
if rename is None: |
| 621 |
self.module.fail_json(msg='Error renaming qos policy group: cannot find %s' % |
| 622 |
self.parameters['from_name']) |
| 623 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) if cd_action is None else {} |
| 624 |
if 'is_shared' in modify or self.na_helper.safe_get(modify, ['fixed_qos_options', 'capacity_shared']) is not None: |
| 625 |
self.module.fail_json(msg="Error cannot modify '%s' attribute." % |
| 626 |
('is_shared' if 'is_shared' in modify else 'fixed_qos_options.capacity_shared')) |
| 627 |
if self.use_rest and cd_action == 'create' and self.validate_adaptive_or_fixed_qos_options(): |
| 628 |
error = "Error: atleast one throughput in 'fixed_qos_options' or all 'adaptive_qos_options' required in creating qos_policy in REST." |
| 629 |
self.module.fail_json(msg=error) |
| 630 |
if self.na_helper.changed and not self.module.check_mode: |
| 631 |
if rename: |
| 632 |
self.rename_policy_group() |
| 633 |
if cd_action == 'create': |
| 634 |
self.create_policy_group() |
| 635 |
elif cd_action == 'delete': |
| 636 |
self.delete_policy_group() |
| 637 |
elif modify: |
| 638 |
self.modify_helper(modify) |
| 639 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 640 |
self.module.exit_json(**result) |
| 641 |
|
| 642 |
|
| 643 |
def main(): |
| 644 |
'''Apply vserver operations from playbook''' |
| 645 |
qos_policy_group = NetAppOntapQosPolicyGroup() |
| 646 |
qos_policy_group.apply() |
| 647 |
|
| 648 |
|
| 649 |
if __name__ == '__main__': |
| 650 |
main() |