1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for
4
- * license information.
5
- */
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
6
3
7
- package com .microsoft . azure .management .containerinstance .samples ;
4
+ package com .azure .resourcemanager .containerinstance .samples ;
8
5
9
- import com .microsoft .azure .management .Azure ;
10
- import com .microsoft .azure .management .containerinstance .ContainerGroup ;
11
- import com .microsoft .azure .management .containerinstance .ContainerGroupRestartPolicy ;
12
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
13
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
14
- import com .microsoft .azure .management .samples .Utils ;
15
- import com .microsoft .rest .LogLevel ;
6
+ import com .azure .core .credential .TokenCredential ;
7
+ import com .azure .core .http .policy .HttpLogDetailLevel ;
8
+ import com .azure .core .management .AzureEnvironment ;
9
+ import com .azure .identity .DefaultAzureCredentialBuilder ;
10
+ import com .azure .resourcemanager .AzureResourceManager ;
11
+ import com .azure .resourcemanager .containerinstance .models .ContainerGroup ;
12
+ import com .azure .resourcemanager .containerinstance .models .ContainerGroupRestartPolicy ;
13
+ import com .azure .core .management .Region ;
14
+ import com .azure .core .management .profile .AzureProfile ;
15
+ import com .azure .resourcemanager .resources .fluentcore .utils .ResourceManagerUtils ;
16
+ import com .azure .resourcemanager .samples .Utils ;
16
17
17
- import java .io . File ;
18
+ import java .time . Duration ;
18
19
19
20
/**
20
21
* Azure Container Instance sample for managing container instances.
@@ -28,20 +29,20 @@ public class ManageContainerInstanceWithMultipleContainerImages {
28
29
/**
29
30
* Main function which runs the actual sample.
30
31
*
31
- * @param azure instance of the azure client
32
+ * @param azureResourceManager instance of the azure client
32
33
* @return true if sample runs successfully
33
34
*/
34
- public static boolean runSample (Azure azure ) {
35
- final String rgName = SdkContext .randomResourceName ("rgACI" , 15 );
36
- final String aciName = SdkContext .randomResourceName ("acisample" , 20 );
35
+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
36
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgACI" , 15 );
37
+ final String aciName = Utils .randomResourceName (azureResourceManager , "acisample" , 20 );
37
38
final String containerImageName1 = "microsoft/aci-helloworld" ;
38
39
final String containerImageName2 = "microsoft/aci-tutorial-sidecar" ;
39
40
40
41
try {
41
42
//=============================================================
42
43
// Create a container group with two container instances
43
44
44
- ContainerGroup containerGroup = azure .containerGroups ().define (aciName )
45
+ ContainerGroup containerGroup = azureResourceManager .containerGroups ().define (aciName )
45
46
.withRegion (Region .US_WEST )
46
47
.withNewResourceGroup (rgName )
47
48
.withLinux ()
@@ -70,40 +71,36 @@ public static boolean runSample(Azure azure) {
70
71
71
72
// warm up
72
73
System .out .println ("Warming up " + containerGroup .ipAddress ());
73
- Utils .curl ("http://" + containerGroup .ipAddress ());
74
- SdkContext .sleep (15000 );
74
+ Utils .sendGetRequest ("http://" + containerGroup .ipAddress ());
75
+ ResourceManagerUtils .sleep (Duration . ofSeconds ( 15 ) );
75
76
System .out .println ("CURLing " + containerGroup .ipAddress ());
76
- System .out .println (Utils .curl ("http://" + containerGroup .ipAddress ()));
77
+ System .out .println (Utils .sendGetRequest ("http://" + containerGroup .ipAddress ()));
77
78
78
79
//=============================================================
79
80
// Check the container instance logs
80
81
81
82
String logContent = containerGroup .getLogContent (aciName + "-1" );
82
- System .out .format ("Logs for container instance: %s\ n %s" , aciName + "-1" , logContent );
83
+ System .out .format ("Logs for container instance: %s% n%s" , aciName + "-1" , logContent );
83
84
logContent = containerGroup .getLogContent (aciName + "-2" );
84
- System .out .format ("Logs for container instance: %s\ n %s" , aciName + "-2" , logContent );
85
+ System .out .format ("Logs for container instance: %s% n%s" , aciName + "-2" , logContent );
85
86
86
87
//=============================================================
87
88
// Remove the container group
88
89
89
- azure .containerGroups ().deleteById (containerGroup .id ());
90
+ azureResourceManager .containerGroups ().deleteById (containerGroup .id ());
90
91
91
92
return true ;
92
- } catch (Exception f ) {
93
- System .out .println (f .getMessage ());
94
- f .printStackTrace ();
95
93
} finally {
96
94
try {
97
95
System .out .println ("Deleting Resource Group: " + rgName );
98
- azure .resourceGroups ().beginDeleteByName (rgName );
96
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
99
97
System .out .println ("Deleted Resource Group: " + rgName );
100
98
} catch (NullPointerException npe ) {
101
99
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
102
100
} catch (Exception g ) {
103
101
g .printStackTrace ();
104
102
}
105
103
}
106
- return false ;
107
104
}
108
105
109
106
/**
@@ -116,17 +113,21 @@ public static void main(String[] args) {
116
113
//=============================================================
117
114
// Authenticate
118
115
119
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
116
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
117
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
118
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
119
+ .build ();
120
120
121
- Azure azure = Azure .configure ()
122
- .withLogLevel (LogLevel .BODY )
123
- .authenticate (credFile )
121
+ AzureResourceManager azureResourceManager = AzureResourceManager
122
+ .configure ()
123
+ .withLogLevel (HttpLogDetailLevel .BASIC )
124
+ .authenticate (credential , profile )
124
125
.withDefaultSubscription ();
125
126
126
127
// Print selected subscription
127
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
128
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
128
129
129
- runSample (azure );
130
+ runSample (azureResourceManager );
130
131
} catch (Exception e ) {
131
132
System .out .println (e .getMessage ());
132
133
e .printStackTrace ();
0 commit comments