Search in sources :

Example 26 with QueryTask

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

the class TestVSphereLinkedCloneProvisionTask method findImage.

private String findImage() {
    QueryTask.Query q = QueryTask.Query.Builder.create().addKindFieldClause(ImageService.ImageState.class).addFieldClause(ImageService.ImageState.FIELD_NAME_NAME, "*" + this.libraryItemName, QueryTask.QueryTerm.MatchType.WILDCARD).build();
    QueryTask task = QueryTask.Builder.createDirectTask().setQuery(q).build();
    Operation op = QueryUtils.createQueryTaskOperation(this.host, task, ServiceTypeCluster.INVENTORY_SERVICE);
    Operation result = this.host.waitForResponse(op);
    try {
        return result.getBody(QueryTask.class).results.documentLinks.get(0);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
        return null;
    }
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Operation(com.vmware.xenon.common.Operation) ImageService(com.vmware.photon.controller.model.resources.ImageService)

Example 27 with QueryTask

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

the class ServerX509TrustManager method certificateChanged.

private void certificateChanged(Operation operation) {
    Utils.log(getClass(), getClass().getName(), Level.WARNING, "process certificate changed for operation %s", operation.toLogString());
    QueryTask queryTask = operation.getBody(QueryTask.class);
    if (queryTask.results != null && queryTask.results.documentLinks != null && !queryTask.results.documentLinks.isEmpty()) {
        queryTask.results.documents.values().forEach(doc -> {
            SslTrustCertificateState cert = Utils.fromJson(doc, SslTrustCertificateState.class);
            if (Action.DELETE.toString().equals(cert.documentUpdateAction)) {
                deleteCertificate(cert.getAlias());
            } else {
                registerCertificate(cert);
            }
        });
    } else {
        Utils.log(getClass(), getClass().getName(), Level.WARNING, "No document links for operation %s", operation.toLogString());
    }
    operation.complete();
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) SslTrustCertificateState(com.vmware.photon.controller.model.security.service.SslTrustCertificateService.SslTrustCertificateState)

Example 28 with QueryTask

use of com.vmware.xenon.services.common.QueryTask 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)

Example 29 with QueryTask

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

the class ResourceUtilsTest method testExpandTags.

@Test
public void testExpandTags() throws Throwable {
    TagState tag1 = new TagState();
    tag1.key = "A";
    tag1.value = "1";
    tag1 = postServiceSynchronously(TagService.FACTORY_LINK, tag1, TagState.class);
    TagState tag2 = new TagState();
    tag2.key = "A";
    tag2.value = "2";
    tag2 = postServiceSynchronously(TagService.FACTORY_LINK, tag2, TagState.class);
    TagState tag3 = new TagState();
    tag3.key = "A";
    tag3.value = "3";
    tag3 = postServiceSynchronously(TagService.FACTORY_LINK, tag3, TagState.class);
    // validate expansion on POST
    ComputeState compute = new ComputeState();
    compute.descriptionLink = "cdLink";
    compute.tagLinks = new HashSet<>();
    compute.tagLinks.add(tag1.documentSelfLink);
    compute.tagLinks.add(tag2.documentSelfLink);
    compute = postServiceSynchronously(ComputeService.FACTORY_LINK, compute, ComputeState.class);
    Collection<String> tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2")));
    // validate tags cannot be modified directly
    compute.expandedTags.remove(1);
    assertEquals(1, compute.expandedTags.size());
    putServiceSynchronously(compute.documentSelfLink, compute);
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2")));
    // validate expansion on PUT
    compute.tagLinks.remove(tag2.documentSelfLink);
    compute.tagLinks.add(tag3.documentSelfLink);
    putServiceSynchronously(compute.documentSelfLink, compute);
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n3")));
    // validate expansion on PATCH
    ComputeState patchState = new ComputeState();
    patchState.tagLinks = new HashSet<>();
    patchState.tagLinks.add(tag2.documentSelfLink);
    compute = patchServiceSynchronously(compute.documentSelfLink, patchState, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(3, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2", "A\n3")));
    // validate expansion through custom PATCH body
    Map<String, Collection<Object>> itemsToRemove = new HashMap<>();
    itemsToRemove.put(ResourceState.FIELD_NAME_TAG_LINKS, Arrays.asList(tag2.documentSelfLink, tag3.documentSelfLink));
    patchServiceSynchronously(compute.documentSelfLink, ServiceStateCollectionUpdateRequest.create(null, itemsToRemove));
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(1, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1")));
    // validate query (case-insensitive) (Note: only 1 tag can be found with Xenon 1.6.1)
    Query tagQuery = Query.Builder.create().addFieldClause(TagInfo.COMPOSITE_FIELD_NAME_TAG, "a*", MatchType.WILDCARD).build();
    QueryTask tagQueryTask = QueryTask.Builder.createDirectTask().setQuery(tagQuery).addOption(QueryOption.EXPAND_CONTENT).build();
    tagQueryTask = postServiceSynchronously(ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, tagQueryTask, QueryTask.class);
    assertEquals(1, tagQueryTask.results.documentLinks.size());
    assertEquals(1, tagQueryTask.results.documents.size());
    assertEquals(compute.documentSelfLink, tagQueryTask.results.documentLinks.get(0));
    ComputeState foundCompute = Utils.fromJson(tagQueryTask.results.documents.values().iterator().next(), ComputeState.class);
    assertEquals(1, foundCompute.expandedTags.size());
    assertEquals("A\n1", foundCompute.expandedTags.get(0).tag);
}
Also used : Arrays(java.util.Arrays) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) QueryTask(com.vmware.xenon.services.common.QueryTask) HashMap(java.util.HashMap) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) ArrayList(java.util.ArrayList) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) Utils(com.vmware.xenon.common.Utils) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Assert.assertNull(org.junit.Assert.assertNull) TagInfo(com.vmware.photon.controller.model.resources.ResourceState.TagInfo) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) Assert.assertEquals(org.junit.Assert.assertEquals) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) HashMap(java.util.HashMap) Collection(java.util.Collection) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 30 with QueryTask

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

the class BaseModelTest method createDirectQueryTask.

public QueryTask createDirectQueryTask(String kind, String propertyName, String propertyValue) {
    QueryTask q = new QueryTask();
    q.querySpec = new QueryTask.QuerySpecification();
    q.taskInfo.isDirect = true;
    QueryTask.Query kindClause = new QueryTask.Query().setTermPropertyName(ServiceDocument.FIELD_NAME_KIND).setTermMatchValue(kind);
    kindClause.occurance = QueryTask.Query.Occurance.MUST_OCCUR;
    q.querySpec.query.addBooleanClause(kindClause);
    QueryTask.Query customPropClause = new QueryTask.Query().setTermPropertyName(propertyName).setTermMatchValue(propertyValue);
    customPropClause.occurance = QueryTask.Query.Occurance.MUST_OCCUR;
    q.querySpec.query.addBooleanClause(customPropClause);
    return q;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask)

Aggregations

QueryTask (com.vmware.xenon.services.common.QueryTask)147 Query (com.vmware.xenon.services.common.QueryTask.Query)62 Operation (com.vmware.xenon.common.Operation)61 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)39 ArrayList (java.util.ArrayList)26 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)20 List (java.util.List)20 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)19 UriUtils (com.vmware.xenon.common.UriUtils)18 Utils (com.vmware.xenon.common.Utils)17 HashSet (java.util.HashSet)16 HashMap (java.util.HashMap)14 QueryOption (com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption)12 TimeUnit (java.util.concurrent.TimeUnit)12 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)11 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)11 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)11 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)11 URI (java.net.URI)11 Set (java.util.Set)11