Skip to content
Draft
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
3 changes: 3 additions & 0 deletions src/azure-cli/azure/cli/command_modules/profile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def load_arguments(self, command):
help='User password or service principal secret. Will prompt if not given.')
c.argument('tenant', options_list=['--tenant', '-t'], validator=validate_tenant,
help='The Microsoft Entra tenant, must be provided when using a service principal.')
c.argument('subscription',
help='The subscription ID that will be used as the default. '
'Specifying this argument will bypass the interactive subscription selector.')
c.argument('scopes', options_list=['--scope'], nargs='+',
help='Used in the /authorize request. It can cover only one static resource.')
c.argument('allow_no_subscriptions', action='store_true',
Expand Down
10 changes: 8 additions & 2 deletions src/azure-cli/azure/cli/command_modules/profile/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def account_clear(cmd):


# pylint: disable=too-many-branches, too-many-locals
def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_subscriptions=False,
def login(cmd, username=None, password=None,
tenant=None, subscription=None,
scopes=None, allow_no_subscriptions=False,
claims_challenge=None,
# Device code flow
use_device_code=False,
Expand Down Expand Up @@ -187,7 +189,8 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
from azure.cli.core.telemetry import set_login_experience_v2
set_login_experience_v2(login_experience_v2)

select_subscription = interactive and sys.stdin.isatty() and sys.stdout.isatty() and login_experience_v2
select_subscription = (not subscription and
interactive and sys.stdin.isatty() and sys.stdout.isatty() and login_experience_v2)

subscriptions = profile.login(
interactive,
Expand Down Expand Up @@ -215,6 +218,9 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
logger.warning(LOGIN_OUTPUT_WARNING)
return

if subscription:
profile.set_active_subscription(subscription)

all_subscriptions = list(subscriptions)
for sub in all_subscriptions:
sub['cloudName'] = sub.pop('environmentName', None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from azure.cli.core.mock import DummyCli
from knack.util import CLIError

from azure.cli.testsdk.scenario_tests.const import MOCKED_TENANT_ID, MOCKED_SUBSCRIPTION_ID

class ProfileCommandTest(unittest.TestCase):
@mock.patch('azure.cli.core.api.load_subscriptions', autospec=True)
Expand Down Expand Up @@ -107,6 +107,27 @@ def login_with_managed_identity_mock(*args, **kwargs):
# assert
self.assertTrue(invoked)

@mock.patch('sys.stdin.isatty', return_value=True)
@mock.patch('sys.stdout.isatty', return_value=True)
@mock.patch('azure.cli.command_modules.profile._subscription_selector.SubscriptionSelector')
@mock.patch('azure.cli.core._profile.Profile.set_active_subscription')
@mock.patch('azure.cli.core._profile.Profile.login')
def test_login_with_subscription(self, login_mock, set_active_subscription_mock, subscription_selector_mock, *_):
cmd = mock.MagicMock()
login_mock.return_value = [{
'environmentName': 'AzureCloud',
'homeTenantId': MOCKED_TENANT_ID,
'id': MOCKED_SUBSCRIPTION_ID,
'isDefault': False,
'managedByTenants': [],
'name': 'test subscription name',
'state': 'Enabled',
'tenantId': MOCKED_TENANT_ID,
'user': {'name': 'test@microsoft.com', 'type': 'user'}}]
login(cmd, subscription=MOCKED_SUBSCRIPTION_ID)
subscription_selector_mock.assert_not_called()
set_active_subscription_mock.assert_called_with(MOCKED_SUBSCRIPTION_ID)

def test_login_validate_tenant(self):
from azure.cli.command_modules.profile._validators import validate_tenant

Expand Down
Loading