Search in sources :

Example 16 with Network

use of com.microsoft.azure.management.network.Network in project azure-tools-for-java by Microsoft.

the class AzureSDKManager method createVirtualMachine.

public static VirtualMachine createVirtualMachine(String subscriptionId, @NotNull String name, @NotNull String resourceGroup, boolean withNewResourceGroup, @NotNull String size, @NotNull String region, final VirtualMachineImage vmImage, Object knownImage, boolean isKnownImage, final StorageAccount storageAccount, final Network network, VirtualNetwork newNetwork, boolean withNewNetwork, @NotNull String subnet, @Nullable PublicIPAddress pip, boolean withNewPip, @Nullable AvailabilitySet availabilitySet, boolean withNewAvailabilitySet, @NotNull final String username, @Nullable final String password, @Nullable String publicKey) throws Exception {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    Azure azure = azureManager.getAzure(subscriptionId);
    boolean isWindows;
    if (isKnownImage) {
        isWindows = knownImage instanceof KnownWindowsVirtualMachineImage;
    } else {
        isWindows = vmImage.osDiskImage().operatingSystem().equals(OperatingSystemTypes.WINDOWS);
    }
    // ------ Resource Group ------
    VirtualMachine.DefinitionStages.WithGroup withGroup = azure.virtualMachines().define(name).withRegion(region);
    Creatable<ResourceGroup> newResourceGroup = null;
    VirtualMachine.DefinitionStages.WithNetwork withNetwork;
    if (withNewResourceGroup) {
        newResourceGroup = azure.resourceGroups().define(resourceGroup).withRegion(region);
        withNetwork = withGroup.withNewResourceGroup(newResourceGroup);
    } else {
        withNetwork = withGroup.withExistingResourceGroup(resourceGroup);
    }
    // ------ Virtual Network -----
    VirtualMachine.DefinitionStages.WithPublicIPAddress withPublicIpAddress;
    if (withNewNetwork) {
        Network.DefinitionStages.WithGroup networkWithGroup = azure.networks().define(newNetwork.name).withRegion(region);
        Creatable<Network> newVirtualNetwork;
        if (withNewResourceGroup) {
            newVirtualNetwork = networkWithGroup.withNewResourceGroup(newResourceGroup).withAddressSpace(newNetwork.addressSpace).withSubnet(newNetwork.subnet.name, newNetwork.subnet.addressSpace);
        } else {
            newVirtualNetwork = networkWithGroup.withExistingResourceGroup(resourceGroup).withAddressSpace(newNetwork.addressSpace).withSubnet(newNetwork.subnet.name, newNetwork.subnet.addressSpace);
        }
        withPublicIpAddress = withNetwork.withNewPrimaryNetwork(newVirtualNetwork).withPrimaryPrivateIPAddressDynamic();
    // withPublicIpAddress = withNetwork.withNewPrimaryNetwork("10.0.0.0/28").
    // .withPrimaryPrivateIpAddressDynamic();
    } else {
        withPublicIpAddress = withNetwork.withExistingPrimaryNetwork(network).withSubnet(subnet).withPrimaryPrivateIPAddressDynamic();
    }
    // ------ Public IP Address------
    VirtualMachine.DefinitionStages.WithOS withOS;
    if (pip == null) {
        if (withNewPip) {
            withOS = withPublicIpAddress.withNewPrimaryPublicIPAddress(name + "pip");
        } else {
            withOS = withPublicIpAddress.withoutPrimaryPublicIPAddress();
        }
    } else {
        withOS = withPublicIpAddress.withExistingPrimaryPublicIPAddress(pip);
    }
    // ------ OS and credentials -----
    VirtualMachine.DefinitionStages.WithCreate withCreate;
    if (isWindows) {
        VirtualMachine.DefinitionStages.WithWindowsAdminUsernameManagedOrUnmanaged withWindowsAdminUsername;
        if (isKnownImage) {
            withWindowsAdminUsername = withOS.withPopularWindowsImage((KnownWindowsVirtualMachineImage) knownImage);
        } else {
            withWindowsAdminUsername = withOS.withSpecificWindowsImageVersion(vmImage.imageReference());
        }
        withCreate = withWindowsAdminUsername.withAdminUsername(username).withAdminPassword(password).withUnmanagedDisks();
    } else {
        VirtualMachine.DefinitionStages.WithLinuxRootPasswordOrPublicKeyManagedOrUnmanaged withLinuxRootPasswordOrPublicKey;
        if (isKnownImage) {
            withLinuxRootPasswordOrPublicKey = withOS.withPopularLinuxImage((KnownLinuxVirtualMachineImage) knownImage).withRootUsername(username);
        } else {
            withLinuxRootPasswordOrPublicKey = withOS.withSpecificLinuxImageVersion(vmImage.imageReference()).withRootUsername(username);
        }
        VirtualMachine.DefinitionStages.WithLinuxCreateManagedOrUnmanaged withLinuxCreate;
        // we assume either password or public key is not empty
        if (password != null && !password.isEmpty()) {
            withLinuxCreate = withLinuxRootPasswordOrPublicKey.withRootPassword(password);
            if (publicKey != null) {
                withLinuxCreate = withLinuxCreate.withSsh(publicKey);
            }
        } else {
            withLinuxCreate = withLinuxRootPasswordOrPublicKey.withSsh(publicKey);
        }
        withCreate = withLinuxCreate.withUnmanagedDisks();
    }
    withCreate = withCreate.withSize(size);
    // ---- Storage Account --------
    com.microsoft.azure.management.storage.StorageAccount existedStorageAccount = azure.storageAccounts().getById(storageAccount.id());
    withCreate = withCreate.withExistingStorageAccount(existedStorageAccount);
    if (withNewAvailabilitySet) {
        withCreate = withCreate.withNewAvailabilitySet(name + "as");
    } else if (availabilitySet != null) {
        withCreate = withCreate.withExistingAvailabilitySet(availabilitySet);
    }
    return withCreate.create();
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) KnownWindowsVirtualMachineImage(com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage) Network(com.microsoft.azure.management.network.Network) VirtualNetwork(com.microsoft.tooling.msservices.model.vm.VirtualNetwork) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup)

