def create_policy(module, blade):
    """Create snapshot policy"""
    changed = True
    if not module.check_mode:
        try:
            if module.params['at'] and module.params['every']:
                if not module.params['every'] % 86400 == 0:
                    module.fail_json(msg='At time can only be set if every value is a multiple of 86400')
                if not module.params['timezone']:
                    module.params['timezone'] = _get_local_tz(module)
                    if module.params['timezone'] not in pytz.all_timezones_set:
                        module.fail_json(msg='Timezone {0} is not valid'.format(module.params['timezone']))
            if module.params['keep_for'] < module.params['every']:
                module.fail_json(msg='Retention period cannot be less than snapshot interval.')
            if module.params['at'] and not module.params['timezone']:
                module.params['timezone'] = _get_local_tz(module)
                if module.params['timezone'] not in set(pytz.all_timezones_set):
                    module.fail_json(msg='Timezone {0} is not valid'.format(module.params['timezone']))

            if module.params['keep_for']:
                if not 300 <= module.params['keep_for'] <= 34560000:
                    module.fail_json(msg="keep_for parameter is out of range (300 to 34560000)")
                if not 300 <= module.params['every'] <= 34560000:
                    module.fail_json(msg="every parameter is out of range (300 to 34560000)")
                if module.params['at']:
                    attr = Policy(enabled=module.params['enabled'], rules=[PolicyRule(keep_for=module.params['keep_for'] * 1000,
                                                                                      every=module.params['every'] * 1000,
                                                                                      at=_convert_to_millisecs(module.params['at']),
                                                                                      time_zone=module.params['timezone'])])
                else:
                    attr = Policy(enabled=module.params['enabled'], rules=[PolicyRule(keep_for=module.params['keep_for'] * 1000,
                                                                                      every=module.params['every'] * 1000)])
            else:
                attr = Policy(enabled=module.params['enabled'])
            blade.policies.create_policies(names=[module.params['name']], policy=attr)
        except Exception:
            module.fail_json(msg="Failed to create policy {0}.".format(module.params['name']))
        if module.params['filesystem']:
            try:
                blade.file_systems.list_file_systems(names=module.params['filesystem'])
                blade.policies.create_policy_filesystems(policy_names=[module.params['name']],
                                                         member_names=module.params['filesystem'])
            except Exception:
                delete_policy(module, blade)
                module.fail_json(msg="Failed to connect filesystems to policy {0}, "
                                 "or one of {1} doesn't exist.".format(module.params['name'],
                                                                       module.params['filesystem']))
        if module.params['replica_link']:
            for link in module.params['replica_link']:
                remote_array = blade.file_system_replica_links.list_file_system_replica_links(local_file_system_names=[link])
                try:
                    blade.policies.create_policy_file_system_replica_links(policy_names=[module.params['name']],
                                                                           member_names=[link],
                                                                           remote_names=[remote_array.items[0].remote.name])
                except Exception:
                    delete_policy(module, blade)
                    module.fail_json(msg="Failed to connect filesystem replicsa link {0} to policy {1}. "
                                     "Replica Link {0} does not exist.".format(link, module.params['name']))
    module.exit_json(changed=changed)
def update_policy(module, blade, policy):
    """Update snapshot policy"""
    changed = True
    if not module.check_mode:
        changed = False
        if not policy.rules:
            current_policy = {'time_zone': None,
                              'every': 0,
                              'keep_for': 0,
                              'at': 0,
                              'enabled': policy.enabled}
        else:
            if policy.rules[0].keep_for != 0:
                policy.rules[0].keep_for = int(policy.rules[0].keep_for / 1000)
            if policy.rules[0].every != 0:
                policy.rules[0].every = int(policy.rules[0].every / 1000)

            current_policy = {'time_zone': policy.rules[0].time_zone,
                              'every': policy.rules[0].every,
                              'keep_for': policy.rules[0].keep_for,
                              'at': policy.rules[0].at,
                              'enabled': policy.enabled}
        if not module.params['every']:
            every = 0
        else:
            every = module.params['every']
        if not module.params['keep_for']:
            keep_for = 0
        else:
            keep_for = module.params['keep_for']
        if module.params['at']:
            at_time = _convert_to_millisecs(module.params['at'])
        else:
            at_time = 0
        new_policy = {'time_zone': module.params['timezone'],
                      'every': every,
                      'keep_for': keep_for,
                      'at': at_time,
                      'enabled': module.params['enabled']}
        if new_policy['time_zone'] and new_policy['time_zone'] not in pytz.all_timezones_set:
            module.fail_json(msg='Timezone {0} is not valid'.format(module.params['timezone']))

        if current_policy != new_policy:
            if not module.params['at']:
                module.params['at'] = current_policy['at']
            if not module.params['keep_for']:
                module.params['keep_for'] = current_policy['keep_for']
            if not module.params['every']:
                module.params['every'] = current_policy['every']
            if not module.params['timezone']:
                module.params['timezone'] = current_policy['time_zone']
            if module.params['at'] and module.params['every']:
                if not module.params['every'] % 86400 == 0:
                    module.fail_json(msg='At time can only be set if every value is a multiple of 86400')
            if not module.params['timezone']:
                module.params['timezone'] = _get_local_tz(module)
                if module.params['timezone'] not in pytz.all_timezones_set:
                    module.fail_json(msg='Timezone {0} is not valid'.format(module.params['timezone']))
            if module.params['keep_for'] < module.params['every']:
                module.fail_json(msg='Retention period cannot be less than snapshot interval.')
            if module.params['at'] and not module.params['timezone']:
                module.params['timezone'] = _get_local_tz(module)
                if module.params['timezone'] not in set(pytz.all_timezones_set):
                    module.fail_json(msg='Timezone {0} is not valid'.format(module.params['timezone']))

            try:
                attr = PolicyPatch()
                attr.enabled = module.params['enabled']
                attr.add_rules = [PolicyRule(keep_for=module.params['keep_for'] * 1000,
                                             every=module.params['every'] * 1000,
                                             at=at_time,
                                             time_zone=module.params['timezone'])]
                attr.remove_rules = [PolicyRule(keep_for=current_policy['keep_for'] * 1000,
                                                every=current_policy['every'] * 1000,
                                                at=current_policy['at'],
                                                time_zone=current_policy['time_zone'])]
                blade.policies.update_policies(names=[module.params['name']],
                                               policy_patch=attr)
                changed = True
            except Exception:
                module.fail_json(msg='Failed to update policy {0}.'.format(module.params['name']))
        else:
            changed = False
    module.exit_json(changed=changed)
