Skip to content

Commit

Permalink
Release azure-resourcemanager 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-Fluent committed Oct 26, 2020
1 parent 27e9b7f commit 52b39de
Show file tree
Hide file tree
Showing 4 changed files with 1,662 additions and 1,429 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extensions:

To run this sample:

Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-libraries-for-java/blob/master/AUTH.md).
See [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/identity/azure-identity#defaultazurecredential) and prepare the authentication works best for you. For more details on authentication, please refer to [AUTH.md](https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/resourcemanager/docs/AUTH.md).

git clone https://github.com/Azure-Samples/compute-java-manage-virtual-machine-using-vm-extensions.git

Expand All @@ -35,9 +35,11 @@ Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an aut

## More information ##

[http://azure.com/java](http://azure.com/java)
For general documentation as well as quickstarts on how to use Azure Management Libraries for Java, please see [here](https://aka.ms/azsdk/java/mgmt).

If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
Start to develop applications with Java on Azure [here](http://azure.com/java).

If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212).

---

Expand Down
34 changes: 15 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.microsoft.azure.management.compute.samples.ManageVirtualMachineExtension</mainClass>
<mainClass>com.azure.resourcemanager.compute.samples.ManageVirtualMachineExtension</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -40,7 +41,7 @@
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.microsoft.azure.management.compute.samples.ManageVirtualMachineExtension.java</mainClass>
<mainClass>com.azure.resourcemanager.compute.samples.ManageVirtualMachineExtension.java</mainClass>
</manifest>
</archive>
</configuration>
Expand All @@ -51,24 +52,19 @@
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.36.3</version>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.microsoft.azure.management.compute.samples;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.resourcemanager.compute.samples;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.management.AzureEnvironment;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.compute.models.KnownWindowsVirtualMachineImage;
import com.azure.resourcemanager.compute.models.VirtualMachine;
import com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.samples.Utils;

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage;
import com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage;
import com.microsoft.azure.management.compute.VirtualMachine;
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.azure.management.samples.Utils;
import com.microsoft.rest.LogLevel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -32,33 +30,29 @@
public final class ManageVirtualMachineExtension {
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @param azureResourceManager instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
public static boolean runSample(AzureResourceManager azureResourceManager) {
final Region region = Region.US_WEST_CENTRAL;
final String linuxVMName = SdkContext.randomResourceName("lVM", 10);
final String windowsVMName = SdkContext.randomResourceName("wVM", 10);
final String rgName = SdkContext.randomResourceName("rgCOVE", 15);
final String pipDnsLabelLinuxVM = SdkContext.randomResourceName("rgPip1", 25);
final String pipDnsLabelWindowsVM = SdkContext.randomResourceName("rgPip2", 25);
final String linuxVMName = Utils.randomResourceName(azureResourceManager, "lVM", 10);
final String windowsVMName = Utils.randomResourceName(azureResourceManager, "wVM", 10);
final String rgName = Utils.randomResourceName(azureResourceManager, "rgCOVE", 15);
final String pipDnsLabelLinuxVM = Utils.randomResourceName(azureResourceManager, "rgPip1", 25);
final String pipDnsLabelWindowsVM = Utils.randomResourceName(azureResourceManager, "rgPip2", 25);

// Linux configurations
//
final String firstLinuxUserName = "tirekicker";
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
final String firstLinuxUserPassword = "12NewPA$$w0rd!";
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
final String firstLinuxUserNewPassword = "muy!234OR";
final String firstLinuxUserPassword = Utils.password();
final String firstLinuxUserNewPassword = Utils.password();

final String secondLinuxUserName = "seconduser";
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
final String secondLinuxUserPassword = "B12a6@12xyz!";
final String secondLinuxUserPassword = Utils.password();
final String secondLinuxUserExpiration = "2020-12-31";

final String thirdLinuxUserName = "thirduser";
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
final String thirdLinuxUserPassword = "12xyz!B12a6@";
final String thirdLinuxUserPassword = Utils.password();
final String thirdLinuxUserExpiration = "2020-12-31";

final String linuxCustomScriptExtensionName = "CustomScriptForLinux";
Expand Down Expand Up @@ -89,18 +83,14 @@ public static boolean runSample(Azure azure) {
// Windows configurations
//
final String firstWindowsUserName = "tirekicker";
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
final String firstWindowsUserPassword = "12NewPA$$w0rd!";
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
final String firstWindowsUserNewPassword = "muy!234OR";
final String firstWindowsUserPassword = Utils.password();
final String firstWindowsUserNewPassword = Utils.password();

final String secondWindowsUserName = "seconduser";
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
final String secondWindowsUserPassword = "B12a6@12xyz!";
final String secondWindowsUserPassword = Utils.password();

final String thirdWindowsUserName = "thirduser";
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
final String thirdWindowsUserPassword = "12xyz!B12a6@";
final String thirdWindowsUserPassword = Utils.password();

final String windowsVMAccessExtensionName = "VMAccessAgent";
final String windowsVMAccessExtensionPublisherName = "Microsoft.Compute";
Expand All @@ -114,13 +104,14 @@ public static boolean runSample(Azure azure) {

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

VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName)
VirtualMachine linuxVM = azureResourceManager.virtualMachines().define(linuxVMName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withNewPrimaryNetwork("10.0.0.0/28")
.withPrimaryPrivateIPAddressDynamic()
.withNewPrimaryPublicIPAddress(pipDnsLabelLinuxVM)
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_14_04_LTS)
// mysql-server-5.6 not available for Ubuntu 16 and 18
.withLatestLinuxImage("Canonical", "UbuntuServer", "14.04.4-LTS")
.withRootUsername(firstLinuxUserName)
.withRootPassword(firstLinuxUserPassword)
.withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
Expand Down Expand Up @@ -212,7 +203,7 @@ public static boolean runSample(Azure azure) {

System.out.println("Creating a Windows VM");

VirtualMachine windowsVM = azure.virtualMachines().define(windowsVMName)
VirtualMachine windowsVM = azureResourceManager.virtualMachines().define(windowsVMName)
.withRegion(region)
.withExistingResourceGroup(rgName)
.withNewPrimaryNetwork("10.0.0.0/28")
Expand Down Expand Up @@ -283,23 +274,17 @@ public static boolean runSample(Azure azure) {
System.out.println("Removed the VM Access extensions from Windows VM");
Utils.print(windowsVM);
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);
azureResourceManager.resourceGroups().beginDeleteByName(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;
}

/**
Expand All @@ -312,17 +297,21 @@ public static void main(String[] args) {
//=============================================================
// Authenticate

final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
final TokenCredential credential = new DefaultAzureCredentialBuilder()
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.build();

Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
AzureResourceManager azureResourceManager = AzureResourceManager
.configure()
.withLogLevel(HttpLogDetailLevel.BASIC)
.authenticate(credential, profile)
.withDefaultSubscription();

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

runSample(azure);
runSample(azureResourceManager);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
Expand Down
Loading

0 comments on commit 52b39de

Please sign in to comment.