8000 feat(dataplex): add code samples for Entry Type (#9574) · ludoch/java-docs-samples@34c0352 · GitHub
[go: up one dir, main page]

Skip to content

Commit 34c0352

Browse files
jacspa96Jacek Spalinski
and
Jacek Spalinski
authored
feat(dataplex): add code samples for Entry Type (GoogleCloudPlatform#9574)
* feat(dataplex): add sample for list Entry Types * feat(dataplex): add sample for get Entry Type * feat(dataplex): add sample for create Entry Type * feat(dataplex): add sample for update Entry Type * feat(dataplex): add sample for delete Entry Type * feat(dataplex): add integration tests for Entry Type --------- Co-authored-by: Jacek Spalinski <jspa@google.com>
1 parent c6e4279 commit 34c0352

File tree

6 files changed

+389
-0
lines changed

6 files changed

+389
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_type]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryType;
22+
import com.google.cloud.dataplex.v1.LocationName;
23+
24+
// Samples to create Entry Type
25+
public class CreateEntryType {
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 entryTypeId = "MY_ENTRY_TYPE_ID";
33+
34+
EntryType createdEntryType = createEntryType(projectId, location, entryTypeId);
35+
System.out.println("Successfully created entry type: " + createdEntryType.getName());
36+
}
37+
38+
public static EntryType createEntryType(String projectId, String location, String entryTypeId)
39+
throws Exception {
40+
LocationName locationName = LocationName.of(projectId, location);
41+
EntryType entryType =
42+
EntryType.newBuilder()
43+
.setDescription("description of the entry type")
44+
// Required aspects will need to be attached to every entry created for this entry type.
45+
// You cannot change required aspects for entry type once it is created.
46+
.addRequiredAspects(
47+
EntryType.AspectInfo.newBuilder()
48+
// Example of system aspect type.
49+
// It is also possible to specify custom aspect type.
50+
.setType("projects/dataplex-types/locations/global/aspectTypes/schema")
51+
.build())
52+
.build();
53+
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests. After completing all of your requests, call
56+
// the "close" method on the client to safely clean up any remaining background resources,
57+
// or use "try-with-close" statement to do this automatically.
58+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
59+
return client.createEntryTypeAsync(locationName, entryType, entryTypeId).get();
60+
}
61+
}
62+
}
63+
// [END dataplex_create_entry_type]
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_type]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryTypeName;
22+
23+
// Sample to delete Entry Type
24+
public class DeleteEntryType {
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 entryTypeId = "MY_ENTRY_TYPE_ID";
32+
33+
deleteEntryType(projectId, location, entryTypeId);
34+
System.out.println("Successfully deleted entry type");
35+
}
36+
37+
public static void deleteEntryType(String projectId, String location, String entryTypeId)
38+
throws Exception {
39+
EntryTypeName entryTypeName = EntryTypeName.of(projectId, location, entryTypeId);
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.deleteEntryTypeAsync(entryTypeName).get();
47+
}
48+
}
49+
}
50+
// [END dataplex_delete_entry_type]
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_type]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryType;
22+
import com.google.cloud.dataplex.v1.EntryTypeName;
23+
import java.io.IOException;
24+
25+
// Sample to get Entry Type
26+
public class GetEntryType {
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 entryTypeId = "MY_ENTRY_TYPE_ID";
34+
35+
EntryType entryType = getEntryType(projectId, location, entryTypeId);
36+
System.out.println("Entry type retrieved successfully: " + entryType.getName());
37+
}
38+
39+
public static EntryType getEntryType(String projectId, String location, String entryTypeId)
40+
throws IOException {
41+
EntryTypeName entryTypeName = EntryTypeName.of(projectId, location, entryTypeId);
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.getEntryType(entryTypeName);
49+
}
50+
}
51+
}
52+
// [END dataplex_get_entry_type]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_types]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryType;
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 Types
28+
public class ListEntryTypes {
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<EntryType> entryTypes = listEntryTypes(projectId, location);
37+
entryTypes.forEach(entryType -> System.out.println("Entry type name: " + entryType.getName()));
38+
}
39+
40+
public static List<EntryType> listEntryTypes(String projectId, String location)
41+
throws IOException {
42+
LocationName locationName = LocationName.of(projectId, location);
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+
CatalogServiceClient.ListEntryTypesPagedResponse listEntryTypesResponse =
50+
client.listEntryTypes(locationName);
51+
// Paging is implicitly handled by .iterateAll(), all results will be returned
52+
return ImmutableList.copyOf(listEntryTypesResponse.iterateAll());
53+
}
54+
}
55+
}
56+
// [END dataplex_list_entry_types]
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_type]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.EntryType;
22+
import com.google.cloud.dataplex.v1.EntryTypeName;
23+
import com.google.protobuf.FieldMask;
24+
25+
// Sample to update Entry Type
26+
public class UpdateEntryType {
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 entryTypeId = "MY_ENTRY_TYPE_ID";
34+
35+
EntryType updatedEntryType = updateEntryType(projectId, location, entryTypeId);
36+
System.out.println("Successfully updated entry type: " + updatedEntryType.getName());
37+
}
38+
39+
public static EntryType updateEntryType(String projectId, String location, String entryTypeId)
40+
throws Exception {
41+
EntryType entryType =
42+
EntryType.newBuilder()
43+
.setName(EntryTypeName.of(projectId, location, entryTypeId).toString())
44+
.setDescription("updated description of the entry type")
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.updateEntryTypeAsync(entryType, updateMask).get();
59+
}
60+
}
61+
}
62+
// [END dataplex_update_entry_type]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.EntryType;
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 EntryTypeIT {
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 entryTypeId = "test-entry-type" + ID;
34+
private static String expectedEntryType;
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+
public static void setUp() throws Exception {
52+
expectedEntryType =
53+
String.format("projects/%s/locations/%s/entryTypes/%s", PROJECT_ID, LOCATION, entryTypeId);
54+
// Create Entry Type resource that will be used in tests for "get", "list" and "update" methods
55+
CreateEntryType.createEntryType(PROJECT_ID, LOCATION, entryTypeId);
56+
}
57+
58+
@Test
59+
public void testListEntryTypes() throws IOException {
60+
List<EntryType> entryTypes = ListEntryTypes.listEntryTypes(PROJECT_ID, LOCATION);
61+
assertThat(entryTypes.stream().map(EntryType::getName)).contains(expectedEntryType);
62+
}
63+
64+
@Test
65+
public void testGetEntryType() throws IOException {
66+
EntryType entryType = GetEntryType.getEntryType(PROJECT_ID, LOCATION, entryTypeId);
67+
assertThat(entryType.getName()).isEqualTo(expectedEntryType);
68+
}
69+
70+
@Test
71+
public void testUpdateEntryType() throws Exception {
72+
EntryType entryType = UpdateEntryType.updateEntryType(PROJECT_ID, LOCATION, entryTypeId);
73+
assertThat(entryType.getName()).contains(expectedEntryType);
74+
}
75+
76+
@Test
77+
public void testCreateEntryType() throws Exception {
78+
String entryTypeIdToCreate = "test-entry-type" + UUID.randomUUID().toString().substring(0, 8);
79+
String expectedEntryTypeToCreate =
80+
String.format(
81+
"projects/%s/locations/%s/entryTypes/%s", PROJECT_ID, LOCATION, entryTypeIdToCreate);
82+
83+
EntryType entryType =
84+
CreateEntryType.createEntryType(PROJECT_ID, LOCATION, entryTypeIdToCreate);
85+
// Clean-up created Entry Type
86+
DeleteEntryType.deleteEntryType(PROJECT_ID, LOCATION, entryTypeIdToCreate);
87+
88+
assertThat(entryType.getName()).contains(expectedEntryTypeToCreate);
89+
}
90+
91+
@Test
92+
public void testDeleteEntryType() throws Exception {
93+
String entryTypeIdToDelete = "test-entry-type" + UUID.randomUUID().toString().substring(0, 8);
94+
// Create Entry Group to be deleted
95+
CreateEntryType.createEntryType(PROJECT_ID, LOCATION, entryTypeIdToDelete);
96+
97+
// No exception means successful call.
98+
DeleteEntryType.deleteEntryType(PROJECT_ID, LOCATION, entryTypeIdToDelete);
99+
}
100+
101+
@AfterClass
102+
public static void tearDown() throws Exception {
103+
// Clean-up Entry Group resource created in setUp()
104+
DeleteEntryType.deleteEntryType(PROJECT_ID, LOCATION, entryTypeId);
105+
}
106+
}

0 commit comments

Comments
 (0)
0