diff --git a/README.md b/README.md index ba096f7..0b66930 100644 --- a/README.md +++ b/README.md @@ -18,15 +18,18 @@ This repository interfaces Service Discovery, in this instance CloudMap, in orde ## TODO -- Lambda (`request`). - SQS (`listen`). - Http (`request`|`call`). - Fargate/ECS Task (`run`). +- Lambda (`request`). + + +## Note -## Note: This library requires *Python 3.5 and above*. -## Examples: + +## Examples ### Lambda Call @@ -43,3 +46,14 @@ from ais_service_discovery import call response=call('namespace', 'service', 'handler', {}, {'InvocationType': 'Event'}) print(response) ``` + +## Configuration + +This library can utilise the following environment variables: + +``` +BOTO_MAX_ATTEMPTS=10 // Boto3 exponential back-off attemps +BOTO_READ_TIMEOUT=300 // Boto3 timeout +BOTO_CONNECTION_TIMEOUT=60 // Boto3 connection timeout +``` + diff --git a/ais_service_discovery/call_service.py b/ais_service_discovery/call_service.py index 39bef3a..4af3df1 100644 --- a/ais_service_discovery/call_service.py +++ b/ais_service_discovery/call_service.py @@ -1,15 +1,35 @@ +""" +call_service + +Calls services via the service discovery library +""" +from os import environ as env +from json import dumps + from boto3 import client -from .services import default_namespace, extract_service_parts, Services, \ +from botocore.config import Config + +from .services import default_namespace, Services, \ CloudmapAdapter from .functions import LambdaAdaptor, Func from .sns import SnsAdaptor, Publish from .sqs import SqsAdaptor, Send -from json import dumps -function = client('lambda') -sns = client('sns') -sqs = client('sqs') -service_discovery = client('servicediscovery') + +BOTO_MAX_ATTEMPTS = int(env.get('BOTO_MAX_ATTEMPTS', 1)) +BOTO_READ_TIMEOUT = float(env.get('BOTO_READ_TIMEOUT', 5)) +BOTO_CONNECTION_TIMEOUT = float(env.get('BOTO_CONNECTION_TIMEOUT', 60)) + +config = Config( + read_timeout=BOTO_READ_TIMEOUT, + retries={'total_max_attempts': BOTO_MAX_ATTEMPTS}, + connect_timeout=BOTO_CONNECTION_TIMEOUT +) + +function = client('lambda', config=config) +sns = client('sns', config=config) +sqs = client('sqs', config=config) +service_discovery = client('servicediscovery', config=config) cloudmap_adaptor = CloudmapAdapter(service_discovery) services = Services(cloudmap_adaptor) @@ -23,9 +43,11 @@ sqs_adaptor = SqsAdaptor(sqs) send = Send(sqs_adaptor) + def run_service(service, body, opts={}): - type = service['Attributes']['type'] - if type in ['cloud-function', 'function', 'lambda']: + """ run_service """ + service_type = service['Attributes']['type'] + if service_type in ['cloud-function', 'function', 'lambda']: return func.call(**{ 'FunctionName': service['Attributes']['arn'], 'Payload': dumps(body), @@ -47,6 +69,7 @@ def run_service(service, body, opts={}): def call_service(namespace, service, handler, body, opts={}): + """ call_service """ instances = services.discover( namespace or default_namespace(), service, handler)['Instances'] if not instances: @@ -54,7 +77,7 @@ def call_service(namespace, service, handler, body, opts={}): namespace or default_namespace(), service, handler)) [service_to_run] = instances payload = run_service(service_to_run, body, opts) - if (payload and type(payload) is dict and + if (payload and isinstance(payload, dict) and ('errorMessage' in payload) and ('errorMessage' in payload)): raise Exception(payload) return dumps(payload) diff --git a/ais_service_discovery/services/cloudmap.py b/ais_service_discovery/services/cloudmap.py index 00c722a..7967a7b 100644 --- a/ais_service_discovery/services/cloudmap.py +++ b/ais_service_discovery/services/cloudmap.py @@ -4,10 +4,10 @@ def __init__(self, client): def filter_instances(self, response, id): filter_function = lambda instance : instance['InstanceId'] == id - + return { **response, - 'Instances': filter(filter_function, response['Instances']) + 'Instances': list(filter(filter_function, response['Instances'])) } def discover(self, namespace, name, id): @@ -15,3 +15,4 @@ def discover(self, namespace, name, id): NamespaceName=namespace, ServiceName=name ), id) + diff --git a/examples.py b/examples.py new file mode 100644 index 0000000..ab3b815 --- /dev/null +++ b/examples.py @@ -0,0 +1,13 @@ +from ais_service_discovery import call + +try: + res = call( + 'my-namespace', + 'my-service', + 'my-instance', { + 'arg': 'hello-word', + }) + print(res) +except Exception as e: + print(e) + diff --git a/setup.py b/setup.py index f7c44f8..cddee63 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='ais_service_discovery', - version='0.2.0', + version='0.2.1-rc7', author='Peak AI', author_email='infra-notifications@peak.ai', description='AIS service discovery package for python3',