Skip to content

Commit

Permalink
Merge pull request #896 from alvadb/master
Browse files Browse the repository at this point in the history
Initial check in of resource samples.
  • Loading branch information
jianghaolu authored Jun 30, 2016
2 parents 56ea085 + 9e0078c commit 688b497
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

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

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

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.Azure;
import com.microsoft.azure.management.resources.DeploymentMode;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;

import okhttp3.logging.HttpLoggingInterceptor;

/**
* Azure Resource sample for deploying resources using an ARM template.
*/
Expand All @@ -15,21 +30,113 @@ public final class DeployUsingARMTemplate {

/**
* Main entry point.
*
* @param args the parameters
*/
public static void main(String[] args) {

try {
final String rgName = ResourceNamer.randomResourceName("rgRSAT", 24);
final String deploymentName = ResourceNamer.randomResourceName("dpRSAT", 24);

try {


//=================================================================
// Authenticate

final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));

Azure azure = Azure.configure()
.withLogLevel(HttpLoggingInterceptor.Level.NONE)
.authenticate(credFile)
.withDefaultSubscription();

try {
String templateJson = DeployUsingARMTemplate.getTemplate();


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

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

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

// Deploy using an ARM template
System.out.println("Created a resource group with name: " + rgName);


//=============================================================
// Create a deployment for an Azure App Service via an ARM
// template.

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

azure.deployments().define(deploymentName)
.withExistingResourceGroup(rgName)
.withTemplate(templateJson)
.withParameters("{}")
.withMode(DeploymentMode.INCREMENTAL)
.create();

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

} catch (Exception f) {

System.out.println(f.getMessage());
f.printStackTrace();

} finally {

try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().delete(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();
}

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

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

final ObjectMapper mapper = new ObjectMapper();
final JsonNode tmp = mapper.readTree(embeddedTemplate);

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

return tmp.toString();
}

private static void validateAndAddFieldValue(String type, String fieldValue, String fieldName, String errorMessage,
JsonNode tmp) throws IllegalAccessException {
// Add count variable for loop....
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode parameter = mapper.createObjectNode();
parameter.put("type", type);
if (type == "int") {
parameter.put("defaultValue", Integer.parseInt(fieldValue));
} else {
parameter.put("defaultValue", fieldValue);
}
ObjectNode.class.cast(tmp.get("parameters")).replace(fieldName, parameter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,144 @@

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

import java.io.File;

import com.microsoft.azure.Azure;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
import com.microsoft.azure.management.storage.SkuName;
import com.microsoft.azure.management.storage.StorageAccount;

import okhttp3.logging.HttpLoggingInterceptor;

/**
* Azure Resource sample for managing resources -
* - Create a resource
* - Update a resource
* - Create another resource
* - List resources
* - Delete a resource.
* - Create a resource
* - Update a resource
* - Create another resource
* - List resources
* - Delete a resource.
*/

public final class ManageResource {

/**
* Main entry point.
*
* @param args the parameters
*/
public static void main(String[] args) {

final String rgName = ResourceNamer.randomResourceName("rgRSMR", 24);
final String resourceName1 = ResourceNamer.randomResourceName("rn1", 24);
final String resourceName2 = ResourceNamer.randomResourceName("rn2", 24);

try {

// Create a resource

// Update a resource
//=================================================================
// Authenticate

// Create another resource
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));

// List resources
Azure azure = Azure.configure()
.withLogLevel(HttpLoggingInterceptor.Level.NONE)
.authenticate(credFile)
.withDefaultSubscription();

// Delete a resource
try {

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

private ManageResource() {
//=============================================================
// Create resource group.

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

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


//=============================================================
// Create storage account.

System.out.println("Creating a storage account with name: " + resourceName1);

StorageAccount storageAccount = azure.storageAccounts()
.define(resourceName1)
.withRegion(Region.US_WEST)
.withExistingResourceGroup(rgName)
.create();

System.out.println("Storage account created: " + storageAccount.id());


//=============================================================
// Update - set the sku name

System.out.println("Updating the storage account with name: " + resourceName1);

storageAccount.update()
.withSku(SkuName.STANDARD_RAGRS)
.apply();

System.out.println("Updated the storage account with name: " + resourceName1);

}

//=============================================================
// Create another storage account.

System.out.println("Creating another storage account with name: " + resourceName2);

StorageAccount storageAccount2 = azure.storageAccounts().define(resourceName2)
.withRegion(Region.US_WEST)
.withExistingResourceGroup(rgName)
.create();

System.out.println("Storage account created: " + storageAccount2.id());


//=============================================================
// List storage accounts.

System.out.println("Listing all storage accounts for resource group: " + rgName);

for (StorageAccount sAccount : azure.storageAccounts().list()) {
System.out.println("Storage account: " + sAccount.name());
}


//=============================================================
// Delete a storage accounts.

System.out.println("Deleting storage account: " + resourceName2);

azure.storageAccounts().delete(storageAccount2.id());

System.out.println("Deleted storage account: " + resourceName2);

} catch (Exception f) {

System.out.println(f.getMessage());
f.printStackTrace();

} finally {

try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().delete(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();
}

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

0 comments on commit 688b497

Please sign in to comment.