Search in sources :

Example 1 with LifecycleState

use of com.vmware.photon.controller.model.resources.ComputeService.LifecycleState in project photon-model by vmware.

the class AWSNetworkStateEnumerationAdapterService method deleteStaleLocalStates.

private DeferredResult<AWSNetworkStateCreationContext> deleteStaleLocalStates(AWSNetworkStateCreationContext context, Class<? extends ResourceState> localStateClass, Set<String> remoteResourcesKeys) {
    final String msg = "Delete %ss that no longer exist in the endpoint: %s";
    logFine(() -> String.format(msg, localStateClass.getSimpleName(), "STARTING"));
    Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(localStateClass).addFieldClause("lifecycleState", LifecycleState.PROVISIONING.toString(), Occurance.MUST_NOT_OCCUR).addRangeClause(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS, createLessThanRange(context.enumStartTimeInMicros));
    if (context.request.regionId != null) {
        // Delete resources only in this End-point region
        qBuilder.addFieldClause(ResourceState.FIELD_NAME_REGION_ID, context.request.regionId);
    }
    if (!remoteResourcesKeys.isEmpty() && remoteResourcesKeys.size() <= MAX_RESOURCES_TO_QUERY_ON_DELETE) {
        // do not load resources from enumExternalResourcesIds
        qBuilder.addInClause(ResourceState.FIELD_NAME_ID, remoteResourcesKeys, Occurance.MUST_NOT_OCCUR);
    }
    QueryByPages<? extends ResourceState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), localStateClass, context.request.tenantLinks, context.request.request.endpointLink);
    queryLocalStates.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    List<DeferredResult<Operation>> disassociateDRs = new ArrayList<>();
    // Delete stale resources.
    return queryLocalStates.queryDocuments(localState -> {
        if (remoteResourcesKeys.contains(localState.id)) {
            return;
        }
        // Deleting the localResourceState is done by disassociating the
        // endpointLink from the localResourceState. If the localResourceState
        // isn't associated with any other endpointLink, it should be eventually
        // deleted by the groomer task
        Operation disassociateOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, context.request.request.endpointLink, localState);
        if (disassociateOp == null) {
            return;
        }
        // NOTE: The original Op is set with completion that must be executed.
        // Since sendWithDeferredResult is used we must manually call it, otherwise it's
        // just ignored.
        CompletionHandler disassociateOpCompletion = disassociateOp.getCompletion();
        DeferredResult<Operation> disassociateDR = sendWithDeferredResult(disassociateOp).whenComplete(disassociateOpCompletion::handle).whenComplete((o, e) -> {
            final String message = "Disassociate stale %s state";
            if (e != null) {
                logWarning(message + ": FAILED with %s", localState.documentSelfLink, Utils.toString(e));
            } else {
                log(Level.FINEST, message + ": SUCCESS", localState.documentSelfLink);
            }
        });
        disassociateDRs.add(disassociateDR);
    }).thenCompose(ignore -> DeferredResult.allOf(disassociateDRs)).thenApply(ignore -> context);
}
Also used : Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) AWSNetworkUtils.mapVPCToNetworkState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.mapVPCToNetworkState) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) AWSResourceType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AWSNetworkUtils.createQueryToGetExistingNetworkStatesFilteredByDiscoveredVPCs(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.createQueryToGetExistingNetworkStatesFilteredByDiscoveredVPCs) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) SubnetStateWithParentVpcId(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSNetworkStateEnumerationAdapterService.AWSNetworkStateCreationContext.SubnetStateWithParentVpcId) AWS_ATTACHMENT_VPC_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_ATTACHMENT_VPC_FILTER) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) AWS_MAIN_ROUTE_ASSOCIATION(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_MAIN_ROUTE_ASSOCIATION) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) AWSNetworkUtils.mapSubnetToSubnetState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.mapSubnetToSubnetState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) AWS_GATEWAY_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_GATEWAY_ID) List(java.util.List) AWS_FILTER_VPC_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_FILTER_VPC_ID) DescribeRouteTablesRequest(com.amazonaws.services.ec2.model.DescribeRouteTablesRequest) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) DescribeRouteTablesResult(com.amazonaws.services.ec2.model.DescribeRouteTablesResult) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) JoinedCompletionHandler(com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) BiConsumer(java.util.function.BiConsumer) Filter(com.amazonaws.services.ec2.model.Filter) RouteTable(com.amazonaws.services.ec2.model.RouteTable) Subnet(com.amazonaws.services.ec2.model.Subnet) AWS_VPC_ROUTE_TABLE_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ROUTE_TABLE_ID) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Vpc(com.amazonaws.services.ec2.model.Vpc) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) AWSNetworkUtils.createQueryToGetExistingSubnetStatesFilteredByDiscoveredSubnets(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.createQueryToGetExistingSubnetStatesFilteredByDiscoveredSubnets) NumericRange.createLessThanRange(com.vmware.xenon.services.common.QueryTask.NumericRange.createLessThanRange) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) AWS_VPC_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID_FILTER) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Query(com.vmware.xenon.services.common.QueryTask.Query) ArrayList(java.util.ArrayList) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) JoinedCompletionHandler(com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) DeferredResult(com.vmware.xenon.common.DeferredResult)

Aggregations

AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)1 DescribeInternetGatewaysRequest (com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest)1 DescribeInternetGatewaysResult (com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult)1 DescribeRouteTablesRequest (com.amazonaws.services.ec2.model.DescribeRouteTablesRequest)1 DescribeRouteTablesResult (com.amazonaws.services.ec2.model.DescribeRouteTablesResult)1 DescribeSubnetsRequest (com.amazonaws.services.ec2.model.DescribeSubnetsRequest)1 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)1 DescribeVpcsRequest (com.amazonaws.services.ec2.model.DescribeVpcsRequest)1 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)1 Filter (com.amazonaws.services.ec2.model.Filter)1 InternetGateway (com.amazonaws.services.ec2.model.InternetGateway)1 InternetGatewayAttachment (com.amazonaws.services.ec2.model.InternetGatewayAttachment)1 RouteTable (com.amazonaws.services.ec2.model.RouteTable)1 Subnet (com.amazonaws.services.ec2.model.Subnet)1 Tag (com.amazonaws.services.ec2.model.Tag)1 Vpc (com.amazonaws.services.ec2.model.Vpc)1 UriPaths (com.vmware.photon.controller.model.UriPaths)1 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)1 AWSConstants (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants)1