Search in sources :

Example 21 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class TestAzureImageEnumerationTask method testPublicImageEnumeration_multi.

@Test
public void testPublicImageEnumeration_multi() throws Throwable {
    Assume.assumeFalse(this.isMock);
    // This test takes about less than 2 mins!
    getHost().setTimeoutSeconds((int) TimeUnit.MINUTES.toSeconds(2));
    ImageEnumerationTaskState task = kickOffImageEnumeration(this.endpointState, PUBLIC, AZURE_MULTI_IMAGES_FILTER);
    // Validate at least 200+ image states are created.
    QueryByPages<ImageState> queryAll = new QueryByPages<ImageState>(getHost(), Builder.create().addKindFieldClause(ImageState.class).build(), ImageState.class, task.tenantLinks);
    queryAll.setMaxPageSize(QueryUtils.DEFAULT_MAX_RESULT_LIMIT);
    Long imagesCount = PhotonModelUtils.waitToComplete(queryAll.collectLinks(Collectors.counting()));
    Assert.assertTrue("Expected at least " + 200 + " images, but found only " + imagesCount, imagesCount > 200);
}
Also used : QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ImageEnumerationTaskState(com.vmware.photon.controller.model.tasks.ImageEnumerationTaskService.ImageEnumerationTaskState) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) Test(org.junit.Test)

Example 22 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class TestAzureImageEnumerationTask method afterImageEnumTest.

@After
public final void afterImageEnumTest() throws Throwable {
    QueryByPages<ImageState> queryAll = new QueryByPages<ImageState>(getHost(), Builder.create().addKindFieldClause(ImageState.class).build(), ImageState.class, null);
    queryAll.setMaxPageSize(QueryUtils.DEFAULT_MAX_RESULT_LIMIT);
    AtomicInteger counter = new AtomicInteger(0);
    PhotonModelUtils.waitToComplete(queryAll.queryLinks(imageLink -> {
        try {
            deleteServiceSynchronously(imageLink);
            counter.incrementAndGet();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }));
    getHost().log(Level.INFO, "[" + this.currentTestName.getMethodName() + "] Deleted " + counter + " ImageStates");
}
Also used : QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) Level(java.util.logging.Level) HashSet(java.util.HashSet) Utils(com.vmware.xenon.common.Utils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImagesLoadMode(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureImageEnumerationAdapterService.ImagesLoadMode) Map(java.util.Map) After(org.junit.After) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) ProvisioningUtils.queryDocumentsAndAssertExpectedCount(com.vmware.photon.controller.model.tasks.ProvisioningUtils.queryDocumentsAndAssertExpectedCount) Assume(org.junit.Assume) ImageService(com.vmware.photon.controller.model.resources.ImageService) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) EnumSet(java.util.EnumSet) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Collection(java.util.Collection) DiskConfiguration(com.vmware.photon.controller.model.resources.ImageService.ImageState.DiskConfiguration) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) ImageEnumerationTaskService(com.vmware.photon.controller.model.tasks.ImageEnumerationTaskService) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Ignore(org.junit.Ignore) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) AzureAdaptersTestUtils(com.vmware.photon.controller.model.adapters.azure.base.AzureAdaptersTestUtils) ImageEnumerationTaskState(com.vmware.photon.controller.model.tasks.ImageEnumerationTaskService.ImageEnumerationTaskState) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) Assert(org.junit.Assert) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) After(org.junit.After)

Example 23 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class QueryUtilsTest method testQueryReferrers_error.

@Test
public void testQueryReferrers_error() throws Throwable {
    ComputeDescription cd = ModelUtils.createComputeDescription(this, null, null);
    ModelUtils.createCompute(this, cd);
    ModelUtils.createCompute(this, cd);
    Query queryForReferrers = QueryUtils.queryForReferrers(cd.documentSelfLink, ComputeState.class, ComputeState.FIELD_NAME_DESCRIPTION_LINK);
    // The class under testing
    QueryStrategy<ComputeState> queryStrategy = new QueryByPages<>(getHost(), queryForReferrers, ComputeState.class, Collections.emptyList(), null);
    Set<String> actual = new HashSet<>();
    // The method under testing
    waitToComplete(queryStrategy.queryDocuments(cs -> {
        if (actual.isEmpty()) {
            actual.add(cs.documentSelfLink);
        } else {
            throw new RuntimeException("consume error");
        }
    }).handle((v, e) -> {
        // Validate processed docs
        assertThat(actual.size(), equalTo(1));
        // Validate exception propagation
        assertThat(e, notNullValue());
        assertThat(e, instanceOf(CompletionException.class));
        assertThat(e.getCause().getMessage(), equalTo("consume error"));
        // Recover from exception to prevent its propagation to outer test method
        return (Void) null;
    }));
}
Also used : Arrays(java.util.Arrays) ModelUtils(com.vmware.photon.controller.model.ModelUtils) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) HashSet(java.util.HashSet) List(java.util.List) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Collectors.mapping(java.util.stream.Collectors.mapping) Matchers.equalTo(org.hamcrest.Matchers.equalTo) QueryTemplate(com.vmware.photon.controller.model.query.QueryUtils.QueryTemplate) DeferredResult(com.vmware.xenon.common.DeferredResult) PhotonModelUtils.waitToComplete(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.waitToComplete) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) Collectors.toSet(java.util.stream.Collectors.toSet) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) HashSet(java.util.HashSet) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 24 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class TestAWSImageEnumerationTask method testPublicImageEnumeration_all.

