Skip to content

Commit b9e720a

Browse files
committed
login-subscription
1 parent f87bc09 commit b9e720a

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/azure-cli/azure/cli/command_modules/profile/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def load_arguments(self, command):
5050
help='User password or service principal secret. Will prompt if not given.')
5151
c.argument('tenant', options_list=['--tenant', '-t'], validator=validate_tenant,
5252
help='The Microsoft Entra tenant, must be provided when using a service principal.')
53+
c.argument('subscription',
54+
help='The subscription ID that will be used as the default. '
55+
'Specifying this argument will bypass the interactive subscription selector.')
5356
c.argument('scopes', options_list=['--scope'], nargs='+',
5457
help='Used in the /authorize request. It can cover only one static resource.')
5558
c.argument('allow_no_subscriptions', action='store_true',

src/azure-cli/azure/cli/command_modules/profile/custom.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def account_clear(cmd):
121121

122122

123123
# pylint: disable=too-many-branches, too-many-locals
124-
def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_subscriptions=False,
124+
def login(cmd, username=None, password=None,
125+
tenant=None, subscription=None,
126+
scopes=None, allow_no_subscriptions=False,
125127
claims_challenge=None,
126128
# Device code flow
127129
use_device_code=False,
@@ -187,7 +189,8 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
187189
from azure.cli.core.telemetry import set_login_experience_v2
188190
set_login_experience_v2(login_experience_v2)
189191

190-
select_subscription = interactive and sys.stdin.isatty() and sys.stdout.isatty() and login_experience_v2
192+
select_subscription = (not subscription and
193+
interactive and sys.stdin.isatty() and sys.stdout.isatty() and login_experience_v2)
191194

192195
subscriptions = profile.login(
193196
interactive,
@@ -215,6 +218,9 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
215218
logger.warning(LOGIN_OUTPUT_WARNING)
216219
return
217220

221+
if subscription:
222+
profile.set_active_subscription(subscription)
223+
218224
all_subscriptions = list(subscriptions)
219225
for sub in all_subscriptions:
220226
sub['cloudName'] = sub.pop('environmentName', None)

src/azure-cli/azure/cli/command_modules/profile/tests/latest/test_profile_custom.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from azure.cli.core.mock import DummyCli
1313
from knack.util import CLIError
14-
14+
from azure.cli.testsdk.scenario_tests.const import MOCKED_TENANT_ID, MOCKED_SUBSCRIPTION_ID
1515

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

110+
@mock.patch('sys.stdin.isatty', return_value=True)
111+
@mock.patch('sys.stdout.isatty', return_value=True)
112+
@mock.patch('azure.cli.command_modules.profile._subscription_selector.SubscriptionSelector')
113+
@mock.patch('azure.cli.core._profile.Profile.set_active_subscription')
114+
@mock.patch('azure.cli.core._profile.Profile.login')
115+
def test_login_with_subscription(self, login_mock, set_active_subscription_mock, subscription_selector_mock, *_):
116+
cmd = mock.MagicMock()
117+
login_mock.return_value = [{
118+
'environmentName': 'AzureCloud',
119+
'homeTenantId': MOCKED_TENANT_ID,
120+
'id': MOCKED_SUBSCRIPTION_ID,
121+
'isDefault': False,
122+
'managedByTenants': [],
123+
'name': 'test subscription name',
124+
'state': 'Enabled',
125+
'tenantId': MOCKED_TENANT_ID,
126+
'user': {'name': '[email protected]', 'type': 'user'}}]
127+
login(cmd, subscription=MOCKED_SUBSCRIPTION_ID)
128+
subscription_selector_mock.assert_not_called()
129+
set_active_subscription_mock.assert_called_with(MOCKED_SUBSCRIPTION_ID)
130+
110131
def test_login_validate_tenant(self):
111132
from azure.cli.command_modules.profile._validators import validate_tenant
112133

0 commit comments

Comments
 (0)