Search in sources :

Example 61 with VirtualMachine

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

the class AzureVMControlImpl method startIfStopped.

@Override
public boolean startIfStopped(final String id, final Timeout timeout) throws InterruptedException {
    VirtualMachine vm = getById(id);
    switch(vm.powerState()) {
        case RUNNING:
        case DEALLOCATING:
        case STARTING:
            return false;
        case DEALLOCATED:
            log.info(vm.resourceGroupName() + " - " + vm.name() + " is deallocated, starting");
            start(id, timeout);
            return true;
        default:
            throw new IllegalArgumentException("Unknown power state");
    }
}
Also used : VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 62 with VirtualMachine

use of com.microsoft.azure.management.compute.VirtualMachine in project photon-model by vmware.

the class AzureInstanceService method attachPersistentDisksToVM.

private DeferredResult<AzureInstanceContext> attachPersistentDisksToVM(AzureInstanceContext ctx) {
    // Attach all the persistent disks to the VM
    DeferredResult<AzureInstanceContext> dr = new DeferredResult<>();
    VirtualMachine.Update updateVM = ctx.virtualMachine.update();
    for (Disk disk : ctx.persistentDisks) {
        updateVM = updateVM.withExistingDataDisk(disk);
    }
    rx.Observable observable = updateVM.applyAsync();
    observable.doOnNext(resource -> {
        if (resource instanceof VirtualMachine) {
            // update both the inner VM and the VirtualMachine objects in context
            ctx.virtualMachine = (VirtualMachine) resource;
            ctx.provisionedVm = ((VirtualMachine) resource).inner();
        }
    }).doOnCompleted(AzureUtils.injectOperationContext(() -> {
        // Done with attaching disks. Completing DR.
        getHost().log(Level.INFO, "Attached persistent " + ctx.persistentDisks.size() + " disks" + " to vm " + ctx.provisionedVm.name());
        dr.complete(ctx);
    })).doOnError(AzureUtils.injectOperationContext(dr::fail)).publish().connect();
    return dr;
}
Also used : OSDisk(com.microsoft.azure.management.compute.OSDisk) Disk(com.microsoft.azure.management.compute.Disk) DataDisk(com.microsoft.azure.management.compute.DataDisk) VirtualHardDisk(com.microsoft.azure.management.compute.VirtualHardDisk) DeferredResult(com.vmware.xenon.common.DeferredResult) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 63 with VirtualMachine

use of com.microsoft.azure.management.compute.VirtualMachine in project photon-model by vmware.

the class AzureComputeDiskDay2ServiceTest method assertAttachDiskToVM.

