Search in sources :

Example 1 with ResourceState

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

the class TestAWSEnumerationDocumentCountInLongRun method verifyResourceDuplicates.

/**
 * Verify documents for duplicates after multiple enumerations.
 */
private void verifyResourceDuplicates() {
    int total_dup_resource_count = 0;
    for (Class resource : resourcesList) {
        QueryTask.Query.Builder qBuilder = QueryTask.Query.Builder.create().addKindFieldClause(resource).addFieldClause("endpointLinks.item", this.endpointState.documentSelfLink, QueryTask.QueryTerm.MatchType.TERM, QueryTask.Query.Occurance.MUST_OCCUR);
        if (resource.getSimpleName().equals("ComputeState")) {
            qBuilder.addFieldClause("type", "VM_GUEST", QueryTask.QueryTerm.MatchType.TERM, QueryTask.Query.Occurance.MUST_OCCUR);
        }
        QueryTask resourceQt = QueryTask.Builder.createDirectTask().setQuery(qBuilder.build()).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).addOption(QueryTask.QuerySpecification.QueryOption.TOP_RESULTS).setResultLimit(10000).build();
        Operation queryDocuments = QueryUtils.createQueryTaskOperation(this.host, resourceQt, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
        Operation queryResponse = this.host.waitForResponse(queryDocuments);
        Assert.assertTrue("Error retrieving enumerated documents", queryResponse.getStatusCode() == 200);
        QueryTask qt = queryResponse.getBody(QueryTask.class);
        Set<String> resourceIdSet = new HashSet<>();
        if (qt.results != null && qt.results.documentLinks != null && qt.results.documentLinks.size() > 0) {
            this.host.log("Number of %s docs: %d", resource.getSimpleName(), qt.results.documentLinks.size());
            for (String resourceDocumentLink : qt.results.documentLinks) {
                Object object = qt.results.documents.get(resourceDocumentLink);
                ResourceState resourceState = Utils.fromJson(object, ResourceState.class);
                String resourceId = resourceState.id;
                if (!resourceIdSet.contains(resourceId)) {
                    resourceIdSet.add(resourceId);
                } else {
                    this.host.log("duplicate %s id = %s, with state: ", resource.getSimpleName(), resourceId, Utils.toJsonHtml(resourceState));
                    total_dup_resource_count++;
                }
            }
        }
    }
    assertEquals("Duplicate resources found: ", 0, total_dup_resource_count);
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) TestAWSSetupUtils.deleteVMsOnThisEndpoint(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsOnThisEndpoint) HashSet(java.util.HashSet)

Example 2 with ResourceState

use of com.vmware.photon.controller.model.resources.ResourceState 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)

Example 3 with ResourceState

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

the class ResourceOperationUtils method isAvailable.

/**
 * Evaluates provided {@code spec}'s target criteria against the specified {@code resourceState}
 * and returns if the  {@link ResourceOperationSpec} is applicable for the given {@link
 * ResourceState}
 * @param resourceState
 *         the resource state for which to check whether given {@code spec} is available
 * @param spec
 *         the {@link ResourceOperationSpec} which to check whether is available for the given
 *         {@code resourceState}
 * @return {@literal true} only in case there is targetCriteria, and the targetCriteria is
 * evaluated to {@literal true} for the given {@code resourceState}
 */
