| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2019-2026, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
''' |
| 7 |
na_ontap_snapmirror_policy |
| 8 |
''' |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
|
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
DOCUMENTATION = """ |
| 15 |
module: na_ontap_snapmirror_policy |
| 16 |
short_description: NetApp ONTAP create, delete or modify SnapMirror policies |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: '20.3.0' |
| 20 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 21 |
description: |
| 22 |
- NetApp ONTAP create, modify, or destroy the SnapMirror policy |
| 23 |
- Add, modify and remove SnapMirror policy rules |
| 24 |
- Following parameters are not supported in REST; 'owner', 'restart', 'transfer_priority', 'tries', 'ignore_atime', 'common_snapshot_schedule' |
| 25 |
options: |
| 26 |
state: |
| 27 |
description: |
| 28 |
- Whether the specified SnapMirror policy should exist or not. |
| 29 |
choices: ['present', 'absent'] |
| 30 |
default: present |
| 31 |
type: str |
| 32 |
vserver: |
| 33 |
description: |
| 34 |
- Specifies the vserver for the SnapMirror policy. |
| 35 |
- Required with ZAPI. |
| 36 |
- Name of a data vserver with REST. |
| 37 |
- With current versions of ONTAP, when using REST, this must be set to the cluster name for cluster scoped policies (9.12.1 and older). |
| 38 |
- Current versions of ONTAP fail with "svm.uuid" is required when the vserver field is not set. |
| 39 |
- With newer versions of ONTAP, omit the value, or omit this option for a cluster scoped policy with REST. |
| 40 |
type: str |
| 41 |
policy_name: |
| 42 |
description: |
| 43 |
- Specifies the SnapMirror policy name. |
| 44 |
- C(name) added as an alias in 22.0.0. |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
aliases: ['name'] |
| 48 |
version_added: '22.0.0' |
| 49 |
policy_type: |
| 50 |
description: |
| 51 |
- Specifies the SnapMirror policy type. Modifying the type of an existing SnapMirror policy is not supported. |
| 52 |
- The Policy types 'sync' and 'async' are only supported in REST. |
| 53 |
choices: ['vault', 'async_mirror', 'mirror_vault', 'strict_sync_mirror', 'sync_mirror', 'sync', 'async'] |
| 54 |
type: str |
| 55 |
comment: |
| 56 |
description: |
| 57 |
- Specifies the SnapMirror policy comment. |
| 58 |
type: str |
| 59 |
tries: |
| 60 |
description: |
| 61 |
- Specifies the number of tries. |
| 62 |
- Not supported with REST. |
| 63 |
type: str |
| 64 |
transfer_priority: |
| 65 |
description: |
| 66 |
- Specifies the priority at which a SnapMirror transfer runs. |
| 67 |
- Not supported with REST. |
| 68 |
choices: ['low', 'normal'] |
| 69 |
type: str |
| 70 |
transfer_schedule: |
| 71 |
description: |
| 72 |
- Specifies the name of the schedule used to update asynchronous SnapMirror relationships. |
| 73 |
- Not supported with ZAPI. |
| 74 |
type: str |
| 75 |
version_added: '22.2.0' |
| 76 |
common_snapshot_schedule: |
| 77 |
description: |
| 78 |
- Specifies the common Snapshot copy schedule associated with the policy, only required for strict_sync_mirror and sync_mirror. |
| 79 |
- Not supported with REST. |
| 80 |
type: str |
| 81 |
owner: |
| 82 |
description: |
| 83 |
- Specifies the owner of the SnapMirror policy. |
| 84 |
- Not supported with REST. |
| 85 |
choices: ['cluster_admin', 'vserver_admin'] |
| 86 |
type: str |
| 87 |
is_network_compression_enabled: |
| 88 |
description: |
| 89 |
- Specifies whether network compression is enabled for transfers. |
| 90 |
type: bool |
| 91 |
ignore_atime: |
| 92 |
description: |
| 93 |
- Specifies whether incremental transfers will ignore files which have only their access time changed. Applies to SnapMirror vault relationships only. |
| 94 |
- Not supported with REST. |
| 95 |
type: bool |
| 96 |
restart: |
| 97 |
description: |
| 98 |
- Defines the behavior of SnapMirror if an interrupted transfer exists, applies to data protection only. |
| 99 |
- Not supported with REST. |
| 100 |
choices: ['always', 'never', 'default'] |
| 101 |
type: str |
| 102 |
snapmirror_label: |
| 103 |
description: |
| 104 |
- SnapMirror policy rule label. |
| 105 |
- Required when defining policy rules. |
| 106 |
- Use an empty list to remove all user-defined rules. |
| 107 |
type: list |
| 108 |
elements: str |
| 109 |
version_added: '20.7.0' |
| 110 |
keep: |
| 111 |
description: |
| 112 |
- SnapMirror policy rule retention count for snapshots created. |
| 113 |
- Required when defining policy rules. |
| 114 |
type: list |
| 115 |
elements: int |
| 116 |
version_added: '20.7.0' |
| 117 |
prefix: |
| 118 |
description: |
| 119 |
- SnapMirror policy rule prefix. |
| 120 |
- Optional when defining policy rules. |
| 121 |
- Set to '' to not set or remove an existing custom prefix. |
| 122 |
- Prefix name should be unique within the policy. |
| 123 |
- When specifying a custom prefix, schedule must also be specified. |
| 124 |
type: list |
| 125 |
elements: str |
| 126 |
version_added: '20.7.0' |
| 127 |
schedule: |
| 128 |
description: |
| 129 |
- SnapMirror policy rule schedule. |
| 130 |
- Optional when defining policy rules. |
| 131 |
- Set to '' to not set or remove a schedule. |
| 132 |
- When specifying a schedule a custom prefix can be set otherwise the prefix will be set to snapmirror_label. |
| 133 |
type: list |
| 134 |
elements: str |
| 135 |
version_added: '20.7.0' |
| 136 |
identity_preservation: |
| 137 |
description: |
| 138 |
- Specifies which configuration of the source SVM is replicated to the destination SVM. |
| 139 |
- This property is applicable only for SVM data protection with "async" policy type. |
| 140 |
- Only supported with REST. |
| 141 |
type: str |
| 142 |
choices: ['full', 'exclude_network_config', 'exclude_network_and_protocol_config'] |
| 143 |
version_added: '22.0.0' |
| 144 |
copy_all_source_snapshots: |
| 145 |
description: |
| 146 |
- Specifies whether all source Snapshot copies should be copied to the destination on a transfer rather than specifying specific retentions. |
| 147 |
- This property is applicable only to async policies. |
| 148 |
- Property can only be set to 'true'. |
| 149 |
- Only supported with REST and requires ONTAP 9.10.1 or later. |
| 150 |
type: bool |
| 151 |
version_added: '22.1.0' |
| 152 |
copy_latest_source_snapshot: |
| 153 |
description: |
| 154 |
- Specifies that the latest source Snapshot copy (created by SnapMirror before the transfer begins) should be copied to the destination on a transfer. |
| 155 |
- Retention properties cannot be specified along with this property. |
| 156 |
- Property can only be set to 'true'. |
| 157 |
- Only supported with REST and requires ONTAP 9.11.1 or later. |
| 158 |
type: bool |
| 159 |
version_added: '22.2.0' |
| 160 |
create_snapshot_on_source: |
| 161 |
description: |
| 162 |
- Specifies whether a new Snapshot copy should be created on the source at the beginning of an update or resync operation. |
| 163 |
- This property is applicable only to async policies. |
| 164 |
- Property can only be set to 'false'. |
| 165 |
- Only supported with REST and requires ONTAP 9.11.1 or later. |
| 166 |
type: bool |
| 167 |
version_added: '22.2.0' |
| 168 |
sync_type: |
| 169 |
description: |
| 170 |
- This property is only applicable to sync policy types. |
| 171 |
- If the "sync_type" is "sync" then a write success is returned to the client |
| 172 |
after writing the data to the primary endpoint and before writing the data to the secondary endpoint. |
| 173 |
- If the "sync_type" is "strict_sync" then a write success is returned to the client after writing the data to the both primary and secondary endpoints. |
| 174 |
- The "sync_type" of "automated_failover" can be associated with a SnapMirror relationship that has Consistency Group as the endpoint and |
| 175 |
it requires ONTAP 9.7 or later. |
| 176 |
- Only supported with REST. |
| 177 |
type: str |
| 178 |
choices: ['sync', 'strict_sync', 'automated_failover'] |
| 179 |
version_added: '22.2.0' |
| 180 |
lambda_config: |
| 181 |
description: |
| 182 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 183 |
- These option and suboptions are only supported with REST. |
| 184 |
type: dict |
| 185 |
version_added: 23.5.0 |
| 186 |
suboptions: |
| 187 |
function_name: |
| 188 |
description: |
| 189 |
- The name of the AWS Lambda function to invoke. |
| 190 |
type: str |
| 191 |
required: true |
| 192 |
aws_region: |
| 193 |
description: |
| 194 |
- The name of the AWS region. |
| 195 |
type: str |
| 196 |
required: true |
| 197 |
aws_profile: |
| 198 |
description: |
| 199 |
- The name of the AWS profile to use for authentication. |
| 200 |
type: str |
| 201 |
|
| 202 |
notes: |
| 203 |
- In REST, policy types 'mirror_vault', 'vault' and 'async_mirror' are mapped to 'async' policy_type. |
| 204 |
- In REST, policy types 'sync_mirror' and 'strict_sync_mirror' are mapped to 'sync' policy_type. |
| 205 |
- In REST, use policy_type 'async' to configure 'mirror-vault' in CLI. |
| 206 |
- In REST, use policy_type 'async' with 'copy_all_source_snapshots' to configure 'async-mirror' with |
| 207 |
'all_source_snapshots' in CLI. |
| 208 |
- In REST, use policy_type 'async' with 'copy_latest_source_snapshot' to configure 'async-mirror' without |
| 209 |
'all_source_snapshots' in CLI. |
| 210 |
- In REST, use policy_type 'async' with 'create_snapshot_on_source' to configure 'vault' in CLI. |
| 211 |
- In REST, use policy_type 'sync' with sync_type 'sync' to configure 'sync-mirror' in CLI. |
| 212 |
- In REST, use policy_type 'sync' with sync_type 'strict_sync' to configure 'strict-sync-mirror' in CLI. |
| 213 |
- In REST, use policy_type 'sync' with sync_type 'automated_failover' to configure 'automated-failover' in CLI. |
| 214 |
- Supports AWS Lambda proxy functionality when using REST. See the README file for examples. |
| 215 |
""" |
| 216 |
|
| 217 |
EXAMPLES = """ |
| 218 |
- name: Create SnapMirror policy |
| 219 |
netapp.ontap.na_ontap_snapmirror_policy: |
| 220 |
state: present |
| 221 |
vserver: "SVM1" |
| 222 |
policy_name: "ansible_policy" |
| 223 |
policy_type: "async_mirror" |
| 224 |
copy_latest_source_snapshot: true |
| 225 |
comment: "created by ansible" |
| 226 |
transfer_schedule: "daily" # when using REST |
| 227 |
hostname: "{{ netapp_hostname }}" |
| 228 |
username: "{{ netapp_username }}" |
| 229 |
password: "{{ netapp_password }}" |
| 230 |
https: true |
| 231 |
validate_certs: false |
| 232 |
|
| 233 |
- name: Modify SnapMirror policy |
| 234 |
netapp.ontap.na_ontap_snapmirror_policy: |
| 235 |
state: present |
| 236 |
vserver: "SVM1" |
| 237 |
policy_name: "ansible_policy" |
| 238 |
policy_type: "async_mirror" |
| 239 |
copy_latest_source_snapshot: true |
| 240 |
transfer_priority: "low" # when using ZAPI |
| 241 |
transfer_schedule: "weekly" # when using REST |
| 242 |
hostname: "{{ netapp_hostname }}" |
| 243 |
username: "{{ netapp_username }}" |
| 244 |
password: "{{ netapp_password }}" |
| 245 |
https: true |
| 246 |
validate_certs: false |
| 247 |
|
| 248 |
- name: Create SnapMirror policy with basic rules |
| 249 |
netapp.ontap.na_ontap_snapmirror_policy: |
| 250 |
state: present |
| 251 |
vserver: "SVM1" |
| 252 |
policy_name: "snapmirror_policy" |
| 253 |
policy_type: "async" |
| 254 |
snapmirror_label: ['daily', 'weekly', 'monthly'] |
| 255 |
keep: [7, 5, 12] |
| 256 |
hostname: "{{ netapp_hostname }}" |
| 257 |
username: "{{ netapp_username }}" |
| 258 |
password: "{{ netapp_password }}" |
| 259 |
https: true |
| 260 |
validate_certs: false |
| 261 |
|
| 262 |
- name: Modify SnapMirror policy with rules and schedules (no schedule for daily rule) |
| 263 |
netapp.ontap.na_ontap_snapmirror_policy: |
| 264 |
state: present |
| 265 |
vserver: "SVM1" |
| 266 |
policy_name: "snapmirror_policy" |
| 267 |
policy_type: "mirror_vault" |
| 268 |
snapmirror_label: ['daily', 'weekly', 'monthly'] |
| 269 |
keep: [7, 5, 12] |
| 270 |
schedule: ['', 'weekly', 'monthly'] |
| 271 |
prefix: ['', '', 'monthly_mv'] |
| 272 |
hostname: "{{ netapp_hostname }}" |
| 273 |
username: "{{ netapp_username }}" |
| 274 |
password: "{{ netapp_password }}" |
| 275 |
https: true |
| 276 |
validate_certs: false |
| 277 |
|
| 278 |
- name: Modify SnapMirror policy with rules, remove existing schedules and prefixes |
| 279 |
netapp.ontap.na_ontap_snapmirror_policy: |
| 280 |
state: present |
| 281 |
vserver: "SVM1" |
| 282 |
policy_name: "snapmirror_policy" |
| 283 |
policy_type: "mirror_vault" |
| 284 |
snapmirror_label: ['daily', 'weekly', 'monthly'] |
| 285 |
keep: [7, 5, 12] |
| 286 |
schedule: ['', '', ''] |
| 287 |
prefix: ['', '', ''] |
| 288 |
hostname: "{{ netapp_hostname }}" |
| 289 |
username: "{{ netapp_username }}" |
| 290 |
password: "{{ netapp_password }}" |
| 291 |
https: true |
| 292 |
validate_certs: false |
| 293 |
|
| 294 |
- name: Modify SnapMirror policy, delete all rules (excludes builtin rules) |
| 295 |
netapp.ontap.na_ontap_snapmirror_policy: |
| 296 |
state: present |
| 297 |
vserver: "SVM1" |
| 298 |
policy_name: "snapmirror_policy" |
| 299 |
policy_type: "mirror_vault" |
| 300 |
snapmirror_label: [] |
| 301 |
hostname: "{{ netapp_hostname }}" |
| 302 |
username: "{{ netapp_username }}" |
| 303 |
password: "{{ netapp_password }}" |
| 304 |
https: true |
| 305 |
validate_certs: false |
| 306 |
|
| 307 |
- name: Delete SnapMirror policy |
| 308 |
netapp.ontap.na_ontap_snapmirror_policy: |
| 309 |
state: absent |
| 310 |
vserver: "SVM1" |
| 311 |
policy_name: "ansible_policy" |
| 312 |
hostname: "{{ netapp_hostname }}" |
| 313 |
username: "{{ netapp_username }}" |
| 314 |
password: "{{ netapp_password }}" |
| 315 |
https: true |
| 316 |
validate_certs: false |
| 317 |
""" |
| 318 |
|
| 319 |
RETURN = """ |
| 320 |
""" |
| 321 |
|
| 322 |
import traceback |
| 323 |
|
| 324 |
from ansible.module_utils.basic import AnsibleModule |
| 325 |
from ansible.module_utils._text import to_native |
| 326 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 327 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 328 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic |
| 329 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_vserver |
| 330 |
|
| 331 |
|
| 332 |
class NetAppOntapSnapMirrorPolicy: |
| 333 |
""" |
| 334 |
Create, Modifies and Destroys a SnapMirror policy |
| 335 |
""" |
| 336 |
def __init__(self): |
| 337 |
""" |
| 338 |
Initialize the Ontap SnapMirror policy class |
| 339 |
""" |
| 340 |
|
| 341 |
self.use_rest = False |
| 342 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 343 |
self.argument_spec.update(dict( |
| 344 |
state=dict(required=False, choices=['present', 'absent'], default='present'), |
| 345 |
vserver=dict(required=False, type='str'), |
| 346 |
policy_name=dict(required=True, type='str', aliases=['name']), |
| 347 |
comment=dict(required=False, type='str'), |
| 348 |
policy_type=dict(required=False, type='str', |
| 349 |
choices=['vault', 'async_mirror', 'mirror_vault', 'strict_sync_mirror', 'sync_mirror', 'sync', 'async']), |
| 350 |
tries=dict(required=False, type='str'), |
| 351 |
transfer_priority=dict(required=False, type='str', choices=['low', 'normal']), |
| 352 |
transfer_schedule=dict(required=False, type='str'), |
| 353 |
common_snapshot_schedule=dict(required=False, type='str'), |
| 354 |
ignore_atime=dict(required=False, type='bool'), |
| 355 |
is_network_compression_enabled=dict(required=False, type='bool'), |
| 356 |
owner=dict(required=False, type='str', choices=['cluster_admin', 'vserver_admin']), |
| 357 |
restart=dict(required=False, type='str', choices=['always', 'never', 'default']), |
| 358 |
snapmirror_label=dict(required=False, type="list", elements="str"), |
| 359 |
keep=dict(required=False, type="list", elements="int"), |
| 360 |
prefix=dict(required=False, type="list", elements="str"), |
| 361 |
schedule=dict(required=False, type="list", elements="str"), |
| 362 |
identity_preservation=dict(required=False, type="str", choices=['full', 'exclude_network_config', 'exclude_network_and_protocol_config']), |
| 363 |
copy_all_source_snapshots=dict(required=False, type='bool'), |
| 364 |
copy_latest_source_snapshot=dict(required=False, type='bool'), |
| 365 |
create_snapshot_on_source=dict(required=False, type='bool'), |
| 366 |
sync_type=dict(required=False, type="str", choices=['sync', 'strict_sync', 'automated_failover']), |
| 367 |
)) |
| 368 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 369 |
self.module = AnsibleModule( |
| 370 |
argument_spec=self.argument_spec, |
| 371 |
supports_check_mode=True, |
| 372 |
mutually_exclusive=[('copy_all_source_snapshots', 'copy_latest_source_snapshot', 'create_snapshot_on_source', 'sync_type')], |
| 373 |
required_if=[('use_lambda', True, ('lambda_config',))], |
| 374 |
) |
| 375 |
|
| 376 |
# set up variables |
| 377 |
self.na_helper = NetAppModule() |
| 378 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 379 |
|
| 380 |
# API should be used for ONTAP 9.6 or higher, Zapi for lower version |
| 381 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 382 |
# some attributes are not supported in earlier REST implementation |
| 383 |
unsupported_rest_properties = ['owner', 'restart', 'transfer_priority', 'tries', 'ignore_atime', |
| 384 |
'common_snapshot_schedule'] |
| 385 |
partially_supported_rest_properties = [['copy_all_source_snapshots', (9, 10, 1)], ['copy_latest_source_snapshot', (9, 11, 1)], |
| 386 |
['create_snapshot_on_source', (9, 11, 1)]] |
| 387 |
self.unsupported_zapi_properties = ['identity_preservation', 'copy_all_source_snapshots', 'copy_latest_source_snapshot', 'sync_type', |
| 388 |
'transfer_schedule'] |
| 389 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties, partially_supported_rest_properties) |
| 390 |
self.validate_policy_type() |
| 391 |
if self.use_rest: |
| 392 |
self.scope = self.set_scope() |
| 393 |
else: |
| 394 |
if self.parameters.get('use_lambda'): |
| 395 |
self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.") |
| 396 |
if not netapp_utils.has_netapp_lib(): |
| 397 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 398 |
for unsupported_zapi_property in self.unsupported_zapi_properties: |
| 399 |
if self.parameters.get(unsupported_zapi_property) is not None: |
| 400 |
msg = "Error: %s option is not supported with ZAPI. It can only be used with REST." % unsupported_zapi_property |
| 401 |
self.module.fail_json(msg=msg) |
| 402 |
if 'vserver' not in self.parameters: |
| 403 |
self.module.fail_json(msg="Error: vserver is a required parameter when using ZAPI.") |
| 404 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 405 |
|
| 406 |
def set_scope(self): |
| 407 |
if self.parameters.get('vserver') is None: |
| 408 |
return 'cluster' |
| 409 |
record, error = rest_vserver.get_vserver(self.rest_api, self.parameters['vserver']) |
| 410 |
if error: |
| 411 |
self.module.fail_json(msg='Error getting vserver %s info: %s' % (self.parameters['vserver'], error)) |
| 412 |
if record: |
| 413 |
return 'svm' |
| 414 |
self.module.warn("vserver %s is not a data vserver, assuming cluster scope" % self.parameters['vserver']) |
| 415 |
return 'cluster' |
| 416 |
|
| 417 |
def get_snapmirror_policy(self): |
| 418 |
if self.use_rest: |
| 419 |
return self.get_snapmirror_policy_rest() |
| 420 |
|
| 421 |
snapmirror_policy_get_iter = netapp_utils.zapi.NaElement('snapmirror-policy-get-iter') |
| 422 |
snapmirror_policy_info = netapp_utils.zapi.NaElement('snapmirror-policy-info') |
| 423 |
snapmirror_policy_info.add_new_child('policy-name', self.parameters['policy_name']) |
| 424 |
snapmirror_policy_info.add_new_child('vserver', self.parameters['vserver']) |
| 425 |
query = netapp_utils.zapi.NaElement('query') |
| 426 |
query.add_child_elem(snapmirror_policy_info) |
| 427 |
snapmirror_policy_get_iter.add_child_elem(query) |
| 428 |
|
| 429 |
try: |
| 430 |
result = self.server.invoke_successfully(snapmirror_policy_get_iter, True) |
| 431 |
except netapp_utils.zapi.NaApiError as error: |
| 432 |
if 'NetApp API failed. Reason - 13001:' in to_native(error): |
| 433 |
# policy does not exist |
| 434 |
return None |
| 435 |
self.module.fail_json(msg='Error getting snapmirror policy %s: %s' % (self.parameters['policy_name'], to_native(error)), |
| 436 |
exception=traceback.format_exc()) |
| 437 |
|
| 438 |
return_value = None |
| 439 |
if result and result.get_child_by_name('attributes-list'): |
| 440 |
snapmirror_policy_attributes = result['attributes-list']['snapmirror-policy-info'] |
| 441 |
|
| 442 |
return_value = { |
| 443 |
'policy_name': snapmirror_policy_attributes['policy-name'], |
| 444 |
'tries': snapmirror_policy_attributes['tries'], |
| 445 |
'transfer_priority': snapmirror_policy_attributes['transfer-priority'], |
| 446 |
'is_network_compression_enabled': self.na_helper.get_value_for_bool(True, |
| 447 |
snapmirror_policy_attributes['is-network-compression-enabled']), |
| 448 |
'restart': snapmirror_policy_attributes['restart'], |
| 449 |
'ignore_atime': self.na_helper.get_value_for_bool(True, snapmirror_policy_attributes['ignore-atime']), |
| 450 |
'vserver': snapmirror_policy_attributes['vserver-name'], |
| 451 |
'comment': '', |
| 452 |
'snapmirror_label': [], |
| 453 |
'keep': [], |
| 454 |
'prefix': [], |
| 455 |
'schedule': [], |
| 456 |
} |
| 457 |
if snapmirror_policy_attributes.get_child_content('comment') is not None: |
| 458 |
return_value['comment'] = snapmirror_policy_attributes['comment'] |
| 459 |
|
| 460 |
if snapmirror_policy_attributes.get_child_content('type') is not None: |
| 461 |
return_value['policy_type'] = snapmirror_policy_attributes['type'] |
| 462 |
|
| 463 |
if snapmirror_policy_attributes.get_child_content('common-snapshot-schedule') is not None: |
| 464 |
return_value['common_snapshot_schedule'] = snapmirror_policy_attributes['common-snapshot-schedule'] |
| 465 |
|
| 466 |
if snapmirror_policy_attributes.get_child_by_name('snapmirror-policy-rules'): |
| 467 |
for rule in snapmirror_policy_attributes['snapmirror-policy-rules'].get_children(): |
| 468 |
# Ignore builtin rules |
| 469 |
if rule.get_child_content('snapmirror-label') in ["sm_created", "all_source_snapshots"]: |
| 470 |
continue |
| 471 |
|
| 472 |
return_value['snapmirror_label'].append(rule.get_child_content('snapmirror-label')) |
| 473 |
return_value['keep'].append(int(rule.get_child_content('keep'))) |
| 474 |
|
| 475 |
prefix = rule.get_child_content('prefix') |
| 476 |
if prefix is None or prefix == '-': |
| 477 |
prefix = '' |
| 478 |
return_value['prefix'].append(prefix) |
| 479 |
|
| 480 |
schedule = rule.get_child_content('schedule') |
| 481 |
if schedule is None or schedule == '-': |
| 482 |
schedule = '' |
| 483 |
return_value['schedule'].append(schedule) |
| 484 |
|
| 485 |
return return_value |
| 486 |
|
| 487 |
def get_snapmirror_policy_rest(self): |
| 488 |
query = {'fields': 'uuid,name,svm.name,comment,network_compression_enabled,type,retention,identity_preservation,sync_type,transfer_schedule,', |
| 489 |
'name': self.parameters['policy_name'], |
| 490 |
'scope': self.scope} |
| 491 |
if self.scope == 'svm': |
| 492 |
query['svm.name'] = self.parameters['vserver'] |
| 493 |
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 10, 1): |
| 494 |
query['fields'] += 'copy_all_source_snapshots,' |
| 495 |
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 11, 1): |
| 496 |
query['fields'] += 'copy_latest_source_snapshot,create_snapshot_on_source' |
| 497 |
api = "snapmirror/policies" |
| 498 |
record, error = rest_generic.get_one_record(self.rest_api, api, query) |
| 499 |
if error: |
| 500 |
self.module.fail_json(msg='Error getting snapmirror policy: %s' % error) |
| 501 |
return self.format_record(record) if record else None |
| 502 |
|
| 503 |
def format_record(self, record): |
| 504 |
return_value = { |
| 505 |
'uuid': record['uuid'], |
| 506 |
'vserver': self.na_helper.safe_get(record, ['svm', 'name']), |
| 507 |
'policy_name': record['name'], |
| 508 |
'comment': '', |
| 509 |
'is_network_compression_enabled': False, |
| 510 |
'snapmirror_label': [], |
| 511 |
'keep': [], |
| 512 |
'prefix': [], |
| 513 |
'schedule': [], |
| 514 |
'identity_preservation': '', |
| 515 |
'copy_all_source_snapshots': False, |
| 516 |
'copy_latest_source_snapshot': False, |
| 517 |
'transfer_schedule': '', |
| 518 |
} |
| 519 |
if 'type' in record: |
| 520 |
return_value['policy_type'] = record['type'] |
| 521 |
if 'network_compression_enabled' in record: |
| 522 |
return_value['is_network_compression_enabled'] = record['network_compression_enabled'] |
| 523 |
if 'comment' in record: |
| 524 |
return_value['comment'] = record['comment'] |
| 525 |
if 'retention' in record: |
| 526 |
for rule in record['retention']: |
| 527 |
return_value['snapmirror_label'].append(rule['label']) |
| 528 |
return_value['keep'].append(int(rule['count'])) |
| 529 |
if 'prefix' in rule and rule['prefix'] != '-': |
| 530 |
return_value['prefix'].append(rule['prefix']) |
| 531 |
else: |
| 532 |
return_value['prefix'].append('') |
| 533 |
if 'creation_schedule' in rule and rule['creation_schedule']['name'] != '-': |
| 534 |
return_value['schedule'].append(rule['creation_schedule']['name']) |
| 535 |
else: |
| 536 |
return_value['schedule'].append('') |
| 537 |
if 'identity_preservation' in record: |
| 538 |
return_value['identity_preservation'] = record['identity_preservation'] |
| 539 |
if 'sync_type' in record: |
| 540 |
return_value['sync_type'] = record['sync_type'] |
| 541 |
if 'copy_all_source_snapshots' in record: |
| 542 |
return_value['copy_all_source_snapshots'] = record['copy_all_source_snapshots'] |
| 543 |
if 'copy_latest_source_snapshot' in record: |
| 544 |
return_value['copy_latest_source_snapshot'] = record['copy_latest_source_snapshot'] |
| 545 |
if 'create_snapshot_on_source' in record: |
| 546 |
return_value['create_snapshot_on_source'] = record['create_snapshot_on_source'] |
| 547 |
if 'transfer_schedule' in record: |
| 548 |
return_value['transfer_schedule'] = record['transfer_schedule']['name'] |
| 549 |
return return_value |
| 550 |
|
| 551 |
def validate_parameters(self): |
| 552 |
""" |
| 553 |
Validate snapmirror policy rules |
| 554 |
:return: None |
| 555 |
""" |
| 556 |
|
| 557 |
# For snapmirror policy rules, 'snapmirror_label' is required. |
| 558 |
if 'snapmirror_label' in self.parameters: |
| 559 |
|
| 560 |
# Check size of 'snapmirror_label' list is 0-10. Can have zero rules. |
| 561 |
# Take builtin 'sm_created' rule into account for 'mirror_vault'. |
| 562 |
if (('policy_type' in self.parameters and self.parameters['policy_type'] == 'mirror_vault' and len(self.parameters['snapmirror_label']) > 9) |
| 563 |
or len(self.parameters['snapmirror_label']) > 10): |
| 564 |
self.module.fail_json(msg="Error: A SnapMirror Policy can have up to a maximum of " |
| 565 |
"10 rules (including builtin rules), with a 'keep' value " |
| 566 |
"representing the maximum number of Snapshot copies for each rule") |
| 567 |
|
| 568 |
# 'keep' must be supplied as long as there is at least one snapmirror_label |
| 569 |
if len(self.parameters['snapmirror_label']) > 0 and 'keep' not in self.parameters: |
| 570 |
self.module.fail_json(msg="Error: Missing 'keep' parameter. When specifying the " |
| 571 |
"'snapmirror_label' parameter, the 'keep' parameter must " |
| 572 |
"also be supplied") |
| 573 |
|
| 574 |
# Make sure other rule values match same number of 'snapmirror_label' values. |
| 575 |
for rule_parameter in ['keep', 'prefix', 'schedule']: |
| 576 |
if rule_parameter in self.parameters: |
| 577 |
if len(self.parameters['snapmirror_label']) > len(self.parameters[rule_parameter]): |
| 578 |
self.module.fail_json(msg="Error: Each 'snapmirror_label' value must have " |
| 579 |
"an accompanying '%s' value" % rule_parameter) |
| 580 |
if len(self.parameters[rule_parameter]) > len(self.parameters['snapmirror_label']): |
| 581 |
self.module.fail_json(msg="Error: Each '%s' value must have an accompanying " |
| 582 |
"'snapmirror_label' value" % rule_parameter) |
| 583 |
else: |
| 584 |
# 'snapmirror_label' not supplied. |
| 585 |
# Bail out if other rule parameters have been supplied. |
| 586 |
for rule_parameter in ['keep', 'prefix', 'schedule']: |
| 587 |
if rule_parameter in self.parameters: |
| 588 |
self.module.fail_json(msg="Error: Missing 'snapmirror_label' parameter. When " |
| 589 |
"specifying the '%s' parameter, the 'snapmirror_label' " |
| 590 |
"parameter must also be supplied" % rule_parameter) |
| 591 |
|
| 592 |
# Schedule must be supplied if prefix is supplied. |
| 593 |
if 'prefix' in self.parameters and 'schedule' not in self.parameters: |
| 594 |
self.module.fail_json(msg="Error: Missing 'schedule' parameter. When " |
| 595 |
"specifying the 'prefix' parameter, the 'schedule' " |
| 596 |
"parameter must also be supplied") |
| 597 |
|
| 598 |
def create_snapmirror_policy(self, body=None): |
| 599 |
""" |
| 600 |
Creates a new storage efficiency policy |
| 601 |
""" |
| 602 |
if self.use_rest: |
| 603 |
api = "snapmirror/policies" |
| 604 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 605 |
if error: |
| 606 |
self.module.fail_json(msg='Error creating snapmirror policy: %s' % error) |
| 607 |
else: |
| 608 |
snapmirror_policy_obj = netapp_utils.zapi.NaElement("snapmirror-policy-create") |
| 609 |
snapmirror_policy_obj.add_new_child("policy-name", self.parameters['policy_name']) |
| 610 |
if 'policy_type' in self.parameters.keys(): |
| 611 |
snapmirror_policy_obj.add_new_child("type", self.parameters['policy_type']) |
| 612 |
snapmirror_policy_obj = self.create_snapmirror_policy_obj(snapmirror_policy_obj) |
| 613 |
|
| 614 |
try: |
| 615 |
self.server.invoke_successfully(snapmirror_policy_obj, True) |
| 616 |
except netapp_utils.zapi.NaApiError as error: |
| 617 |
self.module.fail_json(msg='Error creating snapmirror policy %s: %s' % (self.parameters['policy_name'], to_native(error)), |
| 618 |
exception=traceback.format_exc()) |
| 619 |
|
| 620 |
def create_snapmirror_policy_obj(self, snapmirror_policy_obj): |
| 621 |
if 'comment' in self.parameters.keys(): |
| 622 |
snapmirror_policy_obj.add_new_child("comment", self.parameters['comment']) |
| 623 |
if 'common_snapshot_schedule' in self.parameters.keys() and self.parameters['policy_type'] in ('sync_mirror', 'strict_sync_mirror'): |
| 624 |
snapmirror_policy_obj.add_new_child("common-snapshot-schedule", self.parameters['common_snapshot_schedule']) |
| 625 |
if 'ignore_atime' in self.parameters.keys(): |
| 626 |
snapmirror_policy_obj.add_new_child("ignore-atime", self.na_helper.get_value_for_bool(False, self.parameters['ignore_atime'])) |
| 627 |
if 'is_network_compression_enabled' in self.parameters.keys(): |
| 628 |
snapmirror_policy_obj.add_new_child("is-network-compression-enabled", |
| 629 |
self.na_helper.get_value_for_bool(False, self.parameters['is_network_compression_enabled'])) |
| 630 |
if 'owner' in self.parameters.keys(): |
| 631 |
snapmirror_policy_obj.add_new_child("owner", self.parameters['owner']) |
| 632 |
if 'restart' in self.parameters.keys(): |
| 633 |
snapmirror_policy_obj.add_new_child("restart", self.parameters['restart']) |
| 634 |
if 'transfer_priority' in self.parameters.keys(): |
| 635 |
snapmirror_policy_obj.add_new_child("transfer-priority", self.parameters['transfer_priority']) |
| 636 |
if 'tries' in self.parameters.keys(): |
| 637 |
snapmirror_policy_obj.add_new_child("tries", self.parameters['tries']) |
| 638 |
return snapmirror_policy_obj |
| 639 |
|
| 640 |
def build_body_for_create(self): |
| 641 |
|
| 642 |
body = {'name': self.parameters['policy_name']} |
| 643 |
if self.parameters.get('vserver') is not None: |
| 644 |
body['svm'] = {'name': self.parameters['vserver']} |
| 645 |
# if policy type is omitted, REST assumes async |
| 646 |
policy_type = 'async' |
| 647 |
if 'policy_type' in self.parameters: |
| 648 |
if 'async' in self.parameters['policy_type']: |
| 649 |
policy_type = 'async' |
| 650 |
elif 'sync' in self.parameters['policy_type']: |
| 651 |
policy_type = 'sync' |
| 652 |
body['sync_type'] = 'sync' |
| 653 |
if 'sync_type' in self.parameters: |
| 654 |
body['sync_type'] = self.parameters['sync_type'] |
| 655 |
body['type'] = policy_type |
| 656 |
if 'copy_all_source_snapshots' in self.parameters: |
| 657 |
body["copy_all_source_snapshots"] = self.parameters['copy_all_source_snapshots'] |
| 658 |
if 'copy_latest_source_snapshot' in self.parameters: |
| 659 |
body["copy_latest_source_snapshot"] = self.parameters['copy_latest_source_snapshot'] |
| 660 |
if 'create_snapshot_on_source' in self.parameters: |
| 661 |
# To set 'create_snapshot_on_source' as 'False' requires retention objects label(snapmirror_label) and count(keep) |
| 662 |
snapmirror_policy_retention_objs = [] |
| 663 |
for index, rule in enumerate(self.parameters['snapmirror_label']): |
| 664 |
retention = {'label': rule, 'count': str(self.parameters['keep'][index])} |
| 665 |
if 'prefix' in self.parameters and self.parameters['prefix'] != '': |
| 666 |
retention['prefix'] = self.parameters['prefix'][index] |
| 667 |
if 'schedule' in self.parameters and self.parameters['schedule'] != '': |
| 668 |
retention['creation_schedule'] = {'name': self.parameters['schedule'][index]} |
| 669 |
snapmirror_policy_retention_objs.append(retention) |
| 670 |
body['retention'] = snapmirror_policy_retention_objs |
| 671 |
body["create_snapshot_on_source"] = self.parameters['create_snapshot_on_source'] |
| 672 |
|
| 673 |
return self.build_body_for_create_or_modify(policy_type, body) |
| 674 |
|
| 675 |
def build_body_for_create_or_modify(self, policy_type, body=None): |
| 676 |
|
| 677 |
if body is None: |
| 678 |
body = {} |
| 679 |
if 'comment' in self.parameters.keys(): |
| 680 |
body["comment"] = self.parameters['comment'] |
| 681 |
if 'is_network_compression_enabled' in self.parameters: |
| 682 |
if policy_type == 'sync': |
| 683 |
self.module.fail_json(msg="Error: input parameter network_compression_enabled is not valid for SnapMirror policy type sync") |
| 684 |
body["network_compression_enabled"] = self.parameters['is_network_compression_enabled'] |
| 685 |
for option in ('identity_preservation', 'transfer_schedule'): |
| 686 |
if option in self.parameters: |
| 687 |
if policy_type == 'sync': |
| 688 |
self.module.fail_json(msg='Error: %s is only supported with async (async) policy_type, got: %s' |
| 689 |
% (option, self.parameters['policy_type'])) |
| 690 |
body[option] = self.parameters[option] |
| 691 |
return body |
| 692 |
|
| 693 |
def create_snapmirror_policy_retention_obj_for_rest(self, rules=None): |
| 694 |
""" |
| 695 |
Create SnapMirror policy retention REST object. |
| 696 |
:param list rules: e.g. [{'snapmirror_label': 'daily', 'keep': 7, 'prefix': 'daily', 'schedule': 'daily'}, ... ] |
| 697 |
:return: List of retention REST objects. |
| 698 |
e.g. [{'label': 'daily', 'count': 7, 'prefix': 'daily', 'creation_schedule': {'name': 'daily'}}, ... ] |
| 699 |
""" |
| 700 |
snapmirror_policy_retention_objs = [] |
| 701 |
if rules is not None: |
| 702 |
for rule in rules: |
| 703 |
retention = {'label': rule['snapmirror_label'], 'count': str(rule['keep'])} |
| 704 |
if 'prefix' in rule and rule['prefix'] != '': |
| 705 |
retention['prefix'] = rule['prefix'] |
| 706 |
if 'schedule' in rule and rule['schedule'] != '': |
| 707 |
retention['creation_schedule'] = {'name': rule['schedule']} |
| 708 |
snapmirror_policy_retention_objs.append(retention) |
| 709 |
return snapmirror_policy_retention_objs |
| 710 |
|
| 711 |
def delete_snapmirror_policy(self, uuid=None): |
| 712 |
""" |
| 713 |
Deletes a snapmirror policy |
| 714 |
""" |
| 715 |
if self.use_rest: |
| 716 |
api = "snapmirror/policies" |
| 717 |
dummy, error = rest_generic.delete_async(self.rest_api, api, uuid) |
| 718 |
if error: |
| 719 |
self.module.fail_json(msg='Error deleting snapmirror policy: %s' % error) |
| 720 |
else: |
| 721 |
snapmirror_policy_obj = netapp_utils.zapi.NaElement("snapmirror-policy-delete") |
| 722 |
snapmirror_policy_obj.add_new_child("policy-name", self.parameters['policy_name']) |
| 723 |
|
| 724 |
try: |
| 725 |
self.server.invoke_successfully(snapmirror_policy_obj, True) |
| 726 |
except netapp_utils.zapi.NaApiError as error: |
| 727 |
self.module.fail_json(msg='Error deleting snapmirror policy %s: %s' % (self.parameters['policy_name'], to_native(error)), |
| 728 |
exception=traceback.format_exc()) |
| 729 |
|
| 730 |
def modify_snapmirror_policy(self, uuid=None, body=None): |
| 731 |
""" |
| 732 |
Modifies a snapmirror policy |
| 733 |
""" |
| 734 |
if self.use_rest: |
| 735 |
if not body: |
| 736 |
return |
| 737 |
api = "snapmirror/policies" |
| 738 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid, body) |
| 739 |
if error: |
| 740 |
self.module.fail_json(msg='Error modifying snapmirror policy: %s' % error) |
| 741 |
else: |
| 742 |
snapmirror_policy_obj = netapp_utils.zapi.NaElement("snapmirror-policy-modify") |
| 743 |
snapmirror_policy_obj = self.create_snapmirror_policy_obj(snapmirror_policy_obj) |
| 744 |
# Only modify snapmirror policy if a specific snapmirror policy attribute needs |
| 745 |
# modifying. It may be that only snapmirror policy rules are being modified. |
| 746 |
if snapmirror_policy_obj.get_children(): |
| 747 |
snapmirror_policy_obj.add_new_child("policy-name", self.parameters['policy_name']) |
| 748 |
|
| 749 |
try: |
| 750 |
self.server.invoke_successfully(snapmirror_policy_obj, True) |
| 751 |
except netapp_utils.zapi.NaApiError as error: |
| 752 |
self.module.fail_json(msg='Error modifying snapmirror policy %s: %s' % (self.parameters['policy_name'], to_native(error)), |
| 753 |
exception=traceback.format_exc()) |
| 754 |
|
| 755 |
def identify_new_snapmirror_policy_rules(self, current=None): |
| 756 |
""" |
| 757 |
Identify new rules that should be added. |
| 758 |
:return: List of new rules to be added |
| 759 |
e.g. [{'snapmirror_label': 'daily', 'keep': 7, 'prefix': '', 'schedule': ''}, ... ] |
| 760 |
""" |
| 761 |
new_rules = [] |
| 762 |
if 'snapmirror_label' in self.parameters: |
| 763 |
for snapmirror_label in self.parameters['snapmirror_label']: |
| 764 |
snapmirror_label = snapmirror_label.strip() |
| 765 |
|
| 766 |
# Construct new rule. prefix and schedule are optional. |
| 767 |
snapmirror_label_index = self.parameters['snapmirror_label'].index(snapmirror_label) |
| 768 |
rule = dict({ |
| 769 |
'snapmirror_label': snapmirror_label, |
| 770 |
'keep': self.parameters['keep'][snapmirror_label_index] |
| 771 |
}) |
| 772 |
if 'prefix' in self.parameters: |
| 773 |
rule['prefix'] = self.parameters['prefix'][snapmirror_label_index] |
| 774 |
else: |
| 775 |
rule['prefix'] = '' |
| 776 |
if 'schedule' in self.parameters: |
| 777 |
rule['schedule'] = self.parameters['schedule'][snapmirror_label_index] |
| 778 |
else: |
| 779 |
rule['schedule'] = '' |
| 780 |
|
| 781 |
if current is None or 'snapmirror_label' not in current or snapmirror_label not in current['snapmirror_label']: |
| 782 |
# Rule doesn't exist. Add new rule. |
| 783 |
new_rules.append(rule) |
| 784 |
return new_rules |
| 785 |
|
| 786 |
def identify_obsolete_snapmirror_policy_rules(self, current=None): |
| 787 |
""" |
| 788 |
Identify existing rules that should be deleted |
| 789 |
:return: List of rules to be deleted |
| 790 |
e.g. [{'snapmirror_label': 'daily', 'keep': 7, 'prefix': '', 'schedule': ''}, ... ] |
| 791 |
""" |
| 792 |
obsolete_rules = [] |
| 793 |
if 'snapmirror_label' in self.parameters and current is not None and 'snapmirror_label' in current: |
| 794 |
# Iterate existing rules. |
| 795 |
for snapmirror_label in current['snapmirror_label']: |
| 796 |
snapmirror_label = snapmirror_label.strip() |
| 797 |
if snapmirror_label not in [item.strip() for item in self.parameters['snapmirror_label']]: |
| 798 |
# Existing rule isn't in parameters. Delete existing rule. |
| 799 |
current_snapmirror_label_index = current['snapmirror_label'].index(snapmirror_label) |
| 800 |
rule = dict({ |
| 801 |
'snapmirror_label': snapmirror_label, |
| 802 |
'keep': current['keep'][current_snapmirror_label_index], |
| 803 |
'prefix': current['prefix'][current_snapmirror_label_index], |
| 804 |
'schedule': current['schedule'][current_snapmirror_label_index] |
| 805 |
}) |
| 806 |
obsolete_rules.append(rule) |
| 807 |
return obsolete_rules |
| 808 |
|
| 809 |
def set_rule(self, rule, key, current, snapmirror_label_index, current_snapmirror_label_index): |
| 810 |
if key not in self.parameters or self.parameters[key][snapmirror_label_index] == current[key][current_snapmirror_label_index]: |
| 811 |
modified = False |
| 812 |
rule[key] = current[key][current_snapmirror_label_index] |
| 813 |
else: |
| 814 |
modified = True |
| 815 |
rule[key] = self.parameters[key][snapmirror_label_index] |
| 816 |
return modified |
| 817 |
|
| 818 |
def identify_modified_snapmirror_policy_rules(self, current=None): |
| 819 |
""" |
| 820 |
Identify self.parameters rules that will be modified or not. |
| 821 |
:return: List of 'modified' rules and a list of 'unmodified' rules |
| 822 |
e.g. [{'snapmirror_label': 'daily', 'keep': 7, 'prefix': '', 'schedule': ''}, ... ] |
| 823 |
""" |
| 824 |
modified_rules = [] |
| 825 |
unmodified_rules = [] |
| 826 |
if 'snapmirror_label' in self.parameters: |
| 827 |
for snapmirror_label in self.parameters['snapmirror_label']: |
| 828 |
snapmirror_label = snapmirror_label.strip() |
| 829 |
if current is not None and 'snapmirror_label' in current and snapmirror_label in current['snapmirror_label']: |
| 830 |
# Rule exists. Identify whether it requires modification or not. |
| 831 |
modified = False |
| 832 |
rule = {'snapmirror_label': snapmirror_label} |
| 833 |
# Get indexes of current and supplied rule. |
| 834 |
current_snapmirror_label_index = current['snapmirror_label'].index(snapmirror_label) |
| 835 |
snapmirror_label_index = self.parameters['snapmirror_label'].index(snapmirror_label) |
| 836 |
|
| 837 |
# Check if keep modified |
| 838 |
if self.set_rule(rule, 'keep', current, snapmirror_label_index, current_snapmirror_label_index): |
| 839 |
modified = True |
| 840 |
|
| 841 |
# Check if prefix modified |
| 842 |
if self.set_rule(rule, 'prefix', current, snapmirror_label_index, current_snapmirror_label_index): |
| 843 |
modified = True |
| 844 |
|
| 845 |
# Check if schedule modified |
| 846 |
if self.set_rule(rule, 'schedule', current, snapmirror_label_index, current_snapmirror_label_index): |
| 847 |
modified = True |
| 848 |
|
| 849 |
if modified: |
| 850 |
modified_rules.append(rule) |
| 851 |
else: |
| 852 |
unmodified_rules.append(rule) |
| 853 |
return modified_rules, unmodified_rules |
| 854 |
|
| 855 |
def identify_snapmirror_policy_rules_with_schedule(self, rules=None): |
| 856 |
""" |
| 857 |
Identify rules that are using a schedule or not. At least one |
| 858 |
non-schedule rule must be added to a policy before schedule rules |
| 859 |
are added. |
| 860 |
:return: List of rules with schedules and a list of rules without schedules |
| 861 |
e.g. [{'snapmirror_label': 'daily', 'keep': 7, 'prefix': 'daily', 'schedule': 'daily'}, ... ], |
| 862 |
[{'snapmirror_label': 'weekly', 'keep': 5, 'prefix': '', 'schedule': ''}, ... ] |
| 863 |
""" |
| 864 |
schedule_rules = [] |
| 865 |
non_schedule_rules = [] |
| 866 |
if rules is not None: |
| 867 |
for rule in rules: |
| 868 |
if 'schedule' in rule: |
| 869 |
schedule_rules.append(rule) |
| 870 |
else: |
| 871 |
non_schedule_rules.append(rule) |
| 872 |
return schedule_rules, non_schedule_rules |
| 873 |
|
| 874 |
def modify_snapmirror_policy_rules(self, current=None, uuid=None): |
| 875 |
""" |
| 876 |
Modify existing rules in snapmirror policy |
| 877 |
:return: None |
| 878 |
""" |
| 879 |
# Need 'snapmirror_label' to add/modify/delete rules |
| 880 |
if 'snapmirror_label' not in self.parameters: |
| 881 |
return |
| 882 |
|
| 883 |
obsolete_rules = self.identify_obsolete_snapmirror_policy_rules(current) |
| 884 |
new_rules = self.identify_new_snapmirror_policy_rules(current) |
| 885 |
modified_rules, unmodified_rules = self.identify_modified_snapmirror_policy_rules(current) |
| 886 |
self.rest_api.log_debug('OBS', obsolete_rules) |
| 887 |
self.rest_api.log_debug('NEW', new_rules) |
| 888 |
self.rest_api.log_debug('MOD', modified_rules) |
| 889 |
self.rest_api.log_debug('UNM', unmodified_rules) |
| 890 |
|
| 891 |
if self.use_rest: |
| 892 |
return self.modify_snapmirror_policy_rules_rest(uuid, obsolete_rules, unmodified_rules, modified_rules, new_rules) |
| 893 |
|
| 894 |
delete_rules = obsolete_rules + modified_rules |
| 895 |
add_schedule_rules, add_non_schedule_rules = self.identify_snapmirror_policy_rules_with_schedule(new_rules + modified_rules) |
| 896 |
# Delete rules no longer required or modified rules that will be re-added. |
| 897 |
for rule in delete_rules: |
| 898 |
options = {'policy-name': self.parameters['policy_name'], |
| 899 |
'snapmirror-label': rule['snapmirror_label']} |
| 900 |
self.modify_snapmirror_policy_rule(options, 'snapmirror-policy-remove-rule') |
| 901 |
|
| 902 |
# Add rules. At least one non-schedule rule must exist before |
| 903 |
# a rule with a schedule can be added, otherwise zapi will complain. |
| 904 |
for rule in add_non_schedule_rules + add_schedule_rules: |
| 905 |
options = {'policy-name': self.parameters['policy_name'], |
| 906 |
'snapmirror-label': rule['snapmirror_label'], |
| 907 |
'keep': str(rule['keep'])} |
| 908 |
if 'prefix' in rule and rule['prefix'] != '': |
| 909 |
options['prefix'] = rule['prefix'] |
| 910 |
if 'schedule' in rule and rule['schedule'] != '': |
| 911 |
options['schedule'] = rule['schedule'] |
| 912 |
self.modify_snapmirror_policy_rule(options, 'snapmirror-policy-add-rule') |
| 913 |
|
| 914 |
def modify_snapmirror_policy_rules_rest(self, uuid, obsolete_rules, unmodified_rules, modified_rules, new_rules): |
| 915 |
api = "snapmirror/policies" |
| 916 |
if not modified_rules and not new_rules and not obsolete_rules: |
| 917 |
return |
| 918 |
rules = unmodified_rules + modified_rules + new_rules |
| 919 |
# This will also delete all existing rules if everything is now obsolete |
| 920 |
body = {'retention': self.create_snapmirror_policy_retention_obj_for_rest(rules)} |
| 921 |
dummy, error = rest_generic.patch_async(self.rest_api, api, uuid, body) |
| 922 |
if error: |
| 923 |
self.module.fail_json(msg='Error modifying snapmirror policy rules: %s' % error) |
| 924 |
|
| 925 |
def modify_snapmirror_policy_rule(self, options, zapi): |
| 926 |
""" |
| 927 |
Add, modify or remove a rule to/from a snapmirror policy |
| 928 |
""" |
| 929 |
snapmirror_obj = netapp_utils.zapi.NaElement.create_node_with_children(zapi, **options) |
| 930 |
try: |
| 931 |
self.server.invoke_successfully(snapmirror_obj, enable_tunneling=True) |
| 932 |
except netapp_utils.zapi.NaApiError as error: |
| 933 |
self.module.fail_json(msg='Error modifying snapmirror policy rule %s: %s' % |
| 934 |
(self.parameters['policy_name'], to_native(error)), |
| 935 |
exception=traceback.format_exc()) |
| 936 |
|
| 937 |
def fail_invalid_option(self, policy_type, option): |
| 938 |
self.module.fail_json(msg="Error: option %s is not supported with policy type %s." % (option, policy_type)) |
| 939 |
|
| 940 |
def validate_async_options(self): |
| 941 |
if 'policy_type' in self.parameters: |
| 942 |
disallowed_options = { |
| 943 |
'vault': ['copy_latest_source_snapshot', 'copy_all_source_snapshots'], |
| 944 |
'mirror-vault': ['copy_latest_source_snapshot', 'copy_all_source_snapshots', 'create_snapshot_on_source'], |
| 945 |
'async_mirror': ['create_snapshot_on_source'], |
| 946 |
'async': [], |
| 947 |
'sync': ['copy_latest_source_snapshot', 'copy_all_source_snapshots', 'create_snapshot_on_source'], |
| 948 |
} |
| 949 |
try: |
| 950 |
options = disallowed_options[self.parameters['policy_type']] |
| 951 |
except KeyError: |
| 952 |
options = disallowed_options['sync'] |
| 953 |
for option in options: |
| 954 |
if option in self.parameters: |
| 955 |
self.fail_invalid_option(self.parameters['policy_type'], option) |
| 956 |
|
| 957 |
if self.use_rest: |
| 958 |
if 'copy_all_source_snapshots' in self.parameters and self.parameters.get('copy_all_source_snapshots') is not True: |
| 959 |
self.module.fail_json(msg='Error: the property copy_all_source_snapshots can only be set to true when present') |
| 960 |
if 'copy_latest_source_snapshot' in self.parameters and self.parameters.get('copy_latest_source_snapshot') is not True: |
| 961 |
self.module.fail_json(msg='Error: the property copy_latest_source_snapshot can only be set to true when present') |
| 962 |
if 'create_snapshot_on_source' in self.parameters and self.parameters['create_snapshot_on_source'] is not False: |
| 963 |
self.module.fail_json(msg='Error: the property create_snapshot_on_source can only be set to false when present') |
| 964 |
|
| 965 |
def validate_policy_type(self): |
| 966 |
# policy_type is only required for create or modify |
| 967 |
if self.parameters['state'] != 'present': |
| 968 |
return |
| 969 |
self.validate_async_options() |
| 970 |
if 'policy_type' in self.parameters: |
| 971 |
if self.use_rest: |
| 972 |
# Policy types 'mirror_vault', 'vault', 'async_mirror' are mapped to async policy type |
| 973 |
if self.parameters['policy_type'] == 'vault': |
| 974 |
self.parameters['policy_type'] = 'async' |
| 975 |
self.parameters['create_snapshot_on_source'] = False |
| 976 |
self.module.warn("policy type changed to 'async' with 'create_snapshot_on_source' set to False") |
| 977 |
if self.parameters['policy_type'] == 'async_mirror': |
| 978 |
# async_mirror accepts two choices with copy_all_source_snapshots or copy_latest_source_snapshot |
| 979 |
self.parameters['policy_type'] = 'async' |
| 980 |
if 'copy_latest_source_snapshot' not in self.parameters or 'copy_all_source_snapshots' not in self.parameters: |
| 981 |
self.parameters['copy_latest_source_snapshot'] = True |
| 982 |
self.module.warn("policy type changed to 'async' with copy_latest_source_snapshot set to True. " |
| 983 |
"Use async with copy_latest_source_snapshot or copy_all_source_snapshots for async-mirror") |
| 984 |
if 'copy_all_source_snapshots' in self.parameters or 'copy_latest_source_snapshot' in self.parameters: |
| 985 |
if 'snapmirror_label' in self.parameters or 'keep' in self.parameters or 'prefix' in self.parameters or 'schedule' in self.parameters: |
| 986 |
self.module.fail_json(msg='Error: Retention properties cannot be specified along with copy_all_source_snapshots or ' |
| 987 |
'copy_latest_source_snapshot properties') |
| 988 |
|
| 989 |
if 'create_snapshot_on_source' in self.parameters: |
| 990 |
if 'snapmirror_label' not in self.parameters or 'keep' not in self.parameters: |
| 991 |
self.module.fail_json(msg="Error: The properties snapmirror_label and keep must be specified with " |
| 992 |
"create_snapshot_on_source set to false") |
| 993 |
if self.parameters['policy_type'] == 'mirror_vault': |
| 994 |
self.parameters['policy_type'] = 'async' |
| 995 |
# Policy types ''sync_mirror', 'strict_sync_mirror' are mapped to sync policy type |
| 996 |
if self.parameters['policy_type'] in ('sync_mirror', 'strict_sync_mirror'): |
| 997 |
self.parameters['sync_type'] = 'sync' if self.parameters['policy_type'] == 'sync_mirror' else 'strict_sync' |
| 998 |
self.parameters['policy_type'] = 'sync' |
| 999 |
if self.parameters['policy_type'] != 'sync' and 'sync_type' in self.parameters: |
| 1000 |
self.module.fail_json(msg="Error: 'sync_type' is only applicable for sync policy_type") |
| 1001 |
|
| 1002 |
elif self.parameters['policy_type'] in ['async', 'sync']: |
| 1003 |
self.module.fail_json(msg='Error: The policy types async and sync are not supported in ZAPI.') |
| 1004 |
|
| 1005 |
def get_actions(self): |
| 1006 |
current, modify = self.get_snapmirror_policy(), None |
| 1007 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 1008 |
if current and cd_action is None and self.parameters['state'] == 'present': |
| 1009 |
# Inconsistency in REST API - POST requires a vserver, but GET does not return it |
| 1010 |
current.pop('vserver') |
| 1011 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 1012 |
for property in ('policy_type', 'copy_all_source_snapshots', 'copy_latest_source_snapshot', 'sync_type', 'create_snapshot_on_source'): |
| 1013 |
if property in modify: |
| 1014 |
self.module.fail_json(msg='Error: The policy property %s cannot be modified from %s to %s' |
| 1015 |
% (property, current.get(property), modify[property])) |
| 1016 |
|
| 1017 |
body = None |
| 1018 |
modify_body = any(key not in ('keep', 'prefix', 'schedule', 'snapmirror_label', 'copy_all_source_snapshots', 'copy_latest_source_snapshot', |
| 1019 |
'sync_type', 'create_snapshot_on_source') for key in modify) if modify else False |
| 1020 |
if self.na_helper.changed and (cd_action == 'create' or modify): |
| 1021 |
# report any error even in check_mode |
| 1022 |
self.validate_parameters() |
| 1023 |
if self.use_rest and (cd_action == 'create' or modify_body): |
| 1024 |
body = self.build_body_for_create_or_modify(current.get('policy_type')) if modify_body else self.build_body_for_create() |
| 1025 |
return cd_action, modify, current, body |
| 1026 |
|
| 1027 |
def apply(self): |
| 1028 |
cd_action, modify, current, body = self.get_actions() |
| 1029 |
if self.na_helper.changed and not self.module.check_mode: |
| 1030 |
uuid = None |
| 1031 |
if cd_action == 'create': |
| 1032 |
self.create_snapmirror_policy(body) |
| 1033 |
if self.use_rest: |
| 1034 |
current = self.get_snapmirror_policy() |
| 1035 |
if not current: |
| 1036 |
self.module.fail_json(msg="Error: policy %s not present after create." % self.parameters['policy_name']) |
| 1037 |
uuid = current['uuid'] |
| 1038 |
if 'create_snapshot_on_source' not in self.parameters: |
| 1039 |
self.modify_snapmirror_policy_rules(current, uuid) |
| 1040 |
elif cd_action == 'delete': |
| 1041 |
if self.use_rest: |
| 1042 |
uuid = current['uuid'] |
| 1043 |
self.delete_snapmirror_policy(uuid) |
| 1044 |
elif modify: |
| 1045 |
if self.use_rest: |
| 1046 |
uuid = current['uuid'] |
| 1047 |
self.modify_snapmirror_policy(uuid, body) |
| 1048 |
self.modify_snapmirror_policy_rules(current, uuid) |
| 1049 |
|
| 1050 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 1051 |
self.module.exit_json(**result) |
| 1052 |
|
| 1053 |
|
| 1054 |
def main(): |
| 1055 |
""" |
| 1056 |
Creates the NetApp Ontap SnapMirror policy object and runs the correct play task |
| 1057 |
""" |
| 1058 |
obj = NetAppOntapSnapMirrorPolicy() |
| 1059 |
obj.apply() |
| 1060 |
|
| 1061 |
|
| 1062 |
if __name__ == '__main__': |
| 1063 |
main() |