Microsoft Azure common code library providing shared utilities, exceptions, and profile management for all Azure Python SDK packages.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
⚠️ DEPRECATED: All client factory functions are deprecated as of version 1.1.28. Use azure-identity and AzureCliCredential instead.
Client factory functions for creating Azure service clients from various authentication sources including CLI profiles, JSON authentication dictionaries, and authentication files. These functions provide automatic parameter filling and client instantiation.
These functions are no longer recommended. Use the modern authentication approach:
from azure.identity import AzureCliCredential
from azure.mgmt.compute import ComputeManagementClient
# New recommended approach
credential = AzureCliCredential()
client = ComputeManagementClient(credential, subscription_id)Create SDK clients using Azure CLI credentials and profile settings.
def get_client_from_cli_profile(client_class, **kwargs):
"""
Return a SDK client initialized with current CLI credentials, CLI default
subscription and CLI default cloud.
**DEPRECATED**: This method is deprecated as of version 1.1.28.
Use azure-identity and AzureCliCredential instead.
This method is not working for azure-cli-core>=2.21.0 (released in March 2021).
For compatible azure-cli-core (< 2.20.0), this method will automatically
fill the following client parameters:
- credentials/credential
- subscription_id
- base_url
Parameters provided in kwargs will override CLI parameters and be passed
directly to the client.
Args:
client_class: A SDK client class to instantiate
**kwargs: Additional parameters passed to client constructor
Returns:
An instantiated client of the specified class
Raises:
ImportError: If azure-cli-core package is not available
Example:
from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.compute import ComputeManagementClient
client = get_client_from_cli_profile(ComputeManagementClient)
"""
...Create SDK clients from JSON authentication dictionaries.
def get_client_from_json_dict(client_class, config_dict, **kwargs):
"""
Return a SDK client initialized with a JSON auth dict.
**DEPRECATED**: This method is deprecated as of version 1.1.28.
Use azure-identity for modern authentication.
This method will fill automatically the following client parameters:
- credentials
- subscription_id
- base_url
- tenant_id
Parameters provided in kwargs will override parameters and be passed
directly to the client.
Args:
client_class: A SDK client class to instantiate
config_dict (dict): Authentication configuration dictionary
**kwargs: Additional parameters passed to client constructor
Returns:
An instantiated client of the specified class
Raises:
ValueError: For autorest v3/track2 clients (not supported)
Example:
from azure.common.client_factory import get_client_from_json_dict
from azure.mgmt.compute import ComputeManagementClient
config_dict = {
"clientId": "ad735158-65ca-11e7-ba4d-ecb1d756380e",
"clientSecret": "b70bb224-65ca-11e7-810c-ecb1d756380e",
"subscriptionId": "bfc42d3a-65ca-11e7-95cf-ecb1d756380e",
"tenantId": "c81da1d8-65ca-11e7-b1d1-ecb1d756380e",
"activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
"resourceManagerEndpointUrl": "https://management.azure.com/",
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
"sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
"galleryEndpointUrl": "https://gallery.azure.com/",
"managementEndpointUrl": "https://management.core.windows.net/"
}
client = get_client_from_json_dict(ComputeManagementClient, config_dict)
"""
...Create SDK clients from authentication files.
def get_client_from_auth_file(client_class, auth_path=None, **kwargs):
"""
Return a SDK client initialized with auth file.
**DEPRECATED**: This method is deprecated as of version 1.1.28.
Use azure-identity for modern authentication.
You can specify the file path directly, or fill the environment variable
AZURE_AUTH_LOCATION. File must be UTF-8.
This method will fill automatically the following client parameters:
- credentials
- subscription_id
- base_url
Parameters provided in kwargs will override parameters and be passed
directly to the client.
Args:
client_class: A SDK client class to instantiate
auth_path (str, optional): Path to the authentication file
**kwargs: Additional parameters passed to client constructor
Returns:
An instantiated client of the specified class
Raises:
KeyError: If AZURE_AUTH_LOCATION not set and no path provided
FileNotFoundError: If provided file path does not exist
json.JSONDecodeError: If provided file is not valid JSON
UnicodeDecodeError: If file is not UTF8 compliant
Example:
from azure.common.client_factory import get_client_from_auth_file
from azure.mgmt.compute import ComputeManagementClient
client = get_client_from_auth_file(ComputeManagementClient)
"""
...Internal utility functions used by the client factory methods.
def _instantiate_client(client_class, **kwargs):
"""
Internal function to instantiate a client from kwargs, filtering kwargs
to match client signature.
Args:
client_class: The client class to instantiate
**kwargs: Parameters to pass to client constructor
Returns:
Instantiated client instance
"""
...
def _client_resource(client_class, cloud):
"""
Internal function to return resource and base URL for a client.
Args:
client_class: The client class to get resource info for
cloud: The cloud configuration object
Returns:
tuple: (resource, base_url) - either or both can be None
"""
...
def _is_autorest_v3(client_class):
"""
Internal function to detect if client is autorest v3/track2.
Args:
client_class: The client class to check
Returns:
bool: True if client is autorest v3/track2
"""
...Functions for working with Azure CLI credentials (also deprecated).
def get_cli_profile():
"""
Return a CLI profile class.
**DEPRECATED**: This method is deprecated as of version 1.1.28.
Not working for azure-cli-core>=2.21.0 (released in March 2021).
Returns:
azure.cli.core._profile.Profile: A CLI Profile instance
Raises:
ImportError: If azure-cli-core package is not available
"""
...
def get_azure_cli_credentials(resource=None, with_tenant=False):
"""
Return Credentials and default SubscriptionID of current loaded profile of the CLI.
**DEPRECATED**: This method is deprecated as of version 1.1.28.
Not working for azure-cli-core>=2.21.0 (released in March 2021).
Use azure-identity and AzureCliCredential instead.
Args:
resource (str, optional): Alternative resource for credentials if not ARM
with_tenant (bool): If True, return three-tuple with tenant ID
Returns:
tuple: (credentials, subscription_id) or (credentials, subscription_id, tenant_id)
Raises:
NotImplementedError: For azure-cli-core>=2.21.0
"""
...
def get_cli_active_cloud():
"""
Return a CLI active cloud.
**DEPRECATED**: This method is deprecated as of version 1.1.28.
Not working for azure-cli-core>=2.21.0 (released in March 2021).
Returns:
azure.cli.core.cloud.Cloud: A CLI Cloud instance
Raises:
ImportError: If azure-cli-core package is not available
"""
...The authentication file should be a UTF-8 encoded JSON file with the following structure:
{
"clientId": "ad735158-65ca-11e7-ba4d-ecb1d756380e",
"clientSecret": "b70bb224-65ca-11e7-810c-ecb1d756380e",
"subscriptionId": "bfc42d3a-65ca-11e7-95cf-ecb1d756380e",
"tenantId": "c81da1d8-65ca-11e7-b1d1-ecb1d756380e",
"activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
"resourceManagerEndpointUrl": "https://management.azure.com/",
"activeDirectoryGraphResourceId": "https://graph.windows.net/",
"sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
"galleryEndpointUrl": "https://gallery.azure.com/",
"managementEndpointUrl": "https://management.core.windows.net/"
}Old approach (deprecated):
from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.compute import ComputeManagementClient
client = get_client_from_cli_profile(ComputeManagementClient)New approach:
from azure.identity import AzureCliCredential
from azure.mgmt.compute import ComputeManagementClient
credential = AzureCliCredential()
client = ComputeManagementClient(credential, subscription_id)Old approach (deprecated):
from azure.common.client_factory import get_client_from_auth_file
from azure.mgmt.compute import ComputeManagementClient
client = get_client_from_auth_file(ComputeManagementClient, "path/to/auth.json")New approach:
from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
import json
with open("path/to/auth.json") as f:
auth_data = json.load(f)
credential = ClientSecretCredential(
tenant_id=auth_data["tenantId"],
client_id=auth_data["clientId"],
client_secret=auth_data["clientSecret"]
)
client = ComputeManagementClient(credential, auth_data["subscriptionId"])Install with Tessl CLI
npx tessl i tessl/pypi-azure-common