Search in sources :

Example 31 with Query

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

the class TestAzureEnumerationTask method getSubnetStates.

/**
 * Get all SubnetStates within passed NetworkState. In other words, get all subnet states that
 * refer the network state passed.
 */
// TODO : Duplicated from AWS TestUtils. Please advice where to put common test utils
public static List<SubnetState> getSubnetStates(VerificationHost host, NetworkState networkState) {
    Query queryForReferrers = QueryUtils.queryForReferrers(networkState.documentSelfLink, SubnetState.class, SubnetState.FIELD_NAME_NETWORK_LINK);
    QueryByPages<SubnetState> querySubnetStatesReferrers = new QueryByPages<>(host, queryForReferrers, SubnetState.class, networkState.tenantLinks, networkState.endpointLink);
    DeferredResult<List<SubnetState>> subnetDR = querySubnetStatesReferrers.collectDocuments(Collectors.toList());
    return waitToComplete(subnetDR);
}
Also used : QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Query(com.vmware.xenon.services.common.QueryTask.Query) PhotonModelUtils.createOriginTagQuery(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.createOriginTagQuery) ArrayList(java.util.ArrayList) List(java.util.List) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 32 with Query

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

the class TestAzureEnumerationTask method assertRemoteResources.

// Assert that remote resources are enumerated and exists locally.
private void assertRemoteResources() {
    assertResourceExists(this.host, NetworkService.FACTORY_LINK, NIC_SPEC.network.name, true);
    for (NicSpec nicSpec : NIC_SPEC.nicSpecs) {
        assertResourceExists(this.host, SubnetService.FACTORY_LINK, nicSpec.getSubnetSpec().name, true);
    }
    assertResourceExists(this.host, ResourceGroupService.FACTORY_LINK, azureVMName, true);
    assertResourceExists(this.host, SecurityGroupService.FACTORY_LINK, AZURE_SECURITY_GROUP_NAME + "-" + azureVMName, true);
    assertResourceExists(this.host, StorageDescriptionService.FACTORY_LINK, (azureVMName + "sa").replace("-", ""), true);
    assertDiskExist(this.host, DiskService.FACTORY_LINK, azureVMName + "-boot-disk", true);
    validateDiskInternalTag(this.host);
    // Tags
    final Map<String, String> expectedTags = new HashMap<>();
    expectedTags.put(NETWORK_TAG_KEY_PREFIX + azureVMName, NETWORK_TAG_VALUE);
    expectedTags.put(VM_TAG_KEY_PREFIX + azureVMName, VM_TAG_VALUE);
    expectedTags.put(SG_TAG_KEY_PREFIX + azureVMName, SG_TAG_VALUE);
    final List<String> keysToLowerCase = expectedTags.keySet().stream().map(String::toLowerCase).collect(Collectors.toList());
    Query query = Query.Builder.create().addKindFieldClause(TagState.class).addInClause(TagState.FIELD_NAME_KEY, keysToLowerCase).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);
    QueryStrategy<TagState> queryLocalTags = new QueryTop<>(getHost(), query, TagState.class, null).setMaxResultsLimit(expectedTags.size() + 1);
    List<TagState> tagStates = waitToComplete(queryLocalTags.collectDocuments(Collectors.toList()));
    this.host.log(Level.INFO, "external tag states discovered: " + tagStates.size());
    if (!AzureUtils.isAzureClientMock()) {
        assertEquals("TagStates were not discovered.", expectedTags.size(), tagStates.size());
    }
    for (TagState tag : tagStates) {
        assertEquals(expectedTags.get(tag.key), tag.value);
    }
}
Also used : Query(com.vmware.xenon.services.common.QueryTask.Query) PhotonModelUtils.createOriginTagQuery(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.createOriginTagQuery) HashMap(java.util.HashMap) AzureTestUtil.randomString(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.randomString) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) NicSpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec)

Example 33 with Query

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

the class TestAzureEnumerationTask method assertInternalTagResources.

/**
 * Verify internal tags are created.
 */
