Search in sources :

Example 1 with QueryTop

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

the class AzureSubscriptionEndpointsEnumerationService method fetchExistingSubscriptionEndpoints.

private void fetchExistingSubscriptionEndpoints(AzureSubscriptionEndpointsEnumerationContext enumerationContext, AzureSubscriptionEndpointComputeEnumerationStages nextStage) {
    Query azureEndpointsQuery = createQueryForAzureSubscriptionEndpoints(enumerationContext);
    // Use Top query with 10K max EndpointStates
    QueryTop<EndpointState> querySubscriptionEndpoints = new QueryTop<>(getHost(), azureEndpointsQuery, EndpointState.class, enumerationContext.parent.tenantLinks);
    querySubscriptionEndpoints.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    querySubscriptionEndpoints.queryDocuments(epState -> {
        if (epState.endpointProperties != null && epState.endpointProperties.containsKey(EndpointConfigRequest.USER_LINK_KEY)) {
            String subscriptionUuid = epState.endpointProperties.get(EndpointConfigRequest.USER_LINK_KEY);
            enumerationContext.idToSubscription.remove(subscriptionUuid);
        }
    }).whenComplete((aVoid, t) -> {
        if (t != null) {
            getFailureConsumer(enumerationContext).accept(t);
            return;
        }
        enumerationContext.stage = nextStage;
        handleAzureCostComputeEnumerationRequest(enumerationContext);
    });
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) Operation(com.vmware.xenon.common.Operation) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) BaseAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext) Collectors(java.util.stream.Collectors) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) ResourceRequest(com.vmware.photon.controller.model.adapterapi.ResourceRequest) Consumer(java.util.function.Consumer) Utils(com.vmware.xenon.common.Utils) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) AzureSubscriptionEndpointCreationRequest(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionEndpointCreationService.AzureSubscriptionEndpointCreationRequest) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) Map(java.util.Map) UriUtils(com.vmware.xenon.common.UriUtils) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription) OperationJoin(com.vmware.xenon.common.OperationJoin) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Query(com.vmware.xenon.services.common.QueryTask.Query) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop)

Example 2 with QueryTop

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

the class AWSSecurityGroupEnumerationAdapterService method createResponse.

/**
 * Having the enumerated SecurityGroup Ids, query the States and provide them in the response
 */
private DeferredResult<AWSSecurityGroupEnumerationResponse> createResponse(SecurityGroupEnumContext context) {
    AWSSecurityGroupEnumerationResponse response = new AWSSecurityGroupEnumerationResponse();
    if (context.enumExternalResourcesIds == null || context.enumExternalResourcesIds.isEmpty()) {
        DeferredResult<AWSSecurityGroupEnumerationResponse> deferredResult = new DeferredResult<>();
        deferredResult.complete(response);
        return deferredResult;
    }
    Query.Builder findSecurityGroupStates = Builder.create().addKindFieldClause(SecurityGroupState.class).addFieldClause(ResourceState.FIELD_NAME_COMPUTE_HOST_LINK, context.request.parentCompute.documentSelfLink).addInClause(SecurityGroupState.FIELD_NAME_ID, context.enumExternalResourcesIds);
    QueryTop<SecurityGroupState> querySecurityGroupStates = new QueryTop<>(context.service.getHost(), findSecurityGroupStates.build(), SecurityGroupState.class, context.request.parentCompute.tenantLinks).setMaxResultsLimit(context.enumExternalResourcesIds.size());
    querySecurityGroupStates.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    return querySecurityGroupStates.queryDocuments(sgState -> response.securityGroupStates.put(sgState.id, sgState.documentSelfLink)).thenApply(aVoid -> response);
}
Also used : ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) AWS_SECURITY_GROUP_ADAPTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths.AWS_SECURITY_GROUP_ADAPTER) BaseComputeEnumerationAdapterContext(com.vmware.photon.controller.model.adapters.util.enums.BaseComputeEnumerationAdapterContext) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) Map(java.util.Map) DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AssertUtil(com.vmware.photon.controller.model.util.AssertUtil) AWSSecurityGroupUtils.generateSecurityRuleFromAWSIpPermission(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupUtils.generateSecurityRuleFromAWSIpPermission) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) StatelessService(com.vmware.xenon.common.StatelessService) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) TagService(com.vmware.photon.controller.model.resources.TagService) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) AWSResourceType.ec2_security_group(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType.ec2_security_group) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) IpPermission(com.amazonaws.services.ec2.model.IpPermission) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) AWSDeferredResultAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler) FACTORY_LINK(com.vmware.photon.controller.model.resources.SecurityGroupService.FACTORY_LINK) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) Query(com.vmware.xenon.services.common.QueryTask.Query) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 3 with QueryTop

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

