Skip to content

Commit 4cf11d5

Browse files
peter-zheng-gdzlier-gcp
authored andcommitted
[Asset] Add quickstart code for BatchGetAssetsHistory. (#1270)
* [Asset] Add quickstart code for BatchGetAssetsHistory. * [Asset] Minor fix: move comments.
1 parent a66361b commit 4cf11d5

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2018 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_batch_get_assets_history]
20+
// Imports the Google Cloud client library
21+
22+
import com.google.cloud.ServiceOptions;
23+
import com.google.cloud.asset.v1beta1.AssetServiceClient;
24+
import com.google.cloud.asset.v1beta1.BatchGetAssetsHistoryRequest;
25+
import com.google.cloud.asset.v1beta1.BatchGetAssetsHistoryResponse;
26+
import com.google.cloud.asset.v1beta1.ContentType;
27+
import com.google.cloud.asset.v1beta1.ProjectName;
28+
import com.google.cloud.asset.v1beta1.TimeWindow;
29+
import java.util.Arrays;
30+
31+
public class BatchGetAssetsHistoryExample {
32+
33+
// Use the default project Id.
34+
private static final String projectId = ServiceOptions.getDefaultProjectId();
35+
36+
// Export assets for a project.
37+
// @param args path where the results will be exported to.
38+
public static void main(String... args) throws Exception {
39+
// Asset names, e.g.: "//storage.googleapis.com/[BUCKET_NAME]"
40+
String[] assetNames = args[0].split(",");
41+
try (AssetServiceClient client = AssetServiceClient.create()) {
42+
ProjectName parent = ProjectName.of(projectId);
43+
ContentType contentType = ContentType.CONTENT_TYPE_UNSPECIFIED;
44+
TimeWindow readTimeWindow = TimeWindow.newBuilder().build();
45+
BatchGetAssetsHistoryRequest request = BatchGetAssetsHistoryRequest.newBuilder()
46+
.setParent(parent.toString())
47+
.addAllAssetNames(Arrays.asList(assetNames))
48+
.setContentType(contentType)
49+
.setReadTimeWindow(readTimeWindow)
50+
.build();
51+
BatchGetAssetsHistoryResponse response = client.batchGetAssetsHistory(request);
52+
System.out.println(response);
53+
}
54+
}
55+
}
56+
// [END asset_quickstart_batch_get_assets_history]

asset/cloud-client/src/main/java/com/example/asset/ExportAssetsExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.example.asset;
1818

19+
// [START asset_quickstart_export_assets]
20+
// Imports the Google Cloud client library
21+
1922
import com.google.cloud.ServiceOptions;
2023
import com.google.cloud.asset.v1beta1.AssetServiceClient;
2124
import com.google.cloud.asset.v1beta1.ExportAssetsRequest;
@@ -24,9 +27,6 @@
2427
import com.google.cloud.asset.v1beta1.OutputConfig;
2528
import com.google.cloud.asset.v1beta1.ProjectName;
2629

27-
// [START asset_quickstart_export_assets]
28-
// Imports the Google Cloud client library
29-
3030
public class ExportAssetsExample {
3131

3232
// Use the default project Id.

asset/cloud-client/src/test/java/com/example/asset/QuickStartIT.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ public void testExportAssetExample() throws Exception {
8181
String got = bout.toString();
8282
assertThat(got).contains(String.format("uri: \"%s\"", assetDumpPath));
8383
}
84+
85+
@Test
86+
public void testBatchGetAssetsHistory() throws Exception {
87+
String bucketAssetName = String.format("//storage.googleapis.com/%s", bucketName);
88+
BatchGetAssetsHistoryExample.main(bucketAssetName);
89+
String got = bout.toString();
90+
if (!got.isEmpty()) {
91+
assertThat(got).contains(bucketAssetName);
92+
}
93+
}
8494
}

0 commit comments

Comments
 (0)