Search in sources :

Example 1 with PowerState

use of com.microsoft.azure.management.compute.PowerState in project azure-sdk-for-java by Azure.

the class AzureTests method testExpandableEnum.

/**
     * Stress-tests the resilience of ExpandableEnum to multi-threaded access
     * @throws Exception
     */
@Test
public void testExpandableEnum() throws Exception {
    // Define some threads that read from enum
    Runnable reader1 = new Runnable() {

        @Override
        public void run() {
            Assert.assertEquals(CountryIsoCode.AFGHANISTAN, CountryIsoCode.fromString("AF"));
            Assert.assertEquals(CountryIsoCode.ANTARCTICA, CountryIsoCode.fromString("AQ"));
            Assert.assertEquals(CountryIsoCode.ANDORRA, CountryIsoCode.fromString("AD"));
            Assert.assertEquals(CountryIsoCode.ARGENTINA, CountryIsoCode.fromString("AR"));
            Assert.assertEquals(CountryIsoCode.ALBANIA, CountryIsoCode.fromString("AL"));
            Assert.assertEquals(CountryIsoCode.ALGERIA, CountryIsoCode.fromString("DZ"));
            Assert.assertEquals(CountryIsoCode.AMERICAN_SAMOA, CountryIsoCode.fromString("AS"));
            Assert.assertEquals(CountryIsoCode.ANGOLA, CountryIsoCode.fromString("AO"));
            Assert.assertEquals(CountryIsoCode.ANGUILLA, CountryIsoCode.fromString("AI"));
            Assert.assertEquals(CountryIsoCode.ANTIGUA_AND_BARBUDA, CountryIsoCode.fromString("AG"));
            Assert.assertEquals(CountryIsoCode.ARMENIA, CountryIsoCode.fromString("AM"));
            Assert.assertEquals(CountryIsoCode.ARUBA, CountryIsoCode.fromString("AW"));
            Assert.assertEquals(CountryIsoCode.AUSTRALIA, CountryIsoCode.fromString("AU"));
            Assert.assertEquals(CountryIsoCode.AUSTRIA, CountryIsoCode.fromString("AT"));
            Assert.assertEquals(CountryIsoCode.AZERBAIJAN, CountryIsoCode.fromString("AZ"));
            Assert.assertEquals(PowerState.DEALLOCATED, PowerState.fromString("PowerState/deallocated"));
            Assert.assertEquals(PowerState.DEALLOCATING, PowerState.fromString("PowerState/deallocating"));
            Assert.assertEquals(PowerState.RUNNING, PowerState.fromString("PowerState/running"));
        }
    };
    Runnable reader2 = new Runnable() {

        @Override
        public void run() {
            Assert.assertEquals(CountryIsoCode.BAHAMAS, CountryIsoCode.fromString("BS"));
            Assert.assertEquals(CountryIsoCode.BAHRAIN, CountryIsoCode.fromString("BH"));
            Assert.assertEquals(CountryIsoCode.BANGLADESH, CountryIsoCode.fromString("BD"));
            Assert.assertEquals(CountryIsoCode.BARBADOS, CountryIsoCode.fromString("BB"));
            Assert.assertEquals(CountryIsoCode.BELARUS, CountryIsoCode.fromString("BY"));
            Assert.assertEquals(CountryIsoCode.BELGIUM, CountryIsoCode.fromString("BE"));
            Assert.assertEquals(PowerState.STARTING, PowerState.fromString("PowerState/starting"));
            Assert.assertEquals(PowerState.STOPPED, PowerState.fromString("PowerState/stopped"));
            Assert.assertEquals(PowerState.STOPPING, PowerState.fromString("PowerState/stopping"));
            Assert.assertEquals(PowerState.UNKNOWN, PowerState.fromString("PowerState/unknown"));
        }
    };
    // Define some threads that write to enum
    Runnable writer1 = new Runnable() {

        @Override
        public void run() {
            for (int i = 1; i <= 10; i++) {
                CountryIsoCode.fromString("CountryIsoCode" + i);
                PowerState.fromString("PowerState" + i);
            }
        }
    };
    Runnable writer2 = new Runnable() {

        @Override
        public void run() {
            for (int i = 1; i <= 20; i++) {
                CountryIsoCode.fromString("CountryIsoCode" + i);
                PowerState.fromString("PowerState" + i);
            }
        }
    };
    // Start the threads and repeat a few times
    ExecutorService threadPool = Executors.newFixedThreadPool(4);
    for (int repeat = 0; repeat < 10; repeat++) {
        threadPool.submit(reader1);
        threadPool.submit(reader2);
        threadPool.submit(writer1);
        threadPool.submit(writer2);
    }
    // Give the test a fixed amount of time to finish
    threadPool.awaitTermination(10, TimeUnit.SECONDS);
    // Verify country ISO codes
    Collection<CountryIsoCode> countryIsoCodes = CountryIsoCode.values();
    System.out.println("\n## Country ISO codes: " + countryIsoCodes.size());
    for (CountryIsoCode value : countryIsoCodes) {
        System.out.println(value.toString());
    }
    Assert.assertEquals(257, countryIsoCodes.size());
    // Verify power states
    Collection<PowerState> powerStates = PowerState.values();
    System.out.println("\n## Power states: " + powerStates.size());
    for (PowerState value : powerStates) {
        System.out.println(value.toString());
    }
    Assert.assertEquals(27, powerStates.size());
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountryIsoCode(com.microsoft.azure.management.resources.fluentcore.arm.CountryIsoCode) PowerState(com.microsoft.azure.management.compute.PowerState) Test(org.junit.Test)

Example 2 with PowerState

use of com.microsoft.azure.management.compute.PowerState in project stdlib by petergeneric.

the class AzureExistingVMProvider method determineState.

public ResourceInstanceState determineState(final ResourceInstanceEntity instance) {
    final String id = instance.getProviderInstanceId();
    final PowerState powerState = azure.getPowerState(id);
    final boolean isDiscarding = instance.getState() == ResourceInstanceState.DISCARDING || instance.getState() == ResourceInstanceState.TO_DISCARD;
    final boolean isProvisioning = instance.getState() == ResourceInstanceState.PROVISIONING || instance.getState() == ResourceInstanceState.TO_PROVISION;
    if (isDiscarding) {
        switch(powerState) {
            case DEALLOCATED:
                return ResourceInstanceState.DISCARDED;
            case DEALLOCATING:
                return ResourceInstanceState.DISCARDING;
            case RUNNING:
            case STARTING:
                return ResourceInstanceState.TO_DISCARD;
            default:
                throw new IllegalArgumentException("Unknown Azure power state for " + id + ": " + powerState);
        }
    } else if (isProvisioning) {
        switch(powerState) {
            case DEALLOCATED:
            case DEALLOCATING:
                return ResourceInstanceState.TO_PROVISION;
            case RUNNING:
                return ResourceInstanceState.IN_SERVICE;
            case STARTING:
                return ResourceInstanceState.PROVISIONING;
            default:
                throw new IllegalArgumentException("Unknown Azure power state for " + id + ": " + powerState);
        }
    } else {
        switch(powerState) {
            case DEALLOCATED:
                return ResourceInstanceState.DISCARDED;
            case DEALLOCATING:
                return ResourceInstanceState.DISCARDING;
            case RUNNING:
                return ResourceInstanceState.IN_SERVICE;
            case STARTING:
                return ResourceInstanceState.PROVISIONING;
            default:
                throw new IllegalArgumentException("Unknown Azure power state for " + id + ": " + powerState);
        }
    }
}
Also used : PowerState(com.microsoft.azure.management.compute.PowerState)

Example 3 with PowerState

use of com.microsoft.azure.management.compute.PowerState in project cloudbreak by hortonworks.

the class AzureInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> statuses = new ArrayList<>();
    String stackName = armTemplateUtils.getStackName(ac.getCloudContext());
    for (CloudInstance vm : vms) {
        try {
            AzureClient azureClient = ac.getParameter(AzureClient.class);
            boolean virtualMachineExists = azureClient.isVirtualMachineExists(stackName, vm.getInstanceId());
            if (virtualMachineExists) {
                PowerState virtualMachinePowerState = azureClient.getVirtualMachinePowerState(stackName, vm.getInstanceId());
                statuses.add(new CloudVmInstanceStatus(vm, AzureInstanceStatus.get(virtualMachinePowerState)));
            } else {
                statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
            }
        } catch (CloudException e) {
            if (e.body() != null && "ResourceNotFound".equals(e.body().code())) {
                statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
            } else {
                statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.UNKNOWN));
            }
        } catch (RuntimeException ignored) {
            statuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.UNKNOWN));
        }
    }
    return statuses;
}
Also used : AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudException(com.microsoft.azure.CloudException) PowerState(com.microsoft.azure.management.compute.PowerState)

Aggregations

PowerState (com.microsoft.azure.management.compute.PowerState)3 CloudException (com.microsoft.azure.CloudException)1 CountryIsoCode (com.microsoft.azure.management.resources.fluentcore.arm.CountryIsoCode)1 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)1 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)1 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1