Best Python code snippet using localstack_python
redshift_cross_region_snapshots.py
Source:redshift_cross_region_snapshots.py  
1#!/usr/bin/python2# Copyright: (c) 2018, JR Kerkstra <jrkerkstra@example.org>3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)4ANSIBLE_METADATA = {'status': ['preview'],5                    'supported_by': 'community',6                    'metadata_version': '1.1'}7DOCUMENTATION = '''8---9module: redshift_cross_region_snapshots10short_description: Manage Redshift Cross Region Snapshots11description:12  - Manage Redshift Cross Region Snapshots. Supports KMS-Encrypted Snapshots.13  - For more information, see https://docs.aws.amazon.com/redshift/latest/mgmt/working-with-snapshots.html#cross-region-snapshot-copy14version_added: "2.8"15author: JR Kerkstra (@captainkerk)16options:17  cluster_name:18    description:19      - The name of the cluster to configure cross-region snapshots for.20    required: true21    aliases: [ "cluster" ]22  state:23    description:24      - Create or remove the cross-region snapshot configuration.25    required: true26    choices: [ "present", "absent" ]27    default: present28  region:29    description:30      - The clusters region31    required: true32    aliases: [ "source" ]33  destination_region:34    description:35      - The region to copy snapshots to36    required: true37    aliases: [ "destination" ]38  snapshot_copy_grant:39    description:40      - A grant for Amazon Redshift to use a master key in the destination region.41      - See http://boto3.readthedocs.io/en/latest/reference/services/redshift.html#Redshift.Client.create_snapshot_copy_grant42    required: false43    aliases: [ "copy_grant" ]44  snapshot_retention_period:45    description:46      - Keep cross-region snapshots for N number of days47    required: true48    aliases: [ "retention_period" ]49requirements: [ "botocore", "boto3" ]50extends_documentation_fragment:51  - ec252  - aws53'''54EXAMPLES = '''55- name: configure cross-region snapshot on cluster `johniscool`56  redshift_cross_region_snapshots:57    cluster_name: johniscool58    state: present59    region: us-east-160    destination_region: us-west-261    retention_period: 162- name: configure cross-region snapshot on kms-encrypted cluster63  redshift_cross_region_snapshots:64    cluster_name: whatever65    state: present66    source: us-east-167    destination: us-west-268    copy_grant: 'my-grant-in-destination'69    retention_period: 1070- name: disable cross-region snapshots, necessary before most cluster modifications (rename, resize)71  redshift_cross_region_snapshots:72    cluster_name: whatever73    state: absent74    region: us-east-175    destination_region: us-west-276'''77RETURN = ''' # '''78from ansible.module_utils.aws.core import AnsibleAWSModule79from ansible.module_utils.ec2 import ec2_argument_spec80class SnapshotController(object):81    def __init__(self, client, cluster_name):82        self.client = client83        self.cluster_name = cluster_name84    def get_cluster_snapshot_copy_status(self):85        response = self.client.describe_clusters(86            ClusterIdentifier=self.cluster_name87        )88        return response['Clusters'][0].get('ClusterSnapshotCopyStatus')89    def enable_snapshot_copy(self, destination_region, grant_name, retention_period):90        if grant_name:91            self.client.enable_snapshot_copy(92                ClusterIdentifier=self.cluster_name,93                DestinationRegion=destination_region,94                RetentionPeriod=retention_period,95                SnapshotCopyGrantName=grant_name,96            )97        else:98            self.client.enable_snapshot_copy(99                ClusterIdentifier=self.cluster_name,100                DestinationRegion=destination_region,101                RetentionPeriod=retention_period,102            )103    def disable_snapshot_copy(self):104        self.client.disable_snapshot_copy(105            ClusterIdentifier=self.cluster_name106        )107    def modify_snapshot_copy_retention_period(self, retention_period):108        self.client.modify_snapshot_copy_retention_period(109            ClusterIdentifier=self.cluster_name,110            RetentionPeriod=retention_period111        )112def requesting_unsupported_modifications(actual, requested):113    if (actual['SnapshotCopyGrantName'] != requested['snapshot_copy_grant'] or114            actual['DestinationRegion'] != requested['destination_region']):115        return True116    return False117def needs_update(actual, requested):118    if actual['RetentionPeriod'] != requested['snapshot_retention_period']:119        return True120    return False121def run_module():122    argument_spec = ec2_argument_spec()123    argument_spec.update(124        dict(125            cluster_name=dict(type='str', required=True, aliases=['cluster']),126            state=dict(type='str', choices=['present', 'absent'], default='present'),127            region=dict(type='str', required=True, aliases=['source']),128            destination_region=dict(type='str', required=True, aliases=['destination']),129            snapshot_copy_grant=dict(type='str', aliases=['copy_grant']),130            snapshot_retention_period=dict(type='int', required=True, aliases=['retention_period']),131        )132    )133    module = AnsibleAWSModule(134        argument_spec=argument_spec,135        supports_check_mode=True136    )137    result = dict(138        changed=False,139        message=''140    )141    connection = module.client('redshift')142    snapshot_controller = SnapshotController(client=connection,143                                             cluster_name=module.params.get('cluster_name'))144    current_config = snapshot_controller.get_cluster_snapshot_copy_status()145    if current_config is not None:146        if module.params.get('state') == 'present':147            if requesting_unsupported_modifications(current_config, module.params):148                message = 'Cannot modify destination_region or grant_name. ' \149                          'Please disable cross-region snapshots, and re-run.'150                module.fail_json(msg=message, **result)151            if needs_update(current_config, module.params):152                result['changed'] = True153                if not module.check_mode:154                    snapshot_controller.modify_snapshot_copy_retention_period(155                        module.params.get('snapshot_retention_period')156                    )157        else:158            result['changed'] = True159            if not module.check_mode:160                snapshot_controller.disable_snapshot_copy()161    else:162        if module.params.get('state') == 'present':163            result['changed'] = True164            if not module.check_mode:165                snapshot_controller.enable_snapshot_copy(module.params.get('destination_region'),166                                                         module.params.get('snapshot_copy_grant'),167                                                         module.params.get('snapshot_retention_period'))168    module.exit_json(**result)169def main():170    run_module()171if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