public static boolean isAvailable(ResourceState resourceState, ResourceOperationSpec spec) {
    AssertUtil.assertNotNull(spec, "'spec' must be set.");
    if (spec.targetCriteria == null) {
        return true;
    }
    ScriptEngine engine = new ScriptEngineManager().getEngineByName(SCRIPT_ENGINE_NAME_JS);
    if (resourceState != null) {
        // Clone original object to avoid changing props of original object from vulnerable
        // targetCriteria
        ResourceState clone = Utils.cloneObject(resourceState);
        engine.getBindings(ScriptContext.ENGINE_SCOPE).put(SCRIPT_CONTEXT_RESOURCE, clone);
    }
    try {
        Object res = engine.eval(spec.targetCriteria);
        if (res instanceof Boolean) {
            return ((Boolean) res).booleanValue();
        } else {
            Utils.log(ResourceOperationUtils.class, "isAvailable", Level.WARNING, "Expect boolean result when evaluate targetCriteria \"%s\" of " + "endpointType: %s, resourceType: %s, operation: %s, " + "adapterReference: %s. Result: %s", spec.targetCriteria, spec.endpointType, spec.resourceType, spec.operation, spec.adapterReference, res);
        }
    } catch (ScriptException e) {
        Utils.log(ResourceOperationUtils.class, "isAvailable", Level.SEVERE, "Cannot evaluate targetCriteria '%s' of " + "endpointType: %s, resourceType: %s, operation: %s, " + "adapterReference: %s. Cause: %s", spec.targetCriteria, spec.endpointType, spec.resourceType, spec.operation, spec.adapterReference, Utils.toString(e));
    }
    return false;
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) ScriptEngine(javax.script.ScriptEngine)

Example 4 with ResourceState

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

the class AzureTestUtil method assertResourceExists.

/**
 * Assert that a resource with the provided name exist in the document store.
 *
 * @param factoryLink
 *            Factory link to the stateful service which states to check.
 * @param name
 *            name of the resource to assert if exists.
 * @param shouldExists
 *            whether to assert if a resource exists or not.
 */
public static void assertResourceExists(VerificationHost host, String factoryLink, String name, boolean shouldExists) {
    ServiceDocumentQueryResult result = host.getExpandedFactoryState(UriUtils.buildUri(host, factoryLink));
    boolean exists = false;
    for (Object document : result.documents.values()) {
        ResourceState state = Utils.fromJson(document, ResourceState.class);
        if (name.equals(state.name)) {
            exists = true;
            break;
        }
    }
    assertEquals("Expected: " + shouldExists + ", but was: " + exists, shouldExists, exists);
}
Also used : ResourceState(com.vmware.photon.controller.model.resources.ResourceState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Example 5 with ResourceState

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

the class AzureTestUtil method assertResourceDisassociated.

/**
 * Assert that a resource with the provided name exist in the document store.
 *
 * @param factoryLink
 *            Factory link to the stateful service which states to check.
 * @param name
 *            name of the resource to assert if exists.
 * @param isDisassociated
 *            whether to assert if a resource exists or not.
 */
public static void assertResourceDisassociated(VerificationHost host, String factoryLink, String name, boolean isDisassociated) {
    ServiceDocumentQueryResult result = host.getExpandedFactoryState(UriUtils.buildUri(host, factoryLink));
    boolean disassociated = false;
    for (Object document : result.documents.values()) {
        // Read doc as ServiceDocument to access its 'documentKind'
        ServiceDocument serviceDoc = Utils.fromJson(document, ServiceDocument.class);
        Class<? extends ResourceState> resourceClass = ENDPOINT_LINK_EXPLICIT_SUPPORT.stream().filter(clazz -> serviceDoc.documentKind.equals(Utils.buildKind(clazz))).findFirst().orElse(null);
        if (resourceClass != null) {
            // Read doc as ResourceState to access its 'endpointLinks'
            ResourceState resource = Utils.fromJson(document, resourceClass);
            if (Objects.equals(name, resource.name) && resource.endpointLinks.isEmpty()) {
                String endpointLink = PhotonModelUtils.getEndpointLink(resource);
                if (endpointLink == null || endpointLink.isEmpty()) {
                    disassociated = true;
                    break;
                }
            }
        }
    }
    assertEquals("isDisassociated", isDisassociated, disassociated);
}
Also used : ServiceDocument(com.vmware.xenon.common.ServiceDocument) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Aggregations

ResourceState (com.vmware.photon.controller.model.resources.ResourceState)13 Operation (com.vmware.xenon.common.Operation)4 HashSet (java.util.HashSet)4 ServiceDocumentQueryResult (com.vmware.xenon.common.ServiceDocumentQueryResult)3 QueryTask (com.vmware.xenon.services.common.QueryTask)3 ServiceDocument (com.vmware.xenon.common.ServiceDocument)2 Set (java.util.Set)2 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