use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateNics.
/**
* Deletes stale network interface states that are deleted from the remote.
*/
private DeferredResult<List<Operation>> disassociateNics(EnumerationContext ctx, List<String> remoteNicIds, List<NetworkInterfaceState> allLocalNics) {
List<DeferredResult<Operation>> updateOps = new ArrayList<>();
allLocalNics.stream().filter(localNic -> !remoteNicIds.contains(localNic.id)).forEach(localNic -> {
Operation upOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, localNic);
if (upOp != null) {
CompletionHandler completionHandler = upOp.getCompletion();
upOp.setCompletion(null);
updateOps.add(sendWithDeferredResult(upOp).whenComplete(completionHandler::handle));
}
});
return DeferredResult.allOf(updateOps);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method updateDiskStates.
/**
* Update disk states with additional custom properties
*/
private void updateDiskStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.virtualMachines.isEmpty()) {
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;
}
}
Iterator<Entry<String, VirtualMachineInner>> iterator = ctx.virtualMachines.entrySet().iterator();
Collection<Operation> opCollection = new ArrayList<>();
while (iterator.hasNext()) {
Entry<String, VirtualMachineInner> vmEntry = iterator.next();
VirtualMachineInner virtualMachine = vmEntry.getValue();
if (virtualMachine.storageProfile() == null || virtualMachine.storageProfile().imageReference() == null) {
continue;
}
if (virtualMachine.storageProfile().osDisk() == null) {
logWarning(() -> "VM has empty OS disk.");
continue;
}
String osDiskUri = getVhdUri(virtualMachine);
if (osDiskUri == null) {
logFine(() -> String.format("OS Disk URI not found for vm: %s", virtualMachine.id()));
continue;
}
updateOSDiskProperties(ctx, virtualMachine, osDiskUri, opCollection);
// create data disk states and update their custom properties
updateDataDiskProperties(ctx, virtualMachine, opCollection);
}
if (opCollection.isEmpty()) {
logFine(() -> "No local disk states fount to update.");
ctx.subStage = next;
handleSubStage(ctx);
return;
}
OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
if (exs != null) {
// TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-3256
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
logFine(() -> "Continue on to create network interfaces.");
ctx.subStage = next;
handleSubStage(ctx);
}).sendWith(this);
}
use of com.vmware.xenon.common.Operation 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.xenon.common.Operation 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.xenon.common.Operation in project photon-model by vmware.
the class AzureComputeDiskDay2Service method updateComputeState.
/**
* Update the diskLink of DiskState in ComputeState
*/
private DeferredResult<Operation> updateComputeState(AzureComputeDiskDay2Context context) {
Map<String, Collection<Object>> collectionsToModify = Collections.singletonMap(ComputeState.FIELD_NAME_DISK_LINKS, Collections.singletonList(context.diskState.documentSelfLink));
Map<String, Collection<Object>> collectionsToAdd = null;
Map<String, Collection<Object>> collectionsToRemove = null;
ComputeState computeState = context.computeState;
if (context.request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
collectionsToAdd = collectionsToModify;
} else if (context.request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
collectionsToRemove = collectionsToModify;
}
ServiceStateCollectionUpdateRequest updateDiskLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAdd, collectionsToRemove);
Operation computeStateOp = Operation.createPatch(createInventoryUri(this.getHost(), computeState.documentSelfLink)).setBody(updateDiskLinksRequest).setReferer(this.getUri());
return this.sendWithDeferredResult(computeStateOp);
}
Aggregations