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
- */
6
- package com .microsoft .azure .management .resources .samples ;
7
-
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+ package com .azure .resourcemanager .resources .samples ;
4
+
5
+ import com .azure .core .credential .TokenCredential ;
6
+ import com .azure .core .http .policy .HttpLogDetailLevel ;
7
+ import com .azure .core .management .AzureEnvironment ;
8
+ import com .azure .identity .DefaultAzureCredentialBuilder ;
9
+ import com .azure .resourcemanager .AzureResourceManager ;
10
+ import com .azure .core .management .Region ;
11
+ import com .azure .core .management .profile .AzureProfile ;
12
+ import com .azure .resourcemanager .resources .fluentcore .utils .ResourceManagerUtils ;
13
+ import com .azure .resourcemanager .resources .models .Deployment ;
14
+ import com .azure .resourcemanager .resources .models .DeploymentMode ;
15
+ import com .azure .resourcemanager .samples .Utils ;
8
16
import com .fasterxml .jackson .core .JsonProcessingException ;
9
17
import com .fasterxml .jackson .databind .JsonNode ;
10
18
import com .fasterxml .jackson .databind .ObjectMapper ;
11
19
import com .fasterxml .jackson .databind .node .ObjectNode ;
12
- import com .microsoft .azure .management .Azure ;
13
- import com .microsoft .azure .management .resources .Deployment ;
14
- import com .microsoft .azure .management .resources .DeploymentMode ;
15
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
16
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
17
- import com .microsoft .rest .LogLevel ;
18
-
19
- import java .io .File ;
20
+
20
21
import java .io .IOException ;
21
22
import java .io .InputStream ;
23
+ import java .time .Duration ;
22
24
23
25
/**
24
26
* Azure Resource sample for deploying virtual machine with managed disk using an ARM template.
@@ -27,14 +29,15 @@ public class DeployVirtualMachineUsingARMTemplate {
27
29
28
30
/**
29
31
* Main function which runs the actual sample.
30
- * @param azure instance of the azure client
32
+ *
33
+ * @param azureResourceManager instance of the azure client
31
34
* @return true if sample runs successfully
32
35
*/
33
- public static boolean runSample (Azure azure ) {
34
- final String rgName = SdkContext .randomResourceName ("rgRSAT" , 24 );
35
- final String deploymentName = SdkContext .randomResourceName ("dpRSAT" , 24 );
36
+ public static boolean runSample (AzureResourceManager azureResourceManager ) throws IOException , IllegalAccessException {
37
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgRSAT" , 24 );
38
+ final String deploymentName = Utils .randomResourceName (azureResourceManager , "dpRSAT" , 24 );
36
39
try {
37
- String templateJson = DeployVirtualMachineUsingARMTemplate .getTemplate ();
40
+ String templateJson = DeployVirtualMachineUsingARMTemplate .getTemplate (azureResourceManager );
38
41
39
42
System .out .println (templateJson );
40
43
@@ -43,7 +46,7 @@ public static boolean runSample(Azure azure) {
43
46
44
47
System .out .println ("Creating a resource group with name: " + rgName );
45
48
46
- azure .resourceGroups ().define (rgName )
49
+ azureResourceManager .resourceGroups ().define (rgName )
47
50
.withRegion (Region .US_WEST )
48
51
.create ();
49
52
@@ -56,43 +59,37 @@ public static boolean runSample(Azure azure) {
56
59
57
60
System .out .println ("Starting a deployment for an Azure Virtual Machine with managed disks: " + deploymentName );
58
61
59
- azure .deployments ().define (deploymentName )
62
+ azureResourceManager .deployments ().define (deploymentName )
60
63
.withExistingResourceGroup (rgName )
61
64
.withTemplate (templateJson )
62
65
.withParameters ("{}" )
63
66
.withMode (DeploymentMode .INCREMENTAL )
64
67
.create ();
65
68
66
69
System .out .println ("Started a deployment for an Azure Virtual Machine with managed disks: " + deploymentName );
67
- Deployment deployment = azure .deployments ().getByResourceGroup (rgName , deploymentName );
70
+ Deployment deployment = azureResourceManager .deployments ().getByResourceGroup (rgName , deploymentName );
68
71
System .out .println ("Current deployment status : " + deployment .provisioningState ());
69
72
70
73
while (!(deployment .provisioningState ().equalsIgnoreCase ("Succeeded" )
71
74
|| deployment .provisioningState ().equalsIgnoreCase ("Failed" )
72
75
|| deployment .provisioningState ().equalsIgnoreCase ("Cancelled" ))) {
73
- SdkContext .sleep (10000 );
74
- deployment = azure .deployments ().getByResourceGroup (rgName , deploymentName );
76
+ ResourceManagerUtils .sleep (Duration . ofSeconds ( 10 ) );
77
+ deployment = azureResourceManager .deployments ().getByResourceGroup (rgName , deploymentName );
75
78
System .out .println ("Current deployment status : " + deployment .provisioningState ());
76
79
}
77
80
return true ;
78
- } catch (Exception f ) {
79
-
80
- System .out .println (f .getMessage ());
81
- f .printStackTrace ();
82
-
83
81
} finally {
84
82
85
83
try {
86
84
System .out .println ("Deleting Resource Group: " + rgName );
87
- azure .resourceGroups ().beginDeleteByName (rgName );
85
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
88
86
System .out .println ("Deleted Resource Group: " + rgName );
89
87
} catch (NullPointerException npe ) {
90
88
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
91
89
} catch (Exception g ) {
92
90
g .printStackTrace ();
93
91
}
94
92
}
95
- return false ;
96
93
}
97
94
98
95
/**
@@ -106,36 +103,39 @@ public static void main(String[] args) {
106
103
//=================================================================
107
104
// Authenticate
108
105
109
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
106
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
107
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
108
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
109
+ .build ();
110
110
111
- Azure azure = Azure .configure ()
112
- .withLogLevel (LogLevel .BODY_AND_HEADERS )
113
- .authenticate (credFile )
114
- .withDefaultSubscription ();
111
+ AzureResourceManager azureResourceManager = AzureResourceManager
112
+ .configure ()
113
+ .withLogLevel (HttpLogDetailLevel .BASIC )
114
+ .authenticate (credential , profile )
115
+ .withDefaultSubscription ();
115
116
116
- runSample (azure );
117
+ runSample (azureResourceManager );
117
118
} catch (Exception e ) {
118
119
System .out .println (e .getMessage ());
119
120
e .printStackTrace ();
120
121
}
121
122
}
122
123
123
- private static String getTemplate () throws IllegalAccessException , JsonProcessingException , IOException {
124
+ private static String getTemplate (AzureResourceManager azureResourceManager ) throws IllegalAccessException , JsonProcessingException , IOException {
124
125
final String adminUsername = "tirekicker" ;
125
- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
126
- final String adminPassword = "12NewPA$$w0rd!" ;
127
- final String osDiskName = SdkContext .randomResourceName ("osdisk-" , 24 );
126
+ final String adminPassword = Utils .password ();
127
+ final String osDiskName = Utils .randomResourceName (azureResourceManager , "osdisk-" , 24 );
128
128
129
- final InputStream embeddedTemplate ;
130
- embeddedTemplate = DeployVirtualMachineUsingARMTemplate .class .getResourceAsStream ("/virtualMachineWithManagedDisksTemplate.json" );
129
+ try (InputStream embeddedTemplate = DeployVirtualMachineUsingARMTemplate .class .getResourceAsStream ("/virtualMachineWithManagedDisksTemplate.json" )) {
131
130
132
- final ObjectMapper mapper = new ObjectMapper ();
133
- final JsonNode tmp = mapper .readTree (embeddedTemplate );
131
+ final ObjectMapper mapper = new ObjectMapper ();
132
+ final JsonNode tmp = mapper .readTree (embeddedTemplate );
134
133
135
- DeployVirtualMachineUsingARMTemplate .validateAndAddFieldValue ("string" , adminUsername , "adminUsername" , null , tmp );
136
- DeployVirtualMachineUsingARMTemplate .validateAndAddFieldValue ("string" , adminPassword , "adminPassword" , null , tmp );
137
- DeployVirtualMachineUsingARMTemplate .validateAndAddFieldValue ("string" , osDiskName , "osDiskName" , null , tmp );
138
- return tmp .toString ();
134
+ DeployVirtualMachineUsingARMTemplate .validateAndAddFieldValue ("string" , adminUsername , "adminUsername" , null , tmp );
135
+ DeployVirtualMachineUsingARMTemplate .validateAndAddFieldValue ("string" , adminPassword , "adminPassword" , null , tmp );
136
+ DeployVirtualMachineUsingARMTemplate .validateAndAddFieldValue ("string" , osDiskName , "osDiskName" , null , tmp );
137
+ return tmp .toString ();
138
+ }
139
139
}
140
140
141
141
private static void validateAndAddFieldValue (String type , String fieldValue , String fieldName , String errorMessage ,
0 commit comments