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
Profile definitions for managing API versions across different Azure environments, enabling applications to target specific API versions, switch between cloud environments, and maintain compatibility with hybrid cloud deployments.
Custom profile definition class for creating API version mappings.
class ProfileDefinition:
"""
Custom Profile definition for API version mapping.
Allows defining custom profiles with specific API versions for different
Azure services, enabling consistent API version management across
applications targeting different Azure environments.
Args:
profile_dict (dict): Dictionary mapping service names to API versions
label (str, optional): Human-readable label for the profile
Note:
The dict format is internal and should not be considered stable.
"""
def __init__(self, profile_dict, label=None): ...
@property
def label(self):
"""The label associated to this profile definition."""
...
def get_profile_dict(self):
"""
Return the current profile dict.
This is internal information, and content should not be considered stable.
Returns:
dict: The internal profile dictionary
"""
...
def __repr__(self): ...Store and manage the default profile used across Azure clients.
class DefaultProfile:
"""
Store a default profile.
Manages the default profile used by Azure clients when no specific
profile is provided. The profile can be changed dynamically to affect
all subsequently created clients.
Attributes:
profile (ProfileDefinition): The default profile as class attribute
"""
profile = None
def use(self, profile):
"""
Define a new default profile.
Args:
profile (KnownProfiles | ProfileDefinition): New default profile
Raises:
ValueError: If profile is not a valid profile type
"""
...
def definition(self):
"""
Get the current default profile definition.
Returns:
ProfileDefinition: Current default profile
"""
...Enumeration of predefined Azure profiles with API version mappings for different environments.
class KnownProfiles(Enum):
"""
Predefined Azure Profiles for different environments.
Provides pre-configured profiles for various Azure environments:
- latest: Always use latest available API version on each package
- default: Mutable profile that can be changed to affect all clients
- Versioned profiles: Specific API version sets for hybrid environments
If you change default, this changes all created packages on the fly to
this profile. This can be used to switch a complete set of API versions
without re-creating all clients.
"""
# Meta-profiles
default = DefaultProfile()
latest = ProfileDefinition(None, "latest")
# Versioned profiles for hybrid environments
v2017_03_09_profile = ProfileDefinition({...}, "2017-03-09-profile")
v2018_03_01_hybrid = ProfileDefinition({...}, "2018-03-01-hybrid")
v2019_03_01_hybrid = ProfileDefinition({...}, "2019-03-01-hybrid")
v2020_09_01_hybrid = ProfileDefinition({...}, "2020-09-01-hybrid")
def __init__(self, profile_definition): ...
def use(self, profile):
"""
Set profile (only for default profile).
Args:
profile (KnownProfiles | ProfileDefinition): Profile to use
Raises:
ValueError: If called on non-default profile
"""
...
def definition(self):
"""
Get profile definition (only for default profile).
Returns:
ProfileDefinition: Profile definition
Raises:
ValueError: If called on non-default profile
"""
...
@classmethod
def from_name(cls, profile_name):
"""
Get profile by name.
Args:
profile_name (str): Name of the profile to retrieve
Returns:
KnownProfiles: The requested profile
Raises:
ValueError: If profile name not found
"""
...Mixin class for clients that need to support multiple API versions based on profiles.
class MultiApiClientMixin:
"""
Mixin that contains multi-api version profile management.
To use this mixin, a client must define two class attributes:
- LATEST_PROFILE: ProfileDefinition corresponding to latest profile
- _PROFILE_TAG: A tag that filters a full profile for this particular client
This should not be used directly and will only provide private methods.
"""
def __init__(self, *args, **kwargs):
"""
Initialize multi-API client with profile support.
Args:
api_version (str, optional): Specific API version to use
profile (KnownProfiles, optional): Profile to use for API versions
Raises:
ValueError: If both api_version and profile are specified
InvalidMultiApiClientError: If required class attributes missing
"""
...
def _get_api_version(self, operation_group_name):
"""
Get API version for a specific operation group.
Args:
operation_group_name (str): Name of the operation group
Returns:
str: API version to use for the operation group
Raises:
ValueError: If profile doesn't define the operation group
"""
...class InvalidMultiApiClientError(Exception):
"""
Exception raised when MultiApiClientMixin is used incorrectly.
Raised when the mixin is not used with a compatible class that
defines the required LATEST_PROFILE and _PROFILE_TAG attributes.
"""
passfrom azure.profiles import KnownProfiles
# Use the latest API versions
profile = KnownProfiles.latest
print(f"Using profile: {profile.value.label}")
# Use a specific hybrid profile
hybrid_profile = KnownProfiles.v2020_09_01_hybrid
print(f"Using hybrid profile: {hybrid_profile.value.label}")
# Get profile by name
profile_by_name = KnownProfiles.from_name("2020-09-01-hybrid")from azure.profiles import ProfileDefinition
# Create a custom profile for specific API versions
custom_profile = ProfileDefinition({
"azure.mgmt.compute.ComputeManagementClient": {
None: "2020-06-01",
"disks": "2019-07-01",
"snapshots": "2019-07-01"
},
"azure.mgmt.storage.StorageManagementClient": {
None: "2019-06-01"
}
}, "my-custom-profile")
print(f"Created profile: {custom_profile.label}")
profile_dict = custom_profile.get_profile_dict()from azure.profiles import KnownProfiles
# Change the default profile for all new clients
KnownProfiles.default.use(KnownProfiles.v2020_09_01_hybrid)
# Get the current default profile
current_default = KnownProfiles.default.definition()
print(f"Current default: {current_default.label}")from azure.profiles import KnownProfiles, ProfileDefinition
from azure.profiles.multiapiclient import MultiApiClientMixin
class MyAzureClient(MultiApiClientMixin):
_PROFILE_TAG = "azure.mgmt.myservice.MyServiceClient"
LATEST_PROFILE = ProfileDefinition({
_PROFILE_TAG: {
None: "2021-01-01"
}
}, "latest")
def __init__(self, credentials, **kwargs):
super().__init__(**kwargs)
self.credentials = credentials
def get_operations_api_version(self):
return self._get_api_version("operations")
# Use with specific API version
client = MyAzureClient(credentials, api_version="2020-01-01")
# Use with profile
client = MyAzureClient(credentials, profile=KnownProfiles.v2020_09_01_hybrid)The predefined profiles contain API version mappings for various Azure services. Here are examples of the service mappings:
v2020_09_01_hybrid = ProfileDefinition({
"azure.keyvault.KeyVaultClient": {
None: "2016-10-01"
},
"azure.mgmt.authorization.AuthorizationManagementClient": {
None: "2016-09-01"
},
"azure.mgmt.compute.ComputeManagementClient": {
None: "2020-06-01",
'resource_skus': '2019-04-01',
'disks': '2019-07-01',
'snapshots': '2019-07-01'
},
"azure.mgmt.network.NetworkManagementClient": {
None: "2018-11-01"
},
"azure.mgmt.storage.StorageManagementClient": {
None: "2019-06-01"
}
# ... additional service mappings
}, "2020-09-01-hybrid")Each service mapping can define:
None: Default API version for the serviceInstall with Tessl CLI
npx tessl i tessl/pypi-azure-common