Esempio n. 3
0
def update_policy(module, blade, policy):
    """Update snapshot policy"""
    changed = False
    if not policy.rules:
        current_policy = {
            "time_zone": None,
            "every": 0,
            "keep_for": 0,
            "at": 0,
            "enabled": policy.enabled,
        }
    else:
        if policy.rules[0].keep_for != 0:
            policy.rules[0].keep_for = int(policy.rules[0].keep_for / 1000)
        if policy.rules[0].every != 0:
            policy.rules[0].every = int(policy.rules[0].every / 1000)

        current_policy = {
            "time_zone": policy.rules[0].time_zone,
            "every": policy.rules[0].every,
            "keep_for": policy.rules[0].keep_for,
            "at": policy.rules[0].at,
            "enabled": policy.enabled,
        }
    if not module.params["every"]:
        every = 0
    else:
        every = module.params["every"]
    if not module.params["keep_for"]:
        keep_for = 0
    else:
        keep_for = module.params["keep_for"]
    if module.params["at"]:
        at_time = _convert_to_millisecs(module.params["at"])
    else:
        at_time = None
    if not module.params["timezone"]:
        timezone = _get_local_tz(module)
    else:
        timezone = module.params["timezone"]
    if at_time:
        new_policy = {
            "time_zone": timezone,
            "every": every,
            "keep_for": keep_for,
            "at": at_time,
            "enabled": module.params["enabled"],
        }
    else:
        new_policy = {
            "time_zone": None,
            "every": every,
            "keep_for": keep_for,
            "at": None,
            "enabled": module.params["enabled"],
        }
    if (new_policy["time_zone"]
            and new_policy["time_zone"] not in pytz.all_timezones_set):
        module.fail_json(
            msg="Timezone {0} is not valid".format(module.params["timezone"]))

    if current_policy != new_policy:
        if not module.params["at"]:
            module.params["at"] = current_policy["at"]
        if not module.params["keep_for"]:
            module.params["keep_for"] = current_policy["keep_for"]
        if not module.params["every"]:
            module.params["every"] = current_policy["every"]
        if module.params["at"] and module.params["every"]:
            if not module.params["every"] % 86400 == 0:
                module.fail_json(
                    msg=
                    "At time can only be set if every value is a multiple of 86400"
                )
        if module.params["keep_for"] < module.params["every"]:
            module.fail_json(
                msg="Retention period cannot be less than snapshot interval.")
        if module.params["at"] and not module.params["timezone"]:
            module.params["timezone"] = _get_local_tz(module)
            if module.params["timezone"] not in set(pytz.all_timezones_set):
                module.fail_json(msg="Timezone {0} is not valid".format(
                    module.params["timezone"]))

        changed = True
        if not module.check_mode:
            try:
                attr = PolicyPatch()
                attr.enabled = module.params["enabled"]
                if at_time:
                    attr.add_rules = [
                        PolicyRule(
                            keep_for=module.params["keep_for"] * 1000,
                            every=module.params["every"] * 1000,
                            at=at_time,
                            time_zone=timezone,
                        )
                    ]
                else:
                    attr.add_rules = [
                        PolicyRule(
                            keep_for=module.params["keep_for"] * 1000,
                            every=module.params["every"] * 1000,
                        )
                    ]
                attr.remove_rules = [
                    PolicyRule(
                        keep_for=current_policy["keep_for"] * 1000,
                        every=current_policy["every"] * 1000,
                        at=current_policy["at"],
                        time_zone=current_policy["time_zone"],
                    )
                ]
                blade.policies.update_policies(names=[module.params["name"]],
                                               policy_patch=attr)
            except Exception:
                module.fail_json(msg="Failed to update policy {0}.".format(
                    module.params["name"]))
    module.exit_json(changed=changed)