Search in sources :

Example 1 with Network

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);
}
Also used : Resource(com.microsoft.azure.management.resources.fluentcore.arm.models.Resource) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Network(com.microsoft.azure.management.network.Network) Creatable(com.microsoft.azure.management.resources.fluentcore.model.Creatable) Indexable(com.microsoft.azure.management.resources.fluentcore.model.Indexable) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Network

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()));
    }
}
Also used : Network(com.microsoft.azure.management.network.Network) Creatable(com.microsoft.azure.management.resources.fluentcore.model.Creatable) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Network

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);
}
Also used : HashMap(java.util.HashMap) Network(com.microsoft.azure.management.network.Network) ArrayList(java.util.ArrayList) Subnet(com.microsoft.azure.management.network.Subnet)

Example 4 with Network

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;
}
Also used : Network(com.microsoft.azure.management.network.Network) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) AvailabilitySet(com.microsoft.azure.management.compute.AvailabilitySet) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 5 with Network

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;
}
Also used : Network(com.microsoft.azure.management.network.Network)

Aggregations

Network (com.microsoft.azure.management.network.Network)37 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)17 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)15 ArrayList (java.util.ArrayList)14 Creatable (com.microsoft.azure.management.resources.fluentcore.model.Creatable)11 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)10 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)10 Date (java.util.Date)10 LoadBalancer (com.microsoft.azure.management.network.LoadBalancer)7 NetworkInterface (com.microsoft.azure.management.network.NetworkInterface)7 StopWatch (org.apache.commons.lang3.time.StopWatch)6 NetworkSecurityGroup (com.microsoft.azure.management.network.NetworkSecurityGroup)5 Indexable (com.microsoft.azure.management.resources.fluentcore.model.Indexable)5 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 AvailabilitySet (com.microsoft.azure.management.compute.AvailabilitySet)4 Subnet (com.microsoft.azure.management.network.Subnet)4 VirtualNetwork (com.microsoft.tooling.msservices.model.vm.VirtualNetwork)4 VirtualMachineScaleSet (com.microsoft.azure.management.compute.VirtualMachineScaleSet)3