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 328a47b commit 5a1a8b4
Show file tree
Hide file tree
Showing 4 changed files with 1,671 additions and 1,431 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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-scale-sets.git

Expand All @@ -38,9 +38,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.ManageVirtualMachineScaleSet</mainClass>
<mainClass>com.azure.resourcemanager.compute.samples.ManageVirtualMachineScaleSet</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.ManageVirtualMachineScaleSet.java</mainClass>
<mainClass>com.azure.resourcemanager.compute.samples.ManageVirtualMachineScaleSet.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,32 +1,31 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
// 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.http.rest.PagedIterable;
import com.azure.core.management.AzureEnvironment;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.compute.models.CachingTypes;
import com.azure.resourcemanager.compute.models.ImageReference;
import com.azure.resourcemanager.compute.models.StorageAccountTypes;
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetVM;
import com.azure.resourcemanager.network.models.LoadBalancerInboundNatRule;
import com.azure.resourcemanager.network.models.Network;
import com.azure.resourcemanager.network.models.PublicIpAddress;
import com.azure.resourcemanager.network.models.LoadBalancer;
import com.azure.resourcemanager.network.models.TransportProtocol;
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSet;
import com.azure.resourcemanager.compute.models.VirtualMachineScaleSetSkuTypes;
import com.azure.resourcemanager.network.models.VirtualMachineScaleSetNetworkInterface;
import com.azure.resourcemanager.network.models.VirtualMachineScaleSetNicIpConfiguration;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.samples.Utils;

package com.microsoft.azure.management.compute.samples;

