| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Ansible module for managing snapshot schedules on Unity""" |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: snapshotschedule |
| 13 |
version_added: '1.1.0' |
| 14 |
short_description: Manage snapshot schedules on Unity storage system |
| 15 |
description: |
| 16 |
- Managing snapshot schedules on Unity storage system includes |
| 17 |
creating new snapshot schedule, getting details of snapshot schedule, |
| 18 |
modifying attributes of snapshot schedule, and deleting snapshot schedule. |
| 19 |
|
| 20 |
extends_documentation_fragment: |
| 21 |
- dellemc.unity.unity |
| 22 |
|
| 23 |
author: |
| 24 |
- Akash Shendge (@shenda1) <[email protected]> |
| 25 |
|
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the snapshot schedule. |
| 30 |
- Name is mandatory for a create operation. |
| 31 |
- Specify either I(name) or I(id) (but not both) for any operation. |
| 32 |
type: str |
| 33 |
id: |
| 34 |
description: |
| 35 |
- The ID of the snapshot schedule. |
| 36 |
type: str |
| 37 |
type: |
| 38 |
description: |
| 39 |
- Type of the rule to be included in snapshot schedule. |
| 40 |
- Type is mandatory for any create or modify operation. |
| 41 |
- Once the snapshot schedule is created with one type it can be modified. |
| 42 |
type: str |
| 43 |
choices: ['every_n_hours', 'every_day', 'every_n_days', 'every_week', |
| 44 |
'every_month'] |
| 45 |
interval: |
| 46 |
description: |
| 47 |
- Number of hours between snapshots. |
| 48 |
- Applicable only when rule type is C(every_n_hours). |
| 49 |
type: int |
| 50 |
hours_of_day: |
| 51 |
description: |
| 52 |
- Hours of the day when the snapshot will be taken. |
| 53 |
- Applicable only when rule type is C(every_day). |
| 54 |
type: list |
| 55 |
elements: int |
| 56 |
day_interval: |
| 57 |
description: |
| 58 |
- Number of days between snapshots. |
| 59 |
- Applicable only when rule type is C(every_n_days). |
| 60 |
type: int |
| 61 |
days_of_week: |
| 62 |
description: |
| 63 |
- Days of the week for which the snapshot schedule rule applies. |
| 64 |
- Applicable only when rule type is C(every_week). |
| 65 |
type: list |
| 66 |
elements: str |
| 67 |
choices: ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', |
| 68 |
'FRIDAY', 'SATURDAY'] |
| 69 |
day_of_month: |
| 70 |
description: |
| 71 |
- Day of the month for which the snapshot schedule rule applies. |
| 72 |
- Applicable only when rule type is C(every_month). |
| 73 |
- Value should be [1, 31]. |
| 74 |
type: int |
| 75 |
hour: |
| 76 |
description: |
| 77 |
- The hour when the snapshot will be taken. |
| 78 |
- Applicable for C(every_n_days), C(every_week), C(every_month) rule types. |
| 79 |
- For create operation, if I(hour) parameter is not specified, value will |
| 80 |
be taken as C(0). |
| 81 |
- Value should be [0, 23]. |
| 82 |
type: int |
| 83 |
minute: |
| 84 |
description: |
| 85 |
- Minute offset from the hour when the snapshot will be taken. |
| 86 |
- Applicable for all rule types. |
| 87 |
- For a create operation, if I(minute) parameter is not specified, value will |
| 88 |
be taken as C(0). |
| 89 |
- Value should be [0, 59]. |
| 90 |
type: int |
| 91 |
desired_retention: |
| 92 |
description: |
| 93 |
- The number of days/hours for which snapshot will be retained. |
| 94 |
- When I(auto_delete) is C(true), I(desired_retention) cannot be specified. |
| 95 |
- Maximum desired retention supported is 31 days or 744 hours. |
| 96 |
type: int |
| 97 |
retention_unit: |
| 98 |
description: |
| 99 |
- The retention unit for the snapshot. |
| 100 |
default: 'hours' |
| 101 |
type: str |
| 102 |
choices: ['hours' , 'days'] |
| 103 |
auto_delete: |
| 104 |
description: |
| 105 |
- Indicates whether the system can automatically delete the snapshot. |
| 106 |
type: bool |
| 107 |
state: |
| 108 |
description: |
| 109 |
- Define whether the snapshot schedule should exist or not. |
| 110 |
type: str |
| 111 |
required: true |
| 112 |
choices: [absent, present] |
| 113 |
notes: |
| 114 |
- Snapshot schedule created through Ansible will have only one rule. |
| 115 |
- Modification of rule type is not allowed. Within the same type, other |
| 116 |
parameters can be modified. |
| 117 |
- If an existing snapshot schedule has more than 1 rule in it, only get and |
| 118 |
delete operation is allowed. |
| 119 |
- The I(check_mode) is not supported. |
| 120 |
""" |
| 121 |
|
| 122 |
EXAMPLES = r""" |
| 123 |
- name: Create snapshot schedule (Rule Type - every_n_hours) |
| 124 |
snapshotschedule: |
| 125 |
unispherehost: "{{unispherehost}}" |
| 126 |
validate_certs: "{{validate_certs}}" |
| 127 |
username: "{{username}}" |
| 128 |
password: "{{password}}" |
| 129 |
name: "Ansible_Every_N_Hours_Testing" |
| 130 |
type: "every_n_hours" |
| 131 |
interval: 6 |
| 132 |
desired_retention: 24 |
| 133 |
state: "{{state_present}}" |
| 134 |
|
| 135 |
- name: Create snapshot schedule (Rule Type - every_day) |
| 136 |
snapshotschedule: |
| 137 |
unispherehost: "{{unispherehost}}" |
| 138 |
validate_certs: "{{validate_certs}}" |
| 139 |
username: "{{username}}" |
| 140 |
password: "{{password}}" |
| 141 |
name: "Ansible_Every_Day_Testing" |
| 142 |
type: "every_day" |
| 143 |
hours_of_day: |
| 144 |
- 8 |
| 145 |
- 14 |
| 146 |
auto_delete: true |
| 147 |
state: "{{state_present}}" |
| 148 |
|
| 149 |
- name: Create snapshot schedule (Rule Type - every_n_days) |
| 150 |
snapshotschedule: |
| 151 |
unispherehost: "{{unispherehost}}" |
| 152 |
validate_certs: "{{validate_certs}}" |
| 153 |
username: "{{username}}" |
| 154 |
password: "{{password}}" |
| 155 |
name: "Ansible_Every_N_Day_Testing" |
| 156 |
type: "every_n_days" |
| 157 |
day_interval: 2 |
| 158 |
desired_retention: 16 |
| 159 |
retention_unit: "days" |
| 160 |
state: "{{state_present}}" |
| 161 |
|
| 162 |
- name: Create snapshot schedule (Rule Type - every_week) |
| 163 |
snapshotschedule: |
| 164 |
unispherehost: "{{unispherehost}}" |
| 165 |
validate_certs: "{{validate_certs}}" |
| 166 |
username: "{{username}}" |
| 167 |
password: "{{password}}" |
| 168 |
name: "Ansible_Every_Week_Testing" |
| 169 |
type: "every_week" |
| 170 |
days_of_week: |
| 171 |
- MONDAY |
| 172 |
- FRIDAY |
| 173 |
hour: 12 |
| 174 |
minute: 30 |
| 175 |
desired_retention: 200 |
| 176 |
state: "{{state_present}}" |
| 177 |
|
| 178 |
- name: Create snapshot schedule (Rule Type - every_month) |
| 179 |
snapshotschedule: |
| 180 |
unispherehost: "{{unispherehost}}" |
| 181 |
validate_certs: "{{validate_certs}}" |
| 182 |
username: "{{username}}" |
| 183 |
password: "{{password}}" |
| 184 |
name: "Ansible_Every_Month_Testing" |
| 185 |
type: "every_month" |
| 186 |
day_of_month: 17 |
| 187 |
auto_delete: true |
| 188 |
state: "{{state_present}}" |
| 189 |
|
| 190 |
- name: Get snapshot schedule details using name |
| 191 |
snapshotschedule: |
| 192 |
unispherehost: "{{unispherehost}}" |
| 193 |
validate_certs: "{{validate_certs}}" |
| 194 |
username: "{{username}}" |
| 195 |
password: "{{password}}" |
| 196 |
name: "Ansible_Every_N_Hours_Testing" |
| 197 |
state: "{{state_present}}" |
| 198 |
|
| 199 |
- name: Get snapshot schedule details using id |
| 200 |
snapshotschedule: |
| 201 |
unispherehost: "{{unispherehost}}" |
| 202 |
validate_certs: "{{validate_certs}}" |
| 203 |
username: "{{username}}" |
| 204 |
password: "{{password}}" |
| 205 |
id: "{{id}}" |
| 206 |
state: "{{state_present}}" |
| 207 |
|
| 208 |
- name: Modify snapshot schedule details id |
| 209 |
snapshotschedule: |
| 210 |
unispherehost: "{{unispherehost}}" |
| 211 |
validate_certs: "{{validate_certs}}" |
| 212 |
username: "{{username}}" |
| 213 |
password: "{{password}}" |
| 214 |
id: "{{id}}" |
| 215 |
type: "every_n_hours" |
| 216 |
interval: 8 |
| 217 |
state: "{{state_present}}" |
| 218 |
|
| 219 |
- name: Modify snapshot schedule using name |
| 220 |
snapshotschedule: |
| 221 |
unispherehost: "{{unispherehost}}" |
| 222 |
validate_certs: "{{validate_certs}}" |
| 223 |
username: "{{username}}" |
| 224 |
password: "{{password}}" |
| 225 |
name: "Ansible_Every_Day_Testing" |
| 226 |
type: "every_day" |
| 227 |
desired_retention: 200 |
| 228 |
auto_delete: false |
| 229 |
state: "{{state_present}}" |
| 230 |
|
| 231 |
- name: Delete snapshot schedule using id |
| 232 |
snapshotschedule: |
| 233 |
unispherehost: "{{unispherehost}}" |
| 234 |
validate_certs: "{{validate_certs}}" |
| 235 |
username: "{{username}}" |
| 236 |
password: "{{password}}" |
| 237 |
id: "{{id}}" |
| 238 |
state: "{{state_absent}}" |
| 239 |
|
| 240 |
- name: Delete snapshot schedule using name |
| 241 |
snapshotschedule: |
| 242 |
unispherehost: "{{unispherehost}}" |
| 243 |
validate_certs: "{{validate_certs}}" |
| 244 |
username: "{{username}}" |
| 245 |
password: "{{password}}" |
| 246 |
name: "Ansible_Every_Day_Testing" |
| 247 |
state: "{{state_absent}}" |
| 248 |
""" |
| 249 |
|
| 250 |
RETURN = r""" |
| 251 |
changed: |
| 252 |
description: Whether or not the resource has changed. |
| 253 |
returned: always |
| 254 |
type: bool |
| 255 |
sample: true |
| 256 |
|
| 257 |
snapshot_schedule_details: |
| 258 |
description: Details of the snapshot schedule. |
| 259 |
returned: When snapshot schedule exists |
| 260 |
type: dict |
| 261 |
contains: |
| 262 |
id: |
| 263 |
description: The system ID given to the snapshot schedule. |
| 264 |
type: str |
| 265 |
name: |
| 266 |
description: The name of the snapshot schedule. |
| 267 |
type: str |
| 268 |
luns: |
| 269 |
description: Details of volumes for which snapshot schedule |
| 270 |
applied. |
| 271 |
type: dict |
| 272 |
contains: |
| 273 |
UnityLunList: |
| 274 |
description: List of volumes for which snapshot schedule |
| 275 |
applied. |
| 276 |
type: list |
| 277 |
contains: |
| 278 |
UnityLun: |
| 279 |
description: Detail of volume. |
| 280 |
type: dict |
| 281 |
contains: |
| 282 |
id: |
| 283 |
description: The system ID given to volume. |
| 284 |
type: str |
| 285 |
rules: |
| 286 |
description: Details of rules that apply to snapshot schedule. |
| 287 |
type: list |
| 288 |
contains: |
| 289 |
id: |
| 290 |
description: The system ID of the rule. |
| 291 |
type: str |
| 292 |
interval: |
| 293 |
description: Number of days or hours between snaps, |
| 294 |
depending on the rule type. |
| 295 |
type: int |
| 296 |
hours: |
| 297 |
description: Hourly frequency for the snapshot |
| 298 |
schedule rule. |
| 299 |
type: list |
| 300 |
minute: |
| 301 |
description: Minute frequency for the snapshot |
| 302 |
schedule rule. |
| 303 |
type: int |
| 304 |
days_of_week: |
| 305 |
description: Days of the week for which the snapshot |
| 306 |
schedule rule applies. |
| 307 |
type: dict |
| 308 |
contains: |
| 309 |
DayOfWeekEnumList: |
| 310 |
description: Enumeration of days of the week. |
| 311 |
type: list |
| 312 |
days_of_month: |
| 313 |
description: Days of the month for which the snapshot |
| 314 |
schedule rule applies. |
| 315 |
type: list |
| 316 |
retention_time: |
| 317 |
description: Period of time in seconds for which to keep |
| 318 |
the snapshot. |
| 319 |
type: int |
| 320 |
retention_time_in_hours: |
| 321 |
description: Period of time in hours for which to keep the |
| 322 |
snapshot. |
| 323 |
type: int |
| 324 |
rule_type: |
| 325 |
description: Type of the rule applied to snapshot schedule. |
| 326 |
type: str |
| 327 |
is_auto_delete: |
| 328 |
description: Indicates whether the system can automatically |
| 329 |
delete the snapshot based on pool automatic-deletion |
| 330 |
thresholds. |
| 331 |
type: bool |
| 332 |
storage_resources: |
| 333 |
description: Details of storage resources for which snapshot. |
| 334 |
schedule applied. |
| 335 |
type: dict |
| 336 |
contains: |
| 337 |
UnityStorageResourceList: |
| 338 |
description: List of storage resources for which snapshot |
| 339 |
schedule applied. |
| 340 |
type: list |
| 341 |
contains: |
| 342 |
UnityStorageResource: |
| 343 |
description: Detail of storage resource. |
| 344 |
type: dict |
| 345 |
contains: |
| 346 |
id: |
| 347 |
description: The system ID given to storage |
| 348 |
resource. |
| 349 |
type: str |
| 350 |
sample: { |
| 351 |
"existed": true, |
| 352 |
"hash": 8742032390151, |
| 353 |
"id": "snapSch_63", |
| 354 |
"is_default": false, |
| 355 |
"is_modified": null, |
| 356 |
"is_sync_replicated": false, |
| 357 |
"luns": null, |
| 358 |
"modification_time": "2021-12-14 21:37:47.905000+00:00", |
| 359 |
"name": "SS7_empty_hour_SS", |
| 360 |
"rules": [ |
| 361 |
{ |
| 362 |
"access_type": "FilesystemSnapAccessTypeEnum.CHECKPOINT", |
| 363 |
"days_of_month": null, |
| 364 |
"days_of_week": { |
| 365 |
"DayOfWeekEnumList": [] |
| 366 |
}, |
| 367 |
"existed": true, |
| 368 |
"hash": 8742032280772, |
| 369 |
"hours": [ |
| 370 |
0 |
| 371 |
], |
| 372 |
"id": "SchedRule_109", |
| 373 |
"interval": 2, |
| 374 |
"is_auto_delete": false, |
| 375 |
"minute": 0, |
| 376 |
"retention_time": 86400, |
| 377 |
"retention_time_in_hours": 24, |
| 378 |
"rule_type": "every_n_days", |
| 379 |
"type": "ScheduleTypeEnum.N_DAYS_AT_HHMM" |
| 380 |
} |
| 381 |
], |
| 382 |
"storage_resources": null, |
| 383 |
"version": "ScheduleVersionEnum.LEGACY" |
| 384 |
} |
| 385 |
""" |
| 386 |
|
| 387 |
from ansible.module_utils.basic import AnsibleModule |
| 388 |
from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \ |
| 389 |
import utils |
| 390 |
|
| 391 |
LOG = utils.get_logger('snapshotschedule') |
| 392 |
|
| 393 |
application_type = "Ansible/1.7.1" |
| 394 |
|
| 395 |
|
| 396 |
class SnapshotSchedule(object): |
| 397 |
"""Class with snapshot schedule operations""" |
| 398 |
|
| 399 |
def __init__(self): |
| 400 |
"""Define all parameters required by this module""" |
| 401 |
|
| 402 |
self.module_params = utils.get_unity_management_host_parameters() |
| 403 |
self.module_params.update(get_snapshotschedule_parameters()) |
| 404 |
|
| 405 |
mutually_exclusive = [['name', 'id'], ['interval', 'hour'], |
| 406 |
['hours_of_day', 'hour'], |
| 407 |
['interval', 'hours_of_day', 'day_interval', |
| 408 |
'days_of_week', 'day_of_month']] |
| 409 |
required_one_of = [['name', 'id']] |
| 410 |
|
| 411 |
|
| 412 |
self.module = AnsibleModule( |
| 413 |
argument_spec=self.module_params, |
| 414 |
supports_check_mode=False, |
| 415 |
mutually_exclusive=mutually_exclusive, |
| 416 |
required_one_of=required_one_of |
| 417 |
) |
| 418 |
utils.ensure_required_libs(self.module) |
| 419 |
|
| 420 |
self.unity_conn = utils.get_unity_unisphere_connection( |
| 421 |
self.module.params, application_type) |
| 422 |
|
| 423 |
def schedule_modify_required(self, schedule_details): |
| 424 |
"""Check if the desired snapshot schedule state is different from |
| 425 |
existing snapshot schedule state |
| 426 |
:param schedule_details: The dict containing snapshot schedule |
| 427 |
details |
| 428 |
:return: Boolean value to indicate if modification is needed |
| 429 |
""" |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
if schedule_details['rules'][0]['is_auto_delete'] and\ |
| 434 |
self.module.params['desired_retention']\ |
| 435 |
and self.module.params['auto_delete'] is None: |
| 436 |
self.module.fail_json(msg="Desired retention cannot be " |
| 437 |
"specified when auto_delete is true" |
| 438 |
) |
| 439 |
if schedule_details['rules'][0]['retention_time'] and \ |
| 440 |
self.module.params['auto_delete']: |
| 441 |
self.module.fail_json(msg="auto_delete cannot be specified when" |
| 442 |
" existing desired retention is set") |
| 443 |
|
| 444 |
desired_rule_type = get_schedule_value(self.module.params['type']) |
| 445 |
existing_rule_string = schedule_details['rules'][0][ |
| 446 |
'type'].split(".")[1] |
| 447 |
existing_rule_type = utils.ScheduleTypeEnum[ |
| 448 |
existing_rule_string]._get_properties()['value'] |
| 449 |
modified = False |
| 450 |
|
| 451 |
|
| 452 |
if desired_rule_type != existing_rule_type: |
| 453 |
self.module.fail_json(msg="Modification of rule type is not" |
| 454 |
" allowed.") |
| 455 |
|
| 456 |
|
| 457 |
duration_in_sec = convert_retention_to_seconds( |
| 458 |
self.module.params['desired_retention'], |
| 459 |
self.module.params['retention_unit']) |
| 460 |
|
| 461 |
if not duration_in_sec: |
| 462 |
duration_in_sec = schedule_details['rules'][0]['retention_time'] |
| 463 |
|
| 464 |
|
| 465 |
if (duration_in_sec and duration_in_sec != schedule_details[ |
| 466 |
'rules'][0]['retention_time']): |
| 467 |
modified = True |
| 468 |
elif (self.module.params['auto_delete'] is not None and |
| 469 |
self.module.params['auto_delete'] != schedule_details['rules'] |
| 470 |
[0]['is_auto_delete']): |
| 471 |
modified = True |
| 472 |
|
| 473 |
if (self.module.params['minute'] is not None and self.module.params[ |
| 474 |
'minute'] != schedule_details['rules'][0]['minute']): |
| 475 |
modified = True |
| 476 |
|
| 477 |
if not modified and desired_rule_type == 0: |
| 478 |
if (self.module.params['interval'] and self.module.params[ |
| 479 |
'interval'] != schedule_details['rules'][0]['interval']): |
| 480 |
modified = True |
| 481 |
elif not modified and desired_rule_type == 1: |
| 482 |
if (self.module.params['hours_of_day'] and |
| 483 |
set(self.module.params['hours_of_day']) != |
| 484 |
set(schedule_details['rules'][0]['hours'])): |
| 485 |
modified = True |
| 486 |
elif not modified and desired_rule_type == 2: |
| 487 |
if (self.module.params['day_interval'] and self.module.params[ |
| 488 |
'day_interval'] != schedule_details['rules'][0]['interval'])\ |
| 489 |
or (self.module.params['hour'] is not None and |
| 490 |
self.module.params['hour'] != schedule_details[ |
| 491 |
'rules'][0]['hours'][0]): |
| 492 |
modified = True |
| 493 |
elif not modified and desired_rule_type == 3: |
| 494 |
days = schedule_details['rules'][0]['days_of_week'][ |
| 495 |
'DayOfWeekEnumList'] |
| 496 |
existing_days = list() |
| 497 |
|
| 498 |
for day in days: |
| 499 |
temp = day.split(".") |
| 500 |
existing_days.append(temp[1]) |
| 501 |
|
| 502 |
if (self.module.params['days_of_week'] and |
| 503 |
set(self.module.params['days_of_week']) != |
| 504 |
set(existing_days)) or\ |
| 505 |
(self.module.params['hour'] is not None and |
| 506 |
self.module.params['hour'] != schedule_details['rules'][ |
| 507 |
0]['hours'][0]): |
| 508 |
modified = True |
| 509 |
elif not modified and desired_rule_type == 4: |
| 510 |
if (self.module.params['day_of_month'] and self.module.params[ |
| 511 |
'day_of_month'] != schedule_details['rules'][0][ |
| 512 |
'days_of_month'][0]) or\ |
| 513 |
(self.module.params['hour'] is not None and |
| 514 |
self.module.params['hour'] != schedule_details['rules'][ |
| 515 |
0]['hours'][0]): |
| 516 |
modified = True |
| 517 |
LOG.info("Modify Flag: %s", modified) |
| 518 |
return modified |
| 519 |
|
| 520 |
def get_days_of_week_enum(self, days_of_week): |
| 521 |
"""Get the enum for days of week. |
| 522 |
:param days_of_week: The list of days of week |
| 523 |
:return: The list of days_of_week enum |
| 524 |
""" |
| 525 |
|
| 526 |
days_of_week_enum = [] |
| 527 |
for day in days_of_week: |
| 528 |
if day in utils.DayOfWeekEnum.__members__: |
| 529 |
days_of_week_enum.append(utils.DayOfWeekEnum[day]) |
| 530 |
else: |
| 531 |
errormsg = "Invalid choice {0} for days of week".format(day) |
| 532 |
LOG.error(errormsg) |
| 533 |
self.module.fail_json(msg=errormsg) |
| 534 |
return days_of_week_enum |
| 535 |
|
| 536 |
def create_rule(self, type, interval, hours_of_day, day_interval, |
| 537 |
days_of_week, day_of_month, hour, minute, |
| 538 |
desired_retention, retention_unit, auto_delete, |
| 539 |
schedule_details=None): |
| 540 |
"""Create the rule.""" |
| 541 |
|
| 542 |
duration_in_sec = None |
| 543 |
if desired_retention: |
| 544 |
duration_in_sec = convert_retention_to_seconds(desired_retention, |
| 545 |
retention_unit) |
| 546 |
|
| 547 |
if not duration_in_sec and schedule_details: |
| 548 |
duration_in_sec = schedule_details['rules'][0]['retention_time'] |
| 549 |
|
| 550 |
if hour is None and schedule_details is None: |
| 551 |
hour = 0 |
| 552 |
|
| 553 |
if hour is None and schedule_details: |
| 554 |
if schedule_details['rules'][0]['hours'] is not None: |
| 555 |
hour = schedule_details['rules'][0]['hours'][0] |
| 556 |
|
| 557 |
if minute is None and schedule_details is None: |
| 558 |
minute = 0 |
| 559 |
|
| 560 |
if minute is None and schedule_details: |
| 561 |
minute = schedule_details['rules'][0]['minute'] |
| 562 |
|
| 563 |
try: |
| 564 |
if type == "every_n_hours": |
| 565 |
if not interval: |
| 566 |
interval = schedule_details['rules'][0]['interval'] |
| 567 |
rule_dict = utils.snap_schedule.UnitySnapScheduleRule.\ |
| 568 |
every_n_hours(hour_interval=interval, minute=minute, |
| 569 |
retention_time=duration_in_sec, |
| 570 |
is_auto_delete=auto_delete) |
| 571 |
elif type == "every_day": |
| 572 |
if not hours_of_day: |
| 573 |
hours_of_day = schedule_details['rules'][0]['hours'] |
| 574 |
|
| 575 |
rule_dict = utils.snap_schedule.UnitySnapScheduleRule.\ |
| 576 |
every_day(hours=hours_of_day, minute=minute, |
| 577 |
retention_time=duration_in_sec, |
| 578 |
is_auto_delete=auto_delete) |
| 579 |
elif type == "every_n_days": |
| 580 |
if not day_interval: |
| 581 |
day_interval = schedule_details['rules'][0]['interval'] |
| 582 |
|
| 583 |
rule_dict = utils.snap_schedule.UnitySnapScheduleRule.\ |
| 584 |
every_n_days(day_interval=day_interval, hour=hour, |
| 585 |
minute=minute, |
| 586 |
retention_time=duration_in_sec, |
| 587 |
is_auto_delete=auto_delete) |
| 588 |
elif type == "every_week": |
| 589 |
if days_of_week: |
| 590 |
days_of_week_enum = self.get_days_of_week_enum(days_of_week) |
| 591 |
else: |
| 592 |
days = schedule_details['rules'][0]['days_of_week'][ |
| 593 |
'DayOfWeekEnumList'] |
| 594 |
existing_days = list() |
| 595 |
|
| 596 |
for day in days: |
| 597 |
temp = day.split(".") |
| 598 |
existing_days.append(temp[1]) |
| 599 |
days_of_week_enum = self.get_days_of_week_enum(days_of_week) |
| 600 |
|
| 601 |
rule_dict = utils.snap_schedule.UnitySnapScheduleRule.\ |
| 602 |
every_week(days_of_week=days_of_week_enum, hour=hour, |
| 603 |
minute=minute, retention_time=duration_in_sec, |
| 604 |
is_auto_delete=auto_delete) |
| 605 |
else: |
| 606 |
if day_of_month: |
| 607 |
day_of_month_list = [day_of_month] |
| 608 |
else: |
| 609 |
day_of_month_list = schedule_details['rules'][0][ |
| 610 |
'days_of_month'] |
| 611 |
|
| 612 |
rule_dict = utils.snap_schedule.UnitySnapScheduleRule.\ |
| 613 |
every_month(days_of_month=day_of_month_list, hour=hour, |
| 614 |
minute=minute, retention_time=duration_in_sec, |
| 615 |
is_auto_delete=auto_delete) |
| 616 |
|
| 617 |
return rule_dict |
| 618 |
|
| 619 |
except Exception as e: |
| 620 |
errormsg = "Create operation of snapshot schedule rule " \ |
| 621 |
" failed with error {0}".format(str(e)) |
| 622 |
LOG.error(errormsg) |
| 623 |
self.module.fail_json(msg=errormsg) |
| 624 |
|
| 625 |
def create_snapshot_schedule(self, name, rule_dict): |
| 626 |
"""Create snapshot schedule. |
| 627 |
:param name: The name of the snapshot schedule |
| 628 |
:param rule_dict: The dict of the rule |
| 629 |
:return: Boolean value to indicate if snapshot schedule created |
| 630 |
""" |
| 631 |
|
| 632 |
try: |
| 633 |
utils.snap_schedule.UnitySnapSchedule.create( |
| 634 |
cli=self.unity_conn._cli, name=name, rules=[rule_dict]) |
| 635 |
return True |
| 636 |
|
| 637 |
except Exception as e: |
| 638 |
errormsg = "Create operation of snapshot schedule {0} failed" \ |
| 639 |
" with error {1}".format(name, str(e)) |
| 640 |
LOG.error(errormsg) |
| 641 |
self.module.fail_json(msg=errormsg) |
| 642 |
|
| 643 |
def validate_desired_retention(self, desired_retention, retention_unit): |
| 644 |
"""Validates the specified desired retention. |
| 645 |
:param desired_retention: Desired retention of the snapshot |
| 646 |
schedule |
| 647 |
:param retention_unit: Retention unit for the snapshot schedule |
| 648 |
""" |
| 649 |
|
| 650 |
if retention_unit == 'hours' and (desired_retention < 1 or |
| 651 |
desired_retention > 744): |
| 652 |
self.module.fail_json(msg="Please provide a valid integer as the" |
| 653 |
" desired retention between 1 and 744.") |
| 654 |
elif retention_unit == 'days' and (desired_retention < 1 or |
| 655 |
desired_retention > 31): |
| 656 |
self.module.fail_json(msg="Please provide a valid integer as the" |
| 657 |
" desired retention between 1 and 31.") |
| 658 |
|
| 659 |
def return_schedule_instance(self, id): |
| 660 |
"""Return the snapshot schedule instance |
| 661 |
:param id: The id of the snapshot schedule |
| 662 |
:return: Instance of the snapshot schedule |
| 663 |
""" |
| 664 |
|
| 665 |
try: |
| 666 |
obj_schedule = utils.snap_schedule.UnitySnapSchedule.get( |
| 667 |
self.unity_conn._cli, id) |
| 668 |
return obj_schedule |
| 669 |
|
| 670 |
except Exception as e: |
| 671 |
error_msg = "Failed to get the snapshot schedule {0} instance" \ |
| 672 |
" with error {1}".format(id, str(e)) |
| 673 |
LOG.error(error_msg) |
| 674 |
self.module.fail_json(msg=error_msg) |
| 675 |
|
| 676 |
def delete_snapshot_schedule(self, id): |
| 677 |
"""Delete snapshot schedule. |
| 678 |
:param id: The ID of the snapshot schedule |
| 679 |
:return: The boolean value to indicate if snapshot schedule |
| 680 |
deleted |
| 681 |
""" |
| 682 |
|
| 683 |
try: |
| 684 |
obj_schedule = self.return_schedule_instance(id=id) |
| 685 |
obj_schedule.delete() |
| 686 |
return True |
| 687 |
|
| 688 |
except Exception as e: |
| 689 |
errormsg = "Delete operation of snapshot schedule id:{0} failed" \ |
| 690 |
" with error {1}".format(id, str(e)) |
| 691 |
LOG.error(errormsg) |
| 692 |
self.module.fail_json(msg=errormsg) |
| 693 |
|
| 694 |
def modify_snapshot_schedule(self, id, schedule_details): |
| 695 |
"""Modify snapshot schedule details. |
| 696 |
:param id: The id of the snapshot schedule |
| 697 |
:param schedule_details: The dict containing schedule details |
| 698 |
:return: The boolean value to indicate if snapshot schedule |
| 699 |
modified |
| 700 |
""" |
| 701 |
|
| 702 |
try: |
| 703 |
obj_schedule = self.return_schedule_instance(id=id) |
| 704 |
rule_id = schedule_details['rules'][0]['id'] |
| 705 |
|
| 706 |
if self.module.params['auto_delete'] is None: |
| 707 |
auto_delete = schedule_details['rules'][0]['is_auto_delete'] |
| 708 |
else: |
| 709 |
auto_delete = self.module.params['auto_delete'] |
| 710 |
|
| 711 |
if schedule_details['rules'][0]['is_auto_delete'] and\ |
| 712 |
self.module.params['desired_retention'] and\ |
| 713 |
self.module.params['auto_delete'] is False: |
| 714 |
auto_delete = False |
| 715 |
elif schedule_details['rules'][0]['retention_time']: |
| 716 |
auto_delete = None |
| 717 |
|
| 718 |
rule_dict = self.create_rule( |
| 719 |
self.module.params['type'], self.module.params['interval'], |
| 720 |
self.module.params['hours_of_day'], |
| 721 |
self.module.params['day_interval'], |
| 722 |
self.module.params['days_of_week'], |
| 723 |
self.module.params['day_of_month'], |
| 724 |
self.module.params['hour'], self.module.params['minute'], |
| 725 |
self.module.params['desired_retention'], |
| 726 |
self.module.params['retention_unit'], auto_delete, |
| 727 |
schedule_details) |
| 728 |
|
| 729 |
obj_schedule.modify(add_rules=[rule_dict], |
| 730 |
remove_rule_ids=[rule_id]) |
| 731 |
return True |
| 732 |
except Exception as e: |
| 733 |
errormsg = "Modify operation of snapshot schedule id:{0} failed" \ |
| 734 |
" with error {1}".format(id, str(e)) |
| 735 |
LOG.error(errormsg) |
| 736 |
self.module.fail_json(msg=errormsg) |
| 737 |
|
| 738 |
def get_details(self, id=None, name=None): |
| 739 |
"""Get snapshot schedule details. |
| 740 |
:param id: The id of the snapshot schedule |
| 741 |
:param name: The name of the snapshot schedule |
| 742 |
:return: Dict containing snapshot schedule details if exists |
| 743 |
""" |
| 744 |
|
| 745 |
id_or_name = id if id else name |
| 746 |
errormsg = "Failed to get details of snapshot schedule {0} with" \ |
| 747 |
" error {1}" |
| 748 |
try: |
| 749 |
if not id: |
| 750 |
details = utils.snap_schedule.UnitySnapScheduleList.get( |
| 751 |
self.unity_conn._cli, name=name) |
| 752 |
|
| 753 |
if details: |
| 754 |
id = details[0].id |
| 755 |
|
| 756 |
if id: |
| 757 |
details = self.unity_conn.get_snap_schedule(_id=id) |
| 758 |
|
| 759 |
if id and details.existed: |
| 760 |
rule_list = [rules._get_properties() for rules in |
| 761 |
details.rules] |
| 762 |
for rule in rule_list: |
| 763 |
rule['retention_time_in_hours'] = int( |
| 764 |
rule['retention_time'] / 3600) |
| 765 |
rule['rule_type'] = get_rule_type(rule['type']) |
| 766 |
schedule_details = details._get_properties() |
| 767 |
schedule_details['rules'] = rule_list |
| 768 |
return schedule_details |
| 769 |
else: |
| 770 |
LOG.info("Failed to get the snapshot schedule %s", id_or_name) |
| 771 |
return None |
| 772 |
|
| 773 |
except utils.HttpError as e: |
| 774 |
if e.http_status == 401: |
| 775 |
auth_err = "Incorrect username or password, {0}".format( |
| 776 |
e.message) |
| 777 |
msg = errormsg.format(id_or_name, auth_err) |
| 778 |
LOG.error(msg) |
| 779 |
self.module.fail_json(msg=msg) |
| 780 |
else: |
| 781 |
msg = errormsg.format(id_or_name, str(e)) |
| 782 |
LOG.error(msg) |
| 783 |
self.module.fail_json(msg=msg) |
| 784 |
|
| 785 |
except utils.UnityResourceNotFoundError as e: |
| 786 |
msg = errormsg.format(id_or_name, str(e)) |
| 787 |
LOG.error(msg) |
| 788 |
return None |
| 789 |
|
| 790 |
except Exception as e: |
| 791 |
msg = errormsg.format(id_or_name, str(e)) |
| 792 |
LOG.error(msg) |
| 793 |
self.module.fail_json(msg=msg) |
| 794 |
|
| 795 |
def validate_parameters(self): |
| 796 |
"""Validate the parameters.""" |
| 797 |
|
| 798 |
try: |
| 799 |
if self.module.params['interval'] is not None and\ |
| 800 |
self.module.params['interval'] <= 0: |
| 801 |
self.module.fail_json(msg="Interval can not be less than or" |
| 802 |
" equal to 0.") |
| 803 |
|
| 804 |
param_list = ['day_interval', 'day_of_month'] |
| 805 |
for param in param_list: |
| 806 |
if self.module.params[param] is not None and\ |
| 807 |
self.module.params[param] == 0: |
| 808 |
self.module.fail_json(msg="{0} can not be 0.".format( |
| 809 |
param)) |
| 810 |
|
| 811 |
except Exception as e: |
| 812 |
errormsg = "Failed to validate the module param with error" \ |
| 813 |
" {0}".format(str(e)) |
| 814 |
LOG.error(errormsg) |
| 815 |
self.module.fail_json(msg=errormsg) |
| 816 |
|
| 817 |
def perform_module_operation(self): |
| 818 |
""" |
| 819 |
Perform different actions on snapshot schedule module based on |
| 820 |
parameters chosen in playbook |
| 821 |
""" |
| 822 |
name = self.module.params['name'] |
| 823 |
id = self.module.params['id'] |
| 824 |
type = self.module.params['type'] |
| 825 |
interval = self.module.params['interval'] |
| 826 |
hours_of_day = self.module.params['hours_of_day'] |
| 827 |
day_interval = self.module.params['day_interval'] |
| 828 |
days_of_week = self.module.params['days_of_week'] |
| 829 |
day_of_month = self.module.params['day_of_month'] |
| 830 |
hour = self.module.params['hour'] |
| 831 |
minute = self.module.params['minute'] |
| 832 |
desired_retention = self.module.params['desired_retention'] |
| 833 |
retention_unit = self.module.params['retention_unit'] |
| 834 |
auto_delete = self.module.params['auto_delete'] |
| 835 |
state = self.module.params['state'] |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
result = dict( |
| 840 |
changed=False, |
| 841 |
snapshot_schedule_details={} |
| 842 |
) |
| 843 |
|
| 844 |
self.validate_parameters() |
| 845 |
|
| 846 |
if desired_retention is not None: |
| 847 |
self.validate_desired_retention(desired_retention, retention_unit) |
| 848 |
|
| 849 |
if auto_delete and desired_retention: |
| 850 |
self.module.fail_json(msg="Desired retention cannot be " |
| 851 |
"specified when auto_delete is true" |
| 852 |
) |
| 853 |
|
| 854 |
schedule_details = self.get_details(name=name, id=id) |
| 855 |
|
| 856 |
if not id and schedule_details: |
| 857 |
id = schedule_details['id'] |
| 858 |
|
| 859 |
if state == 'present' and not schedule_details: |
| 860 |
if not name: |
| 861 |
msg = "The parameter name length is 0. It is too short." \ |
| 862 |
" The min length is 1." |
| 863 |
self.module.fail_json(msg=msg) |
| 864 |
|
| 865 |
if not type: |
| 866 |
self.module.fail_json(msg="Rule type is necessary to create" |
| 867 |
" snapshot schedule") |
| 868 |
|
| 869 |
if type == "every_n_hours" and interval is None: |
| 870 |
self.module.fail_json(msg="To create snapshot schedule with" |
| 871 |
" rule type every_n_hours, interval" |
| 872 |
" is the mandatory parameter.") |
| 873 |
elif type == "every_day" and hours_of_day is None: |
| 874 |
self.module.fail_json(msg="To create snapshot schedule with" |
| 875 |
" rule type every_day, hours_of_day" |
| 876 |
" is the mandatory parameter.") |
| 877 |
elif type == "every_n_days" and day_interval is None: |
| 878 |
self.module.fail_json(msg="To create snapshot schedule with" |
| 879 |
" rule type every_n_days," |
| 880 |
" day_interval is the mandatory" |
| 881 |
" parameter.") |
| 882 |
elif type == "every_week" and days_of_week is None: |
| 883 |
self.module.fail_json(msg="To create snapshot schedule with" |
| 884 |
" rule type every_week," |
| 885 |
" days_of_week is the mandatory" |
| 886 |
" parameter.") |
| 887 |
elif type == "every_month" and day_of_month is None: |
| 888 |
self.module.fail_json(msg="To create snapshot schedule with" |
| 889 |
" rule type every_month," |
| 890 |
" day_of_month is the mandatory" |
| 891 |
" parameter.") |
| 892 |
|
| 893 |
rule_dict = self.create_rule(type, interval, hours_of_day, |
| 894 |
day_interval, days_of_week, |
| 895 |
day_of_month, hour, minute, |
| 896 |
desired_retention, retention_unit, |
| 897 |
auto_delete) |
| 898 |
result['changed'] = self.create_snapshot_schedule(name, rule_dict) |
| 899 |
|
| 900 |
elif state == 'absent' and schedule_details: |
| 901 |
result['changed'] = self.delete_snapshot_schedule(id) |
| 902 |
|
| 903 |
if state == 'present' and type and schedule_details and\ |
| 904 |
len(schedule_details['rules']) == 1: |
| 905 |
if (self.schedule_modify_required(schedule_details)): |
| 906 |
result['changed'] = self.modify_snapshot_schedule( |
| 907 |
id, schedule_details) |
| 908 |
|
| 909 |
result['snapshot_schedule_details'] = self.get_details(name=name, |
| 910 |
id=id) |
| 911 |
self.module.exit_json(**result) |
| 912 |
|
| 913 |
|
| 914 |
def get_rule_type(type): |
| 915 |
"""Get the rule type of schedule. |
| 916 |
:param type: The schedule type enum |
| 917 |
:return: The rule type of snapshot schedule |
| 918 |
""" |
| 919 |
|
| 920 |
schedule_type = { |
| 921 |
"ScheduleTypeEnum.N_HOURS_AT_MM": "every_n_hours", |
| 922 |
"ScheduleTypeEnum.DAY_AT_HHMM": "every_day", |
| 923 |
"ScheduleTypeEnum.N_DAYS_AT_HHMM": "every_n_days", |
| 924 |
"ScheduleTypeEnum.SELDAYS_AT_HHMM": "every_week", |
| 925 |
"ScheduleTypeEnum.NTH_DAYOFMONTH_AT_HHMM": "every_month" |
| 926 |
} |
| 927 |
|
| 928 |
return schedule_type.get(type) |
| 929 |
|
| 930 |
|
| 931 |
def get_schedule_value(type): |
| 932 |
"""Get the enum for schedule. |
| 933 |
:param type: The type of rule |
| 934 |
:return: The enum value for rule |
| 935 |
""" |
| 936 |
|
| 937 |
rule_type = { |
| 938 |
"every_n_hours": 0, |
| 939 |
"every_day": 1, |
| 940 |
"every_n_days": 2, |
| 941 |
"every_week": 3, |
| 942 |
"every_month": 4 |
| 943 |
} |
| 944 |
|
| 945 |
return rule_type.get(type) |
| 946 |
|
| 947 |
|
| 948 |
def convert_retention_to_seconds(desired_retention, retention_unit): |
| 949 |
"""Convert desired retention to seconds. |
| 950 |
:param desired_retention: The desired retention for snapshot |
| 951 |
schedule |
| 952 |
:param retention_unit: The retention unit for snapshot schedule |
| 953 |
:return: The integer value in seconds |
| 954 |
""" |
| 955 |
|
| 956 |
duration_in_sec = None |
| 957 |
if desired_retention: |
| 958 |
if retention_unit == 'hours': |
| 959 |
duration_in_sec = desired_retention * 60 * 60 |
| 960 |
else: |
| 961 |
duration_in_sec = desired_retention * 24 * 60 * 60 |
| 962 |
return duration_in_sec |
| 963 |
|
| 964 |
|
| 965 |
def get_snapshotschedule_parameters(): |
| 966 |
"""This method provide parameters required for the ansible snapshot |
| 967 |
schedule module on Unity""" |
| 968 |
|
| 969 |
return dict( |
| 970 |
name=dict(type='str'), |
| 971 |
id=dict(type='str'), |
| 972 |
type=dict(type='str', choices=['every_n_hours', 'every_day', |
| 973 |
'every_n_days', 'every_week', |
| 974 |
'every_month']), |
| 975 |
interval=dict(type='int'), |
| 976 |
hours_of_day=dict(type='list', elements='int'), |
| 977 |
day_interval=dict(type='int'), |
| 978 |
days_of_week=dict(type='list', elements='str', |
| 979 |
choices=['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', |
| 980 |
'THURSDAY', 'FRIDAY', 'SATURDAY']), |
| 981 |
day_of_month=dict(type='int'), |
| 982 |
hour=dict(type='int'), |
| 983 |
minute=dict(type='int'), |
| 984 |
desired_retention=dict(type='int'), |
| 985 |
retention_unit=dict(type='str', choices=['hours', 'days'], |
| 986 |
default='hours'), |
| 987 |
auto_delete=dict(type='bool'), |
| 988 |
state=dict(required=True, type='str', choices=['present', 'absent']) |
| 989 |
) |
| 990 |
|
| 991 |
|
| 992 |
def main(): |
| 993 |
""" Create Unity snapshot schedule object and perform action on it |
| 994 |
based on user input from playbook""" |
| 995 |
obj = SnapshotSchedule() |
| 996 |
obj.perform_module_operation() |
| 997 |
|
| 998 |
|
| 999 |
if __name__ == '__main__': |
| 1000 |
main() |