Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -43,3 +46,14 @@ from ais_service_discovery import call
response=call('namespace', 'service', 'handler', {<Payload>}, {'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
```

41 changes: 32 additions & 9 deletions ais_service_discovery/call_service.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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),
Expand All @@ -47,14 +69,15 @@ 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:
raise Exception('Service {}.{}.{} not found'.format(
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)
5 changes: 3 additions & 2 deletions ais_service_discovery/services/cloudmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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):
return self.filter_instances(self.client.discover_instances(
NamespaceName=namespace,
ServiceName=name
), id)

13 changes: 13 additions & 0 deletions examples.py
Original file line number Diff line number Diff line change
@@ -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)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down