Example 17 with Network

use of com.microsoft.azure.management.network.Network in project azure-tools-for-java by Microsoft.

the class SettingsStep method retrieveVirtualNetworks.

private void retrieveVirtualNetworks() {
    AzureTaskManager.getInstance().runInBackground("Loading virtual networks...", new Runnable() {

        @Override
        public void run() {
            if (virtualNetworks == null) {
                virtualNetworks = wizard.getAzure().networks().list();
            }
            DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                @Override
                public void run() {
                    networkComboBox.removeAll();
                    networkComboBox.add(CREATE_NEW);
                    for (Network network : filterVN()) {
                        networkComboBox.add(network.name());
                        networkComboBox.setData(network.name(), network);
                    }
                }
            });
        }
    });
    networkComboBox.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (CREATE_NEW.equals(networkComboBox.getText())) {
                showNewVirtualNetworkForm();
            } else if ((Network) networkComboBox.getData(networkComboBox.getText()) != null) {
                Network network = (Network) networkComboBox.getData(networkComboBox.getText());
                wizard.setVirtualNetwork(network);
                wizard.setNewNetwork(false);
                wizard.setNewNetwork(null);
                subnetComboBox.removeAll();
                for (String subnet : network.subnets().keySet()) {
                    subnetComboBox.add(subnet);
                }
                subnetComboBox.setEnabled(true);
                if (network.subnets().size() > 0) {
                    subnetComboBox.select(0);
                    wizard.setSubnet(subnetComboBox.getText());
                }
            }
        }
    });
    if (virtualNetworks == null) {
        DefaultLoader.getIdeHelper().invokeAndWait(new Runnable() {

            @Override
            public void run() {
                networkComboBox.setItems(new String[] { CREATE_NEW, LOADING });
                subnetComboBox.removeAll();
                subnetComboBox.setEnabled(false);
            }
        });
    }
}
Also used : Network(com.microsoft.azure.management.network.Network) VirtualNetwork(com.microsoft.tooling.msservices.model.vm.VirtualNetwork) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 18 with Network