the class TestAzureImageEnumerationTask method testPrivateImageEnumeration_single.

/**
 * That's the private image we are testing:
 * {@link /resourceGroups/Images/providers/Microsoft.Compute/images/LinuxImageWithOsAndDataUnmanaged/overview}
 */
@Test
@Ignore("For now run the test manually. Will enable it once the image is created programatically, but not hardcoded")
public void testPrivateImageEnumeration_single() throws Throwable {
    Assume.assumeFalse(this.isMock);
    kickOffImageEnumeration(this.endpointState, PRIVATE, AZURE_ALL_IMAGES_FILTER);
    // Validate at least 1 image state is CREATED
    QueryTop<ImageState> queryAll = new QueryTop<ImageState>(getHost(), Builder.create().addKindFieldClause(ImageState.class).build(), ImageState.class, this.endpointState.tenantLinks, this.endpointState.documentSelfLink);
    List<ImageState> images = PhotonModelUtils.waitToComplete(queryAll.collectDocuments(Collectors.toList()));
    Assert.assertTrue("Expected at least " + 1 + " private image, but found " + images.size(), images.size() >= 1);
    ImageState image = images.stream().filter(imageState -> {
        return imageState.name.equalsIgnoreCase(PRIVATE_IMAGE_NAME);
    }).findFirst().orElse(null);
    // Validate created image is correctly populated
    Assert.assertNotNull("Private image with '" + PRIVATE_IMAGE_NAME + "' name must have been enumerated.", image);
    Assert.assertNull("Private image must NOT have endpointType set.", image.endpointType);
    Assert.assertEquals("Private image must have endpointLink set.", this.endpointState.documentSelfLink, image.endpointLink);
    Assert.assertNotNull("Private image must have endpointLinks set.", image.endpointLinks);
    Assert.assertTrue("Private image must have endpointLinks set.", image.endpointLinks.contains(this.endpointState.documentSelfLink));
    Assert.assertEquals("Private image must have tenantLinks set.", this.endpointState.tenantLinks, image.tenantLinks);
    Assert.assertTrue("Image.id is invalid", image.id.endsWith(PRIVATE_IMAGE_NAME));
    Assert.assertEquals("Image.name is invalid", PRIVATE_IMAGE_NAME, image.name);
    Assert.assertEquals("Image.description is invalid", PRIVATE_IMAGE_NAME, image.description);
    Assert.assertNotNull("Image.diskConfigs", image.diskConfigs);
    Assert.assertEquals("Image.diskConfigs.size", 2, image.diskConfigs.size());
    {
        DiskConfiguration osDiskConfig = image.diskConfigs.get(0);
        Assert.assertNotNull("Image.osDiskConfig.properties", osDiskConfig.properties);
        Assert.assertNotNull("Image.osDiskConfig.properties.blobUri", osDiskConfig.properties.get(AzureConstants.AZURE_DISK_BLOB_URI));
        Assert.assertNull("Image.osDiskConfig.properties.lun", osDiskConfig.properties.get(AzureConstants.AZURE_DISK_LUN));
    }
    {
        DiskConfiguration dataDiskConfig = image.diskConfigs.get(1);
        Assert.assertNotNull("Image.dataDiskConfig.properties", dataDiskConfig.properties);
        Assert.assertNotNull("Image.dataDiskConfig.properties.blobUri", dataDiskConfig.properties.get(AzureConstants.AZURE_DISK_BLOB_URI));
        Assert.assertEquals("Image.dataDiskConfig.properties.lun", "0", dataDiskConfig.properties.get(AzureConstants.AZURE_DISK_LUN));
    }
}
Also used : DiskConfiguration(com.vmware.photon.controller.model.resources.ImageService.ImageState.DiskConfiguration) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) Ignore(org.junit.Ignore) AzureBaseTest(com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest) Test(org.junit.Test)