import com.microsoft.azure.PagedList;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.CachingTypes;
import com.microsoft.azure.management.compute.ImageReference;
import com.microsoft.azure.management.compute.StorageAccountTypes;
import com.microsoft.azure.management.compute.VirtualMachineScaleSetVM;
import com.microsoft.azure.management.network.LoadBalancerInboundNatRule;
import com.microsoft.azure.management.network.Network;
import com.microsoft.azure.management.network.PublicIPAddress;
import com.microsoft.azure.management.network.LoadBalancer;
import com.microsoft.azure.management.network.TransportProtocol;
import com.microsoft.azure.management.compute.VirtualMachineScaleSet;
import com.microsoft.azure.management.compute.VirtualMachineScaleSetSkuTypes;
import com.microsoft.azure.management.network.VirtualMachineScaleSetNetworkInterface;
import com.microsoft.azure.management.network.VirtualMachineScaleSetNicIPConfiguration;
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.Date;
import java.util.List;
Expand All @@ -47,14 +46,14 @@ public final class ManageVirtualMachineScaleSet {

/**
* 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_EAST2;
final String rgName = SdkContext.randomResourceName("rgCOVS", 15);
final String vnetName = SdkContext.randomResourceName("vnet", 24);
final String loadBalancerName1 = SdkContext.randomResourceName("intlb" + "-", 18);
final String rgName = Utils.randomResourceName(azureResourceManager, "rgCOVS", 15);
final String vnetName = Utils.randomResourceName(azureResourceManager, "vnet", 24);
final String loadBalancerName1 = Utils.randomResourceName(azureResourceManager, "intlb" + "-", 18);
final String publicIpName = "pip-" + loadBalancerName1;
final String frontendName = loadBalancerName1 + "-FE1";
final String backendPoolName1 = loadBalancerName1 + "-BAP1";
Expand All @@ -66,12 +65,12 @@ public static boolean runSample(Azure azure) {
final String httpsLoadBalancingRule = "httpsRule";
final String natPool50XXto22 = "natPool50XXto22";
final String natPool60XXto23 = "natPool60XXto23";
final String vmssName = SdkContext.randomResourceName("vmss", 24);
final String vmssName = Utils.randomResourceName(azureResourceManager, "vmss", 24);

final String userName = "tirekicker";
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD [email protected]";

final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-libraries-for-java/master/azure-samples/src/main/resources/install_apache.sh";
final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/resourcemanager/azure-resourcemanager-samples/src/main/resources/install_apache.sh";
final String installCommand = "bash install_apache.sh";
List<String> fileUris = new ArrayList<>();
fileUris.add(apacheInstallScript);
Expand All @@ -81,7 +80,7 @@ public static boolean runSample(Azure azure) {
// Create a virtual network with a frontend subnet
System.out.println("Creating virtual network with a frontend subnet ...");

Network network = azure.networks().define(vnetName)
Network network = azureResourceManager.networks().define(vnetName)
.withRegion(region)
.withNewResourceGroup(rgName)
.withAddressSpace("172.16.0.0/16")
Expand All @@ -98,7 +97,7 @@ public static boolean runSample(Azure azure) {
// Create a public IP address
System.out.println("Creating a public IP address...");

PublicIPAddress publicIPAddress = azure.publicIPAddresses().define(publicIpName)
PublicIpAddress publicIPAddress = azureResourceManager.publicIpAddresses().define(publicIpName)
.withRegion(region)
.withExistingResourceGroup(rgName)
.withLeafDomainLabel(publicIpName)
Expand Down Expand Up @@ -133,7 +132,7 @@ public static boolean runSample(Azure azure) {
+ " balancer to a port for a specific virtual machine in the backend address pool\n"
+ " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23");

LoadBalancer loadBalancer1 = azure.loadBalancers().define(loadBalancerName1)
LoadBalancer loadBalancer1 = azureResourceManager.loadBalancers().define(loadBalancerName1)
.withRegion(region)
.withExistingResourceGroup(rgName)
// Add two rules that uses above backend and probe
Expand Down Expand Up @@ -169,7 +168,7 @@ public static boolean runSample(Azure azure) {

// Explicitly define the frontend
.definePublicFrontend(frontendName)
.withExistingPublicIPAddress(publicIPAddress)
.withExistingPublicIpAddress(publicIPAddress)
.attach()

// Add two probes one per rule
Expand Down Expand Up @@ -204,7 +203,7 @@ public static boolean runSample(Azure azure) {
.withSku("18.04-LTS")
.withVersion("latest");

VirtualMachineScaleSet virtualMachineScaleSet = azure.virtualMachineScaleSets().define(vmssName)
VirtualMachineScaleSet virtualMachineScaleSet = azureResourceManager.virtualMachineScaleSets().define(vmssName)
.withRegion(region)
.withExistingResourceGroup(rgName)
.withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D3_V2)
Expand Down Expand Up @@ -238,13 +237,13 @@ public static boolean runSample(Azure azure) {
System.out.println();

// Print virtual machine scale set details
// Utils.print(virtualMachineScaleSet);
// ResourceManagerUtils.print(virtualMachineScaleSet);

//=============================================================
// List virtual machine scale set network interfaces

System.out.println("Listing scale set network interfaces ...");
PagedList<VirtualMachineScaleSetNetworkInterface> vmssNics = virtualMachineScaleSet.listNetworkInterfaces();
PagedIterable<VirtualMachineScaleSetNetworkInterface> vmssNics = virtualMachineScaleSet.listNetworkInterfaces();
for (VirtualMachineScaleSetNetworkInterface vmssNic : vmssNics) {
System.out.println(vmssNic.id());
}
Expand All @@ -256,10 +255,10 @@ public static boolean runSample(Azure azure) {
for (VirtualMachineScaleSetVM instance : virtualMachineScaleSet.virtualMachines().list()) {
System.out.println("Scale set virtual machine instance #" + instance.instanceId());
System.out.println(instance.id());
PagedList<VirtualMachineScaleSetNetworkInterface> networkInterfaces = instance.listNetworkInterfaces();
PagedIterable<VirtualMachineScaleSetNetworkInterface> networkInterfaces = instance.listNetworkInterfaces();
// Pick the first NIC
VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.get(0);
for (VirtualMachineScaleSetNicIPConfiguration ipConfig :networkInterface.ipConfigurations().values()) {
VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.iterator().next();
for (VirtualMachineScaleSetNicIpConfiguration ipConfig :networkInterface.ipConfigurations().values()) {
if (ipConfig.isPrimary()) {
List<LoadBalancerInboundNatRule> natRules = ipConfig.listAssociatedLoadBalancerInboundNatRules();
for (LoadBalancerInboundNatRule natRule : natRules) {
Expand Down Expand Up @@ -329,23 +328,17 @@ public static boolean runSample(Azure azure) {
System.out.println("re-started virtual machine scale set");

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 @@ -359,18 +352,21 @@ public static void main(String[] args) {
//=============================================================
// Authenticate

System.out.println(System.getenv("AZURE_AUTH_LOCATION"));
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());
Expand Down
Loading

0 comments on commit 5a1a8b4

Please sign in to comment.