private void assertInternalTagResources() {
    final List<String> expectedTagValues = new ArrayList<>();
    expectedTagValues.add(NETWORK_TAG_TYPE_VALUE);
    expectedTagValues.add(SUBNET_TAG_TYPE_VALUE);
    expectedTagValues.add(NETWORK_INTERFACE_TAG_TYPE_VALUE);
    Query query = Query.Builder.create().addKindFieldClause(TagState.class).addFieldClause(TagState.FIELD_NAME_KEY, PhotonModelConstants.TAG_KEY_TYPE).build();
    Query externalQuery = new Query().setTermPropertyName(TagState.FIELD_NAME_EXTERNAL).setTermMatchValue(Boolean.FALSE.toString());
    externalQuery.occurance = Query.Occurance.SHOULD_OCCUR;
    Query originQuery = new Query().addBooleanClause(Query.Builder.create().addCollectionItemClause(TagState.FIELD_NAME_ORIGIN, DISCOVERED.toString(), Query.Occurance.SHOULD_OCCUR).addCollectionItemClause(TagState.FIELD_NAME_ORIGIN, SYSTEM.toString(), Query.Occurance.SHOULD_OCCUR).addCollectionItemClause(TagState.FIELD_NAME_ORIGIN, USER_DEFINED.toString(), Query.Occurance.MUST_NOT_OCCUR).build()).setOccurance(Query.Occurance.SHOULD_OCCUR);
    Query originOrExternalQuery = new Query().addBooleanClause(externalQuery).addBooleanClause(originQuery).setOccurance(Query.Occurance.MUST_OCCUR);
    query.addBooleanClause(originOrExternalQuery);
    QueryStrategy<TagState> queryLocalTags = new QueryTop<>(getHost(), query, TagState.class, null).setMaxResultsLimit(20);
    List<TagState> tagStates = waitToComplete(queryLocalTags.collectDocuments(Collectors.toList()));
    this.host.log(Level.INFO, "internal tag states discovered: " + tagStates.size());
    assertTrue(tagStates.size() >= 3);
    final List<String> actualTagValues = new ArrayList<>();
    for (TagState tag : tagStates) {
        assertNotNull(tag);
        actualTagValues.add(tag.value);
    }
    // assert check if every expected tag value is present in actual values list.
    for (String tagValue : expectedTagValues) {
        assertTrue(actualTagValues.contains(tagValue));
        actualTagValues.remove(tagValue);
    }
    // verify tag values no more exist in list that verifies duplicate tags are not created
    for (String tagValue : expectedTagValues) {
        assertFalse(actualTagValues.contains(tagValue));
    }
}
Also used : Query(com.vmware.xenon.services.common.QueryTask.Query) PhotonModelUtils.createOriginTagQuery(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.createOriginTagQuery) ArrayList(java.util.ArrayList) AzureTestUtil.randomString(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.randomString) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)

Example 34 with Query

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

the class EndpointAdapterUtils method getEndpointLinks.

private static DeferredResult<List<String>> getEndpointLinks(ServiceHost host, Query endpointQuery, List<String> credentialsLinks, String endpointType, List<String> queryTaskTenantLinks) {
    if (credentialsLinks.isEmpty()) {
        return DeferredResult.completed(Collections.emptyList());
    }
    Query.Builder qBuilder = Builder.create().addKindFieldClause(EndpointState.class).addFieldClause(EndpointState.FIELD_NAME_ENDPOINT_TYPE, endpointType).addInClause(EndpointState.FIELD_NAME_AUTH_CREDENTIALS_LINK, credentialsLinks);
    if (endpointQuery != null) {
        qBuilder.addClause(endpointQuery);
    }
    QueryTop<EndpointState> queryEndpoints = new QueryTop<>(host, qBuilder.build(), EndpointState.class, queryTaskTenantLinks).setQueryTaskTenantLinks(queryTaskTenantLinks).setMaxResultsLimit(1);
    queryEndpoints.setClusterType(INVENTORY_SERVICE);
    return queryEndpoints.collectLinks(Collectors.toList());
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Query(com.vmware.xenon.services.common.QueryTask.Query) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop)

Example 35 with Query

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

the class EndpointAdapterUtils method getAuthLinks.

private static DeferredResult<List<String>> getAuthLinks(ServiceHost host, Query authQuery, List<String> queryTaskTenantLinks) {
    Query.Builder authQueryBuilder = Builder.create().addKindFieldClause(AuthCredentialsServiceState.class);
    if (authQuery != null) {
        authQueryBuilder.addClause(authQuery);
    }
    QueryTop<AuthCredentialsServiceState> queryAuth = new QueryTop<>(host, authQueryBuilder.build(), AuthCredentialsServiceState.class, queryTaskTenantLinks).setQueryTaskTenantLinks(queryTaskTenantLinks);
    queryAuth.setClusterType(INVENTORY_SERVICE);
    return queryAuth.collectLinks(Collectors.toList());
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Query(com.vmware.xenon.services.common.QueryTask.Query) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder)

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