ashb commented on a change in pull request #8701: URL: https://github.com/apache/airflow/pull/8701#discussion_r455349067
########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] Review comment: ```suggestion return self.describe_replication_group(replication_group_id)["ReplicationGroups"][0]["Status"] ``` ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE Review comment: ```suggestion return self.get_replication_group_status(replication_group_id) == "available" ``` ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE + + def _has_reached_terminal_state(self, replication_group_id): + """ + Helper for checking if we should stop poking replication group for availability or not + + :param replication_group_id: ID of replication group to check + :return: Flag to check if availability check should be stopped or not and current status + """ + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + return status in ( + self.STATUS_AVAILABLE, + self.STATUS_CREATE_FAILED, + self.STATUS_DELETING + ), status + + def wait_for_availability( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Check if replication is available or not by performing a describe over it + + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in seconds + :param replication_group_id: ID of replication group to check for availability + :return: True if replication is available else False + """ + sleep_time = initial_sleep_time or self.initial_poke_interval + exponential_back_off_factor = exponential_back_off_factor or self.exponential_back_off_factor + max_retries = max_retries or self.max_retries + num_retries = 0 + status = self.STATUS_CREATE_FAILED + + while num_retries < max_retries: + stop_poking, status = self._has_reached_terminal_state(replication_group_id=replication_group_id) + + self.log.info( + 'Current status of replication group with ID : %s is %s', + replication_group_id, + status + ) + + if stop_poking: + break + + num_retries += 1 + + self.log.info('Poke retry: %s. Sleep time: %s. Sleeping...', num_retries, sleep_time) + + sleep(sleep_time) + + sleep_time = sleep_time * exponential_back_off_factor + + if status != self.STATUS_AVAILABLE: Review comment: ```suggestion if status != "available": ``` ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE + + def _has_reached_terminal_state(self, replication_group_id): + """ + Helper for checking if we should stop poking replication group for availability or not + + :param replication_group_id: ID of replication group to check + :return: Flag to check if availability check should be stopped or not and current status + """ + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + return status in ( + self.STATUS_AVAILABLE, + self.STATUS_CREATE_FAILED, + self.STATUS_DELETING + ), status + + def wait_for_availability( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Check if replication is available or not by performing a describe over it + + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in seconds + :param replication_group_id: ID of replication group to check for availability Review comment: Could you add types to these please? ```suggestion :param max_retries: Max tries for checking availability of replication group :type max_retries: int :param exponential_back_off_factor: Factor for deciding next sleep time :type exponential_back_off_factor: ... :param initial_sleep_time: Initial sleep time in seconds :type initial_sleep_time: :param replication_group_id: ID of replication group to check for availability :type replication_group_id: ``` Same for all the other public methods too please. ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE + + def _has_reached_terminal_state(self, replication_group_id): + """ + Helper for checking if we should stop poking replication group for availability or not + + :param replication_group_id: ID of replication group to check + :return: Flag to check if availability check should be stopped or not and current status + """ + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + return status in ( + self.STATUS_AVAILABLE, + self.STATUS_CREATE_FAILED, + self.STATUS_DELETING + ), status + + def wait_for_availability( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Check if replication is available or not by performing a describe over it + + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in seconds + :param replication_group_id: ID of replication group to check for availability + :return: True if replication is available else False + """ + sleep_time = initial_sleep_time or self.initial_poke_interval + exponential_back_off_factor = exponential_back_off_factor or self.exponential_back_off_factor + max_retries = max_retries or self.max_retries + num_retries = 0 + status = self.STATUS_CREATE_FAILED + + while num_retries < max_retries: + stop_poking, status = self._has_reached_terminal_state(replication_group_id=replication_group_id) + + self.log.info( + 'Current status of replication group with ID : %s is %s', + replication_group_id, + status + ) + + if stop_poking: + break + + num_retries += 1 + + self.log.info('Poke retry: %s. Sleep time: %s. Sleeping...', num_retries, sleep_time) + + sleep(sleep_time) + + sleep_time = sleep_time * exponential_back_off_factor + + if status != self.STATUS_AVAILABLE: + self.log.warning('Replication group is not available. Current status : "%s"', status) + + return False + + return True + + def wait_for_deletion( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Helper for deleting a replication group ensuring it is either deleted or can't be deleted + + :param replication_group_id: ID of replication to delete + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in second + :return: Response from ElastiCache delete replication group API and flag to identify if deleted or not + """ + deleted = False + sleep_time = initial_sleep_time or self.initial_poke_interval + exponential_back_off_factor = exponential_back_off_factor or self.exponential_back_off_factor + max_retries = max_retries or self.max_retries + num_retries = 0 + response = None + + while not deleted and num_retries < max_retries: + try: + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + self.log.info( + 'Current status of replication group with ID : %s is %s', + replication_group_id, + status + ) + + # Can only delete if status is `available` + # Status becomes `deleting` after this call so this will only run once + if status == self.STATUS_AVAILABLE: + response = self.delete_replication_group(replication_group_id=replication_group_id) + + except self.conn.exceptions.ReplicationGroupNotFoundFault: + self.log.info("Replication group with ID : '%s' does not exist", replication_group_id) + + deleted = True + + # This should never occur as we only issue a delete request when status is `available` + # which is a valid status for deletion. Still handling for safety. + except self.conn.exceptions.InvalidReplicationGroupStateFault as exp: + # status Error Response + # creating - Cache cluster <cluster_id> is not in a valid state to be deleted. + # deleting - Replication group <replication_group_id> has status deleting which is not valid + # for deletion. + # modifying - Replication group <replication_group_id> has status deleting which is not valid + # for deletion. + + message = exp.response['Error']['Message'] + + self.log.info('Received message : %s', message) Review comment: (Or maybe at warning?) ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' Review comment: ```suggestion ``` These constants make it harder to understand the code and isn't the style we use in Airfow, please use the string literals directly. ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE + + def _has_reached_terminal_state(self, replication_group_id): + """ + Helper for checking if we should stop poking replication group for availability or not + + :param replication_group_id: ID of replication group to check + :return: Flag to check if availability check should be stopped or not and current status + """ + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + return status in ( + self.STATUS_AVAILABLE, + self.STATUS_CREATE_FAILED, + self.STATUS_DELETING + ), status + + def wait_for_availability( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Check if replication is available or not by performing a describe over it + + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in seconds + :param replication_group_id: ID of replication group to check for availability + :return: True if replication is available else False + """ + sleep_time = initial_sleep_time or self.initial_poke_interval + exponential_back_off_factor = exponential_back_off_factor or self.exponential_back_off_factor + max_retries = max_retries or self.max_retries + num_retries = 0 + status = self.STATUS_CREATE_FAILED Review comment: ```suggestion status = "not-found" ``` ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE + + def _has_reached_terminal_state(self, replication_group_id): + """ + Helper for checking if we should stop poking replication group for availability or not + + :param replication_group_id: ID of replication group to check + :return: Flag to check if availability check should be stopped or not and current status + """ + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + return status in ( + self.STATUS_AVAILABLE, + self.STATUS_CREATE_FAILED, + self.STATUS_DELETING + ), status Review comment: ```suggestion return status in ( "available", "create-failed", "deleting" ), status ``` This one could be worth extracting to a class variable: ```python TERMINAL_STATES = frozenset({"available", "create-failed", "deleting"}) ``` And then this becomes ```suggestion return status in self.TERMINAL_STATES, status ``` ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE + + def _has_reached_terminal_state(self, replication_group_id): + """ + Helper for checking if we should stop poking replication group for availability or not + + :param replication_group_id: ID of replication group to check + :return: Flag to check if availability check should be stopped or not and current status + """ + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + return status in ( + self.STATUS_AVAILABLE, + self.STATUS_CREATE_FAILED, + self.STATUS_DELETING + ), status + + def wait_for_availability( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Check if replication is available or not by performing a describe over it Review comment: This doc talks about replication -- but is it just health of elasticache? (Because this still applies when a single node EC is used, right?) ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE + + def _has_reached_terminal_state(self, replication_group_id): + """ + Helper for checking if we should stop poking replication group for availability or not + + :param replication_group_id: ID of replication group to check + :return: Flag to check if availability check should be stopped or not and current status + """ + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + return status in ( + self.STATUS_AVAILABLE, + self.STATUS_CREATE_FAILED, + self.STATUS_DELETING + ), status + + def wait_for_availability( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Check if replication is available or not by performing a describe over it + + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in seconds + :param replication_group_id: ID of replication group to check for availability + :return: True if replication is available else False + """ + sleep_time = initial_sleep_time or self.initial_poke_interval + exponential_back_off_factor = exponential_back_off_factor or self.exponential_back_off_factor + max_retries = max_retries or self.max_retries + num_retries = 0 + status = self.STATUS_CREATE_FAILED + + while num_retries < max_retries: + stop_poking, status = self._has_reached_terminal_state(replication_group_id=replication_group_id) + + self.log.info( + 'Current status of replication group with ID : %s is %s', + replication_group_id, + status + ) + + if stop_poking: + break + + num_retries += 1 + + self.log.info('Poke retry: %s. Sleep time: %s. Sleeping...', num_retries, sleep_time) + + sleep(sleep_time) + + sleep_time = sleep_time * exponential_back_off_factor + + if status != self.STATUS_AVAILABLE: + self.log.warning('Replication group is not available. Current status : "%s"', status) + + return False + + return True + + def wait_for_deletion( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Helper for deleting a replication group ensuring it is either deleted or can't be deleted + + :param replication_group_id: ID of replication to delete + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in second + :return: Response from ElastiCache delete replication group API and flag to identify if deleted or not + """ + deleted = False + sleep_time = initial_sleep_time or self.initial_poke_interval + exponential_back_off_factor = exponential_back_off_factor or self.exponential_back_off_factor + max_retries = max_retries or self.max_retries + num_retries = 0 + response = None + + while not deleted and num_retries < max_retries: + try: + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + self.log.info( + 'Current status of replication group with ID : %s is %s', + replication_group_id, + status + ) + + # Can only delete if status is `available` + # Status becomes `deleting` after this call so this will only run once + if status == self.STATUS_AVAILABLE: + response = self.delete_replication_group(replication_group_id=replication_group_id) + + except self.conn.exceptions.ReplicationGroupNotFoundFault: + self.log.info("Replication group with ID : '%s' does not exist", replication_group_id) Review comment: ```suggestion self.log.info("Replication group with ID '%s' does not exist", replication_group_id) ``` ########## File path: tests/providers/amazon/aws/hooks/test_elasticache.py ########## @@ -0,0 +1,188 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from unittest import TestCase +from unittest.mock import Mock + +from airflow.exceptions import AirflowException + + +class TestElastiCacheHook(TestCase): + """ + Test ElastiCacheHook + """ + replication_group_id = "test-elasticache-hook" + + replication_group_config = { + 'ReplicationGroupId': replication_group_id, + 'ReplicationGroupDescription': replication_group_id, + 'AutomaticFailoverEnabled': False, + 'NumCacheClusters': 1, + 'CacheNodeType': 'cache.m5.large', + 'Engine': 'redis', + 'EngineVersion': '5.0.4', + 'CacheParameterGroupName': 'default.redis5.0' + } + + valid_status = ('creating', 'available', 'modifying', 'deleting', 'create - failed', 'snapshotting') + + hook = Mock() + + def test_get_conn_not_none(self): + """ + Test connection is not None + """ + self.assertIsNotNone(self.hook.conn) + + def test_create_replication_group(self): + """ + Test creation of replication group + """ + self.hook.create_replication_group.return_value = { + "ReplicationGroup": { + "ReplicationGroupId": self.replication_group_id, + "Status": "creating" + } + } + response = self.hook.create_replication_group(config=self.replication_group_config) + + assert response["ReplicationGroup"]["ReplicationGroupId"] == self.replication_group_id + assert response["ReplicationGroup"]["Status"] == "creating" Review comment: This isn't testing real code here -- you are only calling a mock function but not actually calling the real code anywhere from the hook. ########## File path: airflow/providers/amazon/aws/hooks/elasticache.py ########## @@ -0,0 +1,265 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from time import sleep + +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook + + +class ElastiCacheHook(AwsBaseHook): + """ + Interact with AWS ElastiCache + """ + + # Constants for ElastiCache describe API response keys + REPLICATION_GROUPS = 'ReplicationGroups' + REPLICATION_GROUP = 'ReplicationGroup' + STATUS = 'Status' + + # Constants for ElastiCache replication group status + STATUS_CREATING = 'creating' + STATUS_AVAILABLE = 'available' + STATUS_DELETING = 'deleting' + STATUS_CREATE_FAILED = 'create-failed' + STATUS_MODIFYING = 'modifying' + STATUS_SNAPSHOTTING = 'snapshotting' + + def __init__( + self, max_retries=10, exponential_back_off_factor=1, initial_poke_interval=60, *args, **kwargs + ): + """ + :param max_retries: Max tries for checking availability of and deleting replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_poke_interval: Initial sleep time in seconds + """ + self.max_retries = max_retries + self.exponential_back_off_factor = exponential_back_off_factor + self.initial_poke_interval = initial_poke_interval + + super().__init__(client_type='elasticache', *args, **kwargs) + + def create_replication_group(self, config): + """ + Call ElastiCache API for creating a replication group + + :param config: Python dictionary to use as config for creating replication group + :return: Response from ElastiCache create replication group API + """ + return self.conn.create_replication_group(**config) + + def delete_replication_group(self, replication_group_id): + """ + Call ElastiCache API for deleting a replication group + + :param replication_group_id: ID of replication group to delete + :return: Response from ElastiCache delete replication group API + """ + return self.conn.delete_replication_group(ReplicationGroupId=replication_group_id) + + def describe_replication_group(self, replication_group_id): + """ + Call ElastiCache API for describing a replication group + + :param replication_group_id: ID of replication group to describe + :return: Response from ElastiCache describe replication group API + """ + return self.conn.describe_replication_groups(ReplicationGroupId=replication_group_id) + + def get_replication_group_status(self, replication_group_id): + """ + Get current status of replication group + + :param replication_group_id: ID of replication group to check for status + :return: Current status of replication group + """ + return self.describe_replication_group(replication_group_id)[self.REPLICATION_GROUPS][0][self.STATUS] + + def is_replication_group_available(self, replication_group_id): + """ + Helper for checking if replication group is available or not + + :param replication_group_id: ID of replication group to check for availability + :return: True if available else False + """ + return self.get_replication_group_status(replication_group_id) == self.STATUS_AVAILABLE + + def _has_reached_terminal_state(self, replication_group_id): + """ + Helper for checking if we should stop poking replication group for availability or not + + :param replication_group_id: ID of replication group to check + :return: Flag to check if availability check should be stopped or not and current status + """ + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + return status in ( + self.STATUS_AVAILABLE, + self.STATUS_CREATE_FAILED, + self.STATUS_DELETING + ), status + + def wait_for_availability( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Check if replication is available or not by performing a describe over it + + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in seconds + :param replication_group_id: ID of replication group to check for availability + :return: True if replication is available else False + """ + sleep_time = initial_sleep_time or self.initial_poke_interval + exponential_back_off_factor = exponential_back_off_factor or self.exponential_back_off_factor + max_retries = max_retries or self.max_retries + num_retries = 0 + status = self.STATUS_CREATE_FAILED + + while num_retries < max_retries: + stop_poking, status = self._has_reached_terminal_state(replication_group_id=replication_group_id) + + self.log.info( + 'Current status of replication group with ID : %s is %s', + replication_group_id, + status + ) + + if stop_poking: + break + + num_retries += 1 + + self.log.info('Poke retry: %s. Sleep time: %s. Sleeping...', num_retries, sleep_time) + + sleep(sleep_time) + + sleep_time = sleep_time * exponential_back_off_factor + + if status != self.STATUS_AVAILABLE: + self.log.warning('Replication group is not available. Current status : "%s"', status) + + return False + + return True + + def wait_for_deletion( + self, + replication_group_id, + initial_sleep_time=None, + exponential_back_off_factor=None, + max_retries=None + ): + """ + Helper for deleting a replication group ensuring it is either deleted or can't be deleted + + :param replication_group_id: ID of replication to delete + :param max_retries: Max tries for checking availability of replication group + :param exponential_back_off_factor: Factor for deciding next sleep time + :param initial_sleep_time: Initial sleep time in second + :return: Response from ElastiCache delete replication group API and flag to identify if deleted or not + """ + deleted = False + sleep_time = initial_sleep_time or self.initial_poke_interval + exponential_back_off_factor = exponential_back_off_factor or self.exponential_back_off_factor + max_retries = max_retries or self.max_retries + num_retries = 0 + response = None + + while not deleted and num_retries < max_retries: + try: + status = self.get_replication_group_status(replication_group_id=replication_group_id) + + self.log.info( + 'Current status of replication group with ID : %s is %s', + replication_group_id, + status + ) + + # Can only delete if status is `available` + # Status becomes `deleting` after this call so this will only run once + if status == self.STATUS_AVAILABLE: + response = self.delete_replication_group(replication_group_id=replication_group_id) + + except self.conn.exceptions.ReplicationGroupNotFoundFault: + self.log.info("Replication group with ID : '%s' does not exist", replication_group_id) + + deleted = True + + # This should never occur as we only issue a delete request when status is `available` + # which is a valid status for deletion. Still handling for safety. + except self.conn.exceptions.InvalidReplicationGroupStateFault as exp: + # status Error Response + # creating - Cache cluster <cluster_id> is not in a valid state to be deleted. + # deleting - Replication group <replication_group_id> has status deleting which is not valid + # for deletion. + # modifying - Replication group <replication_group_id> has status deleting which is not valid + # for deletion. + + message = exp.response['Error']['Message'] + + self.log.info('Received message : %s', message) Review comment: ```suggestion self.log.error('Received error message from AWS API: %s', message) ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
