Search in sources :

Example 61 with DeferredResult

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);
}
Also used : SecurityRuleProtocol(com.microsoft.azure.management.network.SecurityRuleProtocol) NetworkManagementClientImpl(com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl) IPAllocationMethod(com.microsoft.azure.management.network.IPAllocationMethod) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) StringUtils(org.apache.commons.lang3.StringUtils) LoadBalancingRuleInner(com.microsoft.azure.management.network.implementation.LoadBalancingRuleInner) FrontendIPConfigurationInner(com.microsoft.azure.management.network.implementation.FrontendIPConfigurationInner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BackendAddressPoolInner(com.microsoft.azure.management.network.implementation.BackendAddressPoolInner) URI(java.net.URI) AssertUtil(com.vmware.photon.controller.model.util.AssertUtil) SecurityRuleDirection(com.microsoft.azure.management.network.SecurityRuleDirection) ProbeInner(com.microsoft.azure.management.network.implementation.ProbeInner) HealthCheckConfiguration(com.vmware.photon.controller.model.resources.LoadBalancerDescriptionService.LoadBalancerDescription.HealthCheckConfiguration) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) LoadBalancersInner(com.microsoft.azure.management.network.implementation.LoadBalancersInner) Set(java.util.Set) UUID(java.util.UUID) SecurityRuleAccess(com.microsoft.azure.management.network.SecurityRuleAccess) Collectors(java.util.stream.Collectors) AzureProvisioningCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureProvisioningCallback) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) AzureSecurityGroupUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureSecurityGroupUtils) NetworkSecurityGroupsInner(com.microsoft.azure.management.network.implementation.NetworkSecurityGroupsInner) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) LoadBalancerInner(com.microsoft.azure.management.network.implementation.LoadBalancerInner) NetworkInterfacesInner(com.microsoft.azure.management.network.implementation.NetworkInterfacesInner) Optional(java.util.Optional) NetworkSecurityGroupInner(com.microsoft.azure.management.network.implementation.NetworkSecurityGroupInner) AzureProvisioningCallbackWithRetry(com.vmware.photon.controller.model.adapters.azure.utils.AzureProvisioningCallbackWithRetry) Default(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default) SubResource(com.microsoft.azure.SubResource) RouteConfiguration(com.vmware.photon.controller.model.resources.LoadBalancerDescriptionService.LoadBalancerDescription.RouteConfiguration) TransportProtocol(com.microsoft.azure.management.network.TransportProtocol) ArrayList(java.util.ArrayList) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) AzureBaseAdapterContext(com.vmware.photon.controller.model.adapters.azure.utils.AzureBaseAdapterContext) CollectionUtils(org.apache.commons.collections.CollectionUtils) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) PublicIPAddressesInner(com.microsoft.azure.management.network.implementation.PublicIPAddressesInner) PublicIPAddressInner(com.microsoft.azure.management.network.implementation.PublicIPAddressInner) NetworkInterfaceIPConfigurationInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceIPConfigurationInner) ServiceCallback(com.microsoft.rest.ServiceCallback) LoadBalancerStateExpanded(com.vmware.photon.controller.model.resources.LoadBalancerService.LoadBalancerStateExpanded) Operation(com.vmware.xenon.common.Operation) SecurityGroupService(com.vmware.photon.controller.model.resources.SecurityGroupService) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) ProbeProtocol(com.microsoft.azure.management.network.ProbeProtocol) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) SecurityRuleInner(com.microsoft.azure.management.network.implementation.SecurityRuleInner) LoadBalancerInstanceRequest(com.vmware.photon.controller.model.adapterapi.LoadBalancerInstanceRequest) Comparator(java.util.Comparator) NetworkInterfaceInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceInner) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) NetworkSecurityGroupsInner(com.microsoft.azure.management.network.implementation.NetworkSecurityGroupsInner) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 62 with DeferredResult

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());
    });
}
Also used : Service(com.vmware.xenon.common.Service) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) StringUtils(org.apache.commons.lang3.StringUtils) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ArrayList(java.util.ArrayList) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) Utils(com.vmware.xenon.common.Utils) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) SnapshotRequestType(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType) BiConsumer(java.util.function.BiConsumer) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) TaskInfo(com.vmware.vim25.TaskInfo) OperationSequence(com.vmware.xenon.common.OperationSequence) QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) MapUtils(org.apache.commons.collections.MapUtils) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) List(java.util.List) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Optional(java.util.Optional) VSphereConstants(com.vmware.photon.controller.model.adapters.vsphere.constants.VSphereConstants) FactoryService(com.vmware.xenon.common.FactoryService) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) TaskInfoState(com.vmware.vim25.TaskInfoState) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) OperationJoin(com.vmware.xenon.common.OperationJoin) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)

Example 63 with DeferredResult

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;
}
Also used : QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) QueryTask(com.vmware.xenon.services.common.QueryTask) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 64 with DeferredResult

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());
    }
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) Service(com.vmware.xenon.common.Service) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) StringUtils(org.apache.commons.lang3.StringUtils) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ArrayList(java.util.ArrayList) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) Utils(com.vmware.xenon.common.Utils) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) SnapshotRequestType(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType) BiConsumer(java.util.function.BiConsumer) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) TaskInfo(com.vmware.vim25.TaskInfo) OperationSequence(com.vmware.xenon.common.OperationSequence) QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) MapUtils(org.apache.commons.collections.MapUtils) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) List(java.util.List) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Optional(java.util.Optional) VSphereConstants(com.vmware.photon.controller.model.adapters.vsphere.constants.VSphereConstants) FactoryService(com.vmware.xenon.common.FactoryService) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) TaskInfoState(com.vmware.vim25.TaskInfoState) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) OperationJoin(com.vmware.xenon.common.OperationJoin) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 65 with DeferredResult

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;
}
Also used : SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) Operation(com.vmware.xenon.common.Operation) DeferredResult(com.vmware.xenon.common.DeferredResult)

Aggregations

DeferredResult (com.vmware.xenon.common.DeferredResult)77 ArrayList (java.util.ArrayList)57 Operation (com.vmware.xenon.common.Operation)52 List (java.util.List)49 Collectors (java.util.stream.Collectors)43 StatelessService (com.vmware.xenon.common.StatelessService)42 Utils (com.vmware.xenon.common.Utils)38 Map (java.util.Map)38 HashMap (java.util.HashMap)37 UriUtils (com.vmware.xenon.common.UriUtils)36 DiskService (com.vmware.photon.controller.model.resources.DiskService)33 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)33 URI (java.net.URI)33 Collection (java.util.Collection)32 Set (java.util.Set)31 HashSet (java.util.HashSet)30 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)29 QueryTask (com.vmware.xenon.services.common.QueryTask)28 Consumer (java.util.function.Consumer)28 OperationJoin (com.vmware.xenon.common.OperationJoin)26