Skip to content

Commit 44cad23

Browse files
committed
Release azure-resourcemanager 2.1.0
1 parent a76b439 commit 44cad23

File tree

6 files changed

+1133
-807
lines changed

6 files changed

+1133
-807
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extensions:
2626

2727
To run this sample:
2828

29-
Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-libraries-for-java/blob/master/AUTH.md).
29+
See [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/identity/azure-identity#defaultazurecredential) and prepare the authentication works best for you. For more details on authentication, please refer to [AUTH.md](https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/resourcemanager/docs/AUTH.md).
3030

3131
git clone https://github.com/Azure-Samples/acr-java-manage-azure-container-registry-with-webhooks.git
3232

@@ -36,9 +36,11 @@ Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an aut
3636

3737
## More information ##
3838

39-
[http://azure.com/java](http://azure.com/java)
39+
For general documentation as well as quickstarts on how to use Azure Management Libraries for Java, please see [here](https://aka.ms/azsdk/java/mgmt).
4040

41-
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
41+
Start to develop applications with Java on Azure [here](http://azure.com/java).
42+
43+
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212).
4244

4345
---
4446

pom.xml

+24-23
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
<artifactId>exec-maven-plugin</artifactId>
1616
<version>1.4.0</version>
1717
<configuration>
18-
<mainClass>com.microsoft.azure.management.containerregistry.samples.ManageContainerRegistryWithWebhooks</mainClass>
18+
<mainClass>com.azure.resourcemanager.containerregistry.samples.ManageContainerRegistryWithWebhooks</mainClass>
19+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
1920
</configuration>
2021
</plugin>
2122
<plugin>
2223
<artifactId>maven-compiler-plugin</artifactId>
23-
<version>3.0</version>
24+
<version>3.8.1</version>
2425
<configuration>
25-
<source>1.7</source>
26-
<target>1.7</target>
26+
<source>1.8</source>
27+
<target>1.8</target>
2728
</configuration>
2829
</plugin>
2930
<plugin>
@@ -40,7 +41,7 @@
4041
</descriptorRefs>
4142
<archive>
4243
<manifest>
43-
<mainClass>com.microsoft.azure.management.containerregistry.samples.ManageContainerRegistryWithWebhooks.java</mainClass>
44+
<mainClass>com.azure.resourcemanager.containerregistry.samples.ManageContainerRegistryWithWebhooks.java</mainClass>
4445
</manifest>
4546
</archive>
4647
</configuration>
@@ -51,39 +52,39 @@
5152
</build>
5253
<dependencies>
5354
<dependency>
54-
<groupId>com.microsoft.azure</groupId>
55-
<artifactId>azure</artifactId>
56-
<version>1.36.3</version>
55+
<groupId>com.azure.resourcemanager</groupId>
56+
<artifactId>azure-resourcemanager</artifactId>
57+
<version>2.1.0</version>
5758
</dependency>
5859
<dependency>
59-
<groupId>commons-net</groupId>
60-
<artifactId>commons-net</artifactId>
61-
<version>3.3</version>
60+
<groupId>com.azure</groupId>
61+
<artifactId>azure-identity</artifactId>
62+
<version>1.2.0</version>
6263
</dependency>
6364
<dependency>
64-
<groupId>commons-lang</groupId>
65-
<artifactId>commons-lang</artifactId>
66-
<version>2.6</version>
65+
<groupId>com.jcraft</groupId>
66+
<artifactId>jsch</artifactId>
67+
<version>0.1.55</version>
6768
</dependency>
6869
<dependency>
69-
<groupId>org.apache.commons</groupId>
70-
<artifactId>commons-lang3</artifactId>
71-
<version>3.7</version>
70+
<groupId>commons-net</groupId>
71+
<artifactId>commons-net</artifactId>
72+
<version>3.6</version>
7273
</dependency>
7374
<dependency>
74-
<groupId>com.jcraft</groupId>
75-
<artifactId>jsch</artifactId>
76-
<version>0.1.55</version>
75+
<groupId>com.github.docker-java</groupId>
76+
<artifactId>docker-java</artifactId>
77+
<version>3.2.1</version>
7778
</dependency>
7879
<dependency>
7980
<groupId>com.github.cverges.expect4j</groupId>
8081
<artifactId>expect4j</artifactId>
8182
<version>1.6</version>
8283
</dependency>
8384
<dependency>
84-
<groupId>com.github.docker-java</groupId>
85-
<artifactId>docker-java</artifactId>
86-
<version>3.0.6</version>
85+
<groupId>com.github.spotbugs</groupId>
86+
<artifactId>spotbugs-annotations</artifactId>
87+
<version>4.0.2</version>
8788
</dependency>
8889
</dependencies>
8990
</project>
+49-47
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
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;
921
import com.github.dockerjava.api.DockerClient;
1022
import com.github.dockerjava.api.command.CreateContainerResponse;
23+
import com.github.dockerjava.api.command.PullImageResultCallback;
1124
import com.github.dockerjava.api.exception.NotFoundException;
25+
import com.github.dockerjava.api.model.AuthConfig;
1226
import com.github.dockerjava.api.model.Container;
1327
import com.github.dockerjava.api.model.Image;
14-
import com.github.dockerjava.core.command.PullImageResultCallback;
1528
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;
3031
import java.util.Date;
3132
import java.util.List;
33+
import java.util.stream.Collectors;
3234

3335
/**
3436
* Azure Container Registry sample for managing container registry with webhooks.
@@ -44,12 +46,12 @@ public class ManageContainerRegistryWithWebhooks {
4446
/**
4547
* Main function which runs the actual sample.
4648
*
47-
* @param azure instance of the azure client
49+
* @param azureResourceManager instance of the azure client
4850
* @return true if sample runs successfully
4951
*/
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);
5355
final Region region = Region.US_WEST_CENTRAL;
5456
final String dockerImageName = "hello-world";
5557
final String dockerImageTag = "latest";
@@ -68,7 +70,7 @@ public static boolean runSample(Azure azure) {
6870

6971
Date t1 = new Date();
7072

71-
Registry azureRegistry = azure.containerRegistries().define(acrName)
73+
Registry azureRegistry = azureResourceManager.containerRegistries().define(acrName)
7274
.withRegion(region)
7375
.withNewResourceGroup(rgName)
7476
.withBasicSku()
@@ -98,18 +100,17 @@ public static boolean runSample(Azure azure) {
98100

99101
Webhook webhook = azureRegistry.webhooks().get(webhookName1);
100102
webhook.ping();
101-
List<WebhookEventInfo> webhookEvents = webhook.listEvents();
103+
List<WebhookEventInfo> webhookEvents = webhook.listEvents().stream().collect(Collectors.toList());
102104
System.out.format("Found %d webhook events for: %s with container service: %s/n", webhookEvents.size(), webhook.name(), azureRegistry.name());
103105
for (WebhookEventInfo webhookEventInfo : webhookEvents) {
104106
System.out.print("\t" + webhookEventInfo.eventResponseMessage().content());
105107
}
106108

107-
108109
//=============================================================
109110
// Create a Docker client that will be used to push/pull images to/from the Azure Container Registry
110111

111112
RegistryCredentials acrCredentials = azureRegistry.getCredentials();
112-
DockerClient dockerClient = DockerUtils.createDockerClient(azure, rgName, region,
113+
DockerClient dockerClient = DockerUtils.createDockerClient(azureResourceManager, rgName, region,
113114
azureRegistry.loginServerUrl(), acrCredentials.username(), acrCredentials.accessKeys().get(AccessKeyType.PRIMARY));
114115

115116
//=============================================================
@@ -118,12 +119,13 @@ public static boolean runSample(Azure azure) {
118119

119120
dockerClient.pullImageCmd(dockerImageName)
120121
.withTag(dockerImageTag)
122+
.withAuthConfig(new AuthConfig()) // anonymous
121123
.exec(new PullImageResultCallback())
122-
.awaitSuccess();
124+
.awaitCompletion();
123125
System.out.println("List local Docker images:");
124126
List<Image> images = dockerClient.listImagesCmd().withShowAll(true).exec();
125127
for (Image image : images) {
126-
System.out.format("\tFound Docker image %s (%s)\n", image.getRepoTags()[0], image.getId());
128+
System.out.format("\tFound Docker image %s (%s)%n", image.getRepoTags()[0], image.getId());
127129
}
128130

129131
CreateContainerResponse dockerContainerInstance = dockerClient.createContainerCmd(dockerImageName + ":" + dockerImageTag)
@@ -135,14 +137,14 @@ public static boolean runSample(Azure azure) {
135137
.withShowAll(true)
136138
.exec();
137139
for (Container container : dockerContainers) {
138-
System.out.format("\tFound Docker container %s (%s)\n", container.getImage(), container.getId());
140+
System.out.format("\tFound Docker container %s (%s)%n", container.getImage(), container.getId());
139141
}
140142

141143
//=============================================================
142144
// Commit the new container
143145

144146
String privateRepoUrl = azureRegistry.loginServerUrl() + "/samples/" + dockerContainerName;
145-
String dockerImageId = dockerClient.commitCmd(dockerContainerInstance.getId())
147+
dockerClient.commitCmd(dockerContainerInstance.getId())
146148
.withRepository(privateRepoUrl)
147149
.withTag("latest").exec();
148150

@@ -169,28 +171,24 @@ public static boolean runSample(Azure azure) {
169171
// Gets the container registry webhook after pushing a container image and list the event notifications
170172

171173
webhook = azureRegistry.webhooks().get(webhookName1);
172-
webhookEvents = webhook.listEvents();
174+
webhookEvents = webhook.listEvents().stream().collect(Collectors.toList());
173175
System.out.format("Found %d webhook events for: %s with container service: %s/n", webhookEvents.size(), webhook.name(), azureRegistry.name());
174176
for (WebhookEventInfo webhookEventInfo : webhookEvents) {
175177
System.out.print("\t" + webhookEventInfo.eventResponseMessage().content());
176178
}
177179

178180
return true;
179-
} catch (Exception f) {
180-
System.out.println(f.getMessage());
181-
f.printStackTrace();
182181
} finally {
183182
try {
184183
System.out.println("Deleting Resource Group: " + rgName);
185-
azure.resourceGroups().beginDeleteByName(rgName);
184+
azureResourceManager.resourceGroups().beginDeleteByName(rgName);
186185
System.out.println("Deleted Resource Group: " + rgName);
187186
} catch (NullPointerException npe) {
188187
System.out.println("Did not create any resources in Azure. No clean up is necessary");
189188
} catch (Exception g) {
190189
g.printStackTrace();
191190
}
192191
}
193-
return false;
194192
}
195193

196194
/**
@@ -203,17 +201,21 @@ public static void main(String[] args) {
203201
//=============================================================
204202
// Authenticate
205203

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();
207208

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)
211213
.withDefaultSubscription();
212214

213215
// Print selected subscription
214-
System.out.println("Selected subscription: " + azure.subscriptionId());
216+
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());
215217

216-
runSample(azure);
218+
runSample(azureResourceManager);
217219
} catch (Exception e) {
218220
System.out.println(e.getMessage());
219221
e.printStackTrace();

0 commit comments

Comments
 (0)