Java Code Examples for com.microsoft.azure.management.resources.fluentcore.arm.Region#US_WEST_CENTRAL

The following examples show how to use com.microsoft.azure.management.resources.fluentcore.arm.Region#US_WEST_CENTRAL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConvertVirtualMachineToManagedDisks.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
/**
 * 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 String linuxVMName = Utils.createRandomName("VM1");
    final String rgName = Utils.createRandomName("rgCOMV");
    final String userName = "tirekicker";
    // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
    final String password = "12NewPA$$w0rd!";
    final Region region = Region.US_WEST_CENTRAL;

    try {
        //=============================================================
        // Create a Linux VM using a PIR image with un-managed OS and data disks

        System.out.println("Creating an un-managed Linux VM");

        VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName)
                .withRegion(region)
                .withNewResourceGroup(rgName)
                .withNewPrimaryNetwork("10.0.0.0/28")
                .withPrimaryPrivateIPAddressDynamic()
                .withoutPrimaryPublicIPAddress()
                .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();

        System.out.println("Created a Linux VM with un-managed OS and data disks: " + linuxVM.id());
        Utils.print(linuxVM);

        //=============================================================
        // Deallocate the virtual machine
        System.out.println("Deallocate VM: " + linuxVM.id());

        linuxVM.deallocate();

        System.out.println("De-allocated VM: " + linuxVM.id() + "; state = " + linuxVM.powerState());

        //=============================================================
        // Migrate the virtual machine
        System.out.println("Migrate VM: " + linuxVM.id());

        linuxVM.convertToManaged();

        System.out.println("Migrated VM: " + linuxVM.id());

        Utils.print(linuxVM);

        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;
}
 
Example 2
Source File: ListVirtualMachineImages.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
/**
 * 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;

    //=================================================================
    // List all virtual machine image publishers and
    // list all virtual machine images
    // published by Canonical, Red Hat and SUSE
    // by browsing through locations, publishers, offers, SKUs and images

    List<VirtualMachinePublisher> publishers = azure
            .virtualMachineImages()
            .publishers()
            .listByRegion(region);

    VirtualMachinePublisher chosenPublisher;

    System.out.println("US East data center: printing list of \n"
            + "a) Publishers and\n"
            + "b) Images published by Canonical, Red Hat and Suse");
    System.out.println("=======================================================");
    System.out.println("\n");

    for (VirtualMachinePublisher publisher : publishers) {

        System.out.println("Publisher - " + publisher.name());

        if (publisher.name().equalsIgnoreCase("Canonical")
                || publisher.name().equalsIgnoreCase("Suse")
                || publisher.name().equalsIgnoreCase("RedHat")) {

            chosenPublisher = publisher;
            System.out.print("\n\n");
            System.out.println("=======================================================");
            System.out.println("Located " + chosenPublisher.name());
            System.out.println("=======================================================");
            System.out.println("Printing entries as publisher/offer/sku/image.version()");

            for (VirtualMachineOffer offer : chosenPublisher.offers().list()) {
                for (VirtualMachineSku sku: offer.skus().list()) {
                    for (VirtualMachineImage image : sku.images().list()) {
                        System.out.println("Image - " + chosenPublisher.name() + "/"
                                + offer.name() + "/"
                                + sku.name() + "/" + image.version());
                    }
                }

            }

            System.out.print("\n\n");

        }
    }
    return true;
}
 
Example 3
Source File: ListVirtualMachineExtensionImages.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
/**
 * 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;

    //=================================================================
    // List all virtual machine extension image publishers and
    // list all virtual machine extension images
    // published by Microsoft.OSTCExtensions and Microsoft.Azure.Extensions
    // by browsing through extension image publishers, types, and versions

    List<VirtualMachinePublisher> publishers = azure
            .virtualMachineImages()
            .publishers()
            .listByRegion(region);

    VirtualMachinePublisher chosenPublisher;

    System.out.println("US East data center: printing list of \n"
            + "a) Publishers and\n"
            + "b) virtual machine extension images published by Microsoft.OSTCExtensions and Microsoft.Azure.Extensions");
    System.out.println("=======================================================");
    System.out.println("\n");

    for (VirtualMachinePublisher publisher : publishers) {

        System.out.println("Publisher - " + publisher.name());

        if (publisher.name().equalsIgnoreCase("Microsoft.OSTCExtensions")
                || publisher.name().equalsIgnoreCase("Microsoft.Azure.Extensions")) {

            chosenPublisher = publisher;
            System.out.print("\n\n");
            System.out.println("=======================================================");
            System.out.println("Located " + chosenPublisher.name());
            System.out.println("=======================================================");
            System.out.println("Printing entries as publisher/type/version");

            for (VirtualMachineExtensionImageType imageType : chosenPublisher.extensionTypes().list()) {
                for (VirtualMachineExtensionImageVersion version: imageType.versions().list()) {
                    VirtualMachineExtensionImage image = version.getImage();
                    System.out.println("Image - " + chosenPublisher.name() + "/"
                            + image.typeName() + "/"
                            + image.versionName());
                }
            }

            System.out.print("\n\n");

        }
    }
    return true;
}
 
Example 4
Source File: ManageStorageFromMSIEnabledVirtualMachine.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
/**
 * 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 String linuxVMName = Utils.createRandomName("VM1");
    final String rgName = Utils.createRandomName("rgCOMV");
    final String pipName = Utils.createRandomName("pip1");
    final String userName = "tirekicker";
    // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
    final String password = "12NewPA$$w0rd!";
    final Region region = Region.US_WEST_CENTRAL;

    final String installScript = "https://raw.githubusercontent.com/Azure/azure-libraries-for-java/master/azure-samples/src/main/resources/create_resources_with_msi.sh";
    String installCommand = "bash create_resources_with_msi.sh {stgName} {rgName} {location}";
    List<String> fileUris = new ArrayList<>();
    fileUris.add(installScript);

    try {
        //=============================================================
        // Create a Linux VM with MSI enabled for contributor access to the current resource group

        System.out.println("Creating a Linux VM with MSI enabled");

        VirtualMachine virtualMachine = azure.virtualMachines()
                .define(linuxVMName)
                    .withRegion(region)
                    .withNewResourceGroup(rgName)
                    .withNewPrimaryNetwork("10.0.0.0/28")
                    .withPrimaryPrivateIPAddressDynamic()
                    .withNewPrimaryPublicIPAddress(pipName)
                    .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
                    .withRootUsername(userName)
                    .withRootPassword(password)
                    .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
                    .withOSDiskCaching(CachingTypes.READ_WRITE)
                    .withSystemAssignedManagedServiceIdentity()
                    .withSystemAssignedIdentityBasedAccessToCurrentResourceGroup(BuiltInRole.CONTRIBUTOR)
                    .create();

        System.out.println("Created virtual machine with MSI enabled");
        Utils.print(virtualMachine);

        // Prepare custom script t install az cli that uses MSI to create a storage account
        //
        final String stgName = Utils.createRandomName("st44");
        installCommand = installCommand.replace("{stgName}", stgName)
                .replace("{rgName}", rgName)
                .replace("{location}", region.name());

        // Update the VM by installing custom script extension.
        //
        System.out.println("Installing custom script extension to configure az cli in the virtual machine");
        System.out.println("az cli will use MSI credentials to create storage account");

        virtualMachine
                .update()
                    .defineNewExtension("CustomScriptForLinux")
                        .withPublisher("Microsoft.OSTCExtensions")
                        .withType("CustomScriptForLinux")
                        .withVersion("1.4")
                        .withMinorVersionAutoUpgrade()
                        .withPublicSetting("fileUris", fileUris)
                        .withPublicSetting("commandToExecute", installCommand)
                        .attach()
                    .apply();

        // Retrieve the storage account created by az cli using MSI credentials
        //
        StorageAccount storageAccount = azure.storageAccounts()
                .getByResourceGroup(rgName, stgName);

        System.out.println("Storage account created by az cli using MSI credential");
        Utils.print(storageAccount);
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(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;
}
 
Example 5
Source File: ManageVirtualMachineFromMSIEnabledVirtualMachine.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
/**
 * Main entry point.
 *
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final Region region = Region.US_WEST_CENTRAL;

        // This sample required to be run from a MSI (User Assigned or System Assigned) enabled virtual machine with role
        // based contributor access to the resource group specified as the second command line argument.
        //
        // see https://github.com/Azure-Samples/compute-java-manage-user-assigned-msi-enabled-virtual-machine.git
        //

        final String usage = "Usage: mvn clean compile exec:java -Dexec.args=\"<subscription-id> <rg-name> [<client-id>]\"";
        if (args.length < 2) {
            throw new IllegalArgumentException(usage);
        }

        final String subscriptionId = args[0];
        final String resourceGroupName = args[1];
        final String clientId = args.length > 2 ? args[2] : null;
        final String linuxVMName = Utils.createRandomName("VM1");
        final String userName = "tirekicker";
        // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
        final String password = "12NewPA$$w0rd!";

        //=============================================================
        // MSI Authenticate

        MSICredentials credentials = new MSICredentials(AzureEnvironment.AZURE);
        if (clientId != null) {
            // If User Assigned MSI client id is specified then switch to "User Assigned MSI" auth mode
            //
            credentials = credentials.withClientId(clientId);
        }

        Azure azure = Azure.configure()
                .withLogLevel(LogLevel.BODY_AND_HEADERS)
                .authenticate(credentials)
                .withSubscription(subscriptionId);

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

        //=============================================================
        // Create a Linux VM using MSI credentials

        System.out.println("Creating a Linux VM using MSI credentials");

        VirtualMachine virtualMachine = azure.virtualMachines()
                .define(linuxVMName)
                .withRegion(region)
                .withExistingResourceGroup(resourceGroupName)
                .withNewPrimaryNetwork("10.0.0.0/28")
                .withPrimaryPrivateIPAddressDynamic()
                .withoutPrimaryPublicIPAddress()
                .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
                .withRootUsername(userName)
                .withRootPassword(password)
                .withSize(VirtualMachineSizeTypes.STANDARD_DS2_V2)
                .create();

        System.out.println("Created virtual machine using MSI credentials");
        Utils.print(virtualMachine);

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