use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureLoadBalancerService method updateSecurityGroupRules.
/**
* Update isolation security group with rule to allow traffic on load balancing ports for VMs
* being load balanced
*
* @param context Azure load balancer context
* @return DeferredResult
*/
private DeferredResult<AzureLoadBalancerContext> updateSecurityGroupRules(AzureLoadBalancerContext context) {
if (CollectionUtils.isEmpty(context.securityGroupInners)) {
return DeferredResult.completed(context);
}
// Add security group firewall rules to allow traffic to flow through load balancer routes
updateSecurityRules(context);
NetworkSecurityGroupsInner azureSecurityGroupClient = context.azureSdkClients.getNetworkManagementClientImpl().networkSecurityGroups();
List<DeferredResult<NetworkSecurityGroupInner>> networkSecurityGroupInnerList = context.securityGroupInners.stream().map(networkSecurityGroupInner -> {
final String msg = "Updating security group rules for [" + networkSecurityGroupInner.name() + "] for load balancer [" + context.loadBalancerStateExpanded.name + "].";
logInfo(() -> msg);
return AzureSecurityGroupUtils.createOrUpdateSecurityGroup(this, azureSecurityGroupClient, AzureUtils.getResourceGroupName(networkSecurityGroupInner.id()), networkSecurityGroupInner.name(), networkSecurityGroupInner, msg);
}).collect(Collectors.toList());
return DeferredResult.allOf(networkSecurityGroupInnerList).thenApply(ignored -> context);
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class VSphereAdapterSnapshotService method processNextStepsForDeleteOperation.
private void processNextStepsForDeleteOperation(SnapshotContext context, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
// Update the isCurrent
if (snapshot.isCurrent && snapshot.parentLink != null) {
logInfo("Updating the parent of the snapshot %s to current", snapshot.name);
SnapshotState parentSnapshot = new SnapshotState();
parentSnapshot.isCurrent = Boolean.TRUE;
context.snapshotOperations.add(Operation.createPatch(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshot.parentLink)).setBody(parentSnapshot).setReferer(getUri()));
}
// Check if the deleted snapshot is the last available snapshot
DeferredResult<Boolean> result = isLastSnapshotForCompute(context);
Operation[] patchComputeOp = new Operation[1];
result.whenComplete((b, e) -> {
if (e != null) {
logSevere(e);
deferredResult.fail(e);
return;
}
if (b) {
ComputeStateWithDescription compute = context.computeDescription;
compute.customProperties.put(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS, Boolean.FALSE.toString());
// patch compute adding property that it _hasSnapshots
logInfo("Updating the state of compute resource: %s", compute.name);
patchComputeOp[0] = Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.computeLink)).setBody(compute).setReferer(getUri());
context.snapshotOperations.add(patchComputeOp[0]);
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
return;
}
deferredResult.complete(context);
};
context.snapshotOperations.add(Operation.createDelete(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setReferer(getUri()));
OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
});
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class VSphereAdapterSnapshotService method isLastSnapshotForCompute.
private DeferredResult<Boolean> isLastSnapshotForCompute(SnapshotContext context) {
DeferredResult<Boolean> dr = new DeferredResult<>();
// find if for the compute has only one snapshot (the one which is to be deleted)
QueryTask qTask = getQueryWithFilters(context.snapshotState.computeLink, SnapshotState.FIELD_NAME_COMPUTE_LINK);
QueryUtils.startInventoryQueryTask(this, qTask).whenComplete((o, e) -> {
if (e != null) {
logInfo(String.format("Failure getting snapshot state: %s", Utils.toString(e)));
dr.fail(e);
return;
}
QueryResultsProcessor rp = QueryResultsProcessor.create(o);
List<SnapshotState> snapshotsFinal;
if (!rp.hasResults()) {
dr.complete(Boolean.FALSE);
} else {
snapshotsFinal = rp.streamDocuments(SnapshotState.class).collect(Collectors.toList());
dr.complete(snapshotsFinal.size() == 1);
}
});
return dr;
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class VSphereAdapterSnapshotService method revertSnapshot.
private void revertSnapshot(SnapshotContext context, Connection connection, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
SnapshotState existingSnapshotState = context.existingSnapshotState;
// Physical snapshot processing
ManagedObjectReference snapshotMoref = CustomProperties.of(snapshot).getMoRef(CustomProperties.MOREF);
if (snapshotMoref == null) {
deferredResult.fail(new IllegalStateException(String.format("Cannot find the snapshot %s to revert to", snapshotMoref)));
return;
}
ManagedObjectReference task;
TaskInfo info;
try {
logInfo("Reverting to snapshot with name %s", context.snapshotState.name);
task = connection.getVimPort().revertToSnapshotTask(snapshotMoref, null, false);
info = VimUtils.waitTaskEnd(connection, task);
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (Exception e) {
logSevere("Reverting to the snapshot %s failed", context.snapshotState.name);
deferredResult.fail(e);
return;
}
// cause concurrency issue
if (snapshot.isCurrent) {
deferredResult.complete(context);
} else {
snapshot.isCurrent = true;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setBody(snapshot).setReferer(getUri()));
if (existingSnapshotState != null) {
existingSnapshotState.isCurrent = false;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), existingSnapshotState.documentSelfLink)).setBody(existingSnapshotState).setReferer(getUri()));
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
return;
}
deferredResult.complete(context);
};
OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
}
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class VSphereVMSnapshotEnumerationHelper method updateSnapshot.
static DeferredResult<SnapshotState> updateSnapshot(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, VmOverlay vm, SnapshotState oldState, SnapshotState newState, String id) {
newState.documentSelfLink = oldState.documentSelfLink;
newState.id = id;
newState.regionId = enumerationProgress.getRegionId();
DeferredResult<SnapshotState> res = new DeferredResult<>();
VsphereEnumerationHelper.submitWorkToVSpherePool(service, () -> {
VsphereEnumerationHelper.populateTags(service, enumerationProgress, vm, newState);
newState.tenantLinks = enumerationProgress.getTenantLinks();
service.logFine(() -> String.format("Syncing snapshot %s", oldState.name));
Operation opPatchSnapshot = Operation.createPatch(UriUtils.buildUri(service.getHost(), oldState.documentSelfLink)).setBody(newState);
service.sendWithDeferredResult(opPatchSnapshot, SnapshotState.class).handle((snap, e) -> {
if (e != null) {
res.fail(e);
} else {
res.complete(snap);
}
return null;
});
});
return res;
}
Aggregations