|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.asset; |
| 18 | + |
| 19 | +// [START asset_quickstart_list_assets] |
| 20 | +// Imports the Google Cloud client library |
| 21 | + |
| 22 | +import com.google.cloud.ServiceOptions; |
| 23 | +import com.google.cloud.asset.v1.ProjectName; |
| 24 | +import com.google.cloud.asset.v1p5beta1.AssetServiceClient; |
| 25 | +import com.google.cloud.asset.v1p5beta1.AssetServiceClient.ListAssetsPagedResponse; |
| 26 | +import com.google.cloud.asset.v1p5beta1.ContentType; |
| 27 | +import com.google.cloud.asset.v1p5beta1.ListAssetsRequest; |
| 28 | +import java.util.Arrays; |
| 29 | + |
| 30 | +public class ListAssetsExample { |
| 31 | + |
| 32 | + public static void listAssets() throws Exception { |
| 33 | + // The project id of the asset parent to list. |
| 34 | + String projectId = "YOUR_PROJECT_ID"; |
| 35 | + // The asset types to list. E.g., |
| 36 | + // ["storage.googleapis.com/Bucket", "bigquery.googleapis.com/Table"]. |
| 37 | + // See full list of supported asset types at |
| 38 | + // https://cloud.google.com/asset-inventory/docs/supported-asset-types. |
| 39 | + String[] assetTypes = {"YOUR_ASSET_TYPES_TO_LIST"}; |
| 40 | + // The asset content type to list. E.g., ContentType.CONTENT_TYPE_UNSPECIFIED. |
| 41 | + // See full list of content types at |
| 42 | + // https://cloud.google.com/asset-inventory/docs/reference/rpc/google.cloud.asset.v1p5beta1#contenttype |
| 43 | + ContentType contentType = ContentType.CONTENT_TYPE_UNSPECIFIED; |
| 44 | + listAssets(projectId, assetTypes, contentType); |
| 45 | + } |
| 46 | + |
| 47 | + public static void listAssets(String projectId, String[] assetTypes, ContentType contentType) |
| 48 | + throws Exception { |
| 49 | + try (AssetServiceClient client = AssetServiceClient.create()) { |
| 50 | + ProjectName parent = ProjectName.of(projectId); |
| 51 | + |
| 52 | + // Build initial ListAssetsRequest without setting page token. |
| 53 | + ListAssetsRequest request = ListAssetsRequest.newBuilder() |
| 54 | + .setParent(parent.toString()) |
| 55 | + .addAllAssetTypes(Arrays.asList(assetTypes)) |
| 56 | + .setContentType(contentType) |
| 57 | + .build(); |
| 58 | + |
| 59 | + // Repeatedly call ListAssets until page token is empty. |
| 60 | + ListAssetsPagedResponse response = client.listAssets(request); |
| 61 | + System.out.println(response); |
| 62 | + while (!response.getNextPageToken().isEmpty()) { |
| 63 | + request = request.toBuilder() |
| 64 | + .setPageToken(response.getNextPageToken()).build(); |
| 65 | + response = client.listAssets(request); |
| 66 | + System.out.println(response); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +// [END asset_quickstart_list_assets] |
0 commit comments