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 4d982d7 commit 7ac1c78
Show file tree
Hide file tree
Showing 5 changed files with 1,671 additions and 1,425 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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/resources-java-deploy-using-arm-template-with-progress.git

Expand All @@ -30,9 +30,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.resources.samples.DeployUsingARMTemplateWithProgress</mainClass>
<mainClass>com.azure.resourcemanager.resources.samples.DeployUsingARMTemplateWithProgress</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.resources.samples.DeployUsingARMTemplateWithProgress.java</mainClass>
<mainClass>com.azure.resourcemanager.resources.samples.DeployUsingARMTemplateWithProgress.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,25 +1,27 @@
/**
* 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.resources.samples;

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.resourcemanager.resources.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.resources.models.Deployment;
import com.azure.resourcemanager.resources.models.DeploymentMode;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils;
import com.azure.resourcemanager.samples.Utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.resources.Deployment;
import com.microsoft.azure.management.resources.DeploymentMode;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.rest.LogLevel;

import java.io.File;

import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;

/**
* Azure Resource sample for deploying resources using an ARM template and
Expand All @@ -30,21 +32,22 @@ public final class DeployUsingARMTemplateWithProgress {

/**
* 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) {
final String rgName = SdkContext.randomResourceName("rgRSAP", 24);
final String deploymentName = SdkContext.randomResourceName("dpRSAP", 24);
public static boolean runSample(AzureResourceManager azureResourceManager) throws IOException, IllegalAccessException {
final String rgName = Utils.randomResourceName(azureResourceManager, "rgRSAP", 24);
final String deploymentName = Utils.randomResourceName(azureResourceManager, "dpRSAP", 24);
try {
String templateJson = DeployUsingARMTemplateWithProgress.getTemplate();
String templateJson = DeployUsingARMTemplateWithProgress.getTemplate(azureResourceManager);

//=============================================================
// Create resource group.

System.out.println("Creating a resource group with name: " + rgName);

azure.resourceGroups().define(rgName)
azureResourceManager.resourceGroups().define(rgName)
.withRegion(Region.US_WEST)
.create();

Expand All @@ -57,7 +60,7 @@ public static boolean runSample(Azure azure) {

System.out.println("Starting a deployment for an Azure App Service: " + deploymentName);

azure.deployments().define(deploymentName)
azureResourceManager.deployments().define(deploymentName)
.withExistingResourceGroup(rgName)
.withTemplate(templateJson)
.withParameters("{}")
Expand All @@ -66,26 +69,21 @@ public static boolean runSample(Azure azure) {

System.out.println("Started a deployment for an Azure App Service: " + deploymentName);

Deployment deployment = azure.deployments().getByResourceGroup(rgName, deploymentName);
Deployment deployment = azureResourceManager.deployments().getByResourceGroup(rgName, deploymentName);
System.out.println("Current deployment status : " + deployment.provisioningState());

while (!(deployment.provisioningState().equalsIgnoreCase("Succeeded")
|| deployment.provisioningState().equalsIgnoreCase("Failed")
|| deployment.provisioningState().equalsIgnoreCase("Cancelled"))) {
SdkContext.sleep(10000);
deployment = azure.deployments().getByResourceGroup(rgName, deploymentName);
ResourceManagerUtils.sleep(Duration.ofSeconds(10));
deployment = azureResourceManager.deployments().getByResourceGroup(rgName, deploymentName);
System.out.println("Current deployment status : " + deployment.provisioningState());
}
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);
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");
Expand All @@ -94,49 +92,53 @@ public static boolean runSample(Azure azure) {
}

}
return false;
}

/**
* Main entry point.
*
* @param args the parameters
*/
public static void main(String[] args) {
try {
//=================================================================
// 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();

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

}

private static String getTemplate() throws IllegalAccessException, JsonProcessingException, IOException {
final String hostingPlanName = SdkContext.randomResourceName("hpRSAT", 24);
final String webappName = SdkContext.randomResourceName("wnRSAT", 24);
final InputStream embeddedTemplate;
embeddedTemplate = DeployUsingARMTemplateWithProgress.class.getResourceAsStream("/templateValue.json");
private static String getTemplate(AzureResourceManager azureResourceManager) throws IllegalAccessException, JsonProcessingException, IOException {
final String hostingPlanName = Utils.randomResourceName(azureResourceManager, "hpRSAT", 24);
final String webappName = Utils.randomResourceName(azureResourceManager, "wnRSAT", 24);

final ObjectMapper mapper = new ObjectMapper();
final JsonNode tmp = mapper.readTree(embeddedTemplate);
try (InputStream embeddedTemplate = DeployUsingARMTemplateWithProgress.class.getResourceAsStream("/templateValue.json")) {

DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", hostingPlanName, "hostingPlanName", null, tmp);
DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", webappName, "webSiteName", null, tmp);
DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", "F1", "skuName", null, tmp);
DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("int", "1", "skuCapacity", null, tmp);
final ObjectMapper mapper = new ObjectMapper();
final JsonNode tmp = mapper.readTree(embeddedTemplate);

return tmp.toString();
DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", hostingPlanName, "hostingPlanName", null, tmp);
DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", webappName, "webSiteName", null, tmp);
DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", "F1", "skuName", null, tmp);
DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("int", "1", "skuCapacity", null, tmp);

return tmp.toString();
}
}

private static void validateAndAddFieldValue(String type, String fieldValue, String fieldName, String errorMessage,
Expand Down
Loading

0 comments on commit 7ac1c78

Please sign in to comment.