Search in sources :

Example 1 with GenericResource

use of com.microsoft.azure.management.resources.GenericResource in project azure-sdk-for-java by Azure.

the class AzureTests method testGenericResources.

/**
     * Tests basic generic resources retrieval.
     * @throws Exception
     */
@Test
public void testGenericResources() throws Exception {
    // Create some resources
    NetworkSecurityGroup nsg = azure.networkSecurityGroups().define(SdkContext.randomResourceName("nsg", 13)).withRegion(Region.US_EAST).withNewResourceGroup().create();
    azure.publicIPAddresses().define(SdkContext.randomResourceName("pip", 13)).withRegion(Region.US_EAST).withExistingResourceGroup(nsg.resourceGroupName()).create();
    PagedList<GenericResource> resources = azure.genericResources().listByResourceGroup(nsg.resourceGroupName());
    Assert.assertEquals(2, resources.size());
    GenericResource firstResource = resources.get(0);
    GenericResource resourceById = azure.genericResources().getById(firstResource.id());
    GenericResource resourceByDetails = azure.genericResources().get(firstResource.resourceGroupName(), firstResource.resourceProviderNamespace(), firstResource.resourceType(), firstResource.name());
    Assert.assertTrue(resourceById.id().equalsIgnoreCase(resourceByDetails.id()));
    azure.resourceGroups().beginDeleteByName(nsg.resourceGroupName());
}
Also used : NetworkSecurityGroup(com.microsoft.azure.management.network.NetworkSecurityGroup) GenericResource(com.microsoft.azure.management.resources.GenericResource) Test(org.junit.Test)

Example 2 with GenericResource

use of com.microsoft.azure.management.resources.GenericResource in project azure-sdk-for-java by Azure.

the class DeployUsingARMTemplateWithTags method runSample.

/**
     * 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 rgName = SdkContext.randomResourceName("rgRSAT", 24);
    final String deploymentName = SdkContext.randomResourceName("dpRSAT", 24);
    try {
        String templateJson = getTemplate();
        //=============================================================
        // Create resource group.
        System.out.println("Creating a resource group with name: " + rgName);
        azure.resourceGroups().define(rgName).withRegion(Region.US_WEST).create();
        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);
        Deployment deployment = azure.deployments().define(deploymentName).withExistingResourceGroup(rgName).withTemplate(templateJson).withParameters("{}").withMode(DeploymentMode.INCREMENTAL).create();
        System.out.println("Finished a deployment for an Azure App Service: " + deploymentName);
        List<DeploymentOperation> operations = deployment.deploymentOperations().list();
        List<GenericResource> genericResources = new ArrayList<>();
        // Getting created resources
        for (DeploymentOperation operation : operations) {
            if (operation.targetResource() != null) {
                genericResources.add(azure.genericResources().getById(operation.targetResource().id()));
            }
        }
        System.out.println("Resource created during deployment: " + deploymentName);
        for (GenericResource genericResource : genericResources) {
            System.out.println(genericResource.resourceProviderNamespace() + "/" + genericResource.resourceType() + ": " + genericResource.name());
            // Tag resource
            genericResource.update().withTag("label", "deploy1").apply();
        }
        genericResources = azure.genericResources().listByTag(rgName, "label", "deploy1");
        System.out.println("Tagged resources for deployment: " + deploymentName);
        for (GenericResource genericResource : genericResources) {
            System.out.println(genericResource.resourceProviderNamespace() + "/" + genericResource.resourceType() + ": " + genericResource.name());
        }
        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);
            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;
}
Also used : GenericResource(com.microsoft.azure.management.resources.GenericResource) ArrayList(java.util.ArrayList) Deployment(com.microsoft.azure.management.resources.Deployment) DeploymentOperation(com.microsoft.azure.management.resources.DeploymentOperation) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Aggregations

GenericResource (com.microsoft.azure.management.resources.GenericResource)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 NetworkSecurityGroup (com.microsoft.azure.management.network.NetworkSecurityGroup)1 Deployment (com.microsoft.azure.management.resources.Deployment)1 DeploymentOperation (com.microsoft.azure.management.resources.DeploymentOperation)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1