diff --git a/src/azure/cli/_profile.py b/src/azure/cli/_profile.py index 50190a5a6c6..ecfb312c480 100644 --- a/src/azure/cli/_profile.py +++ b/src/azure/cli/_profile.py @@ -2,21 +2,47 @@ from .main import CONFIG -class Profile(object): - - def update(self, subscriptions, access_token): - subscriptions[0]['active'] = True - CONFIG['subscriptions'] = subscriptions - CONFIG['access_token'] = access_token - - def get_credentials(self): - subscriptions = CONFIG['subscriptions'] - sub = [x for x in subscriptions if x['active'] == True ] - if not sub and subscriptions: - sub = subscriptions - - if sub: - return (BasicTokenAuthentication({ 'access_token': CONFIG['access_token']}), - sub[0]['id'] ) - else: - raise ValueError('you need to login to') \ No newline at end of file +def get_access_token(): + return CONFIG['access_token'] + +def set_access_token(str): + CONFIG['access_token'] = str + +def get_subscriptions(): + return CONFIG['subscriptions'] + +def set_subscriptions(subscriptions, active_subscription): + active_subscription['active'] = True + CONFIG['subscriptions'] = subscriptions + +def get_active_subscription(): + active_subscriptions = [x for x in get_subscriptions() if x['active']] + + if len(active_subscriptions) != 1: + raise ProfileException("There can only be one active subscription, found {0}".format(len(active_subscriptions))) + + return active_subscriptions[0] + +def set_active_subscription(id): + subscriptions = [x for x in get_subscriptions() if x['id'] == id] + + if len(subscriptions) != 1: + raise ProfileException("Expected 1 subscription, but found {0}".format(len(subscriptions))) + + active_subscription = get_active_subscription() + active_subscription['active'] = False + subscriptions[0]['active'] = True + CONFIG.save() + +def get_login_credentials(): + subscription = get_active_subscription() + + if not subscription: + raise ValueError('Login has expired, please login again') + + return (BasicTokenAuthentication({ 'access_token': get_access_token() }), subscription['id'] ) + + +class ProfileException(Exception): + pass + diff --git a/src/azure/cli/commands/login.py b/src/azure/cli/commands/login.py index b84fb4fb846..e1bea5ec833 100644 --- a/src/azure/cli/commands/login.py +++ b/src/azure/cli/commands/login.py @@ -3,9 +3,9 @@ from msrestazure.azure_active_directory import UserPassCredentials from .._logging import logging -from .._profile import Profile from .._util import TableOutput from ..commands import command, description, option +import azure.cli._profile as profile CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46' @@ -22,6 +22,7 @@ def login(args, unexpected): password = getpass.getpass(_('Password: ')) credentials = UserPassCredentials(username, password, client_id=CLIENT_ID) + profile.set_access_token(credentials.token['access_token']) client = SubscriptionClient(SubscriptionClientConfiguration(credentials)) subscriptions = client.subscriptions.list() @@ -36,14 +37,14 @@ def login(args, unexpected): subscription['name'] = s.display_name subscription['state'] = s.state subscription['user'] = username + subscription['active'] = False consolidated.append(subscription) - profile = Profile() - profile.update(consolidated, credentials.token['access_token']) + profile.set_subscriptions(consolidated, consolidated[0]) #TODO, replace with JSON display with TableOutput() as to: - for subscription in consolidated: + for subscription in profile.get_subscriptions(): to.cell('Name', subscription['name']) to.cell('Active', bool(subscription['active'])) to.cell('User', subscription['user']) diff --git a/src/azure/cli/commands/storage.py b/src/azure/cli/commands/storage.py index cf1bef0617c..67f610a1adb 100644 --- a/src/azure/cli/commands/storage.py +++ b/src/azure/cli/commands/storage.py @@ -2,7 +2,7 @@ from .._logging import logging from .._util import TableOutput from ..commands import command, description, option -from .._profile import Profile +import azure.cli._profile as profile @command('storage account list') @description('List storage accounts') @@ -13,10 +13,8 @@ def list_accounts(args, unexpected): from azure.mgmt.storage.models import StorageAccount from msrestazure.azure_active_directory import UserPassCredentials - profile = Profile() - #credentials, subscription_id = profile.get_credentials() smc = StorageManagementClient(StorageManagementClientConfiguration( - *profile.get_credentials(), + *profile.get_login_credentials() )) group = args.get('resource-group')