use of com.microsoft.azure.management.network.Network in project azure-sdk-for-java by Azure.

the class TestVirtualMachineNics method createResource.

@Override
public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exception {
    // Prepare the resource group definition
    final String rgName = "rg" + this.testId;
    Creatable<ResourceGroup> resourceGroupCreatable = virtualMachines.manager().resourceManager().resourceGroups().define(rgName).withRegion(Region.US_EAST);
    // Prepare the virtual network definition [shared by primary and secondary network interfaces]
    final String vnetName = "vnet" + this.testId;
    Creatable<Network> networkCreatable = this.networkManager.networks().define(vnetName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withAddressSpace("10.0.0.0/28");
    // Prepare the secondary network interface definition
    final String secondaryNicName = "nic" + this.testId;
    Creatable<NetworkInterface> secondaryNetworkInterfaceCreatable = this.networkManager.networkInterfaces().define(secondaryNicName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.5");
    // .withNewPrimaryPublicIPAddress();
    // [Secondary NIC cannot have PublicIP - Only primary network interface can reference a public IP address]
    // Prepare the secondary network interface definition
    final String secondaryNicName2 = "nic2" + this.testId;
    Creatable<NetworkInterface> secondaryNetworkInterfaceCreatable2 = this.networkManager.networkInterfaces().define(secondaryNicName2).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.6");
    // Create Virtual Machine
    final String vmName = "vm" + this.testId;
    final String primaryPipName = "pip" + vmName;
    VirtualMachine virtualMachine = virtualMachines.define(vmName).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroupCreatable).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressStatic("10.0.0.4").withNewPrimaryPublicIPAddress(primaryPipName).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_14_04_LTS).withRootUsername("testuser").withRootPassword("12NewPA$$w0rd!").withSize(VirtualMachineSizeTypes.STANDARD_A9).withNewSecondaryNetworkInterface(secondaryNetworkInterfaceCreatable).withNewSecondaryNetworkInterface(secondaryNetworkInterfaceCreatable2).create();
    Assert.assertTrue(virtualMachine.networkInterfaceIds().size() == 3);
    NetworkInterface primaryNetworkInterface = virtualMachine.getPrimaryNetworkInterface();
    Assert.assertEquals(primaryNetworkInterface.primaryPrivateIP(), "10.0.0.4");
    PublicIPAddress primaryPublicIPAddress = primaryNetworkInterface.primaryIPConfiguration().getPublicIPAddress();
    Assert.assertTrue(primaryPublicIPAddress.fqdn().startsWith(primaryPipName));
    return virtualMachine;
}
Also used : Network(com.microsoft.azure.management.network.Network) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 19 with Network

use of com.microsoft.azure.management.network.Network in project azure-sdk-for-java by Azure.

the class VirtualMachineScaleSetManagedDiskOperationsTests method canCreateVirtualMachineScaleSetFromCustomImageWithManagedDisk.

