Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow local dynamodb to be installed on another host than localhost #8965

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions celery/backends/dynamodb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""AWS DynamoDB result store backend."""
from collections import namedtuple
from ipaddress import ip_address
from time import sleep, time
from typing import Any, Dict

Expand Down Expand Up @@ -96,9 +97,9 @@ def __init__(self, url=None, table_name=None, *args, **kwargs):

aws_credentials_given = access_key_given

if region == 'localhost':
if region == 'localhost' or DynamoDBBackend._is_valid_ip(region):
# We are using the downloadable, local version of DynamoDB
self.endpoint_url = f'http://localhost:{port}'
self.endpoint_url = f'http://{region}:{port}'
self.aws_region = 'us-east-1'
logger.warning(
'Using local-only DynamoDB endpoint URL: {}'.format(
Expand Down Expand Up @@ -153,6 +154,14 @@ def __init__(self, url=None, table_name=None, *args, **kwargs):
secret_access_key=aws_secret_access_key
)

@staticmethod
def _is_valid_ip(ip):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if that's the best way to check it.. or if it's worth delaying for 🤔

Anyway, should not be merged before v5.4 release. We're in RC2 at the moment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I could do
pattern = re.compile(r'^[a-z]{2}-[a-z]+-[0-9]{1}$')
if pattern.match(input_string):
do real dynamodb
else:
do local dynamodb
if we trust the AWS region names keeps following the current format of xx-yyyy-n
That would also allow to use non ip hosts names.

try:
ip_address(ip)
return True
except ValueError:
return False

def _get_client(self, access_key_id=None, secret_access_key=None):
"""Get client connection."""
if self._client is None:
Expand Down
11 changes: 7 additions & 4 deletions t/unit/backends/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,26 @@ def test_get_client_explicit_endpoint(self):
)
assert backend.endpoint_url == 'http://my.domain.com:666'

def test_get_client_local(self):
@pytest.mark.parametrize("dynamodb_host", [
'localhost', '127.0.0.1',
])
def test_get_client_local(self, dynamodb_host):
table_creation_path = \
'celery.backends.dynamodb.DynamoDBBackend._get_or_create_table'
with patch('boto3.client') as mock_boto_client, \
patch(table_creation_path):
backend = DynamoDBBackend(
app=self.app,
url='dynamodb://@localhost:8000'
url=f'dynamodb://@{dynamodb_host}:8000'
)
client = backend._get_client()
assert backend.client is client
mock_boto_client.assert_called_once_with(
'dynamodb',
endpoint_url='http://localhost:8000',
endpoint_url=f'http://{dynamodb_host}:8000',
region_name='us-east-1'
)
assert backend.endpoint_url == 'http://localhost:8000'
assert backend.endpoint_url == f'http://{dynamodb_host}:8000'

def test_get_client_credentials(self):
table_creation_path = \
Expand Down