Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Add special case for connecting to S3
Browse files Browse the repository at this point in the history
The typical boto.connect_s3 method doesn't take a 'region' argument like
most of the others, so we use boto.s3.connect_to_region instead.
  • Loading branch information
djmitche committed Mar 9, 2015
1 parent 65443fc commit 719c811
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
22 changes: 19 additions & 3 deletions relengapi/lib/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def connect_to(self, service_name, region_name):
if key in self._connections:
return self._connections[key]

# handle special cases
try:
fn = getattr(self, 'connect_to_' + service_name)
except AttributeError:
fn = self.connect_to_default
conn = fn(service_name, region_name)
self._connections[key] = conn
return conn

def connect_to_default(self, service_name, region_name):
# for the service, import 'boto.$service'
service = importlib.import_module('boto.' + service_name)

Expand All @@ -45,12 +55,18 @@ def connect_to(self, service_name, region_name):
raise RuntimeError("invalid region %r" % (region_name,))

connect_fn = getattr(boto, 'connect_' + service_name)
conn = connect_fn(
return connect_fn(
aws_access_key_id=self.config.get('access_key_id'),
aws_secret_access_key=self.config.get('secret_access_key'),
region=region)
self._connections[key] = conn
return conn

def connect_to_s3(self, service_name, region_name):
# special case for S3, which boto does differently than
# the other services
import boto.s3
return boto.s3.connect_to_region(region_name=region_name,
aws_access_key_id=self.config.get('access_key_id'),
aws_secret_access_key=self.config.get('secret_access_key'))

def get_sqs_queue(self, region_name, queue_name):
key = (region_name, queue_name)
Expand Down
15 changes: 14 additions & 1 deletion relengapi/tests/test_lib_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@
}


@mock_sqs
@test_context.specialize(config=aws_cfg)
def test_connect_to_s3(app):
# for S3, boto doesn't support a 'region' argument to connect_s3, so we use
# boto.s3.connect_to_region instead
with mock.patch('boto.s3.connect_to_region', return_value='s3_conn') as ctr:
eq_(app.aws.connect_to('s3', 'us-west-2'), 's3_conn')
ctr.assert_called_with(
aws_access_key_id='aa',
aws_secret_access_key='ss',
region_name='us-west-2')
# connection is cached
eq_(app.aws.connect_to('s3', 'us-west-2'), 's3_conn')


@test_context.specialize(config=aws_cfg)
def test_connect_to(app):
with mock.patch('boto.connect_sqs', return_value='sqs_conn') as connect_sqs:
Expand Down

0 comments on commit 719c811

Please sign in to comment.