|
| 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