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
-
7
- package com .microsoft .azure .management .containerregistry .samples ;
8
-
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ package com .azure .resourcemanager .containerregistry .samples ;
5
+
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 .containerregistry .models .AccessKeyType ;
12
+ import com .azure .resourcemanager .containerregistry .models .Registry ;
13
+ import com .azure .resourcemanager .containerregistry .models .RegistryCredentials ;
14
+ import com .azure .resourcemanager .containerregistry .models .Webhook ;
15
+ import com .azure .resourcemanager .containerregistry .models .WebhookAction ;
16
+ import com .azure .resourcemanager .containerregistry .models .WebhookEventInfo ;
17
+ import com .azure .core .management .Region ;
18
+ import com .azure .core .management .profile .AzureProfile ;
19
+ import com .azure .resourcemanager .samples .DockerUtils ;
20
+ import com .azure .resourcemanager .samples .Utils ;
9
21
import com .github .dockerjava .api .DockerClient ;
10
22
import com .github .dockerjava .api .command .CreateContainerResponse ;
23
+ import com .github .dockerjava .api .command .PullImageResultCallback ;
11
24
import com .github .dockerjava .api .exception .NotFoundException ;
25
+ import com .github .dockerjava .api .model .AuthConfig ;
12
26
import com .github .dockerjava .api .model .Container ;
13
27
import com .github .dockerjava .api .model .Image ;
14
- import com .github .dockerjava .core .command .PullImageResultCallback ;
15
28
import com .github .dockerjava .core .command .PushImageResultCallback ;
16
- import com .microsoft .azure .management .Azure ;
17
- import com .microsoft .azure .management .containerregistry .AccessKeyType ;
18
- import com .microsoft .azure .management .containerregistry .Registry ;
19
- import com .microsoft .azure .management .containerregistry .RegistryCredentials ;
20
- import com .microsoft .azure .management .containerregistry .Webhook ;
21
- import com .microsoft .azure .management .containerregistry .WebhookAction ;
22
- import com .microsoft .azure .management .containerregistry .WebhookEventInfo ;
23
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
24
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
25
- import com .microsoft .azure .management .samples .DockerUtils ;
26
- import com .microsoft .azure .management .samples .Utils ;
27
- import com .microsoft .rest .LogLevel ;
28
-
29
- import java .io .File ;
29
+
30
+ import java .io .IOException ;
30
31
import java .util .Date ;
31
32
import java .util .List ;
33
+ import java .util .stream .Collectors ;
32
34
33
35
/**
34
36
* Azure Container Registry sample for managing container registry with webhooks.
@@ -44,12 +46,12 @@ public class ManageContainerRegistryWithWebhooks {
44
46
/**
45
47
* Main function which runs the actual sample.
46
48
*
47
- * @param azure instance of the azure client
49
+ * @param azureResourceManager instance of the azure client
48
50
* @return true if sample runs successfully
49
51
*/
50
- public static boolean runSample (Azure azure ) {
51
- final String rgName = SdkContext .randomResourceName ("rgACR" , 15 );
52
- final String acrName = SdkContext .randomResourceName ("acrsample" , 20 );
52
+ public static boolean runSample (AzureResourceManager azureResourceManager ) throws IOException , InterruptedException {
53
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgACR" , 15 );
54
+ final String acrName = Utils .randomResourceName (azureResourceManager , "acrsample" , 20 );
53
55
final Region region = Region .US_WEST_CENTRAL ;
54
56
final String dockerImageName = "hello-world" ;
55
57
final String dockerImageTag = "latest" ;
@@ -68,7 +70,7 @@ public static boolean runSample(Azure azure) {
68
70
69
71
Date t1 = new Date ();
70
72
71
- Registry azureRegistry = azure .containerRegistries ().define (acrName )
73
+ Registry azureRegistry = azureResourceManager .containerRegistries ().define (acrName )
72
74
.withRegion (region )
73
75
.withNewResourceGroup (rgName )
74
76
.withBasicSku ()
@@ -98,18 +100,17 @@ public static boolean runSample(Azure azure) {
98
100
99
101
Webhook webhook = azureRegistry .webhooks ().get (webhookName1 );
100
102
webhook .ping ();
101
- List <WebhookEventInfo > webhookEvents = webhook .listEvents ();
103
+ List <WebhookEventInfo > webhookEvents = webhook .listEvents (). stream (). collect ( Collectors . toList ()) ;
102
104
System .out .format ("Found %d webhook events for: %s with container service: %s/n" , webhookEvents .size (), webhook .name (), azureRegistry .name ());
103
105
for (WebhookEventInfo webhookEventInfo : webhookEvents ) {
104
106
System .out .print ("\t " + webhookEventInfo .eventResponseMessage ().content ());
105
107
}
106
108
107
-
108
109
//=============================================================
109
110
// Create a Docker client that will be used to push/pull images to/from the Azure Container Registry
110
111
111
112
RegistryCredentials acrCredentials = azureRegistry .getCredentials ();
112
- DockerClient dockerClient = DockerUtils .createDockerClient (azure , rgName , region ,
113
+ DockerClient dockerClient = DockerUtils .createDockerClient (azureResourceManager , rgName , region ,
113
114
azureRegistry .loginServerUrl (), acrCredentials .username (), acrCredentials .accessKeys ().get (AccessKeyType .PRIMARY ));
114
115
115
116
//=============================================================
@@ -118,12 +119,13 @@ public static boolean runSample(Azure azure) {
118
119
119
120
dockerClient .pullImageCmd (dockerImageName )
120
121
.withTag (dockerImageTag )
122
+ .withAuthConfig (new AuthConfig ()) // anonymous
121
123
.exec (new PullImageResultCallback ())
122
- .awaitSuccess ();
124
+ .awaitCompletion ();
123
125
System .out .println ("List local Docker images:" );
124
126
List <Image > images = dockerClient .listImagesCmd ().withShowAll (true ).exec ();
125
127
for (Image image : images ) {
126
- System .out .format ("\t Found Docker image %s (%s)\ n " , image .getRepoTags ()[0 ], image .getId ());
128
+ System .out .format ("\t Found Docker image %s (%s)% n" , image .getRepoTags ()[0 ], image .getId ());
127
129
}
128
130
129
131
CreateContainerResponse dockerContainerInstance = dockerClient .createContainerCmd (dockerImageName + ":" + dockerImageTag )
@@ -135,14 +137,14 @@ public static boolean runSample(Azure azure) {
135
137
.withShowAll (true )
136
138
.exec ();
137
139
for (Container container : dockerContainers ) {
138
- System .out .format ("\t Found Docker container %s (%s)\ n " , container .getImage (), container .getId ());
140
+ System .out .format ("\t Found Docker container %s (%s)% n" , container .getImage (), container .getId ());
139
141
}
140
142
141
143
//=============================================================
142
144
// Commit the new container
143
145
144
146
String privateRepoUrl = azureRegistry .loginServerUrl () + "/samples/" + dockerContainerName ;
145
- String dockerImageId = dockerClient .commitCmd (dockerContainerInstance .getId ())
147
+ dockerClient .commitCmd (dockerContainerInstance .getId ())
146
148
.withRepository (privateRepoUrl )
147
149
.withTag ("latest" ).exec ();
148
150
@@ -169,28 +171,24 @@ public static boolean runSample(Azure azure) {
169
171
// Gets the container registry webhook after pushing a container image and list the event notifications
170
172
171
173
webhook = azureRegistry .webhooks ().get (webhookName1 );
172
- webhookEvents = webhook .listEvents ();
174
+ webhookEvents = webhook .listEvents (). stream (). collect ( Collectors . toList ()) ;
173
175
System .out .format ("Found %d webhook events for: %s with container service: %s/n" , webhookEvents .size (), webhook .name (), azureRegistry .name ());
174
176
for (WebhookEventInfo webhookEventInfo : webhookEvents ) {
175
177
System .out .print ("\t " + webhookEventInfo .eventResponseMessage ().content ());
176
178
}
177
179
178
180
return true ;
179
- } catch (Exception f ) {
180
- System .out .println (f .getMessage ());
181
- f .printStackTrace ();
182
181
} finally {
183
182
try {
184
183
System .out .println ("Deleting Resource Group: " + rgName );
185
- azure .resourceGroups ().beginDeleteByName (rgName );
184
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
186
185
System .out .println ("Deleted Resource Group: " + rgName );
187
186
} catch (NullPointerException npe ) {
188
187
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
189
188
} catch (Exception g ) {
190
189
g .printStackTrace ();
191
190
}
192
191
}
193
- return false ;
194
192
}
195
193
196
194
/**
@@ -203,17 +201,21 @@ public static void main(String[] args) {
203
201
//=============================================================
204
202
// Authenticate
205
203
206
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
204
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
205
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
206
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
207
+ .build ();
207
208
208
- Azure azure = Azure .configure ()
209
- .withLogLevel (LogLevel .BODY )
210
- .authenticate (credFile )
209
+ AzureResourceManager azureResourceManager = AzureResourceManager
210
+ .configure ()
211
+ .withLogLevel (HttpLogDetailLevel .BASIC )
212
+ .authenticate (credential , profile )
211
213
.withDefaultSubscription ();
212
214
213
215
// Print selected subscription
214
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
216
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
215
217
216
- runSample (azure );
218
+ runSample (azureResourceManager );
217
219
} catch (Exception e ) {
218
220
System .out .println (e .getMessage ());
219
221
e .printStackTrace ();
0 commit comments