Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add profile_name option to sessions #69

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 14 additions & 1 deletion boto3/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ class Session(object):
:type botocore_session: botocore.session.Session
:param botocore_session: Use this Botocore session instead of creating
a new default one.
:type profile_name: string
:param profile_name: The name of a profile to use. If not given, then
the default profile is used.
"""
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
aws_session_token=None, region_name=None,
botocore_session=None):
botocore_session=None, profile_name=None):
if botocore_session is not None:
self._session = botocore_session
else:
Expand All @@ -58,6 +61,9 @@ def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
self._session.user_agent_name = 'Boto3'
self._session.user_agent_version = boto3.__version__

if profile_name is not None:
self._session.profile = profile_name

if aws_access_key_id or aws_secret_access_key or aws_session_token:
self._session.set_credentials(aws_access_key_id,
aws_secret_access_key, aws_session_token)
Expand All @@ -72,6 +78,13 @@ def __repr__(self):
return 'Session(region={0})'.format(
repr(self._session.get_config_variable('region')))

@property
def profile_name(self):
"""
The **read-only** profile name.
"""
return self._session.profile or 'default'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would strongly prefer that this always return a string, but it could be either 'default' or a blank string. It makes code like this simpler:

if session.profile.startswith('prod'):
    # Do something here!
    pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is probably ok at this layer. I believe the reason it's like that in botocore is because parts of the code need to distinguish between "was explicitly set to default by the user" and "try the default profile if one hasn't been set". The former case is an error, the latter case just means move on to the next credential provider.


def _setup_loader(self):
"""
Setup loader paths so that we can load resources.
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ def test_credentials_can_be_set(self):
bc_session.set_credentials.assert_called_with(
'key', 'secret', 'token')

def test_profile_can_be_set(self):
bc_session = self.bc_session_cls.return_value

session = Session(profile_name='foo')

self.assertEqual(bc_session.profile, 'foo')

# We should also be able to read the value
self.assertEqual(session.profile_name, 'foo')

def test_profile_default(self):
self.bc_session_cls.return_value.profile = None

session = Session()

self.assertEqual(session.profile_name, 'default')

def test_custom_session(self):
bc_session = self.bc_session_cls()
self.bc_session_cls.reset_mock()
Expand Down