Skip to content

Commit 56377d9

Browse files
lvvvvvfNeenu1995gcf-owl-bot[bot]
authored
samples: Add relationship and per-type bigquery samples to Cloud Asset service (#882)
* Add relationship and per-type Bigquery samples * remove wait for BatchGetHistory, as we are not creating new bucket now * fix: fix format * fix typo * fix: use diff table name to avoid conflict * fix: fix format and add comments * fix: add detailed exceptions * fix: fix format * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Neenu Shaji <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent fe5fd04 commit 56377d9

File tree

7 files changed

+101
-27
lines changed

7 files changed

+101
-27
lines changed

asset/src/main/java/com/example/asset/CreateFeedExample.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,29 @@
1818

1919
// [START asset_quickstart_create_feed]
2020
import com.google.cloud.asset.v1.AssetServiceClient;
21+
import com.google.cloud.asset.v1.ContentType;
2122
import com.google.cloud.asset.v1.CreateFeedRequest;
2223
import com.google.cloud.asset.v1.Feed;
2324
import com.google.cloud.asset.v1.FeedOutputConfig;
2425
import com.google.cloud.asset.v1.ProjectName;
2526
import com.google.cloud.asset.v1.PubsubDestination;
27+
import java.io.IOException;
2628
import java.util.Arrays;
2729

2830
public class CreateFeedExample {
2931
// Create a feed
30-
public static void createFeed(String[] assetNames, String feedId, String topic, String projectId)
31-
throws Exception {
32+
public static void createFeed(
33+
String[] assetNames, String feedId, String topic, String projectId, ContentType contentType)
34+
throws IOException, IllegalArgumentException {
3235
// String[] assetNames = {"MY_ASSET_NAME"}
36+
// ContentType contentType = contentType
3337
// String FeedId = "MY_FEED_ID"
3438
// String topic = "projects/[PROJECT_ID]/topics/[TOPIC_NAME]"
3539
// String projectID = "MY_PROJECT_ID"
3640
Feed feed =
3741
Feed.newBuilder()
3842
.addAllAssetNames(Arrays.asList(assetNames))
43+
.setContentType(contentType)
3944
.setFeedOutputConfig(
4045
FeedOutputConfig.newBuilder()
4146
.setPubsubDestination(PubsubDestination.newBuilder().setTopic(topic).build())
@@ -53,7 +58,7 @@ public static void createFeed(String[] assetNames, String feedId, String topic,
5358
try (AssetServiceClient client = AssetServiceClient.create()) {
5459
Feed response = client.createFeed(request);
5560
System.out.println("Feed created successfully: " + response.getName());
56-
} catch (Exception e) {
61+
} catch (IOException | IllegalArgumentException e) {
5762
System.out.println("Error during CreateFeed: \n" + e.toString());
5863
}
5964
}

asset/src/main/java/com/example/asset/ExportAssetsBigqueryExample.java

+37-12
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,58 @@
2222
import com.google.cloud.ServiceOptions;
2323
import com.google.cloud.asset.v1.AssetServiceClient;
2424
import com.google.cloud.asset.v1.BigQueryDestination;
25+
import com.google.cloud.asset.v1.ContentType;
2526
import com.google.cloud.asset.v1.ExportAssetsRequest;
2627
import com.google.cloud.asset.v1.ExportAssetsResponse;
2728
import com.google.cloud.asset.v1.OutputConfig;
29+
import com.google.cloud.asset.v1.PartitionSpec;
2830
import com.google.cloud.asset.v1.ProjectName;
31+
import java.io.IOException;
32+
import java.util.concurrent.ExecutionException;
2933

3034
public class ExportAssetsBigqueryExample {
3135

3236
// Use the default project Id.
3337
private static final String projectId = ServiceOptions.getDefaultProjectId();
3438

35-
// Export assets for a project.
36-
// @param args path where the results will be exported to.
37-
public static void exportBigQuery(String bigqueryDataset, String bigqueryTable) throws Exception {
39+
// Export assets to BigQuery for a project.
40+
public static void exportBigQuery(
41+
String bigqueryDataset, String bigqueryTable, ContentType contentType, boolean isPerType)
42+
throws IOException, IllegalArgumentException, InterruptedException, ExecutionException {
3843
try (AssetServiceClient client = AssetServiceClient.create()) {
3944
ProjectName parent = ProjectName.of(projectId);
40-
OutputConfig outputConfig =
41-
OutputConfig.newBuilder()
42-
.setBigqueryDestination(
43-
BigQueryDestination.newBuilder()
44-
.setDataset(bigqueryDataset)
45-
.setTable(bigqueryTable)
46-
.setForce(true)
47-
.build())
48-
.build();
45+
OutputConfig outputConfig;
46+
// Outputs to per-type BigQuery table.
47+
if (isPerType) {
48+
outputConfig =
49+
OutputConfig.newBuilder()
50+
.setBigqueryDestination(
51+
BigQueryDestination.newBuilder()
52+
.setDataset(bigqueryDataset)
53+
.setTable(bigqueryTable)
54+
.setForce(true)
55+
.setSeparateTablesPerAssetType(true)
56+
.setPartitionSpec(
57+
PartitionSpec.newBuilder()
58+
.setPartitionKey(PartitionSpec.PartitionKey.READ_TIME)
59+
.build())
60+
.build())
61+
.build();
62+
} else {
63+
outputConfig =
64+
OutputConfig.newBuilder()
65+
.setBigqueryDestination(
66+
BigQueryDestination.newBuilder()
67+
.setDataset(bigqueryDataset)
68+
.setTable(bigqueryTable)
69+
.setForce(true)
70+
.build())
71+
.build();
72+
}
4973
ExportAssetsRequest request =
5074
ExportAssetsRequest.newBuilder()
5175
.setParent(parent.toString())
76+
.setContentType(contentType)
5277
.setOutputConfig(outputConfig)
5378
.build();
5479
ExportAssetsResponse response = client.exportAssetsAsync(request).get();

asset/src/main/java/com/example/asset/ExportAssetsExample.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,24 @@
2121

2222
import com.google.cloud.ServiceOptions;
2323
import com.google.cloud.asset.v1.AssetServiceClient;
24+
import com.google.cloud.asset.v1.ContentType;
2425
import com.google.cloud.asset.v1.ExportAssetsRequest;
2526
import com.google.cloud.asset.v1.ExportAssetsResponse;
2627
import com.google.cloud.asset.v1.GcsDestination;
2728
import com.google.cloud.asset.v1.OutputConfig;
2829
import com.google.cloud.asset.v1.ProjectName;
30+
import java.io.IOException;
31+
import java.util.concurrent.ExecutionException;
2932

3033
public class ExportAssetsExample {
3134

3235
// Use the default project Id.
3336
private static final String projectId = ServiceOptions.getDefaultProjectId();
3437

3538
// Export assets for a project.
36-
// @param args path where the results will be exported to.
37-
public static void main(String... args) throws Exception {
38-
// Gcs path, e.g.: "gs://<my_asset_bucket>/<my_asset_dump_file>"
39-
String exportPath = args[0];
39+
// @param exportPath where the results will be exported to.
40+
public static void exportAssets(String exportPath, ContentType contentType)
41+
throws IOException, IllegalArgumentException, InterruptedException, ExecutionException {
4042
try (AssetServiceClient client = AssetServiceClient.create()) {
4143
ProjectName parent = ProjectName.of(projectId);
4244
OutputConfig outputConfig =
@@ -47,10 +49,11 @@ public static void main(String... args) throws Exception {
4749
ExportAssetsRequest.newBuilder()
4850
.setParent(parent.toString())
4951
.setOutputConfig(outputConfig)
52+
.setContentType(contentType)
5053
.build();
5154
ExportAssetsResponse response = client.exportAssetsAsync(request).get();
5255
System.out.println(response);
5356
}
5457
}
5558
}
56-
// [END asset_quickstart]
59+
// [END asset_quickstart_export_assets]

asset/src/main/java/com/example/asset/ListAssetsExample.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
import com.google.cloud.asset.v1.ContentType;
2222
import com.google.cloud.asset.v1.ListAssetsRequest;
2323
import com.google.cloud.asset.v1.ProjectName;
24+
import java.io.IOException;
2425
import java.util.Arrays;
2526

2627
// [START asset_quickstart_list_assets]
2728
// Imports the Google Cloud client library
2829

2930
public class ListAssetsExample {
3031

31-
public static void listAssets() throws Exception {
32+
public static void listAssets() throws IOException, IllegalArgumentException {
3233
// The project id of the asset parent to list.
3334
String projectId = "YOUR_PROJECT_ID";
3435
// The asset types to list. E.g.,
@@ -44,7 +45,7 @@ public static void listAssets() throws Exception {
4445
}
4546

4647
public static void listAssets(String projectId, String[] assetTypes, ContentType contentType)
47-
throws Exception {
48+
throws IOException, IllegalArgumentException {
4849
try (AssetServiceClient client = AssetServiceClient.create()) {
4950
ProjectName parent = ProjectName.of(projectId);
5051

asset/src/test/java/com/example/asset/ListAssets.java

+14
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,18 @@ public void testListAssetsExample() throws Exception {
6464
assertThat(got).contains("asset");
6565
}
6666
}
67+
68+
@Test
69+
public void testListAssetsRelationshipExample() throws Exception {
70+
// Use the default project Id (configure it by setting environment variable
71+
// "GOOGLE_CLOUD_PROJECT").
72+
String projectId = ServiceOptions.getDefaultProjectId();
73+
String[] assetTypes = {"compute.googleapis.com/Instance", "compute.googleapis.com/Disk"};
74+
ContentType contentType = ContentType.RELATIONSHIP;
75+
ListAssetsExample.listAssets(projectId, assetTypes, contentType);
76+
String got = bout.toString();
77+
if (!got.isEmpty()) {
78+
assertThat(got).contains("asset");
79+
}
80+
}
6781
}

asset/src/test/java/com/example/asset/QuickStartIT.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020

2121
import com.google.cloud.ServiceOptions;
22+
import com.google.cloud.asset.v1.ContentType;
2223
import com.google.cloud.bigquery.BigQuery;
2324
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
2425
import com.google.cloud.bigquery.BigQueryOptions;
@@ -90,25 +91,35 @@ public void tearDown() {
9091
@Test
9192
public void testExportAssetExample() throws Exception {
9293
String assetDumpPath = String.format("gs://%s/%s/my-assets-dump.txt", bucketName, path);
93-
ExportAssetsExample.main(assetDumpPath);
94+
ExportAssetsExample.exportAssets(assetDumpPath, ContentType.RESOURCE);
9495
String got = bout.toString();
9596
assertThat(got).contains(String.format("uri: \"%s\"", assetDumpPath));
9697
}
9798

99+
@Test
100+
public void testExportAssetBigqueryPerTypeExample() throws Exception {
101+
String dataset =
102+
String.format("projects/%s/datasets/%s", ServiceOptions.getDefaultProjectId(), datasetName);
103+
String table = "java_test_per_type";
104+
ExportAssetsBigqueryExample.exportBigQuery(
105+
dataset, table, ContentType.RESOURCE, /*perType*/ true);
106+
String got = bout.toString();
107+
assertThat(got).contains(String.format("dataset: \"%s\"", dataset));
108+
}
109+
98110
@Test
99111
public void testExportAssetBigqueryExample() throws Exception {
100112
String dataset =
101113
String.format("projects/%s/datasets/%s", ServiceOptions.getDefaultProjectId(), datasetName);
102114
String table = "java_test";
103-
ExportAssetsBigqueryExample.exportBigQuery(dataset, table);
115+
ExportAssetsBigqueryExample.exportBigQuery(
116+
dataset, table, ContentType.RELATIONSHIP, /*perType*/ false);
104117
String got = bout.toString();
105118
assertThat(got).contains(String.format("dataset: \"%s\"", dataset));
106119
}
107120

108121
@Test
109122
public void testBatchGetAssetsHistory() throws Exception {
110-
// Wait 10 seconds to let bucket creation event go to CAI
111-
Thread.sleep(10000);
112123
String bucketAssetName = String.format("//storage.googleapis.com/%s", bucketName);
113124
BatchGetAssetsHistoryExample.main(bucketAssetName);
114125
String got = bout.toString();

asset/src/test/java/com/example/asset/RealTimeFeed.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.cloud.asset.v1.ContentType;
2122
import com.google.cloud.pubsub.v1.TopicAdminClient;
2223
import com.google.cloud.resourcemanager.ProjectInfo;
2324
import com.google.cloud.resourcemanager.ResourceManager;
@@ -87,7 +88,20 @@ public void tearDown() {
8788

8889
@Test
8990
public void test1CreateFeedExample() throws Exception {
90-
CreateFeedExample.createFeed(assetNames, feedId, topicName.toString(), projectId);
91+
CreateFeedExample.createFeed(
92+
assetNames, feedId, topicName.toString(), projectId, ContentType.RESOURCE);
93+
String got = bout.toString();
94+
assertThat(got).contains("Feed created successfully: " + feedName);
95+
}
96+
97+
@Test
98+
public void test1CreateFeedRelationshipExample() throws Exception {
99+
CreateFeedExample.createFeed(
100+
assetNames,
101+
feedId + "relationship",
102+
topicName.toString(),
103+
projectId,
104+
ContentType.RELATIONSHIP);
91105
String got = bout.toString();
92106
assertThat(got).contains("Feed created successfully: " + feedName);
93107
}
@@ -116,6 +130,7 @@ public void test4UpdateFeedExample() throws Exception {
116130
@Test
117131
public void test5DeleteFeedExample() throws Exception {
118132
DeleteFeedExample.deleteFeed(feedName);
133+
DeleteFeedExample.deleteFeed(feedName + "relationship");
119134
String got = bout.toString();
120135
assertThat(got).contains("Feed deleted");
121136
}

0 commit comments

Comments
 (0)