Skip to content

Commit ace11d8

Browse files
Larittic-GGminherz
authored andcommitted
samples: Add sample code for Cloud Assets Inventory ListAssets v1p5beta1 APIs. (#3408)
* Add sample code for Cloud Assets Inventory ListAssets v1p5beta1 APIs. * Fix lint. * Revamped according to style guide. * Add a missing function call in listAssets().
1 parent 0c2996e commit ace11d8

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.ServiceOptions;
22+
import com.google.cloud.asset.v1p5beta1.ContentType;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
/** Tests for list assets sample. */
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class ListAssets {
36+
private ByteArrayOutputStream bout;
37+
38+
@Before
39+
public void setUp() {
40+
bout = new ByteArrayOutputStream();
41+
System.setOut(new PrintStream(bout));
42+
}
43+
44+
@After
45+
public void tearDown() {
46+
System.setOut(null);
47+
bout.reset();
48+
}
49+
50+
@Test
51+
public void testListAssetsExample() throws Exception {
52+
// Use the default project Id (configure it by setting environment variable
53+
// "GOOGLE_CLOUD_PROJECT").
54+
String projectId = ServiceOptions.getDefaultProjectId();
55+
String[] assetTypes = {"storage.googleapis.com/Bucket", "bigquery.googleapis.com/Table"};
56+
ContentType contentType = ContentType.CONTENT_TYPE_UNSPECIFIED;
57+
ListAssetsExample.listAssets(projectId, assetTypes, contentType);
58+
String got = bout.toString();
59+
if (!got.isEmpty()) {
60+
assertThat(got).contains("asset");
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)