From e45fe4f34d196820544ad538a87b53b65dfba076 Mon Sep 17 00:00:00 2001 From: Waldemar Hummer Date: Sat, 8 Jul 2017 17:13:57 +1000 Subject: [PATCH] refactor code; add missing service endpoints; enable SSL connections --- README.md | 5 ++++ localstack_client/config.py | 30 +++++++++++++++++++ .../{localstack_client.py => session.py} | 18 +++-------- setup.py | 2 +- tests/client/test_python_client.py | 4 +-- 5 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 localstack_client/config.py rename localstack_client/{localstack_client.py => session.py} (75%) diff --git a/README.md b/README.md index f29d748..2cec5bf 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,11 @@ make install make test ``` +## Changelog + +* v0.2: Add missing service endpoints; enable SSL connections; put default endpoints into config.py +* v0.1: Initial version + ## License The LocalStack Python Client is released under the Apache License, Version 2.0 (see `LICENSE`). diff --git a/localstack_client/config.py b/localstack_client/config.py new file mode 100644 index 0000000..db49ef1 --- /dev/null +++ b/localstack_client/config.py @@ -0,0 +1,30 @@ +import os +import json + +_service_endpoints_template = { + 'apigateway': '{proto}://{host}:4567', + 'kinesis': '{proto}://{host}:4568', + 'dynamodb': '{proto}://{host}:4569', + 'dynamodbstreams': '{proto}://{host}:4570', + 'elasticsearch': '{proto}://{host}:4571', + 's3': '{proto}://{host}:4572', + 'firehose': '{proto}://{host}:4573', + 'lambda': '{proto}://{host}:4574', + 'sns': '{proto}://{host}:4575', + 'sqs': '{proto}://{host}:4576', + 'redshift': '{proto}://{host}:4577', + 'es': '{proto}://{host}:4578', + 'ses': '{proto}://{host}:4579', + 'route53': '{proto}://{host}:4580', + 'cloudformation': '{proto}://{host}:4581', + 'cloudwatch': '{proto}://{host}:4582' +} + + +def get_service_endpoints(localstack_host=None): + if localstack_host is None: + localstack_host = os.environ.get('LOCALSTACK_HOST', 'localhost') + protocol = 'https' if os.environ.get('USE_SSL') in ('1', 'true') else 'http' + + return json.loads(json.dumps(_service_endpoints_template) + .replace('{proto}', protocol).replace('{host}', localstack_host)) diff --git a/localstack_client/localstack_client.py b/localstack_client/session.py similarity index 75% rename from localstack_client/localstack_client.py rename to localstack_client/session.py index 933ce78..fda49eb 100644 --- a/localstack_client/localstack_client.py +++ b/localstack_client/session.py @@ -1,6 +1,7 @@ import os import boto3.session from botocore.credentials import Credentials +from localstack_client import config DEFAULT_SESSION = None @@ -19,18 +20,7 @@ def __init__(self, aws_access_key_id='accesskey', aws_secret_access_key='secretk self.aws_secret_access_key = aws_secret_access_key self.aws_session_token = aws_session_token self.region_name = region_name - - if localstack_host is None: - localstack_host = os.environ.get('LOCALSTACK_HOST', 'localhost') - - self._service_endpoint_mapping = { - 'kinesis': 'http://%s:4568' % (localstack_host), - 'dynamodb': 'http://%s:4569' % (localstack_host), - 's3': 'http://%s:4572' % (localstack_host), - 'es': 'http://%s:4578' % (localstack_host), - 'firehose': 'http://%s:4573' % (localstack_host), - 'sqs': 'http://%s:4576' % (localstack_host) - } + self._service_endpoint_mapping = config.get_service_endpoints(localstack_host) def get_credentials(self): """ @@ -45,14 +35,14 @@ def client(self, service_name, **kwargs): raise Exception('%s is not supported by this mock session.' % (service_name)) return boto3.client(service_name, endpoint_url=self._service_endpoint_mapping[service_name], - region_name=self.region_name) + region_name=self.region_name, verify=False) def resource(self, service_name, **kwargs): if service_name not in self._service_endpoint_mapping: raise Exception('%s is not supported by this mock session.' % (service_name)) return boto3.resource(service_name, endpoint_url=self._service_endpoint_mapping[service_name], - region_name=self.region_name) + region_name=self.region_name, verify=False) def _get_default_session(): diff --git a/setup.py b/setup.py index 2fac653..c0a6430 100755 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='localstack-client', - version='0.1', + version='0.2', description='A lightweight python client for LocalStack.', author='Jeff Wu, Waldemar Hummer', author_email='jeffrey.yung.wu@gmail.com, waldemar.hummer@gmail.com', diff --git a/tests/client/test_python_client.py b/tests/client/test_python_client.py index 2f54cc6..dfeb98e 100644 --- a/tests/client/test_python_client.py +++ b/tests/client/test_python_client.py @@ -1,7 +1,7 @@ -from localstack_client import localstack_client +import localstack_client.session def test_session(): - session = localstack_client.Session() + session = localstack_client.session.Session() sqs = session.client('sqs') assert sqs.list_queues() is not None