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+ }
0 commit comments