Skip to content

Commit fd3d9a6

Browse files
committed
Release azure-resourcemanager 2.0.0
1 parent 8a76acc commit fd3d9a6

File tree

5 files changed

+1916
-1674
lines changed

5 files changed

+1916
-1674
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ extensions:
1313

1414

1515
Azure Network sample for managing virtual networks.
16-
- Create a virtual network with Subnets
17-
- Update a virtual network
18-
- Create virtual machines in the virtual network subnets
19-
- Create another virtual network
20-
- List virtual networks
16+
- Create a virtual network with Subnets
17+
- Update a virtual network
18+
- Create virtual machines in the virtual network subnets
19+
- Create another virtual network
20+
- List virtual networks
2121

2222

2323
## Running this Sample ##
2424

2525
To run this sample:
2626

27-
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).
27+
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).
2828

2929
git clone https://github.com/Azure-Samples/network-java-manage-virtual-network-async.git
3030

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

3535
## More information ##
3636

37-
[http://azure.com/java](http://azure.com/java)
37+
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).
3838

39-
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
39+
Start to develop applications with Java on Azure [here](http://azure.com/java).
40+
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).
4042

4143
---
4244

pom.xml

Lines changed: 15 additions & 19 deletions
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.network.samples.ManageVirtualNetworkAsync</mainClass>
18+
<mainClass>com.azure.resourcemanager.network.samples.ManageVirtualNetworkAsync</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.network.samples.ManageVirtualNetworkAsync.java</mainClass>
44+
<mainClass>com.azure.resourcemanager.network.samples.ManageVirtualNetworkAsync.java</mainClass>
4445
</manifest>
4546
</archive>
4647
</configuration>
@@ -51,24 +52,19 @@
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.0.0</version>
5758
</dependency>
5859
<dependency>
59-
<groupId>commons-net</groupId>
60-
<artifactId>commons-net</artifactId>
61-
<version>3.3</version>
62-
</dependency>
63-
<dependency>
64-
<groupId>commons-lang</groupId>
65-
<artifactId>commons-lang</artifactId>
66-
<version>2.6</version>
60+
<groupId>com.azure</groupId>
61+
<artifactId>azure-identity</artifactId>
62+
<version>1.1.3</version>
6763
</dependency>
6864
<dependency>
69-
<groupId>org.apache.commons</groupId>
70-
<artifactId>commons-lang3</artifactId>
71-
<version>3.7</version>
65+
<groupId>commons-net</groupId>
66+
<artifactId>commons-net</artifactId>
67+
<version>3.6</version>
7268
</dependency>
7369
</dependencies>
7470
</project>
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager.network.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.compute.models.KnownLinuxVirtualMachineImage;
12+
import com.azure.resourcemanager.compute.models.VirtualMachine;
13+
import com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes;
14+
import com.azure.resourcemanager.network.models.Network;
15+
import com.azure.resourcemanager.network.models.NetworkSecurityGroup;
16+
import com.azure.resourcemanager.network.models.SecurityRuleProtocol;
17+
import com.azure.core.management.Region;
18+
import com.azure.resourcemanager.resources.fluentcore.model.Indexable;
19+
import com.azure.core.management.profile.AzureProfile;
20+
import com.azure.resourcemanager.samples.Utils;
21+
import reactor.core.publisher.Flux;
22+
23+
import java.util.ArrayList;
24+
import java.util.Date;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.TreeMap;
28+
29+
/**
30+
* Azure Network sample for managing virtual networks.
31+
* - Create a virtual network with Subnets
32+
* - Update a virtual network
33+
* - Create virtual machines in the virtual network subnets
34+
* - Create another virtual network
35+
* - List virtual networks
36+
*/
37+
38+
public final class ManageVirtualNetworkAsync {
39+
40+
private static class Indexable2Duration {
41+
Indexable indexable;
42+
Long duration;
43+
44+
Indexable2Duration(Indexable indexable, long duration) {
45+
this.indexable = indexable;
46+
this.duration = duration;
47+
}
48+
}
49+
50+
/**
51+
* Main function which runs the actual sample.
52+
*
53+
* @param azureResourceManager instance of the azure client
54+
* @return true if sample runs successfully
55+
*/
56+
public static boolean runSample(final AzureResourceManager azureResourceManager) {
57+
final String vnetName1 = Utils.randomResourceName(azureResourceManager, "vnet1", 20);
58+
final String vnetName2 = Utils.randomResourceName(azureResourceManager, "vnet2", 20);
59+
final String vnet1FrontEndSubnetName = "frontend";
60+
final String vnet1BackEndSubnetName = "backend";
61+
final String vnet1FrontEndSubnetNsgName = "frontendnsg";
62+
final String vnet1BackEndSubnetNsgName = "backendnsg";
63+
final String frontEndVMName = Utils.randomResourceName(azureResourceManager, "fevm", 24);
64+
final String backEndVMName = Utils.randomResourceName(azureResourceManager, "bevm", 24);
65+
final String publicIPAddressLeafDnsForFrontEndVM = Utils.randomResourceName(azureResourceManager, "pip1", 24);
66+
final String userName = "tirekicker";
67+
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD [email protected]";
68+
final String rgName = Utils.randomResourceName(azureResourceManager, "rgNEMV", 24);
69+
70+
try {
71+
//============================================================
72+
// Create a virtual network with specific address-space and two subnet
73+
74+
// Creates a network security group for backend subnet
75+
System.out.println("Creating a network security group for virtual network backend subnet...");
76+
77+
// Creates a network security group for frontend subnet
78+
System.out.println("Creating a network security group for virtual network backend subnet...");
79+
80+
final Map<String, Indexable> createdResources = new TreeMap<>();
81+
82+
Flux.merge(
83+
azureResourceManager.networkSecurityGroups().define(vnet1BackEndSubnetNsgName)
84+
.withRegion(Region.US_EAST)
85+
.withNewResourceGroup(rgName)
86+
.defineRule("DenyInternetInComing")
87+
.denyInbound()
88+
.fromAddress("INTERNET")
89+
.fromAnyPort()
90+
.toAnyAddress()
91+
.toAnyPort()
92+
.withAnyProtocol()
93+
.attach()
94+
.defineRule("DenyInternetOutGoing")
95+
.denyOutbound()
96+
.fromAnyAddress()
97+
.fromAnyPort()
98+
.toAddress("INTERNET")
99+
.toAnyPort()
100+
.withAnyProtocol()
101+
.attach()
102+
.createAsync()
103+
.flatMapMany(backEndNsg -> {
104+
System.out.println("Creating virtual network #1...");
105+
return Flux.merge(
106+
Flux.just(backEndNsg),
107+
azureResourceManager.networks().define(vnetName1)
108+
.withRegion(Region.US_EAST)
109+
.withExistingResourceGroup(rgName)
110+
.withAddressSpace("192.168.0.0/16")
111+
.withSubnet(vnet1FrontEndSubnetName, "192.168.1.0/24")
112+
.defineSubnet(vnet1BackEndSubnetName)
113+
.withAddressPrefix("192.168.2.0/24")
114+
.withExistingNetworkSecurityGroup(backEndNsg)
115+
.attach()
116+
.createAsync());
117+
}),
118+
azureResourceManager.networkSecurityGroups().define(vnet1FrontEndSubnetNsgName)
119+
.withRegion(Region.US_EAST)
120+
.withNewResourceGroup(rgName)
121+
.defineRule("AllowHttpInComing")
122+
.allowInbound()
123+
.fromAddress("INTERNET")
124+
.fromAnyPort()
125+
.toAnyAddress()
126+
.toPort(80)
127+
.withProtocol(SecurityRuleProtocol.TCP)
128+
.attach()
129+
.defineRule("DenyInternetOutGoing")
130+
.denyOutbound()
131+
.fromAnyAddress()
132+
.fromAnyPort()
133+
.toAddress("INTERNET")
134+
.toAnyPort()
135+
.withAnyProtocol()
136+
.attach()
137+
.createAsync()
138+
).map(indexable -> {
139+
if (indexable instanceof NetworkSecurityGroup) {
140+
NetworkSecurityGroup nsg = (NetworkSecurityGroup) indexable;
141+
createdResources.put(nsg.name(), nsg);
142+
} else if (indexable instanceof Network) {
143+
Network vn = (Network) indexable;
144+
createdResources.put(vn.name(), vn);
145+
}
146+
return indexable;
147+
}).blockLast();
148+
149+
NetworkSecurityGroup frontEndSubnetNsg = (NetworkSecurityGroup) createdResources.get(vnet1FrontEndSubnetNsgName);
150+
Network virtualNetwork1 = (Network) createdResources.get(vnetName1);
151+
152+
System.out.println("Created network security group");
153+
// Print the network security group
154+
Utils.print(frontEndSubnetNsg);
155+
156+
System.out.println("Created a virtual network");
157+
// Print the virtual network details
158+
Utils.print(virtualNetwork1);
159+
160+
//============================================================
161+
// Update a virtual network
162+
163+
// Update the virtual network frontend subnet by associating it with network security group
164+
165+
System.out.println("Associating network security group rule to frontend subnet");
166+
167+
virtualNetwork1.update()
168+
.updateSubnet(vnet1FrontEndSubnetName)
169+
.withExistingNetworkSecurityGroup(frontEndSubnetNsg)
170+
.parent()
171+
.applyAsync()
172+
.block();
173+
174+
System.out.println("Network security group rule associated with the frontend subnet");
175+
// Print the virtual network details
176+
Utils.print(virtualNetwork1);
177+
178+
179+
//============================================================
180+
// Create a virtual machine in each subnet and another virtual network
181+
// Creates the first virtual machine in frontend subnet
182+
System.out.println("Creating a Linux virtual machine in the frontend subnet");
183+
// Creates the second virtual machine in the backend subnet
184+
System.out.println("Creating a Linux virtual machine in the backend subnet");
185+
// Create a virtual network with default address-space and one default subnet
186+
System.out.println("Creating virtual network #2...");
187+
188+
final List<Indexable2Duration> creations = new ArrayList<>();
189+
final Date t1 = new Date();
190+
191+
Flux.merge(
192+
azureResourceManager.virtualMachines().define(frontEndVMName)
193+
.withRegion(Region.US_EAST)
194+
.withExistingResourceGroup(rgName)
195+
.withExistingPrimaryNetwork(virtualNetwork1)
196+
.withSubnet(vnet1FrontEndSubnetName)
197+
.withPrimaryPrivateIPAddressDynamic()
198+
.withNewPrimaryPublicIPAddress(publicIPAddressLeafDnsForFrontEndVM)
199+
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
200+
.withRootUsername(userName)
201+
.withSsh(sshKey)
202+
.withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
203+
.createAsync(),
204+
azureResourceManager.virtualMachines().define(backEndVMName)
205+
.withRegion(Region.US_EAST)
206+
.withExistingResourceGroup(rgName)
207+
.withExistingPrimaryNetwork(virtualNetwork1)
208+
.withSubnet(vnet1BackEndSubnetName)
209+
.withPrimaryPrivateIPAddressDynamic()
210+
.withoutPrimaryPublicIPAddress()
211+
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
212+
.withRootUsername(userName)
213+
.withSsh(sshKey)
214+
.withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
215+
.createAsync(),
216+
azureResourceManager.networks().define(vnetName2)
217+
.withRegion(Region.US_EAST)
218+
.withNewResourceGroup(rgName)
219+
.createAsync())
220+
.map(indexable -> {
221+
Date t2 = new Date();
222+
long duration = ((t2.getTime() - t1.getTime()) / 1000);
223+
creations.add(new Indexable2Duration(indexable, duration));
224+
return indexable;
225+
});
226+
227+
for (Indexable2Duration creation : creations) {
228+
if (creation.indexable instanceof VirtualMachine) {
229+
VirtualMachine vm = (VirtualMachine) creation.indexable;
230+
System.out.println("Created Linux VM: (took "
231+
+ creation.duration + " seconds) " + vm.id());
232+
// Print virtual machine details
233+
Utils.print(vm);
234+
} else if (creation.indexable instanceof Network) {
235+
Network vn = (Network) creation.indexable;
236+
System.out.println("Created a virtual network: took "
237+
+ creation.duration + " seconds) " + vn.id());
238+
// Print the virtual network details
239+
Utils.print(vn);
240+
}
241+
}
242+
243+
//============================================================
244+
// List virtual networks and print details
245+
for (Network network : azureResourceManager.networks().listByResourceGroup(rgName)) {
246+
Utils.print(network);
247+
}
248+
249+
return true;
250+
} finally {
251+
try {
252+
System.out.println("Deleting Resource Group: " + rgName);
253+
azureResourceManager.resourceGroups().deleteByNameAsync(rgName).block();
254+
System.out.println("Deleted Resource Group: " + rgName);
255+
} catch (NullPointerException npe) {
256+
System.out.println("Did not create any resources in Azure. No clean up is necessary");
257+
} catch (Exception g) {
258+
g.printStackTrace();
259+
}
260+
}
261+
}
262+
263+
/**
264+
* Main entry point.
265+
*
266+
* @param args the parameters
267+
*/
268+
public static void main(String[] args) {
269+
try {
270+
//=============================================================
271+
// Authenticate
272+
273+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
274+
final TokenCredential credential = new DefaultAzureCredentialBuilder()
275+
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
276+
.build();
277+
278+
AzureResourceManager azureResourceManager = AzureResourceManager
279+
.configure()
280+
.withLogLevel(HttpLogDetailLevel.BASIC)
281+
.authenticate(credential, profile)
282+
.withDefaultSubscription();
283+
284+
// Print selected subscription
285+
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());
286+
287+
runSample(azureResourceManager);
288+
} catch (Exception e) {
289+
System.out.println(e.getMessage());
290+
e.printStackTrace();
291+
}
292+
}
293+
294+
private ManageVirtualNetworkAsync() {
295+
}
296+
}

0 commit comments

Comments
 (0)