ArmTests.java Source code

Java tutorial

Introduction

Here is the source code for ArmTests.java

Source

/*
 *
 *  * Copyright 2000-2015 JetBrains s.r.o.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  * http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.management.compute.ComputeManagementClient;
import com.microsoft.azure.management.compute.ComputeManagementClientImpl;
import com.microsoft.azure.management.compute.VirtualMachinesOperations;
import com.microsoft.azure.management.compute.models.PageImpl;
import com.microsoft.azure.management.compute.models.VirtualMachine;
import com.microsoft.azure.management.resources.DeploymentsOperations;
import com.microsoft.azure.management.resources.ResourceManagementClient;
import com.microsoft.azure.management.resources.ResourceManagementClientImpl;
import com.microsoft.azure.management.resources.models.Deployment;
import com.microsoft.azure.management.resources.models.DeploymentExtended;
import com.microsoft.azure.management.resources.models.DeploymentMode;
import com.microsoft.azure.management.resources.models.DeploymentProperties;
import com.microsoft.rest.CloudException;
import com.microsoft.rest.ServiceResponse;
import com.microsoft.rest.credentials.ApplicationTokenCredentials;
import com.microsoft.rest.credentials.ServiceClientCredentials;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

/**
 * @author Dmitry.Tretyakov
 *         Date: 12/15/2015
 *         Time: 7:27 PM
 */
public class ArmTests {

    // Cloud Credentials
    private final static String CLIENT_ID = "%value%";
    private final static String TENANT_ID = "%value%";
    private final static String SECRET = "%value%";
    private final static String SUBSCRIPTION_ID = "%value%";

    // Image parameters
    private final static String GROUP_ID = "%value%";
    private final static String DEPLOYMENT_ID = "%value%";
    private final static String STORAGE_ID = "%value%";
    private final static String IMAGE_PATH = "%value%";
    private final static String VM_PREFIX = "%value%";
    private final static String VM_NAME = VM_PREFIX + "1";
    private final static String VNET_ID = VM_PREFIX + "-vnet";
    private final static String VM_SIZE = "Standard_D2";
    private final static String USERNAME = "%value%";
    private final static String PASSWORD = "%value%";
    private final static String OS_TYPE = "linux";

    @Test
    public void createVm() throws IOException, CloudException, InterruptedException {
        final ResourceManagementClient client = getResourceManagementClient();

        // Create deployment if not exists
        final DeploymentsOperations deployments = client.getDeployments();
        final Deployment deploymentParameters = new Deployment();

        // Set deployment parameters
        final String template = FileUtils.readText(FileUtils.getResourcePath("arm-template.json"));
        final String templateParams = FileUtils.readText(FileUtils.getResourcePath("arm-template.params.json"));
        final String parameters = String.format(templateParams, STORAGE_ID, IMAGE_PATH, VM_NAME, VNET_ID, USERNAME,
                PASSWORD, OS_TYPE, VM_SIZE);
        ObjectMapper mapper = new ObjectMapper();

        final DeploymentProperties deploymentProperties = new DeploymentProperties();
        deploymentProperties.setMode(DeploymentMode.INCREMENTAL);
        deploymentProperties.setTemplate(mapper.readTree(template));
        deploymentProperties.setParameters(mapper.readTree(parameters));
        deploymentParameters.setProperties(deploymentProperties);

        // Update deployment
        final ServiceResponse<DeploymentExtended> deploymentResponse = deployments.createOrUpdate(GROUP_ID,
                DEPLOYMENT_ID, deploymentParameters);

        Assert.assertTrue(deploymentResponse.getResponse().isSuccess());
    }

    @Test
    public void deleteVm() throws IOException, CloudException, InterruptedException {
        final ComputeManagementClient client = getComputeManagementClient();
        final VirtualMachinesOperations operations = client.getVirtualMachines();

        Assert.assertTrue(operations.delete(GROUP_ID, VM_NAME).getResponse().isSuccess());
    }

    @Test
    public void startVm() throws IOException, CloudException, InterruptedException {
        final ComputeManagementClient client = getComputeManagementClient();
        final VirtualMachinesOperations operations = client.getVirtualMachines();

        Assert.assertTrue(operations.start(GROUP_ID, VM_NAME).getResponse().isSuccess());
    }

    @Test
    public void stopVm() throws IOException, CloudException, InterruptedException {
        final ComputeManagementClient client = getComputeManagementClient();
        final VirtualMachinesOperations operations = client.getVirtualMachines();

        Assert.assertTrue(operations.deallocate(GROUP_ID, VM_NAME).getResponse().isSuccess());
    }

    @Test
    public void listVms() throws IOException, CloudException, InterruptedException {
        final ComputeManagementClient client = getComputeManagementClient();
        final VirtualMachinesOperations operations = client.getVirtualMachines();
        final ServiceResponse<PageImpl<VirtualMachine>> listResponse = operations.list(GROUP_ID);
        final List<VirtualMachine> virtualMachines = listResponse.getBody().getItems();

        Assert.assertNotNull(virtualMachines);
    }

    private static ResourceManagementClient getResourceManagementClient() {
        ServiceClientCredentials credentials = new ApplicationTokenCredentials(CLIENT_ID, TENANT_ID, SECRET, null);
        ResourceManagementClient client = new ResourceManagementClientImpl(credentials);

        client.setSubscriptionId(SUBSCRIPTION_ID);
        client.setLogLevel(HttpLoggingInterceptor.Level.BODY);
        return client;
    }

    private static ComputeManagementClient getComputeManagementClient() {
        ServiceClientCredentials credentials = new ApplicationTokenCredentials(CLIENT_ID, TENANT_ID, SECRET, null);
        final ComputeManagementClientImpl client = new ComputeManagementClientImpl(credentials);

        client.setSubscriptionId(SUBSCRIPTION_ID);
        client.setLogLevel(HttpLoggingInterceptor.Level.BODY);
        return client;
    }
}