| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2018-2025, 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 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
module: na_ontap_qos_adaptive_policy_group |
| 13 |
short_description: NetApp ONTAP Adaptive Quality of Service policy group. |
| 14 |
extends_documentation_fragment: |
| 15 |
- netapp.ontap.netapp.na_ontap_zapi |
| 16 |
version_added: 2.9.0 |
| 17 |
author: NetApp Ansible Team (@joshedmonds) <[email protected]> |
| 18 |
|
| 19 |
description: |
| 20 |
- Create, destroy, modify, or rename an Adaptive QoS policy group on NetApp ONTAP. Module is based on the standard QoS policy group module. |
| 21 |
|
| 22 |
options: |
| 23 |
state: |
| 24 |
choices: ['present', 'absent'] |
| 25 |
description: |
| 26 |
- Whether the specified policy group should exist or not. |
| 27 |
default: 'present' |
| 28 |
type: str |
| 29 |
|
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of the policy group to manage. |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
|
| 36 |
vserver: |
| 37 |
description: |
| 38 |
- Name of the vserver to use. |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
|
| 42 |
from_name: |
| 43 |
description: |
| 44 |
- Name of the existing policy group to be renamed to name. |
| 45 |
type: str |
| 46 |
|
| 47 |
absolute_min_iops: |
| 48 |
description: |
| 49 |
- Absolute minimum IOPS defined by this policy. |
| 50 |
type: str |
| 51 |
|
| 52 |
expected_iops: |
| 53 |
description: |
| 54 |
- Minimum expected IOPS defined by this policy. |
| 55 |
type: str |
| 56 |
|
| 57 |
peak_iops: |
| 58 |
description: |
| 59 |
- Maximum possible IOPS per allocated or used TB|GB. |
| 60 |
type: str |
| 61 |
|
| 62 |
peak_iops_allocation: |
| 63 |
choices: ['allocated_space', 'used_space'] |
| 64 |
description: |
| 65 |
- Whether peak_iops is specified by allocated or used space. |
| 66 |
default: 'used_space' |
| 67 |
type: str |
| 68 |
|
| 69 |
force: |
| 70 |
type: bool |
| 71 |
default: False |
| 72 |
description: |
| 73 |
- Setting to 'true' forces the deletion of the workloads associated with the policy group along with the policy group. |
| 74 |
''' |
| 75 |
|
| 76 |
EXAMPLES = """ |
| 77 |
- name: Create adaptive qos policy group |
| 78 |
netapp.ontap.na_ontap_qos_adaptive_policy_group: |
| 79 |
state: present |
| 80 |
name: aq_policy_1 |
| 81 |
vserver: policy_vserver |
| 82 |
absolute_min_iops: 70IOPS |
| 83 |
expected_iops: 100IOPS/TB |
| 84 |
peak_iops: 250IOPS/TB |
| 85 |
peak_iops_allocation: allocated_space |
| 86 |
hostname: "{{ netapp_hostname }}" |
| 87 |
username: "{{ netapp_username }}" |
| 88 |
password: "{{ netapp_password }}" |
| 89 |
|
| 90 |
- name: Modify adaptive qos policy group expected iops |
| 91 |
netapp.ontap.na_ontap_qos_adaptive_policy_group: |
| 92 |
state: present |
| 93 |
name: aq_policy_1 |
| 94 |
vserver: policy_vserver |
| 95 |
absolute_min_iops: 70IOPS |
| 96 |
expected_iops: 125IOPS/TB |
| 97 |
peak_iops: 250IOPS/TB |
| 98 |
peak_iops_allocation: allocated_space |
| 99 |
hostname: "{{ netapp_hostname }}" |
| 100 |
username: "{{ netapp_username }}" |
| 101 |
password: "{{ netapp_password }}" |
| 102 |
|
| 103 |
- name: Modify adaptive qos policy group peak iops allocation |
| 104 |
netapp.ontap.na_ontap_qos_adaptive_policy_group: |
| 105 |
state: present |
| 106 |
name: aq_policy_1 |
| 107 |
vserver: policy_vserver |
| 108 |
absolute_min_iops: 70IOPS |
| 109 |
expected_iops: 125IOPS/TB |
| 110 |
peak_iops: 250IOPS/TB |
| 111 |
peak_iops_allocation: used_space |
| 112 |
hostname: "{{ netapp_hostname }}" |
| 113 |
username: "{{ netapp_username }}" |
| 114 |
password: "{{ netapp_password }}" |
| 115 |
|
| 116 |
- name: Delete qos policy group |
| 117 |
netapp.ontap.na_ontap_qos_adaptive_policy_group: |
| 118 |
state: absent |
| 119 |
name: aq_policy_1 |
| 120 |
vserver: policy_vserver |
| 121 |
hostname: "{{ netapp_hostname }}" |
| 122 |
username: "{{ netapp_username }}" |
| 123 |
password: "{{ netapp_password }}" |
| 124 |
""" |
| 125 |
|
| 126 |
RETURN = """ |
| 127 |
""" |
| 128 |
|
| 129 |
import traceback |
| 130 |
|
| 131 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 132 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 133 |
from ansible.module_utils.basic import AnsibleModule |
| 134 |
from ansible.module_utils._text import to_native |
| 135 |
|
| 136 |
|
| 137 |
class NetAppOntapAdaptiveQosPolicyGroup: |
| 138 |
""" |
| 139 |
Create, delete, modify and rename a policy group. |
| 140 |
""" |
| 141 |
def __init__(self): |
| 142 |
""" |
| 143 |
Initialize the Ontap qos policy group class. |
| 144 |
""" |
| 145 |
self.argument_spec = netapp_utils.na_ontap_zapi_only_spec() |
| 146 |
self.argument_spec.update(dict( |
| 147 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 148 |
name=dict(required=True, type='str'), |
| 149 |
from_name=dict(required=False, type='str'), |
| 150 |
vserver=dict(required=True, type='str'), |
| 151 |
absolute_min_iops=dict(required=False, type='str'), |
| 152 |
expected_iops=dict(required=False, type='str'), |
| 153 |
peak_iops=dict(required=False, type='str'), |
| 154 |
peak_iops_allocation=dict(choices=['allocated_space', 'used_space'], default='used_space'), |
| 155 |
force=dict(required=False, type='bool', default=False) |
| 156 |
)) |
| 157 |
|
| 158 |
self.module = AnsibleModule( |
| 159 |
argument_spec=self.argument_spec, |
| 160 |
supports_check_mode=True |
| 161 |
) |
| 162 |
self.na_helper = NetAppModule() |
| 163 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 164 |
self.na_helper.module_replaces('na_ontap_qos_policy_group', self.module) |
| 165 |
msg = 'The module only supports ZAPI; refer to netapp.ontap.na_ontap_qos_policy_group module for RESTful equivalent.' |
| 166 |
self.na_helper.fall_back_to_zapi(self.module, msg, self.parameters) |
| 167 |
|
| 168 |
if not netapp_utils.has_netapp_lib(): |
| 169 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 170 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module) |
| 171 |
|
| 172 |
def get_policy_group(self, policy_group_name=None): |
| 173 |
""" |
| 174 |
Return details of a policy group. |
| 175 |
:param policy_group_name: policy group name |
| 176 |
:return: policy group details. |
| 177 |
:rtype: dict. |
| 178 |
""" |
| 179 |
if policy_group_name is None: |
| 180 |
policy_group_name = self.parameters['name'] |
| 181 |
policy_group_get_iter = netapp_utils.zapi.NaElement('qos-adaptive-policy-group-get-iter') |
| 182 |
policy_group_info = netapp_utils.zapi.NaElement('qos-adaptive-policy-group-info') |
| 183 |
policy_group_info.add_new_child('policy-group', policy_group_name) |
| 184 |
policy_group_info.add_new_child('vserver', self.parameters['vserver']) |
| 185 |
query = netapp_utils.zapi.NaElement('query') |
| 186 |
query.add_child_elem(policy_group_info) |
| 187 |
policy_group_get_iter.add_child_elem(query) |
| 188 |
result = self.server.invoke_successfully(policy_group_get_iter, True) |
| 189 |
policy_group_detail = None |
| 190 |
|
| 191 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) == 1: |
| 192 |
policy_info = result.get_child_by_name('attributes-list').get_child_by_name('qos-adaptive-policy-group-info') |
| 193 |
|
| 194 |
policy_group_detail = { |
| 195 |
'name': policy_info.get_child_content('policy-group'), |
| 196 |
'vserver': policy_info.get_child_content('vserver'), |
| 197 |
'absolute_min_iops': policy_info.get_child_content('absolute-min-iops'), |
| 198 |
'expected_iops': policy_info.get_child_content('expected-iops'), |
| 199 |
'peak_iops': policy_info.get_child_content('peak-iops'), |
| 200 |
'peak_iops_allocation': policy_info.get_child_content('peak-iops-allocation') |
| 201 |
} |
| 202 |
return policy_group_detail |
| 203 |
|
| 204 |
def create_policy_group(self): |
| 205 |
""" |
| 206 |
create a policy group name. |
| 207 |
""" |
| 208 |
policy_group = netapp_utils.zapi.NaElement('qos-adaptive-policy-group-create') |
| 209 |
policy_group.add_new_child('policy-group', self.parameters['name']) |
| 210 |
policy_group.add_new_child('vserver', self.parameters['vserver']) |
| 211 |
if self.parameters.get('absolute_min_iops'): |
| 212 |
policy_group.add_new_child('absolute-min-iops', self.parameters['absolute_min_iops']) |
| 213 |
if self.parameters.get('expected_iops'): |
| 214 |
policy_group.add_new_child('expected-iops', self.parameters['expected_iops']) |
| 215 |
if self.parameters.get('peak_iops'): |
| 216 |
policy_group.add_new_child('peak-iops', self.parameters['peak_iops']) |
| 217 |
if self.parameters.get('peak_iops_allocation'): |
| 218 |
policy_group.add_new_child('peak-iops-allocation', self.parameters['peak_iops_allocation']) |
| 219 |
try: |
| 220 |
self.server.invoke_successfully(policy_group, True) |
| 221 |
except netapp_utils.zapi.NaApiError as error: |
| 222 |
self.module.fail_json(msg='Error creating adaptive qos policy group %s: %s' % |
| 223 |
(self.parameters['name'], to_native(error)), |
| 224 |
exception=traceback.format_exc()) |
| 225 |
|
| 226 |
def delete_policy_group(self, policy_group=None): |
| 227 |
""" |
| 228 |
delete an existing policy group. |
| 229 |
:param policy_group: policy group name. |
| 230 |
""" |
| 231 |
if policy_group is None: |
| 232 |
policy_group = self.parameters['name'] |
| 233 |
policy_group_obj = netapp_utils.zapi.NaElement('qos-adaptive-policy-group-delete') |
| 234 |
policy_group_obj.add_new_child('policy-group', policy_group) |
| 235 |
if self.parameters.get('force'): |
| 236 |
policy_group_obj.add_new_child('force', str(self.parameters['force'])) |
| 237 |
try: |
| 238 |
self.server.invoke_successfully(policy_group_obj, True) |
| 239 |
except netapp_utils.zapi.NaApiError as error: |
| 240 |
self.module.fail_json(msg='Error deleting adaptive qos policy group %s: %s' % |
| 241 |
(policy_group, to_native(error)), |
| 242 |
exception=traceback.format_exc()) |
| 243 |
|
| 244 |
def modify_policy_group(self): |
| 245 |
""" |
| 246 |
Modify policy group. |
| 247 |
""" |
| 248 |
policy_group_obj = netapp_utils.zapi.NaElement('qos-adaptive-policy-group-modify') |
| 249 |
policy_group_obj.add_new_child('policy-group', self.parameters['name']) |
| 250 |
if self.parameters.get('absolute_min_iops'): |
| 251 |
policy_group_obj.add_new_child('absolute-min-iops', self.parameters['absolute_min_iops']) |
| 252 |
if self.parameters.get('expected_iops'): |
| 253 |
policy_group_obj.add_new_child('expected-iops', self.parameters['expected_iops']) |
| 254 |
if self.parameters.get('peak_iops'): |
| 255 |
policy_group_obj.add_new_child('peak-iops', self.parameters['peak_iops']) |
| 256 |
if self.parameters.get('peak_iops_allocation'): |
| 257 |
policy_group_obj.add_new_child('peak-iops-allocation', self.parameters['peak_iops_allocation']) |
| 258 |
try: |
| 259 |
self.server.invoke_successfully(policy_group_obj, True) |
| 260 |
except netapp_utils.zapi.NaApiError as error: |
| 261 |
self.module.fail_json(msg='Error modifying adaptive qos policy group %s: %s' % |
| 262 |
(self.parameters['name'], to_native(error)), |
| 263 |
exception=traceback.format_exc()) |
| 264 |
|
| 265 |
def rename_policy_group(self): |
| 266 |
""" |
| 267 |
Rename policy group name. |
| 268 |
""" |
| 269 |
rename_obj = netapp_utils.zapi.NaElement('qos-adaptive-policy-group-rename') |
| 270 |
rename_obj.add_new_child('new-name', self.parameters['name']) |
| 271 |
rename_obj.add_new_child('policy-group-name', self.parameters['from_name']) |
| 272 |
try: |
| 273 |
self.server.invoke_successfully(rename_obj, True) |
| 274 |
except netapp_utils.zapi.NaApiError as error: |
| 275 |
self.module.fail_json(msg='Error renaming adaptive qos policy group %s: %s' % |
| 276 |
(self.parameters['from_name'], to_native(error)), |
| 277 |
exception=traceback.format_exc()) |
| 278 |
|
| 279 |
def modify_helper(self, modify): |
| 280 |
""" |
| 281 |
helper method to modify policy group. |
| 282 |
:param modify: modified attributes. |
| 283 |
""" |
| 284 |
for attribute in modify.keys(): |
| 285 |
if attribute in ['absolute_min_iops', 'expected_iops', 'peak_iops', 'peak_iops_allocation']: |
| 286 |
self.modify_policy_group() |
| 287 |
|
| 288 |
def apply(self): |
| 289 |
""" |
| 290 |
Run module based on playbook |
| 291 |
""" |
| 292 |
current, rename = self.get_policy_group(), None |
| 293 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 294 |
if cd_action == 'create' and self.parameters.get('from_name'): |
| 295 |
# form current with from_name. |
| 296 |
current = self.get_policy_group(self.parameters['from_name']) |
| 297 |
if current is None: |
| 298 |
self.module.fail_json(msg='Error: qos adaptive policy igroup with from_name=%s not found' % self.parameters.get('from_name')) |
| 299 |
# allow for rename and check for modify with current from from_name. |
| 300 |
rename, cd_action = True, None |
| 301 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) if cd_action is None else None |
| 302 |
if self.na_helper.changed and not self.module.check_mode: |
| 303 |
if rename: |
| 304 |
self.rename_policy_group() |
| 305 |
if cd_action == 'create': |
| 306 |
self.create_policy_group() |
| 307 |
elif cd_action == 'delete': |
| 308 |
self.delete_policy_group() |
| 309 |
elif modify: |
| 310 |
self.modify_helper(modify) |
| 311 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 312 |
self.module.exit_json(**result) |
| 313 |
|
| 314 |
|
| 315 |
def main(): |
| 316 |
'''Apply vserver operations from playbook''' |
| 317 |
qos_policy_group = NetAppOntapAdaptiveQosPolicyGroup() |
| 318 |
qos_policy_group.apply() |
| 319 |
|
| 320 |
|
| 321 |
if __name__ == '__main__': |
| 322 |
main() |