use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method queryForDiskStates.
/**
* Get all disk states related to given VMs
*/
private void queryForDiskStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.virtualMachines.size() == 0) {
logFine(() -> "No virtual machines found to be associated with local disks");
if (ctx.regions.isEmpty()) {
ctx.subStage = ComputeEnumerationSubStages.DISASSOCIATE_COMPUTE_STATES;
handleSubStage(ctx);
return;
} else {
ctx.subStage = ComputeEnumerationSubStages.UPDATE_COMPUTE_STATES;
handleSubStage(ctx);
return;
}
}
ctx.diskStates.clear();
List<String> diskIdList = new ArrayList<>();
for (String instanceId : ctx.virtualMachines.keySet()) {
VirtualMachineInner virtualMachine = ctx.virtualMachines.get(instanceId);
String diskId = getVhdUri(virtualMachine);
if (diskId == null) {
continue;
}
diskIdList.add(diskId);
List<String> dataDiskIDList = getDataDisksID(virtualMachine, AzureUtils.isDiskManaged(virtualMachine));
if (null != dataDiskIDList && dataDiskIDList.size() > 0) {
diskIdList.addAll(dataDiskIDList);
}
}
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addInClause(DiskState.FIELD_NAME_ID, diskIdList, Occurance.SHOULD_OCCUR);
QueryByPages<DiskState> queryDiskStates = new QueryByPages<>(getHost(), qBuilder.build(), DiskState.class, ctx.parentCompute.tenantLinks, // endpointLink
null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
queryDiskStates.collectDocuments(Collectors.toList()).whenComplete((diskStates, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
if (diskStates == null) {
return;
}
diskStates.forEach(diskState -> ctx.diskStates.put(diskState.id, diskState));
ctx.subStage = next;
handleSubStage(ctx);
});
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method updateOSDiskProperties.
private void updateOSDiskProperties(EnumerationContext ctx, VirtualMachineInner virtualMachine, String diskUri, Collection<Operation> opCollection) {
DiskState diskToUpdate = ctx.diskStates.get(diskUri);
Operation diskToUpdateOp = null;
if (diskToUpdate == null) {
diskToUpdate = createOSDiskState(virtualMachine, ctx);
updateDiskCustomProperties(virtualMachine, ctx, diskToUpdate);
diskToUpdateOp = Operation.createPost(createInventoryUri(getHost(), DiskService.FACTORY_LINK)).setBody(diskToUpdate);
} else {
updateDiskCustomProperties(virtualMachine, ctx, diskToUpdate);
diskToUpdateOp = Operation.createPatch(createInventoryUri(getHost(), diskToUpdate.documentSelfLink)).setBody(diskToUpdate);
}
opCollection.add(diskToUpdateOp);
ctx.diskStates.put(diskToUpdate.id, diskToUpdate);
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateOrRetireHelper.
/**
* Helper method to paginate through resources to be deleted.
*/
private void disassociateOrRetireHelper(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.deletionNextPageLink == null) {
logFine(() -> String.format("Finished %s of compute states for Azure", ctx.request.preserveMissing ? "retiring" : "deletion"));
ctx.subStage = next;
handleSubStage(ctx);
return;
}
CompletionHandler completionHandler = (o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
QueryTask queryTask = o.getBody(QueryTask.class);
ctx.deletionNextPageLink = queryTask.results.nextPageLink;
List<Operation> operations = new ArrayList<>();
for (Object s : queryTask.results.documents.values()) {
ComputeState computeState = Utils.fromJson(s, ComputeState.class);
String vmId = computeState.id;
// present in Azure but have older timestamp in local repository.
if (ctx.vmIds.contains(vmId) || ctx.regionIds.contains(vmId)) {
continue;
}
if (ctx.request.preserveMissing) {
logFine(() -> String.format("Retiring compute state %s", computeState.documentSelfLink));
ComputeState cs = new ComputeState();
cs.powerState = PowerState.OFF;
cs.lifecycleState = LifecycleState.RETIRED;
operations.add(Operation.createPatch(this, computeState.documentSelfLink).setBody(cs));
} else {
// Deleting the localResourceState is done by disassociating the endpointLink from the
// localResourceState. If the localResourceState isn't associated with any other
// endpointLink, we issue a delete then
Operation dOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, computeState);
if (dOp != null) {
dOp.sendWith(getHost());
logFine(() -> String.format("Deleting compute state %s", computeState.documentSelfLink));
}
if (computeState.diskLinks != null && !computeState.diskLinks.isEmpty()) {
computeState.diskLinks.forEach(dl -> {
sendRequest(Operation.createGet(this, dl).setCompletion((op, ex) -> {
if (ex != null) {
logWarning(() -> String.format("Error retrieving " + "diskState: %s", ex.getMessage()));
} else {
DiskState diskState = op.getBody(DiskState.class);
Operation diskOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, diskState);
if (diskOp != null) {
diskOp.sendWith(getHost());
logFine(() -> String.format("Deleting disk state %s of machine %s", dl, computeState.documentSelfLink));
}
}
}));
});
}
if (computeState.networkInterfaceLinks != null && !computeState.networkInterfaceLinks.isEmpty()) {
computeState.networkInterfaceLinks.forEach(nil -> {
sendRequest(Operation.createGet(this, nil).setCompletion((op, ex) -> {
if (ex != null) {
logWarning(() -> String.format("Error retrieving NetworkInterface state: %s", ex.getMessage()));
} else {
NetworkInterfaceState networkInterfaceState = op.getBody(NetworkInterfaceState.class);
Operation nicOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, networkInterfaceState);
if (nicOp != null) {
nicOp.sendWith(getHost());
logFine(() -> String.format("Deleting NetworkInterface state %s of machine %s", nil, computeState.documentSelfLink));
}
}
}));
});
}
}
}
if (operations.size() == 0) {
logFine(() -> String.format("No compute/disk states to %s", ctx.request.preserveMissing ? "retire" : "delete"));
disassociateOrRetireHelper(ctx, next);
return;
}
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null) {
// We don't want to fail the whole data collection if some of the
// operation fails.
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
disassociateOrRetireHelper(ctx, next);
}).sendWith(this);
};
logFine(() -> String.format("Querying page [%s] for resources to be %s", ctx.deletionNextPageLink, ctx.request.preserveMissing ? "retire" : "delete"));
sendRequest(Operation.createGet(createInventoryUri(this.getHost(), ctx.deletionNextPageLink)).setCompletion(completionHandler));
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method createOSDiskState.
private DiskState createOSDiskState(VirtualMachineInner vm, EnumerationContext ctx) {
DiskState diskState = new DiskState();
diskState.id = getVhdUri(vm);
if (diskState.tagLinks == null) {
diskState.tagLinks = new HashSet<>();
}
if (vm.storageProfile() != null && vm.storageProfile().osDisk() != null && vm.storageProfile().osDisk().diskSizeGB() != null) {
OSDisk osDisk = vm.storageProfile().osDisk();
if (osDisk.managedDisk() != null && osDisk.managedDisk().id() != null) {
// tag as managed disk
diskState.tagLinks.add(ctx.managedDiskInternalTagLink);
} else {
// otherwise tag as vhd, as that is the default type
diskState.tagLinks.add(ctx.vhdInternalTagLink);
}
if (osDisk.diskSizeGB() != null) {
diskState.capacityMBytes = osDisk.diskSizeGB() * 1024;
}
diskState.name = osDisk.name();
}
diskState.regionId = vm.location();
diskState.computeHostLink = ctx.parentCompute.documentSelfLink;
diskState.resourcePoolLink = ctx.request.resourcePoolLink;
diskState.tenantLinks = ctx.parentCompute.tenantLinks;
diskState.status = DiskService.DiskStatus.ATTACHED;
String id = UUID.randomUUID().toString();
diskState.documentSelfLink = UriUtils.buildUriPath(DiskService.FACTORY_LINK, id);
diskState.regionId = vm.location();
diskState.endpointLink = ctx.request.endpointLink;
AdapterUtils.addToEndpointLinks(diskState, ctx.request.endpointLink);
return diskState;
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureComputeDiskDay2Service method updateDiskState.
/**
* Update status and LUN of DiskState
*/
private DeferredResult<Operation> updateDiskState(AzureComputeDiskDay2Context context) {
DiskState diskState = context.diskState;
if (context.request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
diskState.status = DiskService.DiskStatus.ATTACHED;
} else if (context.request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
diskState.status = DiskService.DiskStatus.AVAILABLE;
diskState.customProperties.remove(DISK_CONTROLLER_NUMBER);
}
if (!context.request.isMockRequest) {
DataDisk dataDisk = context.provisionedVm.inner().storageProfile().dataDisks().stream().filter(dd -> diskState.name.equalsIgnoreCase(dd.name())).findFirst().orElse(null);
if (dataDisk != null) {
if (diskState.customProperties == null) {
diskState.customProperties = new HashMap<>();
}
diskState.customProperties.put(DISK_CONTROLLER_NUMBER, String.valueOf(dataDisk.lun()));
}
}
Operation diskPatchOp = null;
if (context.request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
diskPatchOp = Operation.createPatch(createInventoryUri(this.getHost(), diskState.documentSelfLink)).setBody(diskState).setReferer(this.getUri());
} else if (context.request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
diskPatchOp = Operation.createPut(createInventoryUri(this.getHost(), diskState.documentSelfLink)).setBody(diskState).setReferer(this.getUri());
}
return this.sendWithDeferredResult(diskPatchOp);
}
Aggregations