use of com.microsoft.azure.management.network.Network in project azure-sdk-for-java by Azure.
the class VirtualMachineOperationsTests method canStreamParallelCreatedVirtualMachinesAndRelatedResources.
@Test
public void canStreamParallelCreatedVirtualMachinesAndRelatedResources() throws Exception {
String vmNamePrefix = "vmz";
String publicIpNamePrefix = generateRandomResourceName("pip-", 15);
String networkNamePrefix = generateRandomResourceName("vnet-", 15);
int count = 5;
final Set<String> virtualMachineNames = new HashSet<>();
for (int i = 0; i < count; i++) {
virtualMachineNames.add(String.format("%s-%d", vmNamePrefix, i));
}
final Set<String> networkNames = new HashSet<>();
for (int i = 0; i < count; i++) {
networkNames.add(String.format("%s-%d", networkNamePrefix, i));
}
final Set<String> publicIPAddressNames = new HashSet<>();
for (int i = 0; i < count; i++) {
publicIPAddressNames.add(String.format("%s-%d", publicIpNamePrefix, i));
}
final CreatablesInfo creatablesInfo = prepareCreatableVirtualMachines(REGION, vmNamePrefix, networkNamePrefix, publicIpNamePrefix, count);
final AtomicInteger resourceCount = new AtomicInteger(0);
List<Creatable<VirtualMachine>> virtualMachineCreatables = creatablesInfo.virtualMachineCreatables;
computeManager.virtualMachines().createAsync(virtualMachineCreatables).map(new Func1<Indexable, Indexable>() {
@Override
public Indexable call(Indexable createdResource) {
if (createdResource instanceof Resource) {
Resource resource = (Resource) createdResource;
System.out.println("Created: " + resource.id());
if (resource instanceof VirtualMachine) {
VirtualMachine virtualMachine = (VirtualMachine) resource;
Assert.assertTrue(virtualMachineNames.contains(virtualMachine.name()));
Assert.assertNotNull(virtualMachine.id());
} else if (resource instanceof Network) {
Network network = (Network) resource;
Assert.assertTrue(networkNames.contains(network.name()));
Assert.assertNotNull(network.id());
} else if (resource instanceof PublicIPAddress) {
PublicIPAddress publicIPAddress = (PublicIPAddress) resource;
Assert.assertTrue(publicIPAddressNames.contains(publicIPAddress.name()));
Assert.assertNotNull(publicIPAddress.id());
}
}
resourceCount.incrementAndGet();
return createdResource;
}
}).toBlocking().last();
// 1 resource group, 1 storage, 5 network, 5 publicIp, 5 nic, 5 virtual machines
// Additional one for CreatableUpdatableResourceRoot.
// TODO - ans - We should not emit CreatableUpdatableResourceRoot.
Assert.assertEquals(resourceCount.get(), 23);
}
use of com.microsoft.azure.management.network.Network in project azure-sdk-for-java by Azure.
the class VirtualMachineOperationsTests method canCreateVirtualMachinesAndRelatedResourcesInParallel.
@Test
public void canCreateVirtualMachinesAndRelatedResourcesInParallel() throws Exception {
String vmNamePrefix = "vmz";
String publicIpNamePrefix = generateRandomResourceName("pip-", 15);
String networkNamePrefix = generateRandomResourceName("vnet-", 15);
int count = 5;
CreatablesInfo creatablesInfo = prepareCreatableVirtualMachines(REGION, vmNamePrefix, networkNamePrefix, publicIpNamePrefix, count);
List<Creatable<VirtualMachine>> virtualMachineCreatables = creatablesInfo.virtualMachineCreatables;
List<String> networkCreatableKeys = creatablesInfo.networkCreatableKeys;
List<String> publicIpCreatableKeys = creatablesInfo.publicIpCreatableKeys;
CreatedResources<VirtualMachine> createdVirtualMachines = computeManager.virtualMachines().create(virtualMachineCreatables);
Assert.assertTrue(createdVirtualMachines.size() == count);
Set<String> virtualMachineNames = new HashSet<>();
for (int i = 0; i < count; i++) {
virtualMachineNames.add(String.format("%s-%d", vmNamePrefix, i));
}
for (VirtualMachine virtualMachine : createdVirtualMachines.values()) {
Assert.assertTrue(virtualMachineNames.contains(virtualMachine.name()));
Assert.assertNotNull(virtualMachine.id());
}
Set<String> networkNames = new HashSet<>();
for (int i = 0; i < count; i++) {
networkNames.add(String.format("%s-%d", networkNamePrefix, i));
}
for (String networkCreatableKey : networkCreatableKeys) {
Network createdNetwork = (Network) createdVirtualMachines.createdRelatedResource(networkCreatableKey);
Assert.assertNotNull(createdNetwork);
Assert.assertTrue(networkNames.contains(createdNetwork.name()));
}
Set<String> publicIPAddressNames = new HashSet<>();
for (int i = 0; i < count; i++) {
publicIPAddressNames.add(String.format("%s-%d", publicIpNamePrefix, i));
}
for (String publicIpCreatableKey : publicIpCreatableKeys) {
PublicIPAddress createdPublicIPAddress = (PublicIPAddress) createdVirtualMachines.createdRelatedResource(publicIpCreatableKey);
Assert.assertNotNull(createdPublicIPAddress);
Assert.assertTrue(publicIPAddressNames.contains(createdPublicIPAddress.name()));
}
}
use of com.microsoft.azure.management.network.Network in project azure-sdk-for-java by Azure.
the class NetworkManager method listAssociatedSubnets.
// Internal utility function
List<Subnet> listAssociatedSubnets(List<SubnetInner> subnetRefs) {
final Map<String, Network> networks = new HashMap<>();
final List<Subnet> subnets = new ArrayList<>();
if (subnetRefs != null) {
for (SubnetInner subnetRef : subnetRefs) {
String networkId = ResourceUtils.parentResourceIdFromResourceId(subnetRef.id());
Network network = networks.get(networkId);
if (network == null) {
network = this.networks().getById(networkId);
networks.put(networkId, network);
}
String subnetName = ResourceUtils.nameFromResourceId(subnetRef.id());
subnets.add(network.subnets().get(subnetName));
}
}
return Collections.unmodifiableList(subnets);
}
use of com.microsoft.azure.management.network.Network in project azure-sdk-for-java by Azure.
the class ManageAvailabilitySet method runSample.
/**
* 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_WEST_CENTRAL;
final String rgName = Utils.createRandomName("rgCOMA");
final String availSetName1 = Utils.createRandomName("av1");
final String availSetName2 = Utils.createRandomName("av2");
final String vm1Name = Utils.createRandomName("vm1");
final String vm2Name = Utils.createRandomName("vm2");
final String vnetName = Utils.createRandomName("vnet");
final String userName = "tirekicker";
final String password = "12NewPA$$w0rd!";
try {
//=============================================================
// Create an availability set
System.out.println("Creating an availability set");
AvailabilitySet availSet1 = azure.availabilitySets().define(availSetName1).withRegion(region).withNewResourceGroup(rgName).withFaultDomainCount(2).withUpdateDomainCount(4).withSku(AvailabilitySetSkuTypes.MANAGED).withTag("cluster", "Windowslinux").withTag("tag1", "tag1val").create();
System.out.println("Created first availability set: " + availSet1.id());
Utils.print(availSet1);
//=============================================================
// Define a virtual network for the VMs in this availability set
Creatable<Network> networkDefinition = azure.networks().define(vnetName).withRegion(region).withExistingResourceGroup(rgName).withAddressSpace("10.0.0.0/28");
//=============================================================
// Create a Windows VM in the new availability set
System.out.println("Creating a Windows VM in the availability set");
VirtualMachine vm1 = azure.virtualMachines().define(vm1Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork(networkDefinition).withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER).withAdminUsername(userName).withAdminPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).withExistingAvailabilitySet(availSet1).create();
System.out.println("Created first VM:" + vm1.id());
Utils.print(vm1);
//=============================================================
// Create a Linux VM in the same availability set
System.out.println("Creating a Linux VM in the availability set");
VirtualMachine vm2 = azure.virtualMachines().define(vm2Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork(networkDefinition).withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).withExistingAvailabilitySet(availSet1).create();
System.out.println("Created second VM: " + vm2.id());
Utils.print(vm2);
//=============================================================
// Update - Tag the availability set
availSet1 = availSet1.update().withTag("server1", "nginx").withTag("server2", "iis").withoutTag("tag1").apply();
System.out.println("Tagged availability set: " + availSet1.id());
//=============================================================
// Create another availability set
System.out.println("Creating an availability set");
AvailabilitySet availSet2 = azure.availabilitySets().define(availSetName2).withRegion(region).withExistingResourceGroup(rgName).create();
System.out.println("Created second availability set: " + availSet2.id());
Utils.print(availSet2);
//=============================================================
// List availability sets
String resourceGroupName = availSet1.resourceGroupName();
System.out.println("Printing list of availability sets =======");
for (AvailabilitySet availabilitySet : azure.availabilitySets().listByResourceGroup(resourceGroupName)) {
Utils.print(availabilitySet);
}
//=============================================================
// Delete an availability set
System.out.println("Deleting an availability set: " + availSet2.id());
azure.availabilitySets().deleteById(availSet2.id());
System.out.println("Deleted availability set: " + availSet2.id());
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(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();
}
}
return false;
}
use of com.microsoft.azure.management.network.Network in project azure-sdk-for-java by Azure.
the class ManageManagedDisks method prepareNetwork.
private static Network prepareNetwork(Azure azure, Region region, String rgName) {
final String vnetName = SdkContext.randomResourceName("vnet", 24);
Network network = azure.networks().define(vnetName).withRegion(region).withNewResourceGroup(rgName).withAddressSpace("172.16.0.0/16").defineSubnet("subnet1").withAddressPrefix("172.16.1.0/24").attach().create();
return network;
}
Aggregations