Example 4 with QueryTop

use of com.vmware.photon.controller.model.query.QueryUtils.QueryTop 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 5 with QueryTop

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

the class ResourceOperationUtils method lookUp.

/**
 * Lookup for {@link ResourceOperationSpec}s by given {@code endpointType},
 * {@code resourceType} and optionally {@code operation}
 * <p>
 * If operation not specified then return all resource operation specs for the given
 * {@code endpointType} and {@code resourceType}
 * @param host
 *         host to use to create operation
 * @param refererURI
 *         the referer to use when send the operation
 * @param endpointType
 *         the resource's endpoint type
 * @param resourceType
 *         the resource type
 * @param operation
 *         optional operation id argument
 * @param queryTaskTenantLinks
 *         tenant links used for the QueryTask
 * @param authorizationContext
 *         authorization context that will be used for operations (if set to null the context
 *         will not be changed)
 * @return
 */
private static DeferredResult<List<ResourceOperationSpec>> lookUp(ServiceHost host, URI refererURI, String endpointType, ResourceType resourceType, String operation, List<String> queryTaskTenantLinks, AuthorizationContext authorizationContext) {
    Query.Builder builder = Query.Builder.create().addKindFieldClause(ResourceOperationSpec.class).addFieldClause(ResourceOperationSpec.FIELD_NAME_ENDPOINT_TYPE, endpointType).addFieldClause(ResourceOperationSpec.FIELD_NAME_RESOURCE_TYPE, resourceType);
    if (operation != null) {
        builder.addFieldClause(ResourceOperationSpec.FIELD_NAME_OPERATION, operation);
    }
    Query query = builder.build();
    QueryTop<ResourceOperationSpec> top = new QueryTop<>(host, query, ResourceOperationSpec.class, null).setQueryTaskTenantLinks(queryTaskTenantLinks).setAuthorizationContext(authorizationContext);
    if (operation != null) {
        // resource operation spec id and selfLink are built from the endpoint type, resource
        // type and operation id, so the query result is guaranteed to return at most 1 element
        top.setMaxResultsLimit(1);
    }
    top.setReferer(refererURI);
    return top.collectDocuments(Collectors.toList());
}
Also used : Query(com.vmware.xenon.services.common.QueryTask.Query) ResourceOperationSpec(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop)

Aggregations

QueryTop (com.vmware.photon.controller.model.query.QueryUtils.QueryTop)8 Query (com.vmware.xenon.services.common.QueryTask.Query)5 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)4 List (java.util.List)3 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)2 AzureUriPaths (com.vmware.photon.controller.model.adapters.azure.AzureUriPaths)2 ImageState (com.vmware.photon.controller.model.resources.ImageService.ImageState)2 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)2 Operation (com.vmware.xenon.common.Operation)2 StatelessService (com.vmware.xenon.common.StatelessService)2 Utils (com.vmware.xenon.common.Utils)2 Builder (com.vmware.xenon.services.common.QueryTask.Query.Builder)2 Map (java.util.Map)2 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)1 DescribeSecurityGroupsRequest (com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest)1 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)1 IpPermission (com.amazonaws.services.ec2.model.IpPermission)1 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)1 Tag (com.amazonaws.services.ec2.model.Tag)1 Page (com.microsoft.azure.Page)1