@Test
public void canCreateVirtualMachineScaleSetFromCustomImageWithManagedDisk() throws Exception {
    final String userName = "tirekicker";
    final String password = "12NewPA$$w0rd!";
    final String publicIpDnsLabel = generateRandomResourceName("pip", 10);
    final String customImageName = generateRandomResourceName("img", 10);
    final String vmssName = generateRandomResourceName("vmss", 10);
    ResourceGroup resourceGroup = this.resourceManager.resourceGroups().define(RG_NAME).withRegion(region).create();
    VirtualMachine vm = this.computeManager.virtualMachines().define(generateRandomResourceName("vm", 10)).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIpDnsLabel).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withUnmanagedDisks().defineUnmanagedDataDisk("disk-1").withNewVhd(100).withLun(1).attach().defineUnmanagedDataDisk("disk-2").withNewVhd(50).withLun(2).attach().withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
    Assert.assertNotNull(vm);
    deprovisionAgentInLinuxVM(vm.getPrimaryPublicIPAddress().fqdn(), 22, userName, password);
    vm.deallocate();
    vm.generalize();
    VirtualMachineCustomImage virtualMachineCustomImage = this.computeManager.virtualMachineCustomImages().define(customImageName).withRegion(region).withExistingResourceGroup(resourceGroup).fromVirtualMachine(vm).create();
    Assert.assertNotNull(virtualMachineCustomImage);
    Network network = this.networkManager.networks().define(generateRandomResourceName("vmssvnet", 15)).withRegion(region).withExistingResourceGroup(resourceGroup).withAddressSpace("10.0.0.0/28").withSubnet("subnet1", "10.0.0.0/28").create();
    LoadBalancer publicLoadBalancer = createHttpLoadBalancers(region, resourceGroup, "1");
    VirtualMachineScaleSet vmScaleSet = this.computeManager.virtualMachineScaleSets().define(vmssName).withRegion(region).withExistingResourceGroup(resourceGroup).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D5_V2).withExistingPrimaryNetworkSubnet(network, "subnet1").withExistingPrimaryInternetFacingLoadBalancer(publicLoadBalancer).withoutPrimaryInternalLoadBalancer().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withRootPassword(password).create();
    VirtualMachineScaleSetVMs virtualMachineScaleSetVMs = vmScaleSet.virtualMachines();
    PagedList<VirtualMachineScaleSetVM> virtualMachines = virtualMachineScaleSetVMs.list();
    Assert.assertEquals(virtualMachines.size(), vmScaleSet.capacity());
    for (VirtualMachineScaleSetVM vm1 : virtualMachines) {
        Assert.assertTrue(vm1.isOSBasedOnCustomImage());
        Assert.assertFalse(vm1.isOSBasedOnPlatformImage());
        Assert.assertFalse(vm1.isOSBasedOnStoredImage());
        Assert.assertTrue(vm1.isManagedDiskEnabled());
        Assert.assertNotNull(vm1.unmanagedDataDisks());
        Assert.assertEquals(vm1.unmanagedDataDisks().size(), 0);
        Assert.assertNotNull(vm1.dataDisks());
        // Disks from data disk image from custom image
        Assert.assertEquals(vm1.dataDisks().size(), 2);
        Assert.assertTrue(vm1.dataDisks().containsKey(1));
        VirtualMachineDataDisk disk = vm1.dataDisks().get(1);
        Assert.assertEquals(disk.size(), 100);
        Assert.assertTrue(vm1.dataDisks().containsKey(2));
        disk = vm1.dataDisks().get(2);
        Assert.assertEquals(disk.size(), 50);
    }
    vmScaleSet.deallocate();
// Updating and adding disk as part of VMSS Update seems consistency failing, CRP is aware of
// this, hence until it is fixed comment-out the test
//
//        {
//            "startTime": "2017-01-25T06:10:55.2243509+00:00",
//                "endTime": "2017-01-25T06:11:07.8649525+00:00",
//                "status": "Failed",
//                "error": {
//            "code": "InternalExecutionError",
//                    "message": "An internal execution error occurred."
//        },
//            "name": "6786df83-ed3f-4d7a-bf58-d295b96fef46"
//        }
//
//        vmScaleSet.update()
//                .withDataDiskUpdated(1, 200) // update not supported
//                .withNewDataDisk(100)
//                .apply();
//
//        vmScaleSet.start();
//
//        virtualMachineScaleSetVMs = vmScaleSet.virtualMachines();
//        virtualMachines = virtualMachineScaleSetVMs.list();
//        for (VirtualMachineScaleSetVM vm1 : virtualMachines) {
//            Assert.assertTrue(vm1.isOSBasedOnCustomImage());
//            Assert.assertFalse(vm1.isOSBasedOnPlatformImage());
//            Assert.assertFalse(vm1.isOSBasedOnStoredImage());
//            Assert.assertTrue(vm1.isManagedDiskEnabled());
//            Assert.assertNotNull(vm1.unmanagedDataDisks());
//            Assert.assertEquals(vm1.unmanagedDataDisks().size(), 0);
//            Assert.assertNotNull(vm1.dataDisks());
//            Assert.assertEquals(vm1.dataDisks().size(), 3);
//            Assert.assertTrue(vm1.dataDisks().containsKey(1));
//            VirtualMachineDataDisk disk = vm1.dataDisks().get(1);
//            Assert.assertEquals(disk.size(), 200);
//            Assert.assertTrue(vm1.dataDisks().containsKey(2));
//            disk = vm1.dataDisks().get(2);
//            Assert.assertEquals(disk.size(), 50);
//            Assert.assertTrue(vm1.dataDisks().containsKey(0));
//            disk = vm1.dataDisks().get(0);
//            Assert.assertEquals(disk.size(), 100);
//        }
}
Also used : Network(com.microsoft.azure.management.network.Network) LoadBalancer(com.microsoft.azure.management.network.LoadBalancer) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Test(org.junit.Test)

