Skip to content

Commit

Permalink
Allow local dynamodb to be installed on another host than localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
peerjakobsen authored and Nusnus committed May 2, 2024
1 parent 7ce2e41 commit 4a4c00c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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):
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

0 comments on commit 4a4c00c

Please sign in to comment.