Skip to content

Commit 446856b

Browse files
jacspa96Jacek Spalinski
andauthored
feat(dataplex): add code samples for Entry Group (#9567)
* feat(dataplex): add sample for list Entry Groups * feat(dataplex): add sample for get Entry Group * feat(dataplex): add sample for delete Entry Group * feat(dataplex): add sample for create Entry Group * feat(dataplex): add sample for update Entry Group * feat(dataplex): add integration tests for Entry Group * feat(dataplex): explicitly pass arguments to the functions * feat(dataplex): adjust integration tests to changed functions' signatures * feat(dataplex): remove snake case from test methods' names * feat(dataplex): add comment explaining paging handling --------- Co-authored-by: Jacek Spalinski <[email protected]>
1 parent 8d977ed commit 446856b

File tree

6 files changed

+383
-0
lines changed

6 files changed

+383
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_create_entry_group]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryGroup;
22+
import com.google.cloud.dataplex.v1.LocationName;
23+
24+
// Samples to create Entry Group
25+
public class CreateEntryGroup {
26+
27+
public static void main(String[] args) throws Exception {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "MY_PROJECT_ID";
30+
// Available locations: https://cloud.google.com/dataplex/docs/locations
31+
String location = "MY_LOCATION";
32+
String entryGroupId = "MY_ENTRY_GROUP_ID";
33+
34+
EntryGroup createdEntryGroup = createEntryGroup(projectId, location, entryGroupId);
35+
System.out.println("Successfully created entry group: " + createdEntryGroup.getName());
36+
}
37+
38+
public static EntryGroup createEntryGroup(String projectId, String location, String entryGroupId)
39+
throws Exception {
40+
LocationName locationName = LocationName.of(projectId, location);
41+
EntryGroup entryGroup =
42+
EntryGroup.newBuilder().setDescription("description of the entry group").build();
43+
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+
// or use "try-with-close" statement to do this automatically.
48+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
49+
return client.createEntryGroupAsync(locationName, entryGroup, entryGroupId).get();
50+
}
51+
}
52+
}
53+
// [END dataplex_create_entry_group]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_delete_entry_group]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryGroupName;
22+
23+
// Sample to delete Entry Group
24+
public class DeleteEntryGroup {
25+
26+
public static void main(String[] args) throws Exception {
27+
// TODO(developer): Replace these variables before running the sample.
28+
String projectId = "MY_PROJECT_ID";
29+
// Available locations: https://cloud.google.com/dataplex/docs/locations
30+
String location = "MY_LOCATION";
31+
String entryGroupId = "MY_ENTRY_GROUP_ID";
32+
33+
deleteEntryGroup(projectId, location, entryGroupId);
34+
System.out.println("Successfully deleted entry group");
35+
}
36+
37+
public static void deleteEntryGroup(String projectId, String location, String entryGroupId)
38+
throws Exception {
39+
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
40+
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests. After completing all of your requests, call
43+
// the "close" method on the client to safely clean up any remaining background resources,
44+
// or use "try-with-close" statement to do this automatically.
45+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
46+
client.deleteEntryGroupAsync(entryGroupName).get();
47+
}
48+
}
49+
}
50+
// [END dataplex_delete_entry_group]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_get_entry_group]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryGroup;
22+
import com.google.cloud.dataplex.v1.EntryGroupName;
23+
import java.io.IOException;
24+
25+
// Sample to get Entry Group
26+
public class GetEntryGroup {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
// Available locations: https://cloud.google.com/dataplex/docs/locations
32+
String location = "MY_LOCATION";
33+
String entryGroupId = "MY_ENTRY_GROUP_ID";
34+
35+
EntryGroup entryGroup = getEntryGroup(projectId, location, entryGroupId);
36+
System.out.println("Entry group retrieved successfully: " + entryGroup.getName());
37+
}
38+
39+
public static EntryGroup getEntryGroup(String projectId, String location, String entryGroupId)
40+
throws IOException {
41+
EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId);
42+
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the "close" method on the client to safely clean up any remaining background resources,
46+
// or use "try-with-close" statement to do this automatically.
47+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
48+
return client.getEntryGroup(entryGroupName);
49+
}
50+
}
51+
}
52+
// [END dataplex_get_entry_group]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_list_entry_groups]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryGroup;
22+
import com.google.cloud.dataplex.v1.LocationName;
23+
import com.google.common.collect.ImmutableList;
24+
import java.io.IOException;
25+
import java.util.List;
26+
27+
// Sample to list Entry Groups
28+
public class ListEntryGroups {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "MY_PROJECT_ID";
33+
// Available locations: https://cloud.google.com/dataplex/docs/locations
34+
String location = "MY_LOCATION";
35+
36+
List<EntryGroup> entryGroups = listEntryGroups(projectId, location);
37+
entryGroups.forEach(
38+
entryGroup -> System.out.println("Entry group name: " + entryGroup.getName()));
39+
}
40+
41+
public static List<EntryGroup> listEntryGroups(String projectId, String location)
42+
throws IOException {
43+
LocationName locationName = LocationName.of(projectId, location);
44+
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests. After completing all of your requests, call
47+
// the "close" method on the client to safely clean up any remaining background resources,
48+
// or use "try-with-close" statement to do this automatically.
49+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
50+
CatalogServiceClient.ListEntryGroupsPagedResponse listEntryGroupsResponse =
51+
client.listEntryGroups(locationName);
52+
// Paging is implicitly handled by .iterateAll(), all results will be returned
53+
return ImmutableList.copyOf(listEntryGroupsResponse.iterateAll());
54+
}
55+
}
56+
}
57+
// [END dataplex_list_entry_groups]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
// [START dataplex_update_entry_group]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryGroup;
22+
import com.google.cloud.dataplex.v1.EntryGroupName;
23+
import com.google.protobuf.FieldMask;
24+
25+
// Sample to update Entry Group
26+
public class UpdateEntryGroup {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
// Available locations: https://cloud.google.com/dataplex/docs/locations
32+
String location = "MY_LOCATION";
33+
String entryGroupId = "MY_ENTRY_GROUP_ID";
34+
35+
EntryGroup updatedEntryGroup = updateEntryGroup(projectId, location, entryGroupId);
36+
System.out.println("Successfully updated entry group: " + updatedEntryGroup.getName());
37+
}
38+
39+
public static EntryGroup updateEntryGroup(String projectId, String location, String entryGroupId)
40+
throws Exception {
41+
EntryGroup entryGroup =
42+
EntryGroup.newBuilder()
43+
.setName(EntryGroupName.of(projectId, location, entryGroupId).toString())
44+
.setDescription("updated description of the entry group")
45+
.build();
46+
47+
// Update mask specifies which fields will be updated.
48+
// If empty mask is given, all modifiable fields from the request will be used for update.
49+
// If update mask is specified as "*" it is treated as full update,
50+
// that means fields not present in the request will be emptied.
51+
FieldMask updateMask = FieldMask.newBuilder().addPaths("description").build();
52+
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources,
56+
// or use "try-with-close" statement to do this automatically.
57+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
58+
return client.updateEntryGroupAsync(entryGroup, updateMask).get();
59+
}
60+
}
61+
}
62+
// [END dataplex_update_entry_group]
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2024 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 dataplex;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.dataplex.v1.EntryGroup;
23+
import java.io.IOException;
24+
import java.util.List;
25+
import java.util.UUID;
26+
import org.junit.AfterClass;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class EntryGroupIT {
31+
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
32+
private static final String LOCATION = "us-central1";
33+
private static final String entryGroupId = "test-entry-group" + ID;
34+
private static String expectedEntryGroup;
35+
36+
private static final String PROJECT_ID = requireProjectIdEnvVar();
37+
38+
private static String requireProjectIdEnvVar() {
39+
String value = System.getenv("GOOGLE_CLOUD_PROJECT");
40+
assertNotNull(
41+
"Environment variable GOOGLE_CLOUD_PROJECT is required to perform these tests.", value);
42+
return value;
43+
}
44+
45+
@BeforeClass
46+
public static void checkRequirements() {
47+
requireProjectIdEnvVar();
48+
}
49+
50+
@BeforeClass
51+
// Set-up code that will be executed before all tests
52+
public static void setUp() throws Exception {
53+
expectedEntryGroup =
54+
String.format(
55+
"projects/%s/locations/%s/entryGroups/%s", PROJECT_ID, LOCATION, entryGroupId);
56+
// Create Entry Group resource that will be used in tests for "get", "list" and "update" methods
57+
CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId);
58+
}
59+
60+
@Test
61+
public void testListEntryGroups() throws IOException {
62+
List<EntryGroup> entryGroups = ListEntryGroups.listEntryGroups(PROJECT_ID, LOCATION);
63+
assertThat(entryGroups.stream().map(EntryGroup::getName)).contains(expectedEntryGroup);
64+
}
65+
66+
@Test
67+
public void testGetEntryGroup() throws IOException {
68+
EntryGroup entryGroup = GetEntryGroup.getEntryGroup(PROJECT_ID, LOCATION, entryGroupId);
69+
assertThat(entryGroup.getName()).isEqualTo(expectedEntryGroup);
70+
}
71+
72+
@Test
73+
public void testUpdateEntryGroup() throws Exception {
74+
EntryGroup entryGroup = UpdateEntryGroup.updateEntryGroup(PROJECT_ID, LOCATION, entryGroupId);
75+
assertThat(entryGroup.getName()).isEqualTo(expectedEntryGroup);
76+
}
77+
78+
@Test
79+
public void testCreateEntryGroup() throws Exception {
80+
String entryGroupIdToCreate = "test-entry-group" + UUID.randomUUID().toString().substring(0, 8);
81+
String expectedEntryGroupToCreate =
82+
String.format(
83+
"projects/%s/locations/%s/entryGroups/%s", PROJECT_ID, LOCATION, entryGroupIdToCreate);
84+
85+
EntryGroup entryGroup =
86+
CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupIdToCreate);
87+
// Clean-up created Entry Group
88+
DeleteEntryGroup.deleteEntryGroup(PROJECT_ID, LOCATION, entryGroupIdToCreate);
89+
90+
assertThat(entryGroup.getName()).isEqualTo(expectedEntryGroupToCreate);
91+
}
92+
93+
@Test
94+
public void testDeleteEntryGroup() throws Exception {
95+
String entryGroupIdToDelete = "test-entry-group" + UUID.randomUUID().toString().substring(0, 8);
96+
// Create Entry Group to be deleted
97+
CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupIdToDelete);
98+
99+
// No exception means successful call
100+
DeleteEntryGroup.deleteEntryGroup(PROJECT_ID, LOCATION, entryGroupIdToDelete);
101+
}
102+
103+
@AfterClass
104+
// Clean-up code that will be executed after all tests
105+
public static void tearDown() throws Exception {
106+
// Clean-up Entry Group resource created in setUp()
107+
DeleteEntryGroup.deleteEntryGroup(PROJECT_ID, LOCATION, entryGroupId);
108+
}
109+
}

0 commit comments

Comments
 (0)