1616"""
1717
1818import os
19+ from random import randint
20+
21+ from azure .ai .ml import MLClient
22+ from azure .core .exceptions import ResourceExistsError , ResourceNotFoundError
23+ from azure .identity import DefaultAzureCredential
24+
25+
26+ def handle_resource_exists_error (func ):
27+ def wrapper (* args , ** kwargs ):
28+ try :
29+ return func (* args , ** kwargs )
30+ except (ResourceExistsError , ResourceNotFoundError ) as e :
31+ pass
32+
33+ return wrapper
34+
35+
36+ subscription_id = os .environ ["AZURE_SUBSCRIPTION_ID" ]
37+ resource_group = os .environ ["RESOURCE_GROUP_NAME" ]
38+ workspace_name = "test-ws1"
39+ credential = DefaultAzureCredential ()
40+ ml_client = MLClient (credential , subscription_id , resource_group , workspace_name = workspace_name )
41+
42+ compute_name_1 = f"compute-{ randint (1 , 1000 )} "
43+ compute_name_2 = f"compute-{ randint (1 , 1000 )} "
44+ ci_name = f"ci-{ randint (1 , 1000 )} "
1945
2046
2147class ComputeConfigurationOptions (object ):
22- def ml_compute_config (self ):
23- from azure .ai .ml import MLClient
24- from azure .identity import DefaultAzureCredential
48+ @handle_resource_exists_error
49+ def ml_compute_config_setup_0 (self ):
50+ # [START compute_instance]
51+ from azure .ai .ml .entities import ComputeInstance
2552
26- subscription_id = os .environ ["AZURE_SUBSCRIPTION_ID" ]
27- resource_group = os .environ ["RESOURCE_GROUP_NAME" ]
28- credential = DefaultAzureCredential ()
29- ml_client = MLClient (credential , subscription_id , resource_group , workspace_name = "test-ws1" )
53+ ci = ComputeInstance (
54+ name = ci_name ,
55+ size = "Standard_DS2_v2" ,
56+ )
57+ ml_client .compute .begin_create_or_update (ci )
58+ # [END compute_instance]
3059
60+ @handle_resource_exists_error
61+ def ml_compute_config_setup_1 (self ):
3162 # [START compute_operations_get]
32- cpu_cluster = ml_client .compute .get ("cpucluster " )
63+ cpu_cluster = ml_client .compute .get ("cpu-cluster " )
3364 # [END compute_operations_get]
3465
3566 # [START load_compute]
@@ -46,14 +77,16 @@ def ml_compute_config(self):
4677 # [END compute_operations_list]
4778
4879 # [START compute_operations_list_nodes]
49- node_list = ml_client .compute .list_nodes (name = "cpucluster " )
80+ node_list = ml_client .compute .list_nodes (name = "cpu-cluster " )
5081 # [END compute_operations_list_nodes]
5182
83+ @handle_resource_exists_error
84+ def ml_compute_config_setup_2 (self ):
5285 # [START compute_operations_create_update]
5386 from azure .ai .ml .entities import AmlCompute
5487
5588 compute_obj = AmlCompute (
56- name = "example-compute" ,
89+ name = compute_name_1 ,
5790 tags = {"key1" : "value1" , "key2" : "value2" },
5891 min_instances = 0 ,
5992 max_instances = 10 ,
@@ -62,9 +95,13 @@ def ml_compute_config(self):
6295 registered_compute = ml_client .compute .begin_create_or_update (compute_obj )
6396 # [END compute_operations_create_update]
6497
98+ @handle_resource_exists_error
99+ def ml_compute_config_setup_3 (self ):
65100 # [START compute_operations_attach]
101+ from azure .ai .ml .entities import AmlCompute
102+
66103 compute_obj = AmlCompute (
67- name = "example-compute-2" ,
104+ name = compute_name_2 ,
68105 tags = {"key1" : "value1" , "key2" : "value2" },
69106 min_instances = 0 ,
70107 max_instances = 10 ,
@@ -74,52 +111,24 @@ def ml_compute_config(self):
74111 # [END compute_operations_attach]
75112
76113 # [START compute_operations_update]
77- compute_obj = ml_client .compute .get ("cpucluster " )
114+ compute_obj = ml_client .compute .get ("cpu-cluster " )
78115 compute_obj .idle_time_before_scale_down = 200
79116 updated_compute = ml_client .compute .begin_update (compute_obj )
80117 # [END compute_operations_update]
81118
82- # [START compute_operations_delete]
83- ml_client .compute .begin_delete ("example-compute" , action = "Detach" )
84-
85- ml_client .compute .begin_delete ("example-compute-2" )
86- # [END compute_operations_delete]
87-
88- compute_obj = ComputeInstance (
89- name = "example-compute" ,
90- tags = {"key1" : "value1" , "key2" : "value2" },
91- min_instances = 0 ,
92- max_instances = 10 ,
93- idle_time_before_scale_down = 100 ,
94- )
95- registered_compute = ml_client .compute .begin_create_or_update (compute_obj )
96-
97- # [START compute_operations_start]
98- ml_client .compute .begin_start ("example-compute" )
99- # [END compute_operations_start]
100-
101- # [START compute_operations_stop]
102- ml_client .compute .begin_stop ("example-compute" )
103- # [END compute_operations_stop]
104-
105- # [START compute_operations_restart]
106- ml_client .compute .begin_stop ("example-compute" )
107- ml_client .compute .begin_restart ("example-compute" )
108- # [END compute_operations_restart]
109-
110119 # [START compute_operations_list_usage]
111- print ( ml_client .compute .list_usage () )
120+ usage_list = ml_client .compute .list_usage ()
112121 # [END compute_operations_list_usage]
113122
114123 # [START compute_operations_list_sizes]
115- print ( ml_client .compute .list_sizes () )
124+ size_list = ml_client .compute .list_sizes ()
116125 # [END compute_operations_list_sizes]
117126
118127 # [START amlcompute]
119128 from azure .ai .ml .entities import AmlCompute , IdentityConfiguration , ManagedIdentityConfiguration
120129
121130 aml_compute = AmlCompute (
122- name = "my-compute" ,
131+ name = "my-aml- compute" ,
123132 min_instances = 0 ,
124133 max_instances = 10 ,
125134 idle_time_before_scale_down = 100 ,
@@ -158,17 +167,13 @@ def ml_compute_config(self):
158167 on_behalf_of_config = AssignedUserConfiguration (user_tenant_id = "12345" , user_object_id = "abcdef" )
159168 # [END assigned_user_configuration]
160169
161- # [START compute_instance]
162- from azure .ai .ml .entities import ComputeInstance
170+ # [START compute_operations_stop]
171+ ml_client .compute .begin_stop (ci_name )
172+ # [END compute_operations_stop]
163173
164- ci = ComputeInstance (
165- name = "my-ci-resource" ,
166- location = "eastus" ,
167- size = "Standard_DS2_v2" ,
168- ssh_public_access_enabled = False ,
169- ssh_key_value = "ssh-rsa ABCDEFGHIJKLMNOPQRSTUVWXYZ administrator@MININT-2023" ,
170- )
171- # [END compute_instance]
174+ # [START compute_operations_start]
175+ ml_client .compute .begin_start (ci_name )
176+ # [END compute_operations_start]
172177
173178 # [START vm_ssh_settings]
174179 from azure .ai .ml .entities import VirtualMachineSshSettings
@@ -261,7 +266,20 @@ def ml_compute_config(self):
261266 materialization_compute = MaterializationComputeResource (instance_type = "standard_e4s_v3" )
262267 # [END materialization_compute_resource]
263268
269+ # [START compute_operations_restart]
270+ ml_client .compute .begin_restart (ci_name )
271+ # [END compute_operations_restart]
272+
273+ # [START compute_operations_delete]
274+ ml_client .compute .begin_delete (compute_name_1 , action = "Detach" )
275+
276+ ml_client .compute .begin_delete (compute_name_2 )
277+ # [END compute_operations_delete]
278+
264279
265280if __name__ == "__main__" :
266281 sample = ComputeConfigurationOptions ()
267- sample .ml_compute_config ()
282+ sample .ml_compute_config_setup_0 ()
283+ sample .ml_compute_config_setup_1 ()
284+ sample .ml_compute_config_setup_2 ()
285+ sample .ml_compute_config_setup_3 ()
0 commit comments