Skip to content
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
62 changes: 44 additions & 18 deletions src/azure/cli/_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
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

9 changes: 5 additions & 4 deletions src/azure/cli/commands/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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()

Expand All @@ -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'])
Expand Down
6 changes: 2 additions & 4 deletions src/azure/cli/commands/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down