mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 17:09:03 +00:00
Add get_account_id function boto_iam
so that we can retrieve the AWS account id associated with the credentials used to connect to AWS.
This commit is contained in:
parent
2b97da4015
commit
0d158cfa12
@ -415,6 +415,24 @@ def delete_role_policy(role_name, policy_name, region=None, key=None,
|
||||
log.error(msg.format(policy_name, role_name))
|
||||
return False
|
||||
|
||||
def get_account_id(region=None, key=None, keyid=None, profile=None):
|
||||
'''
|
||||
Get a the AWS account id associated with the used credentials.
|
||||
|
||||
CLI example::
|
||||
|
||||
salt myminion boto_iam.get_account_id
|
||||
'''
|
||||
cache_key = 'boto_iam.account_id'
|
||||
if cache_key not in __context__:
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
ret = conn.get_user()
|
||||
# the get_user call returns an user ARN:
|
||||
# arn:aws:iam::027050522557:user/salt-test
|
||||
__context__[cache_key] = ret['get_user_response']\
|
||||
['get_user_result']\
|
||||
['user']['arn'].split(':')[4]
|
||||
return __context__[cache_key]
|
||||
|
||||
def _get_conn(region, key, keyid, profile):
|
||||
'''
|
||||
|
17
tests/integration/modules/boto_iam.py
Normal file
17
tests/integration/modules/boto_iam.py
Normal file
@ -0,0 +1,17 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Validate the boto_iam module
|
||||
'''
|
||||
|
||||
|
||||
from salttesting.helpers import ensure_in_syspath
|
||||
ensure_in_syspath('../../')
|
||||
|
||||
import integration
|
||||
|
||||
|
||||
class BotoIAMTest(integration.ModuleCase):
|
||||
|
||||
def test_get_account_id(self):
|
||||
ret = self.run_function('boto_iam.get_account_id')
|
||||
self.assertRegexpMatches(ret, r'^\d{12}$')
|
Loading…
Reference in New Issue
Block a user