use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.
the class SslTrustCertificateServiceUtils method loadCertificates.
static void loadCertificates(ServiceHost host, Consumer<SslTrustCertificateState> consumer) {
Query.Builder query = Query.Builder.create().addKindFieldClause(SslTrustCertificateState.class);
QueryStrategy<SslTrustCertificateState> queryLocalStates = new QueryUtils.QueryByPages<>(host, query.build(), SslTrustCertificateState.class, null, null).setMaxPageSize(QUERY_RESULT_LIMIT);
queryLocalStates.queryDocuments(c -> {
try {
host.log(Level.FINE, "Processing '%s'.", c);
SslTrustCertificateState sslTrustCert = Utils.fromJson(c, SslTrustCertificateState.class);
host.log(Level.FINE, "Certificate with '%s', issuer '%s' and alias '%s' loaded.", sslTrustCert.commonName, sslTrustCert.issuerName, sslTrustCert.getAlias());
consumer.accept(sslTrustCert);
} catch (Exception e) {
host.log(Level.WARNING, "cannot deserialize " + c);
}
});
}
use of com.vmware.xenon.services.common.QueryTask.Query 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);
}
use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.
the class QueryUtilsTest method doTest.
private void doTest(ComputeDescription cd, Set<String> expected, List<String> tenantLinks) {
Query queryForReferrers = QueryUtils.queryForReferrers(cd.documentSelfLink, ComputeState.class, ComputeState.FIELD_NAME_DESCRIPTION_LINK);
// The classes under testing: QueryByPages and QueryTop
List<QueryTemplate<?, ComputeState>> queryStrategies = Arrays.asList(new QueryByPages<>(getHost(), queryForReferrers, ComputeState.class, tenantLinks), new QueryTop<>(getHost(), queryForReferrers, ComputeState.class, tenantLinks));
// Test collectDocuments/queryDocuments/collectLinks/queryLinks per strategy
for (QueryTemplate<?, ComputeState> queryStrategy : queryStrategies) {
for (boolean isDirect : Arrays.asList(true, false)) {
final String msg = queryStrategy.getClass().getSimpleName() + ":" + isDirect;
{
// Test collectDocuments, which internally also tests queryDocuments
DeferredResult<Set<String>> documentLinksDR = queryStrategy.setDirect(isDirect).collectDocuments(mapping(cs -> cs.documentSelfLink, toSet()));
Set<String> actual = waitToComplete(documentLinksDR);
assertThat(msg, actual, equalTo(expected));
}
{
// Test collectLinks, which internally also tests queryLinks
DeferredResult<Set<String>> documentLinksDR = queryStrategy.setDirect(isDirect).collectLinks(toSet());
Set<String> actual = waitToComplete(documentLinksDR);
assertThat(msg, actual, equalTo(expected));
}
}
}
}
use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method disassociateDiskStates.
/*
* Disassociate local disk states that no longer exist in Azure
*
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle. The other
* data point this method uses is the blob discovered as part of get blob call.
*
* A disassociate on a disk state is invoked only if it meets two criteria: - Timestamp older
* than current enumeration cycle. - blob is not present on Azure.
*/
private void disassociateDiskStates(StorageEnumContext context, StorageEnumStages next) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_COMPUTE_HOST_LINK, context.parentCompute.documentSelfLink).addRangeClause(DiskState.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(context.enumerationStartTimeInMicros));
Query.Builder typeFilterQuery = Query.Builder.create(Occurance.MUST_OCCUR);
Query blobFilter = Query.Builder.create(Occurance.SHOULD_OCCUR).addFieldClause(AZURE_STORAGE_TYPE, AZURE_STORAGE_BLOBS).build();
QueryTask.Query diskFilter = QueryTask.Query.Builder.create(QueryTask.Query.Occurance.SHOULD_OCCUR).addFieldClause(AZURE_STORAGE_TYPE, AZURE_STORAGE_DISKS).build();
typeFilterQuery.addClause(blobFilter);
typeFilterQuery.addClause(diskFilter);
qBuilder.addClause(typeFilterQuery.build());
QueryByPages<DiskState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), DiskState.class, context.parentCompute.tenantLinks, context.request.endpointLink).setMaxPageSize(getQueryResultLimit());
queryLocalStates.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
List<DeferredResult<Operation>> ops = new ArrayList<>();
queryLocalStates.queryDocuments(ds -> {
if (context.blobIds.contains(ds.id)) {
return;
}
ops.add(disassociateIfNotAttachedToCompute(context, ds));
}).thenCompose(r -> DeferredResult.allOf(ops)).whenComplete((r, e) -> {
logFine(() -> "Finished disassociation of disk states for Azure");
context.subStage = next;
handleSubStage(context);
});
}
use of com.vmware.xenon.services.common.QueryTask.Query in project photon-model by vmware.
the class TagGroomerTaskService method getDiscoveredNotDeletedTagStates.
/**
* Collect all tag states with origins ["DISCOVERED"] that have not been soft deleted, and
* perform soft deletion of stale ones by page.
*
* If the tag has multiple origins, do not delete the tag.
*/
private void getDiscoveredNotDeletedTagStates(TagDeletionRequest task, SubStage next) {
Query query = Query.Builder.create().addKindFieldClause(TagState.class).build();
Map<String, Query.Occurance> origin = new HashMap<>();
origin.put(DISCOVERED.toString(), Query.Occurance.MUST_OCCUR);
origin.put(SYSTEM.toString(), Query.Occurance.MUST_NOT_OCCUR);
origin.put(USER_DEFINED.toString(), Query.Occurance.MUST_NOT_OCCUR);
Query externalQuery = createOriginTagQuery(Boolean.TRUE, origin);
query.addBooleanClause(externalQuery);
query.addBooleanClause(new Query().setTermPropertyName(TagState.FIELD_NAME_DELETED).setTermMatchValue(Boolean.FALSE.toString()));
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QueryOption.EXPAND_CONTENT).setResultLimit(QUERY_RESULT_LIMIT).build();
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((response, e) -> {
if (e != null) {
task.failureMessage = e.getMessage();
task.subStage = SubStage.FAILED;
sendSelfPatch(task);
return;
}
if (response.results != null && response.results.nextPageLink != null) {
task.tagsNextPageLink = response.results.nextPageLink;
task.subStage = next;
} else {
task.subStage = SubStage.FINISHED;
}
sendSelfPatch(task);
});
}
Aggregations