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");
}
}
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;
}
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());
}
}
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());
}
}
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());
}
}
Aggregations