Example 20 with Network

use of com.microsoft.azure.management.network.Network in project azure-sdk-for-java by Azure.

the class VirtualMachineScaleSetManagedDiskOperationsTests method canCreateUpdateVirtualMachineScaleSetFromPIRWithManagedDisk.

@Test
public void canCreateUpdateVirtualMachineScaleSetFromPIRWithManagedDisk() throws Exception {
    final String vmssName = generateRandomResourceName("vmss", 10);
    ResourceGroup resourceGroup = this.resourceManager.resourceGroups().define(RG_NAME).withRegion(region).create();
    Network network = this.networkManager.networks().define(generateRandomResourceName("vmssvnet", 15)).withRegion(region).withExistingResourceGroup(resourceGroup).withAddressSpace("10.0.0.0/28").withSubnet("subnet1", "10.0.0.0/28").create();
    LoadBalancer publicLoadBalancer = createHttpLoadBalancers(region, resourceGroup, "1");
    VirtualMachineScaleSet vmScaleSet = this.computeManager.virtualMachineScaleSets().define(vmssName).withRegion(region).withExistingResourceGroup(resourceGroup).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D5_V2).withExistingPrimaryNetworkSubnet(network, "subnet1").withExistingPrimaryInternetFacingLoadBalancer(publicLoadBalancer).withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername("jvuser").withRootPassword("123OData!@#123").withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withNewDataDisk(100, 2, CachingTypes.READ_ONLY).create();
    VirtualMachineScaleSetVMs virtualMachineScaleSetVMs = vmScaleSet.virtualMachines();
    PagedList<VirtualMachineScaleSetVM> virtualMachines = virtualMachineScaleSetVMs.list();
    Assert.assertEquals(virtualMachines.size(), vmScaleSet.capacity());
    for (VirtualMachineScaleSetVM vm : virtualMachines) {
        Assert.assertTrue(vm.isOSBasedOnPlatformImage());
        Assert.assertFalse(vm.isOSBasedOnCustomImage());
        Assert.assertFalse(vm.isOSBasedOnStoredImage());
        Assert.assertTrue(vm.isManagedDiskEnabled());
        Assert.assertNotNull(vm.unmanagedDataDisks());
        Assert.assertEquals(vm.unmanagedDataDisks().size(), 0);
        Assert.assertNotNull(vm.dataDisks());
        Assert.assertEquals(vm.dataDisks().size(), 3);
    }
    vmScaleSet.update().withoutDataDisk(0).withNewDataDisk(50).apply();
    virtualMachineScaleSetVMs = vmScaleSet.virtualMachines();
    virtualMachines = virtualMachineScaleSetVMs.list();
    Assert.assertEquals(virtualMachines.size(), vmScaleSet.capacity());
    for (VirtualMachineScaleSetVM vm : virtualMachines) {
        Assert.assertNotNull(vm.dataDisks());
        Assert.assertEquals(vm.dataDisks().size(), 3);
    }
}
Also used : Network(com.microsoft.azure.management.network.Network) LoadBalancer(com.microsoft.azure.management.network.LoadBalancer) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Test(org.junit.Test)

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