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

login: set default subscription to one with the state of "Enabled" #2575

Merged
merged 3 commits into from Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/azure-cli-core/azure/cli/core/_profile.py
Expand Up @@ -182,16 +182,22 @@ def _set_subscriptions(self, new_subscriptions):
s[_IS_DEFAULT_SUBSCRIPTION] = False

if not new_active_one:
new_active_one = new_subscriptions[0]
new_active_one[_IS_DEFAULT_SUBSCRIPTION] = True
default_sub_id = new_active_one[_SUBSCRIPTION_ID]
new_active_one = Profile._pick_working_subscription(new_subscriptions)
else:
new_subscriptions[0][_IS_DEFAULT_SUBSCRIPTION] = True
default_sub_id = new_subscriptions[0][_SUBSCRIPTION_ID]
new_active_one = Profile._pick_working_subscription(new_subscriptions)

new_active_one[_IS_DEFAULT_SUBSCRIPTION] = True
default_sub_id = new_active_one[_SUBSCRIPTION_ID]

set_cloud_subscription(active_cloud.name, default_sub_id)
self._storage[_SUBSCRIPTIONS] = subscriptions

@staticmethod
def _pick_working_subscription(subscriptions):
from azure.mgmt.resource.subscriptions.models import SubscriptionState
s = next((x for x in subscriptions if x['state'] == SubscriptionState.enabled.value), None)
return s or subscriptions[0]
Copy link
Member

Choose a reason for hiding this comment

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

What if the subscriptions list is empty? Is that a scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The caller of _set_subscriptions will ensure the subscriptions will not be empty, so we are fine. Thanks for asking.


def set_active_subscription(self, subscription): # take id or name
subscriptions = self.load_cached_subscriptions(all_clouds=True)
active_cloud = get_active_cloud()
Expand Down
13 changes: 13 additions & 0 deletions src/azure-cli-core/tests/test_profile.py
Expand Up @@ -165,6 +165,19 @@ def test_set_active_subscription(self):
self.assertFalse(storage_mock['subscriptions'][1]['isDefault'])
self.assertTrue(storage_mock['subscriptions'][0]['isDefault'])

def test_default_active_subscription_to_non_disabled_one(self):
storage_mock = {'subscriptions': None}
profile = Profile(storage_mock)

subscriptions = profile._normalize_properties(
self.user2, [self.subscription2, self.subscription1], False)

profile._set_subscriptions(subscriptions)

# verify we skip the overdued subscription and default to the 2nd one in the list
self.assertEqual(storage_mock['subscriptions'][1]['name'], self.subscription1.display_name)
self.assertTrue(storage_mock['subscriptions'][1]['isDefault'])

def test_get_subscription(self):
storage_mock = {'subscriptions': None}
profile = Profile(storage_mock)
Expand Down