@Test
public void testPublicImageEnumeration_all() throws Throwable {
    Assume.assumeFalse(this.isMock);
    Assume.assumeTrue(this.enableLongRunning);
    getHost().setTimeoutSeconds((int) TimeUnit.MINUTES.toSeconds(10));
    // Important: MUST share same Endpoint between the two enum runs.
    final EndpointState endpointState = createEndpointState();
    ImageEnumerationTaskState task = kickOffImageEnumeration(endpointState, PUBLIC, AMAZON_PUBLIC_IMAGE_FILTER_ALL);
    // Validate at least 10K image states are created
    // NOTE: do not use queryDocumentsAndAssertExpectedCount
    // since it fails with 'Query returned large number of results'
    QueryByPages<ImageState> queryAll = new QueryByPages<ImageState>(getHost(), Builder.create().addKindFieldClause(ImageState.class).build(), ImageState.class, task.tenantLinks);
    queryAll.setMaxPageSize(QueryUtils.DEFAULT_MAX_RESULT_LIMIT);
    Long imagesCount = waitToComplete(queryAll.collectLinks(Collectors.counting()));
    Assert.assertTrue("Expected at least " + AMAZON_PUBLIC_IMAGES_ALL_COUNT + " images, but found only " + imagesCount, imagesCount > AMAZON_PUBLIC_IMAGES_ALL_COUNT);
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ImageEnumerationTaskState(com.vmware.photon.controller.model.tasks.ImageEnumerationTaskService.ImageEnumerationTaskState) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 25 with QueryByPages

use of com.vmware.photon.controller.model.query.QueryUtils.QueryByPages in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method queryNetworkStates.

/**
 * Query network states stored in the local document store based on the retrieved azure virtual
 * networks.
 */
private void queryNetworkStates(NetworkEnumContext context, NetworkEnumStages next) {
    logFine(() -> "Query Network States from local document store.");
    Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(NetworkState.class).addInClause(NetworkState.FIELD_NAME_ID, context.virtualNetworks.keySet());
    QueryByPages<NetworkState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), NetworkState.class, context.parentCompute.tenantLinks, null, /* endpoint */
    context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    queryLocalStates.queryDocuments(network -> context.networkStates.put(network.id, network)).whenComplete(thenHandleSubStage(context, next));
}
Also used : QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) LifecycleState(com.vmware.photon.controller.model.support.LifecycleState) StringUtils(org.apache.commons.lang3.StringUtils) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) VirtualNetworkListResult(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_INSTANCE_ADAPTER_REFERENCE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DEFAULT_INSTANCE_ADAPTER_REFERENCE) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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) NETWORK_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.NETWORK_REST_API_VERSION) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) Stream(java.util.stream.Stream) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) AddressSpace(com.vmware.photon.controller.model.adapters.azure.model.network.AddressSpace) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) LIST_VIRTUAL_NETWORKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceGroupStateType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.ResourceGroupStateType) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) BiConsumer(java.util.function.BiConsumer) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Subnet(com.vmware.photon.controller.model.adapters.azure.model.network.Subnet) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Query(com.vmware.xenon.services.common.QueryTask.Query) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Aggregations

QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)29 Query (com.vmware.xenon.services.common.QueryTask.Query)22 List (java.util.List)18 ArrayList (java.util.ArrayList)17 HashSet (java.util.HashSet)17 Collectors (java.util.stream.Collectors)16 Utils (com.vmware.xenon.common.Utils)15 HashMap (java.util.HashMap)15 Map (java.util.Map)15 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)14 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)14 DeferredResult (com.vmware.xenon.common.DeferredResult)14 Set (java.util.Set)14 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)14 PhotonModelUtils (com.vmware.photon.controller.model.resources.util.PhotonModelUtils)13 Operation (com.vmware.xenon.common.Operation)13 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)12 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)12 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)12 StringUtils (org.apache.commons.lang3.StringUtils)12