Microsoft Azure Subscription Management Client Library for Python
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Tenant discovery and management operations that allow applications to identify and work with Azure Active Directory tenants accessible to the authenticated user. These operations are essential for multi-tenant applications and understanding the scope of accessible resources.
Gets all Azure Active Directory tenants that are accessible to the authenticated user. This is useful for understanding which tenants contain subscriptions that can be managed.
def list(**kwargs) -> Iterable[TenantIdDescription]:
"""
Gets the tenants for your account.
Returns:
Iterable[TenantIdDescription]: Paginated list of accessible tenants
"""Usage Example:
from azure.identity import DefaultAzureCredential
from azure.mgmt.subscription import SubscriptionClient
credential = DefaultAzureCredential()
client = SubscriptionClient(credential)
# List all accessible tenants
tenants = list(client.tenants.list())
print(f"Found {len(tenants)} accessible tenants:")
for tenant in tenants:
print(f"Tenant ID: {tenant.tenant_id}")
print(f"Display Name: {tenant.display_name}")
print(f"Default Domain: {tenant.default_domain}")
print(f"Category: {tenant.tenant_category}")
print("---")Advanced Usage - Working with Multiple Tenants:
# Get tenants and then work with subscriptions in each
tenants = list(client.tenants.list())
for tenant in tenants:
print(f"Working with tenant: {tenant.display_name}")
# Create a new client for this specific tenant if needed
# (Note: The credential must have access to this tenant)
tenant_client = SubscriptionClient(credential)
# List subscriptions accessible in this context
subscriptions = list(tenant_client.subscriptions.list())
print(f" Found {len(subscriptions)} subscriptions in this tenant")
for sub in subscriptions:
if sub.tenant_id == tenant.tenant_id:
print(f" - {sub.display_name} ({sub.subscription_id})")Common scenarios when working with tenant operations:
from azure.core.exceptions import HttpResponseError
try:
tenants = list(client.tenants.list())
if not tenants:
print("No accessible tenants found - check authentication and permissions")
except HttpResponseError as e:
if e.status_code == 401:
print("Authentication failed - check credentials")
elif e.status_code == 403:
print("Access denied - insufficient permissions")
else:
print(f"Error listing tenants: {e.status_code} - {e.message}")class TenantIdDescription:
"""Azure Active Directory tenant information."""
id: str # Resource ID for the tenant
tenant_id: str # Azure AD tenant identifier (GUID)
tenant_category: str # Tenant category (Home, ProjectedBy, ManagedBy)
display_name: str # Human-readable tenant name
default_domain: str # Primary domain name for the tenant
country: str # Country/region where tenant is located
country_code: str # ISO country code
domains: List[str] # All verified domains in the tenant
tenant_type: str # Type of tenantWhen working with multiple tenants, consider the following patterns:
# Get only "home" tenants (where the user is a native member)
home_tenants = [t for t in client.tenants.list() if t.tenant_category == "Home"]
# Get guest tenants (where the user is a guest)
guest_tenants = [t for t in client.tenants.list() if t.tenant_category in ["ProjectedBy", "ManagedBy"]]def find_tenant_by_domain(domain_name: str) -> TenantIdDescription:
"""Find a tenant by one of its verified domains."""
for tenant in client.tenants.list():
if domain_name in tenant.domains or tenant.default_domain == domain_name:
return tenant
return None
# Example usage
tenant = find_tenant_by_domain("contoso.com")
if tenant:
print(f"Found tenant: {tenant.display_name} ({tenant.tenant_id})")def check_tenant_access(tenant_id: str) -> bool:
"""Check if we have access to a specific tenant."""
try:
accessible_tenants = list(client.tenants.list())
return any(t.tenant_id == tenant_id for t in accessible_tenants)
except HttpResponseError:
return FalseTenant operations are commonly used in conjunction with subscription operations:
# Get all tenants and their associated subscriptions
for tenant in client.tenants.list():
print(f"Tenant: {tenant.display_name}")
# Get all subscriptions
all_subscriptions = list(client.subscriptions.list())
# Filter subscriptions for this tenant
tenant_subscriptions = [s for s in all_subscriptions if s.tenant_id == tenant.tenant_id]
print(f" Subscriptions in this tenant: {len(tenant_subscriptions)}")
for sub in tenant_subscriptions:
print(f" - {sub.display_name} ({sub.state})")Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-subscription