Skip to content

Commit 9f30395

Browse files
lenalajianghaolu
authored andcommitted
Virtual Network Gateway samples (#1923)
1 parent 2cdc188 commit 9f30395

File tree

6 files changed

+11361
-0
lines changed

6 files changed

+11361
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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.network.samples;
8+
9+
import com.microsoft.azure.management.Azure;
10+
import com.microsoft.azure.management.network.LocalNetworkGateway;
11+
import com.microsoft.azure.management.network.Network;
12+
import com.microsoft.azure.management.network.VirtualNetworkGateway;
13+
import com.microsoft.azure.management.network.VirtualNetworkGatewayConnection;
14+
import com.microsoft.azure.management.network.VirtualNetworkGatewaySkuName;
15+
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
16+
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
17+
import com.microsoft.azure.management.samples.Utils;
18+
import com.microsoft.rest.LogLevel;
19+
20+
import java.io.File;
21+
import java.util.List;
22+
23+
/**
24+
* Azure Network sample for managing virtual network gateway.
25+
* - Create virtual network with gateway subnet
26+
* - Create VPN gateway
27+
* - Create local network gateway
28+
* - Create VPN Site-to-Site connection
29+
* - List VPN Gateway connections for particular gateway
30+
* - Reset virtual network gateway
31+
*/
32+
33+
public final class ManageVpnGatewaySite2SiteConnection {
34+
35+
/**
36+
* Main function which runs the actual sample.
37+
* @param azure instance of the azure client
38+
* @return true if sample runs successfully
39+
*/
40+
public static boolean runSample(Azure azure) {
41+
final Region region = Region.US_WEST2;
42+
final String rgName = SdkContext.randomResourceName("rg", 20);
43+
final String vnetName = SdkContext.randomResourceName("vnet", 20);
44+
final String vpnGatewayName = SdkContext.randomResourceName("vngw", 20);
45+
final String localGatewayName = SdkContext.randomResourceName("lngw", 20);
46+
final String connectionName = SdkContext.randomResourceName("con", 20);
47+
48+
49+
try {
50+
//============================================================
51+
// Create virtual network
52+
System.out.println("Creating virtual network...");
53+
Network network = azure.networks().define(vnetName)
54+
.withRegion(region)
55+
.withNewResourceGroup(rgName)
56+
.withAddressSpace("10.11.0.0/16")
57+
.withSubnet("GatewaySubnet", "10.11.255.0/27")
58+
.create();
59+
System.out.println("Created network");
60+
// Print the virtual network
61+
Utils.print(network);
62+
63+
//============================================================
64+
// Create VPN gateway
65+
System.out.println("Creating virtual network gateway...");
66+
VirtualNetworkGateway vngw = azure.virtualNetworkGateways().define(vpnGatewayName)
67+
.withRegion(region)
68+
.withExistingResourceGroup(rgName)
69+
.withExistingNetwork(network)
70+
.withRouteBasedVpn()
71+
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
72+
.create();
73+
System.out.println("Created virtual network gateway");
74+
75+
//============================================================
76+
// Create local network gateway
77+
System.out.println("Creating virtual network gateway...");
78+
LocalNetworkGateway lngw = azure.localNetworkGateways().define(localGatewayName)
79+
.withRegion(region)
80+
.withExistingResourceGroup(rgName)
81+
.withIPAddress("40.71.184.214")
82+
.withAddressSpace("192.168.3.0/24")
83+
.create();
84+
System.out.println("Created virtual network gateway");
85+
86+
//============================================================
87+
// Create VPN Site-to-Site connection
88+
System.out.println("Creating virtual network gateway connection...");
89+
vngw.connections()
90+
.define(connectionName)
91+
.withSiteToSite()
92+
.withLocalNetworkGateway(lngw)
93+
.withSharedKey("MySecretKey")
94+
.create();
95+
System.out.println("Created virtual network gateway connection");
96+
97+
//============================================================
98+
// List VPN Gateway connections for particular gateway
99+
List<VirtualNetworkGatewayConnection> connections = vngw.listConnections();
100+
101+
//============================================================
102+
// Reset virtual network gateway
103+
vngw.reset();
104+
105+
return true;
106+
} catch (Exception e) {
107+
System.err.println(e.getMessage());
108+
e.printStackTrace();
109+
} finally {
110+
try {
111+
System.out.println("Deleting Resource Group: " + rgName);
112+
azure.resourceGroups().beginDeleteByName(rgName);
113+
} catch (NullPointerException npe) {
114+
System.out.println("Did not create any resources in Azure. No clean up is necessary");
115+
} catch (Exception g) {
116+
g.printStackTrace();
117+
}
118+
}
119+
return false;
120+
}
121+
122+
/**
123+
* Main entry point.
124+
* @param args the parameters
125+
*/
126+
public static void main(String[] args) {
127+
try {
128+
//=============================================================
129+
// Authenticate
130+
131+
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
132+
133+
Azure azure = Azure.configure()
134+
.withLogLevel(LogLevel.BODY)
135+
.authenticate(credFile)
136+
.withDefaultSubscription();
137+
138+
// Print selected subscription
139+
System.out.println("Selected subscription: " + azure.subscriptionId());
140+
141+
runSample(azure);
142+
} catch (Exception e) {
143+
System.out.println(e.getMessage());
144+
e.printStackTrace();
145+
}
146+
}
147+
148+
private ManageVpnGatewaySite2SiteConnection() {
149+
}
150+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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.network.samples;
8+
9+
import com.microsoft.azure.management.Azure;
10+
import com.microsoft.azure.management.network.Network;
11+
import com.microsoft.azure.management.network.VirtualNetworkGateway;
12+
import com.microsoft.azure.management.network.VirtualNetworkGatewayConnection;
13+
import com.microsoft.azure.management.network.VirtualNetworkGatewaySkuName;
14+
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
15+
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
16+
import com.microsoft.azure.management.samples.Utils;
17+
import com.microsoft.rest.LogLevel;
18+
19+
import java.io.File;
20+
import java.util.List;
21+
22+
/**
23+
* Azure Network sample for managing virtual network gateway.
24+
* - Create 2 virtual network with subnets
25+
* - Create first VPN gateway
26+
* - Create second VPN gateway
27+
* - Create VPN VNet-to-VNet connection
28+
* - List VPN Gateway connections for the first gateway
29+
*/
30+
31+
public final class ManageVpnGatewayVNet2VNetConnection {
32+
33+
/**
34+
* Main function which runs the actual sample.
35+
* @param azure instance of the azure client
36+
* @return true if sample runs successfully
37+
*/
38+
public static boolean runSample(Azure azure) {
39+
final Region region = Region.US_WEST2;
40+
final String rgName = SdkContext.randomResourceName("rg", 20);
41+
final String vnetName = SdkContext.randomResourceName("vnet", 20);
42+
final String vpnGatewayName = SdkContext.randomResourceName("vngw", 20);
43+
final String vpnGateway2Name = SdkContext.randomResourceName("vngw2", 20);
44+
final String connectionName = SdkContext.randomResourceName("con", 20);
45+
46+
try {
47+
//============================================================
48+
// Create virtual network
49+
System.out.println("Creating virtual network...");
50+
Network network = azure.networks().define(vnetName)
51+
.withRegion(region)
52+
.withNewResourceGroup(rgName)
53+
.withAddressSpace("10.11.0.0/16")
54+
.withSubnet("GatewaySubnet", "10.11.255.0/27")
55+
.create();
56+
System.out.println("Created network");
57+
// Print the virtual network
58+
Utils.print(network);
59+
60+
VirtualNetworkGateway vngw1 = azure.virtualNetworkGateways().define(vpnGatewayName)
61+
.withRegion(region)
62+
.withNewResourceGroup(rgName)
63+
.withNewNetwork("10.11.0.0/16", "10.11.255.0/27")
64+
.withRouteBasedVpn()
65+
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
66+
.create();
67+
68+
VirtualNetworkGateway vngw2 = azure.virtualNetworkGateways().define(vpnGateway2Name)
69+
.withRegion(region)
70+
.withNewResourceGroup(rgName)
71+
.withNewNetwork("10.41.0.0/16", "10.41.255.0/27")
72+
.withRouteBasedVpn()
73+
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
74+
.create();
75+
76+
vngw1.connections()
77+
.define(connectionName)
78+
.withVNetToVNet()
79+
.withSecondVirtualNetworkGateway(vngw2)
80+
.withSharedKey("MySecretKey")
81+
.create();
82+
83+
84+
//============================================================
85+
// List VPN Gateway connections for particular gateway
86+
List<VirtualNetworkGatewayConnection> connections = vngw1.listConnections();
87+
88+
return true;
89+
} catch (Exception e) {
90+
System.err.println(e.getMessage());
91+
e.printStackTrace();
92+
} finally {
93+
try {
94+
System.out.println("Deleting Resource Group: " + rgName);
95+
azure.resourceGroups().beginDeleteByName(rgName);
96+
} catch (NullPointerException npe) {
97+
System.out.println("Did not create any resources in Azure. No clean up is necessary");
98+
} catch (Exception g) {
99+
g.printStackTrace();
100+
}
101+
}
102+
103+
return false;
104+
}
105+
106+
/**
107+
* Main entry point.
108+
* @param args the parameters
109+
*/
110+
public static void main(String[] args) {
111+
try {
112+
//=============================================================
113+
// Authenticate
114+
115+
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
116+
117+
Azure azure = Azure.configure()
118+
.withLogLevel(LogLevel.BODY)
119+
.authenticate(credFile)
120+
.withDefaultSubscription();
121+
122+
// Print selected subscription
123+
System.out.println("Selected subscription: " + azure.subscriptionId());
124+
125+
runSample(azure);
126+
} catch (Exception e) {
127+
System.out.println(e.getMessage());
128+
e.printStackTrace();
129+
}
130+
}
131+
132+
private ManageVpnGatewayVNet2VNetConnection() {
133+
}
134+
}

azure-samples/src/test/java/com/microsoft/azure/management/samples/NetworkSampleTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,14 @@ public void testManageVirtualNetwork() {
7676
public void testManageVirtualNetworkAsync() {
7777
Assert.assertTrue(ManageVirtualNetworkAsync.runSample(azure));
7878
}
79+
80+
@Test
81+
public void testManageVpnGatewaySite2SiteConnection() {
82+
Assert.assertTrue(ManageVpnGatewaySite2SiteConnection.runSample(azure));
83+
}
84+
85+
@Test
86+
public void testManageVpnGatewayVNet2VNetConnection() {
87+
Assert.assertTrue(ManageVpnGatewayVNet2VNetConnection.runSample(azure));
88+
}
7989
}

0 commit comments

Comments
 (0)