Search in sources :

Example 6 with Query

use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.

the class AzureSubscriptionsEnumerationService method fetchExistingResources.

private void fetchExistingResources(AzureSubscriptionsEnumerationContext enumerationContext, AzureCostComputeEnumerationStages nextStage) {
    Query azureComputesQuery = createQueryForAzureSubscriptionComputes(enumerationContext);
    QueryByPages<ComputeState> querySubscriptionsComputes = new QueryByPages<>(getHost(), azureComputesQuery, ComputeState.class, enumerationContext.parent.tenantLinks);
    querySubscriptionsComputes.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    // Use max page size cause we collect ComputeStates
    querySubscriptionsComputes.setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT);
    querySubscriptionsComputes.queryDocuments(computeState -> {
        if (computeState.customProperties != null && computeState.customProperties.containsKey(AzureConstants.AZURE_SUBSCRIPTION_ID_KEY)) {
            String subscriptionUuid = computeState.customProperties.get(AzureConstants.AZURE_SUBSCRIPTION_ID_KEY);
            enumerationContext.idToSubscription.remove(subscriptionUuid);
        }
    }).whenComplete((aVoid, t) -> {
        if (t != null) {
            getFailureConsumer(enumerationContext).accept(t);
            return;
        }
        enumerationContext.stage = nextStage;
        handleAzureSubscriptionsEnumerationRequest(enumerationContext);
    });
}
Also used : QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) HashMap(java.util.HashMap) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) BaseAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ResourceRequest(com.vmware.photon.controller.model.adapterapi.ResourceRequest) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) Utils(com.vmware.xenon.common.Utils) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription) URI(java.net.URI) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) 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)

Example 7 with Query

use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method querySubnetStates.

/**
 * Query subnet states stored in the local document store based on the retrieved azure subnets.
 */
private void querySubnetStates(NetworkEnumContext context, NetworkEnumStages next) {
    if (context.subnets == null || context.subnets.isEmpty()) {
        handleSubStage(context, next);
        return;
    }
    logFine(() -> "Query Subnet States from local document store.");
    Builder qBuilder = Query.Builder.create().addKindFieldClause(SubnetState.class).addInClause(SubnetState.FIELD_NAME_ID, context.subnets.keySet());
    QueryByPages<SubnetState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), SubnetState.class, context.parentCompute.tenantLinks, null, /* endpoint */
    context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    queryLocalStates.queryDocuments(subnet -> context.subnetStates.put(subnet.id, subnet.documentSelfLink)).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) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 8 with Query

use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method queryResourceGroupStates.

/**
 * Query resource group states stored in the local document store based on the retrieved azure
 * virtual networks.
 */
private void queryResourceGroupStates(NetworkEnumContext context, NetworkEnumStages next) {
    List<String> resourceGroupIds = context.virtualNetworks.values().stream().map(vNet -> AzureUtils.getResourceGroupId(vNet.id)).collect(Collectors.toList());
    Query.Builder qBuilder = Builder.create().addKindFieldClause(ResourceGroupState.class).addInClause(ResourceGroupState.FIELD_NAME_ID, resourceGroupIds).addCompositeFieldClause(ResourceGroupState.FIELD_NAME_CUSTOM_PROPERTIES, ComputeProperties.RESOURCE_TYPE_KEY, ResourceGroupStateType.AzureResourceGroup.name());
    QueryByPages<ResourceGroupState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), ResourceGroupState.class, context.parentCompute.tenantLinks, null, /* endpoint */
    context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    queryLocalStates.queryDocuments(rg -> context.resourceGroupStates.put(rg.id, rg.documentSelfLink)).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) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder)

Example 9 with Query

use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.

the class TestVSphereImageEnumerationTask method testRefresh.

@Test
public void testRefresh() throws Throwable {
    // Create a resource pool where the VM will be housed
    this.resourcePool = createResourcePool();
    this.auth = createAuth();
    this.computeHostDescription = createComputeDescription();
    this.computeHost = createComputeHost(this.computeHostDescription);
    EndpointState ep = createEndpointState(this.computeHost, this.computeHostDescription);
    this.endpoint = TestUtils.doPost(this.host, ep, EndpointState.class, UriUtils.buildUri(this.host, EndpointService.FACTORY_LINK));
    doRefresh();
    captureFactoryState("initial");
    Query q = Query.Builder.create().addKindFieldClause(ImageState.class).build();
    ImageState anImage = null;
    try {
        anImage = findFirstMatching(q, ImageState.class);
    } catch (Exception ignore) {
    }
    if (anImage != null) {
        assertFalse(anImage.tenantLinks.isEmpty());
    }
    doRefresh();
    captureFactoryState("updated");
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Query(com.vmware.xenon.services.common.QueryTask.Query) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) Test(org.junit.Test)

Example 10 with Query

use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.

the class SslTrustCertificateServiceUtils method getQueryTask.

private static QueryTask getQueryTask() {
    Query computeQuery = Query.Builder.create().addKindFieldClause(SslTrustCertificateState.class).build();
    QueryTask task = QueryTask.Builder.create().addOption(QueryOption.CONTINUOUS).addOption(QueryOption.EXPAND_CONTENT).setQuery(computeQuery).build();
    task.documentSelfLink = QUERY_TASK_SELF_LINK_PREFIX;
    task.documentExpirationTimeMicros = Utils.fromNowMicrosUtc(TimeUnit.DAYS.toMicros(QUERY_TASK_EXPIRATION_DAYS));
    return task;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) SslTrustCertificateState(com.vmware.photon.controller.model.security.service.SslTrustCertificateService.SslTrustCertificateState)

Aggregations

Query (com.vmware.xenon.services.common.QueryTask.Query)81 QueryTask (com.vmware.xenon.services.common.QueryTask)50 Operation (com.vmware.xenon.common.Operation)39 ArrayList (java.util.ArrayList)29 HashMap (java.util.HashMap)26 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)25 Utils (com.vmware.xenon.common.Utils)22 List (java.util.List)21 Map (java.util.Map)21 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)20 UriUtils (com.vmware.xenon.common.UriUtils)20 HashSet (java.util.HashSet)20 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)19 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)18 Set (java.util.Set)18 URI (java.net.URI)17 OperationJoin (com.vmware.xenon.common.OperationJoin)16 StatelessService (com.vmware.xenon.common.StatelessService)16 Collections (java.util.Collections)15 Collectors (java.util.stream.Collectors)15