Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual network sample, using specific exceptions in Subnet::NSG() getter #907

Merged
merged 3 commits into from
Jun 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
*/
package com.microsoft.azure.management.network;

import com.microsoft.azure.CloudException;
import com.microsoft.azure.management.network.implementation.api.SubnetInner;
import com.microsoft.azure.management.resources.fluentcore.arm.models.ChildResource;
import com.microsoft.azure.management.resources.fluentcore.model.Attachable;
import com.microsoft.azure.management.resources.fluentcore.model.Settable;
import com.microsoft.azure.management.resources.fluentcore.model.Wrapper;

import java.io.IOException;

/**
* An immutable client-side representation of a subnet of a virtual network.
*/
Expand All @@ -27,9 +30,11 @@ public interface Subnet extends
* @return the network security group associated with this subnet
* <p>
* Note that this method will result in a call to Azure each time it is invoked.
* @throws Exception if there are problems retrieving the associated network security group
* @throws CloudException exceptions thrown from the cloud
* @throws IOException exceptions thrown from serialization/deserialization
* @throws IllegalArgumentException exceptions thrown when something is wrong with the input parameters
*/
NetworkSecurityGroup networkSecurityGroup() throws Exception;
NetworkSecurityGroup networkSecurityGroup() throws CloudException, IllegalArgumentException, IOException;

/**
* Grouping of subnet definition stages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@

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

import com.microsoft.azure.Azure;
import com.microsoft.azure.management.network.Network;
import com.microsoft.azure.management.network.NetworkSecurityGroup;
import com.microsoft.azure.management.network.NetworkSecurityRule;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
import com.microsoft.azure.management.samples.Utils;
import okhttp3.logging.HttpLoggingInterceptor;

import java.io.File;

/**
* Azure Network sample for managing virtual networks -
* - Create a virtual network
Expand All @@ -24,28 +35,164 @@ public final class ManageVirtualNetwork {
* @param args the parameters
*/
public static void main(String[] args) {
final String vnetName1 = ResourceNamer.randomResourceName("vnet1", 20);
final String vnetName2 = ResourceNamer.randomResourceName("vnet2", 20);
final String vnet2FrontEndSubnetName = "frontend";
final String vnet2BackEndSubnetName = "backend";
final String vnet2FrontEndSubnetNsgName = "frontendnsg";
final String vnet2BackEndSubnetNsgName = "backendnsg";

final String rgName = ResourceNamer.randomResourceName("rgNEMVnet", 24);
try {

// Create a virtual network
//=============================================================
// Authenticate

final File credFile = new File("my.azureauth");

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

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

//============================================================
// Create a virtual network with default address-space and one default subnet

System.out.println("Creating virtual network #1...");

Network virtualNetwork1 = azure.networks()
.define(vnetName1)
.withRegion(Region.US_EAST)
.withNewGroup(rgName)
.create();

// Print the virtual network details
Utils.print(virtualNetwork1);

//============================================================
// Create a virtual network with specific address-space and two subnet

// Creates a network security group for backend subnet

System.out.println("Creating a network security group for virtual network backend subnet...");

// Create a virtual network with subnets
NetworkSecurityGroup backEndSubnetNsg = azure.networkSecurityGroups()
.define(vnet2BackEndSubnetNsgName)
.withRegion(Region.US_EAST)
.withExistingGroup(rgName)
.defineRule("DenyInternetInComing")
.denyInbound()
.fromAddress("INTERNET")
.fromAnyPort()
.toAnyAddress()
.toAnyPort()
.withAnyProtocol()
.attach()
.defineRule("DenyInternetOutGoing")
.denyOutbound()
.fromAnyAddress()
.fromAnyPort()
.toAddress("INTERNET")
.toAnyPort()
.withAnyProtocol()
.attach()
.create();

// Update a virtual network
// Create the virtual network

// Create another virtual network
System.out.println("Creating virtual network #2...");

// List virtual networks
Network virtualNetwork2 = azure.networks()
.define(vnetName2)
.withRegion(Region.US_EAST)
.withExistingGroup(rgName)
.withAddressSpace("192.168.0.0/16")
.withSubnet(vnet2FrontEndSubnetName, "192.168.1.0/24")
.defineSubnet(vnet2BackEndSubnetName)
.withAddressPrefix("192.168.2.0/24")
.withExistingNetworkSecurityGroup(backEndSubnetNsg)
.attach()
.create();

// Delete a virtual network
// Print the virtual network details
Utils.print(virtualNetwork2);

//============================================================
// Update a virtual network

// Creates a network security group for frontend subnet

System.out.println("Creating a network security group for virtual network backend subnet...");

NetworkSecurityGroup frontEndSubnetNsg = azure.networkSecurityGroups()
.define(vnet2FrontEndSubnetNsgName)
.withRegion(Region.US_EAST)
.withExistingGroup(rgName)
.defineRule("AllowHttpInComing")
.allowInbound()
.fromAddress("INTERNET")
.fromAnyPort()
.toAnyAddress()
.toPort(80)
.withProtocol(NetworkSecurityRule.Protocol.TCP)
.attach()
.defineRule("DenyInternetOutGoing")
.denyOutbound()
.fromAnyAddress()
.fromAnyPort()
.toAddress("INTERNET")
.toAnyPort()
.withAnyProtocol()
.attach()
.create();

// Update the virtual network front end subnet

virtualNetwork2.update()
.updateSubnet(vnet2FrontEndSubnetName)
.withExistingNetworkSecurityGroup(frontEndSubnetNsg)
.parent()
.apply();

// Print the virtual network details
Utils.print(virtualNetwork2);

//============================================================
// List virtual networks

for (Network virtualNetwork : azure.networks().listByGroup(rgName)) {
Utils.print(virtualNetwork);
}

//============================================================
// Delete a virtual network
azure.networks().delete(virtualNetwork1.id());
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().delete(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}

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

private ManageVirtualNetwork() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public static void print(Network resource) throws CloudException, IOException {
for (Subnet subnet : resource.subnets().values()) {
info.append("\n\tSubnet: ").append(subnet.name())
.append("\n\t\tAddress prefix: ").append(subnet.addressPrefix());
NetworkSecurityGroup subnetNsg = subnet.networkSecurityGroup();
if (subnetNsg != null) {
info.append("\n\t\tNetwork security group: ").append(subnetNsg.id());
}
}

System.out.println(info.toString());
Expand Down