Skip to content

Commit 5e63d69

Browse files
committed
get_subscriptions
1 parent f87bc09 commit 5e63d69

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/azure-cli-core/azure/cli/core/_profile.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def login(self,
147147
password,
148148
is_service_principal,
149149
tenant,
150+
get_subscriptions=None,
150151
scopes=None,
151152
use_device_code=False,
152153
allow_no_subscriptions=False,
@@ -199,7 +200,8 @@ def login(self,
199200
credential = identity.get_service_principal_credential(username)
200201

201202
if tenant:
202-
subscriptions = subscription_finder.find_using_specific_tenant(tenant, credential)
203+
subscriptions = subscription_finder.find_using_specific_tenant(tenant, credential,
204+
get_subscriptions=get_subscriptions)
203205
else:
204206
subscriptions = subscription_finder.find_using_common_tenant(username, credential)
205207

@@ -841,14 +843,21 @@ def find_using_common_tenant(self, username, credential=None):
841843
logger.warning("%s", t.tenant_id_name)
842844
return all_subscriptions
843845

844-
def find_using_specific_tenant(self, tenant, credential, tenant_id_description=None):
846+
def find_using_specific_tenant(self, tenant, credential, tenant_id_description=None, get_subscriptions=None):
845847
"""List subscriptions that can be accessed from a specific tenant.
846848
If called from find_using_common_tenant, tenant_id_description is TenantIdDescription retrieved from
847849
'Tenants - List' REST API. If directly called, tenant_id_description is None.
848850
"""
849851
client = self._create_subscription_client(credential)
850852
# https://learn.microsoft.com/en-us/rest/api/resources/subscriptions/list
851-
subscriptions = client.subscriptions.list()
853+
854+
subscriptions = []
855+
if get_subscriptions:
856+
for s in get_subscriptions:
857+
subscriptions.append(client.subscriptions.get(s))
858+
else:
859+
subscriptions = client.subscriptions.list()
860+
852861
all_subscriptions = []
853862
for s in subscriptions:
854863
_attach_token_tenant(s, tenant, tenant_id_description=tenant_id_description)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ 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('get_subscriptions', nargs='+',
54+
help='Space-separated list of subscriptions to retrieve. '
55+
'This argument must be used together with --tenant. '
56+
'Without this argument, all subscriptions will be retrieved.')
5357
c.argument('scopes', options_list=['--scope'], nargs='+',
5458
help='Used in the /authorize request. It can cover only one static resource.')
5559
c.argument('allow_no_subscriptions', action='store_true',

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

Lines changed: 6 additions & 1 deletion
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, get_subscriptions=None,
126+
scopes=None, allow_no_subscriptions=False,
125127
claims_challenge=None,
126128
# Device code flow
127129
use_device_code=False,
@@ -132,6 +134,8 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
132134
"""Log in to access Azure subscriptions"""
133135

134136
# quick argument usage check
137+
if get_subscriptions and not tenant:
138+
raise CLIError("usage error: --get-subscription must be used together with --tenant")
135139
if any([password, service_principal, tenant]) and identity:
136140
raise CLIError("usage error: '--identity' is not applicable with other arguments")
137141
if identity and username:
@@ -195,6 +199,7 @@ def login(cmd, username=None, password=None, tenant=None, scopes=None, allow_no_
195199
password,
196200
service_principal,
197201
tenant,
202+
get_subscriptions=get_subscriptions,
198203
scopes=scopes,
199204
use_device_code=use_device_code,
200205
allow_no_subscriptions=allow_no_subscriptions,

0 commit comments

Comments
 (0)