Skip to content

Commit

Permalink
Virtual Network Gateway samples (#1923)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenala authored and jianghaolu committed Sep 26, 2017
1 parent 2cdc188 commit 9f30395
Show file tree
Hide file tree
Showing 6 changed files with 11,361 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.microsoft.azure.management.network.samples;

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.network.LocalNetworkGateway;
import com.microsoft.azure.management.network.Network;
import com.microsoft.azure.management.network.VirtualNetworkGateway;
import com.microsoft.azure.management.network.VirtualNetworkGatewayConnection;
import com.microsoft.azure.management.network.VirtualNetworkGatewaySkuName;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.azure.management.samples.Utils;
import com.microsoft.rest.LogLevel;

import java.io.File;
import java.util.List;

/**
* Azure Network sample for managing virtual network gateway.
* - Create virtual network with gateway subnet
* - Create VPN gateway
* - Create local network gateway
* - Create VPN Site-to-Site connection
* - List VPN Gateway connections for particular gateway
* - Reset virtual network gateway
*/

public final class ManageVpnGatewaySite2SiteConnection {

/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final Region region = Region.US_WEST2;
final String rgName = SdkContext.randomResourceName("rg", 20);
final String vnetName = SdkContext.randomResourceName("vnet", 20);
final String vpnGatewayName = SdkContext.randomResourceName("vngw", 20);
final String localGatewayName = SdkContext.randomResourceName("lngw", 20);
final String connectionName = SdkContext.randomResourceName("con", 20);


try {
//============================================================
// Create virtual network
System.out.println("Creating virtual network...");
Network network = azure.networks().define(vnetName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withAddressSpace("10.11.0.0/16")
.withSubnet("GatewaySubnet", "10.11.255.0/27")
.create();
System.out.println("Created network");
// Print the virtual network
Utils.print(network);

//============================================================
// Create VPN gateway
System.out.println("Creating virtual network gateway...");
VirtualNetworkGateway vngw = azure.virtualNetworkGateways().define(vpnGatewayName)
.withRegion(region)
.withExistingResourceGroup(rgName)
.withExistingNetwork(network)
.withRouteBasedVpn()
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
.create();
System.out.println("Created virtual network gateway");

//============================================================
// Create local network gateway
System.out.println("Creating virtual network gateway...");
LocalNetworkGateway lngw = azure.localNetworkGateways().define(localGatewayName)
.withRegion(region)
.withExistingResourceGroup(rgName)
.withIPAddress("40.71.184.214")
.withAddressSpace("192.168.3.0/24")
.create();
System.out.println("Created virtual network gateway");

//============================================================
// Create VPN Site-to-Site connection
System.out.println("Creating virtual network gateway connection...");
vngw.connections()
.define(connectionName)
.withSiteToSite()
.withLocalNetworkGateway(lngw)
.withSharedKey("MySecretKey")
.create();
System.out.println("Created virtual network gateway connection");

//============================================================
// List VPN Gateway connections for particular gateway
List<VirtualNetworkGatewayConnection> connections = vngw.listConnections();

//============================================================
// Reset virtual network gateway
vngw.reset();

return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}

/**
* Main entry point.
* @param args the parameters
*/
public static void main(String[] args) {
try {
//=============================================================
// Authenticate

final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));

Azure azure = Azure.configure()
.withLogLevel(LogLevel.BODY)
.authenticate(credFile)
.withDefaultSubscription();

// Print selected subscription
System.out.println("Selected subscription: " + azure.subscriptionId());

runSample(azure);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

private ManageVpnGatewaySite2SiteConnection() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.microsoft.azure.management.network.samples;

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.network.Network;
import com.microsoft.azure.management.network.VirtualNetworkGateway;
import com.microsoft.azure.management.network.VirtualNetworkGatewayConnection;
import com.microsoft.azure.management.network.VirtualNetworkGatewaySkuName;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.azure.management.samples.Utils;
import com.microsoft.rest.LogLevel;

import java.io.File;
import java.util.List;

/**
* Azure Network sample for managing virtual network gateway.
* - Create 2 virtual network with subnets
* - Create first VPN gateway
* - Create second VPN gateway
* - Create VPN VNet-to-VNet connection
* - List VPN Gateway connections for the first gateway
*/

public final class ManageVpnGatewayVNet2VNetConnection {

/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final Region region = Region.US_WEST2;
final String rgName = SdkContext.randomResourceName("rg", 20);
final String vnetName = SdkContext.randomResourceName("vnet", 20);
final String vpnGatewayName = SdkContext.randomResourceName("vngw", 20);
final String vpnGateway2Name = SdkContext.randomResourceName("vngw2", 20);
final String connectionName = SdkContext.randomResourceName("con", 20);

try {
//============================================================
// Create virtual network
System.out.println("Creating virtual network...");
Network network = azure.networks().define(vnetName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withAddressSpace("10.11.0.0/16")
.withSubnet("GatewaySubnet", "10.11.255.0/27")
.create();
System.out.println("Created network");
// Print the virtual network
Utils.print(network);

VirtualNetworkGateway vngw1 = azure.virtualNetworkGateways().define(vpnGatewayName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withNewNetwork("10.11.0.0/16", "10.11.255.0/27")
.withRouteBasedVpn()
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
.create();

VirtualNetworkGateway vngw2 = azure.virtualNetworkGateways().define(vpnGateway2Name)
.withRegion(region)
.withNewResourceGroup(rgName)
.withNewNetwork("10.41.0.0/16", "10.41.255.0/27")
.withRouteBasedVpn()
.withSku(VirtualNetworkGatewaySkuName.VPN_GW1)
.create();

vngw1.connections()
.define(connectionName)
.withVNetToVNet()
.withSecondVirtualNetworkGateway(vngw2)
.withSharedKey("MySecretKey")
.create();


//============================================================
// List VPN Gateway connections for particular gateway
List<VirtualNetworkGatewayConnection> connections = vngw1.listConnections();

return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}

return false;
}

/**
* Main entry point.
* @param args the parameters
*/
public static void main(String[] args) {
try {
//=============================================================
// Authenticate

final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));

Azure azure = Azure.configure()
.withLogLevel(LogLevel.BODY)
.authenticate(credFile)
.withDefaultSubscription();

// Print selected subscription
System.out.println("Selected subscription: " + azure.subscriptionId());

runSample(azure);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

private ManageVpnGatewayVNet2VNetConnection() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ public void testManageVirtualNetwork() {
public void testManageVirtualNetworkAsync() {
Assert.assertTrue(ManageVirtualNetworkAsync.runSample(azure));
}

@Test
public void testManageVpnGatewaySite2SiteConnection() {
Assert.assertTrue(ManageVpnGatewaySite2SiteConnection.runSample(azure));
}

@Test
public void testManageVpnGatewayVNet2VNetConnection() {
Assert.assertTrue(ManageVpnGatewayVNet2VNetConnection.runSample(azure));
}
}
Loading

0 comments on commit 9f30395

Please sign in to comment.