File: ml_samples_cloud_configurations.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (85 lines) | stat: -rw-r--r-- 3,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: ml_samples_cloud_configurations.py
DESCRIPTION:
    These samples demonstrate different ways to configure clouds for the MLClient.
USAGE:
    python ml_samples_cloud_configurations.py

"""

import os
from azure.ai.ml import MLClient
from azure.ai.ml.constants._common import ArmConstants
from azure.ai.ml.constants._common import AZUREML_CLOUD_ENV_NAME
from azure.identity import AzureAuthorityHosts, DefaultAzureCredential


class CloudConfigurationOptions(object):
    def ml_cloud_config_from_environment_arm(self):
        subscription_id = "AZURE_SUBSCRIPTION_ID"
        resource_group = "RESOURCE_GROUP_NAME"
        credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_PUBLIC_CLOUD)

        # This environment variable should be set when running python.  If it is set, and the configured cloud is not
        # one of the default ones in _azure_environments.py, it will follow the url to find the cloud config.  If it
        # does not find it anywhere, it will throw an "Unknown cloud environment" error.
        os.environ[ArmConstants.METADATA_URL_ENV_NAME] = (
            "https://management.azure.com/metadata/endpoints?api-version=2019-05-01"
        )
        kwargs = {"cloud": "AzureCloud"}

        ml_client = MLClient(
            credential=credential,
            subscription_id=subscription_id,
            resource_group_name=resource_group,
            workspace_name="test-ws1",
            **kwargs,
        )
        # The client will use the cloud that we passed in
        print("Client is using cloud:", ml_client._cloud)
        # In specifying this cloud, we've also set the environment variable to match it
        print("Cloud name environment variable:", os.environ[AZUREML_CLOUD_ENV_NAME])

    def ml_cloud_config_from_keyword_args(self):
        subscription_id = "AZURE_SUBSCRIPTION_ID"
        resource_group = "RESOURCE_GROUP_NAME"
        credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_PUBLIC_CLOUD)

        # All of these configurations are needed in the kwargs, otherwise the SDK will not recognize the configuration
        # and will throw an "Unknown cloud environment" error.
        kwargs = {
            "cloud": "TestCloud",
            "cloud_metadata": {
                "azure_portal": "https://test.portal.azure.com/",
                "resource_manager": "https://test.management.azure.com/",
                "active_directory": "https://test.login.microsoftonline.com/",
                "aml_resource_id": "https://test.ml.azure.com/",
                "storage_endpoint": "test.core.windows.net",
                "registry_discovery_endpoint": "https://test.eastus.api.azureml.ms/",
            },
        }
        ml_client = MLClient(
            credential=credential,
            subscription_id=subscription_id,
            resource_group_name=resource_group,
            workspace_name="test-ws1",
            **kwargs,  # type:ignore
        )
        # The client will use the cloud that we passed in
        print("Client is using cloud:", ml_client._cloud)
        # In configuring this cloud, we've also set the environment variable to match it
        print("Cloud name environment variable:", os.environ[AZUREML_CLOUD_ENV_NAME])


if __name__ == "__main__":
    sample = CloudConfigurationOptions()
    sample.ml_cloud_config_from_environment_arm()
    sample.ml_cloud_config_from_keyword_args()