Skip to content

Commit dcced25

Browse files
cwxie-googleanguillanneuf
authored andcommitted
Add quickstart for Real Time Feed API V1P2Beta1 (#1563)
* asset: add quickstart for Real Time Feed API V1P2Beta1 * address review comments * refine test * address comments * updated based on comments * fix presubmit error
1 parent 538c9f5 commit dcced25

File tree

6 files changed

+354
-0
lines changed

6 files changed

+354
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2019 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_create_feed]
20+
import com.google.cloud.asset.v1.ProjectName;
21+
import com.google.cloud.asset.v1p2beta1.AssetServiceClient;
22+
import com.google.cloud.asset.v1p2beta1.CreateFeedRequest;
23+
import com.google.cloud.asset.v1p2beta1.Feed;
24+
import com.google.cloud.asset.v1p2beta1.FeedOutputConfig;
25+
import com.google.cloud.asset.v1p2beta1.PubsubDestination;
26+
import java.util.Arrays;
27+
28+
public class CreateFeedExample {
29+
// Create a feed
30+
public static void createFeed(
31+
String[] assetNames, String feedId, String topic, String projectId) throws Exception {
32+
// String[] assetNames = {"MY_ASSET_NAME"}
33+
// String FeedId = "MY_FEED_ID"
34+
// String topic = "projects/[PROJECT_ID]/topics/[TOPIC_NAME]"
35+
// String projectID = "MY_PROJECT_ID"
36+
Feed feed = Feed.newBuilder()
37+
.addAllAssetNames(Arrays.asList(assetNames))
38+
.setFeedOutputConfig(
39+
FeedOutputConfig.newBuilder().setPubsubDestination(
40+
PubsubDestination.newBuilder().setTopic(topic).build()).build()).build();
41+
CreateFeedRequest request = CreateFeedRequest.newBuilder()
42+
.setParent(String.format(ProjectName.of(projectId).toString()))
43+
.setFeedId(feedId)
44+
.setFeed(feed)
45+
.build();
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the "close" method on the client to safely clean up any remaining background resources.
49+
try (AssetServiceClient client = AssetServiceClient.create()) {
50+
Feed response = client.createFeed(request);
51+
System.out.println("Feed created successfully: " + response.getName());
52+
} catch (Exception e) {
53+
System.out.println("Error during CreateFeed: \n" + e.toString());
54+
}
55+
}
56+
}
57+
// [END asset_quickstart_create_feed]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 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_delete_feed]
20+
import com.google.cloud.asset.v1p2beta1.AssetServiceClient;
21+
import com.google.cloud.asset.v1p2beta1.Feed;
22+
import java.util.Arrays;
23+
24+
public class DeleteFeedExample {
25+
26+
// Delete a feed with full feed name
27+
public static void deleteFeed(String feedName) throws Exception {
28+
// String feedName = "MY_FEED_NAME"
29+
30+
// Initialize client that will be used to send requests. This client only needs to be created
31+
// once, and can be reused for multiple requests. After completing all of your requests, call
32+
// the "close" method on the client to safely clean up any remaining background resources.
33+
try (AssetServiceClient client = AssetServiceClient.create()) {
34+
client.deleteFeed(feedName);
35+
System.out.println("Feed deleted");
36+
} catch (Exception e) {
37+
System.out.println("Error during DeleteFeed: \n" + e.toString());
38+
}
39+
}
40+
}
41+
// [END asset_quickstart_delete_feed]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 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_get_feed]
20+
import com.google.cloud.asset.v1p2beta1.AssetServiceClient;
21+
import com.google.cloud.asset.v1p2beta1.Feed;
22+
import java.util.Arrays;
23+
24+
public class GetFeedExample {
25+
26+
// Get a feed with full feed name
27+
public static void getFeed(String feedName) throws Exception {
28+
// String feedName = "MY_FEED_NAME"
29+
30+
// Initialize client that will be used to send requests. This client only needs to be created
31+
// once, and can be reused for multiple requests. After completing all of your requests, call
32+
// the "close" method on the client to safely clean up any remaining background resources.
33+
try (AssetServiceClient client = AssetServiceClient.create()) {
34+
Feed feed = client.getFeed(feedName);
35+
System.out.println("Get a feed: " + feedName);
36+
} catch (Exception e) {
37+
System.out.println("Error during GetFeed: \n" + e.toString());
38+
}
39+
}
40+
}
41+
// [END asset_quickstart_get_feed]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019 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_feeds]
20+
import com.google.cloud.asset.v1.ProjectName;
21+
import com.google.cloud.asset.v1p2beta1.AssetServiceClient;
22+
import com.google.cloud.asset.v1p2beta1.ListFeedsResponse;
23+
import java.util.Arrays;
24+
25+
public class ListFeedsExample {
26+
// List feeds in a project.
27+
public static void listFeeds(String projectId) throws Exception {
28+
// String projectId = "MY_PROJECT_ID"
29+
// String topic = "projects/[PROJECT_ID]/topics/[TOPIC_NAME]"
30+
31+
// Initialize client that will be used to send requests. This client only needs to be created
32+
// once, and can be reused for multiple requests. After completing all of your requests, call
33+
// the "close" method on the client to safely clean up any remaining background resources.
34+
try (AssetServiceClient client = AssetServiceClient.create()) {
35+
ListFeedsResponse response = client.listFeeds(ProjectName.of(projectId).toString());
36+
System.out.println("Listed feeds under: " + projectId);
37+
} catch (Exception e) {
38+
System.out.println("Error during ListFeeds: \n" + e.toString());
39+
}
40+
}
41+
}
42+
// [END asset_quickstart_list_feeds]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2019 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_update_feed]
20+
import com.google.cloud.asset.v1p2beta1.AssetServiceClient;
21+
import com.google.cloud.asset.v1p2beta1.Feed;
22+
import com.google.cloud.asset.v1p2beta1.FeedOutputConfig;
23+
import com.google.cloud.asset.v1p2beta1.PubsubDestination;
24+
import com.google.cloud.asset.v1p2beta1.UpdateFeedRequest;
25+
import com.google.protobuf.FieldMask;
26+
import java.util.Arrays;
27+
28+
public class UpdateFeedExample {
29+
30+
// Update a feed
31+
public static void updateFeed(String feedName, String topic) throws Exception {
32+
// String feedName = "MY_FEED_NAME"
33+
// String topic = "projects/[PROJECT_ID]/topics/[TOPIC_NAME]"
34+
Feed feed = Feed.newBuilder()
35+
.setName(feedName)
36+
.setFeedOutputConfig(
37+
FeedOutputConfig.newBuilder().setPubsubDestination(
38+
PubsubDestination.newBuilder().setTopic(topic).build()).build()).build();
39+
UpdateFeedRequest request = UpdateFeedRequest.newBuilder()
40+
.setFeed(feed)
41+
.setUpdateMask(
42+
FieldMask.newBuilder().addPaths("feed_output_config.pubsub_destination.topic").build())
43+
.build();
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests. After completing all of your requests, call
46+
// the "close" method on the client to safely clean up any remaining background resources.
47+
try (AssetServiceClient client = AssetServiceClient.create()) {
48+
Feed response = client.updateFeed(request);
49+
System.out.println("Feed updated successfully: " + response.getName());
50+
} catch (Exception e) {
51+
System.out.println("Error during UpdateFeed: \n" + e.toString());
52+
}
53+
}
54+
}
55+
// [END asset_quickstart_update_feed]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2019 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.pubsub.v1.TopicAdminClient;
23+
import com.google.cloud.resourcemanager.ProjectInfo;
24+
import com.google.cloud.resourcemanager.ResourceManager;
25+
import com.google.cloud.resourcemanager.ResourceManagerOptions;
26+
import com.google.pubsub.v1.ProjectTopicName;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
import java.util.UUID;
30+
import org.junit.After;
31+
import org.junit.AfterClass;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.FixMethodOrder;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.junit.runners.JUnit4;
38+
import org.junit.runners.MethodSorters;
39+
40+
/** Tests for real time feed sample. */
41+
@RunWith(JUnit4.class)
42+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
43+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
44+
public class RealTimeFeed {
45+
private static final String topicId = "topicId";
46+
private static final String feedId = UUID.randomUUID().toString();
47+
private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
48+
private final String projectNumber = getProjectNumber(projectId);
49+
private final String feedName = String.format("projects/%s/feeds/%s", projectNumber, feedId);
50+
private final String[] assetNames = {UUID.randomUUID().toString()};
51+
private static final ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
52+
private ByteArrayOutputStream bout;
53+
54+
private String getProjectNumber(String projectId) {
55+
ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
56+
ProjectInfo project = resourceManager.get(projectId);
57+
return Long.toString(project.getProjectNumber());
58+
}
59+
60+
@BeforeClass
61+
public static void createTopic() throws Exception {
62+
TopicAdminClient topicAdminClient = TopicAdminClient.create();
63+
topicAdminClient.createTopic(topicName);
64+
}
65+
66+
@AfterClass
67+
public static void deleteTopic() throws Exception {
68+
TopicAdminClient topicAdminClient = TopicAdminClient.create();
69+
topicAdminClient.deleteTopic(topicName);
70+
}
71+
72+
@Before
73+
public void beforeTest() {
74+
bout = new ByteArrayOutputStream();
75+
System.setOut(new PrintStream(bout));
76+
}
77+
78+
@After
79+
public void tearDown() {
80+
System.setOut(null);
81+
bout.reset();
82+
}
83+
84+
@Test
85+
public void test1CreateFeedExample() throws Exception {
86+
CreateFeedExample.createFeed(assetNames, feedId, topicName.toString(), projectId);
87+
String got = bout.toString();
88+
assertThat(got).contains("Feed created successfully: " + feedName);
89+
}
90+
91+
@Test
92+
public void test2GetFeedExample() throws Exception {
93+
GetFeedExample.getFeed(feedName);
94+
String got = bout.toString();
95+
assertThat(got).contains("Get a feed: " + feedName);
96+
}
97+
98+
@Test
99+
public void test3ListFeedsExample() throws Exception {
100+
ListFeedsExample.listFeeds(projectId);
101+
String got = bout.toString();
102+
assertThat(got).contains("Listed feeds under: " + projectId);
103+
}
104+
105+
@Test
106+
public void test4UpdateFeedExample() throws Exception {
107+
UpdateFeedExample.updateFeed(feedName, topicName.toString());
108+
String got = bout.toString();
109+
assertThat(got).contains("Feed updated successfully: " + feedName);
110+
}
111+
112+
@Test
113+
public void test5DeleteFeedExample() throws Exception {
114+
DeleteFeedExample.deleteFeed(feedName);
115+
String got = bout.toString();
116+
assertThat(got).contains("Feed deleted");
117+
}
118+
}

0 commit comments

Comments
 (0)