private void assertAttachDiskToVM() {
    ComputeService.ComputeState provisionedVM = this.host.getServiceState(null, ComputeService.ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
    assertEquals(this.computeVM.diskLinks.size() + 1, provisionedVM.diskLinks.size());
    DiskService.DiskState attachedDisk = this.host.getServiceState(null, DiskService.DiskState.class, UriUtils.buildUri(this.host, this.diskState.documentSelfLink));
    assertEquals("Disk status is not matching", DiskService.DiskStatus.ATTACHED, attachedDisk.status);
    if (!this.isMock) {
        assertNotNull(attachedDisk.customProperties.get(DISK_CONTROLLER_NUMBER));
        VirtualMachine vm = this.getAzureSdkClients().getComputeManager().virtualMachines().getById(provisionedVM.id);
        this.host.log("Number of disks attached to VM is - " + vm.dataDisks().size());
    }
}
Also used : ComputeService(com.vmware.photon.controller.model.resources.ComputeService) DiskService(com.vmware.photon.controller.model.resources.DiskService) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 64 with VirtualMachine

use of com.microsoft.azure.management.compute.VirtualMachine in project photon-model by vmware.

the class AzureComputeDiskDay2ServiceTest method assertDetachDiskFromVM.

private void assertDetachDiskFromVM() {
    ComputeService.ComputeState provisionedVM = this.host.getServiceState(null, ComputeService.ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
    assertEquals(this.computeVM.diskLinks.size() - 1, provisionedVM.diskLinks.size());
    DiskService.DiskState detachedDisk = this.host.getServiceState(null, DiskService.DiskState.class, UriUtils.buildUri(this.host, this.diskState.documentSelfLink));
    assertEquals("Disk status is not matching", DiskService.DiskStatus.AVAILABLE, detachedDisk.status);
    if (!this.isMock) {
        assertNull("LUN number not removed from Detached Disk", detachedDisk.customProperties.get(DISK_CONTROLLER_NUMBER));
        VirtualMachine vm = this.getAzureSdkClients().getComputeManager().virtualMachines().getById(provisionedVM.id);
        // Check total numbers of disk match previous count - 1 = current data disks + 1 os disk
        assertEquals("Number of disks not correct on Azure", this.computeVM.diskLinks.size() - 1, vm.dataDisks().size() + 1);
        this.host.log("Number of disks attached to VM is - " + vm.dataDisks().size());
    }
}
Also used : ComputeService(com.vmware.photon.controller.model.resources.ComputeService) DiskService(com.vmware.photon.controller.model.resources.DiskService) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 65 with VirtualMachine

use of com.microsoft.azure.management.compute.VirtualMachine in project photon-model by vmware.

the class AzureRemoteCleanup method cleanResourceGroups.

/**
 * Initiating a clean up of a resource group will clean the following resources:
 * 1) VM
 * 2) Storage Accounts
 * 3) Public IP
 * 4) Virtual Network
 * 5) Security groups (if used by other resources it will not be deleted )
 * 6) Nics (if used by other resources it will not be deleted )
 *
 * Note: Currently we are provisioning resources on Azure in the following two tests with the prefixes mentioned below:
 * 1) TestAzureLongRunningEnumeration: az-lrt-
 * 2) TestAzureEnumerationTask: enumtest-
 * We are cleaning up all the resources based on the above mentioned tag after an hour its created.
 */
@Test
public void cleanResourceGroups() throws IOException {
    ResourceGroups resourceGroups = this.azureClient.resourceGroups();
    List<ResourceGroup> resourceGroupsToBeDeleted = new ArrayList<>();
    try {
        resourceGroups.list().stream().forEach(resourceGroup -> {
            this.host.log(Level.INFO, "Client reading resource group name: %s in region: %s", resourceGroup.name(), resourceGroup.regionName());
            if (resourceGroup.name().startsWith(AZ_LRT_RESOURCE_GROUP_TAG) || resourceGroup.name().startsWith(ENUMTEST_RESOURCE_GROUP_TAG)) {
                // get the creation from the time_stamp tag value in the vm's tag
                // if the resource is more than an hour old add the resource group to resourceGroupsToBeDeleted list
                PagedList<VirtualMachine> virtualMachines = this.azureClient.virtualMachines().listByResourceGroup(resourceGroup.name());
                virtualMachines.stream().forEach(vm -> {
                    if (vm.tags().containsKey(TIME_STAMP_TAG_KEY)) {
                        this.host.log(Level.INFO, "Virtual machine is tagged with: %s", vm.name());
                        long vmCreationTime = Long.valueOf(vm.tags().get(TIME_STAMP_TAG_KEY));
                        long timeDifference = Utils.getNowMicrosUtc() - vmCreationTime;
                        if (timeDifference > TimeUnit.HOURS.toMicros(1)) {
                            resourceGroupsToBeDeleted.add(resourceGroup);
                        }
                    } else {
                        this.host.log(Level.INFO, "Tag not found for Virtual Machine: %s, will perform tagging.", vm.name());
                        // if a vm does not have a time_stamp tag the update the vm with the current time
                        // And the next run of this script will delete this particular resource
                        vm.update().withTag(TIME_STAMP_TAG_KEY, String.valueOf(Utils.getNowMicrosUtc())).apply();
                    }
                });
            }
        });
        resourceGroupsToBeDeleted.stream().forEach(resourceGroup -> {
            this.host.log(Level.INFO, "Terminating stale resource group: %s", resourceGroup.name());
            this.azureClient.resourceGroups().deleteByName(resourceGroup.name());
            this.host.log("Terminated stale resource group: %s", resourceGroup.name());
        });
    } catch (Exception ex) {
        this.host.log(Level.INFO, ex.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) ResourceGroups(com.microsoft.azure.management.resources.ResourceGroups) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) IOException(java.io.IOException) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) Test(org.junit.Test)

Aggregations

VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)68 ArrayList (java.util.ArrayList)21 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)20 Network (com.microsoft.azure.management.network.Network)17 Date (java.util.Date)12 Disk (com.microsoft.azure.management.compute.Disk)10 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)10 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)9 Creatable (com.microsoft.azure.management.resources.fluentcore.model.Creatable)9 Azure (com.microsoft.azure.management.Azure)8 NetworkInterface (com.microsoft.azure.management.network.NetworkInterface)8 IOException (java.io.IOException)8 StopWatch (org.apache.commons.lang3.time.StopWatch)7 VirtualMachineDataDisk (com.microsoft.azure.management.compute.VirtualMachineDataDisk)6 HashMap (java.util.HashMap)6 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)5 JSchException (com.jcraft.jsch.JSchException)4 CloudException (com.microsoft.azure.CloudException)4 DockerHost (com.microsoft.azure.docker.model.DockerHost)4 NetworkSecurityGroup (com.microsoft.azure.management.network.NetworkSecurityGroup)4