Search in sources :

Example 1 with SearchAllResourcesRequest

use of com.google.cloud.asset.v1.SearchAllResourcesRequest in project gapic-generator-java by googleapis.

the class AsyncSearchAllResources method asyncSearchAllResources.

public static void asyncSearchAllResources() throws Exception {
    // It may require modifications to work in your environment.
    try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
        SearchAllResourcesRequest request = SearchAllResourcesRequest.newBuilder().setScope("scope109264468").setQuery("query107944136").addAllAssetTypes(new ArrayList<String>()).setPageSize(883849137).setPageToken("pageToken873572522").setOrderBy("orderBy-1207110587").setReadMask(FieldMask.newBuilder().build()).build();
        ApiFuture<ResourceSearchResult> future = assetServiceClient.searchAllResourcesPagedCallable().futureCall(request);
        // Do something.
        for (ResourceSearchResult element : future.get().iterateAll()) {
        // doThingsWith(element);
        }
    }
}
Also used : AssetServiceClient(com.google.cloud.asset.v1.AssetServiceClient) SearchAllResourcesRequest(com.google.cloud.asset.v1.SearchAllResourcesRequest) ResourceSearchResult(com.google.cloud.asset.v1.ResourceSearchResult) ArrayList(java.util.ArrayList)

Example 2 with SearchAllResourcesRequest

use of com.google.cloud.asset.v1.SearchAllResourcesRequest in project gapic-generator-java by googleapis.

the class AssetServiceClientTest method searchAllResourcesTest.

@Test
public void searchAllResourcesTest() throws Exception {
    ResourceSearchResult responsesElement = ResourceSearchResult.newBuilder().build();
    SearchAllResourcesResponse expectedResponse = SearchAllResourcesResponse.newBuilder().setNextPageToken("").addAllResults(Arrays.asList(responsesElement)).build();
    mockAssetService.addResponse(expectedResponse);
    String scope = "scope109264468";
    String query = "query107944136";
    List<String> assetTypes = new ArrayList<>();
    SearchAllResourcesPagedResponse pagedListResponse = client.searchAllResources(scope, query, assetTypes);
    List<ResourceSearchResult> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getResultsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockAssetService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    SearchAllResourcesRequest actualRequest = ((SearchAllResourcesRequest) actualRequests.get(0));
    Assert.assertEquals(scope, actualRequest.getScope());
    Assert.assertEquals(query, actualRequest.getQuery());
    Assert.assertEquals(assetTypes, actualRequest.getAssetTypesList());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : SearchAllResourcesPagedResponse(com.google.cloud.asset.v1.AssetServiceClient.SearchAllResourcesPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with SearchAllResourcesRequest

use of com.google.cloud.asset.v1.SearchAllResourcesRequest in project gapic-generator-java by googleapis.

the class AsyncSearchAllResourcesPaged method asyncSearchAllResourcesPaged.

public static void asyncSearchAllResourcesPaged() throws Exception {
    // It may require modifications to work in your environment.
    try (AssetServiceClient assetServiceClient = AssetServiceClient.create()) {
        SearchAllResourcesRequest request = SearchAllResourcesRequest.newBuilder().setScope("scope109264468").setQuery("query107944136").addAllAssetTypes(new ArrayList<String>()).setPageSize(883849137).setPageToken("pageToken873572522").setOrderBy("orderBy-1207110587").setReadMask(FieldMask.newBuilder().build()).build();
        while (true) {
            SearchAllResourcesResponse response = assetServiceClient.searchAllResourcesCallable().call(request);
            for (ResourceSearchResult element : response.getResponsesList()) {
            // doThingsWith(element);
            }
            String nextPageToken = response.getNextPageToken();
            if (!Strings.isNullOrEmpty(nextPageToken)) {
                request = request.toBuilder().setPageToken(nextPageToken).build();
            } else {
                break;
            }
        }
    }
}
Also used : SearchAllResourcesResponse(com.google.cloud.asset.v1.SearchAllResourcesResponse) AssetServiceClient(com.google.cloud.asset.v1.AssetServiceClient) SearchAllResourcesRequest(com.google.cloud.asset.v1.SearchAllResourcesRequest) ResourceSearchResult(com.google.cloud.asset.v1.ResourceSearchResult) ArrayList(java.util.ArrayList)

Example 4 with SearchAllResourcesRequest

use of com.google.cloud.asset.v1.SearchAllResourcesRequest in project java-asset by googleapis.

the class SearchAllResourcesExample method searchAllResources.

// Searches for all the resources within the given scope.
public static void searchAllResources(String scope, String query) {
    // TODO(developer): Replace these variables before running the sample.
    String[] assetTypes = {};
    int pageSize = 0;
    String pageToken = "";
    String orderBy = "";
    SearchAllResourcesRequest request = SearchAllResourcesRequest.newBuilder().setScope(scope).setQuery(query).addAllAssetTypes(Arrays.asList(assetTypes)).setPageSize(pageSize).setPageToken(pageToken).setOrderBy(orderBy).build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AssetServiceClient client = AssetServiceClient.create()) {
        SearchAllResourcesPagedResponse response = client.searchAllResources(request);
        System.out.println("Search completed successfully:\n" + response.getPage().getValues());
    } catch (IOException e) {
        System.out.println(String.format("Failed to create client:%n%s", e.toString()));
    } catch (InvalidArgumentException e) {
        System.out.println(String.format("Invalid request:%n%s", e.toString()));
    } catch (ApiException e) {
        System.out.println(String.format("Error during SearchAllResources:%n%s", e.toString()));
    }
}
Also used : SearchAllResourcesPagedResponse(com.google.cloud.asset.v1.AssetServiceClient.SearchAllResourcesPagedResponse) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) SearchAllResourcesRequest(com.google.cloud.asset.v1.SearchAllResourcesRequest) AssetServiceClient(com.google.cloud.asset.v1.AssetServiceClient) IOException(java.io.IOException) ApiException(com.google.api.gax.rpc.ApiException)

Aggregations

AssetServiceClient (com.google.cloud.asset.v1.AssetServiceClient)3 SearchAllResourcesRequest (com.google.cloud.asset.v1.SearchAllResourcesRequest)3 ArrayList (java.util.ArrayList)3 SearchAllResourcesPagedResponse (com.google.cloud.asset.v1.AssetServiceClient.SearchAllResourcesPagedResponse)2 ResourceSearchResult (com.google.cloud.asset.v1.ResourceSearchResult)2 ApiException (com.google.api.gax.rpc.ApiException)1 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 SearchAllResourcesResponse (com.google.cloud.asset.v1.SearchAllResourcesResponse)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 IOException (java.io.IOException)1 Test (org.junit.Test)1