Skip to content

Commit e19017e

Browse files
Praful Makanisteffnaystephaniewang526
authored
docs(samples): adds samples from documentation with correct region tags (#392)
* docs(samples): adds samples from documentation with correct region tags * docs(samples): modified samples and add ITs * Fix IT failure * Fix IT * Update CreateEntryTests.java * Update Quickstart.java * Update Quickstart.java * Update Quickstart.java * Update Quickstart.java * Update Quickstart.java Co-authored-by: steffnay <[email protected]> Co-authored-by: Stephanie Wang <[email protected]>
1 parent 85fa8ab commit e19017e

12 files changed

+904
-18
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2020 Google Inc.
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.datacatalog;
18+
19+
// [START data_catalog_create_custom_entry]
20+
import com.google.cloud.datacatalog.v1.ColumnSchema;
21+
import com.google.cloud.datacatalog.v1.CreateEntryGroupRequest;
22+
import com.google.cloud.datacatalog.v1.CreateEntryRequest;
23+
import com.google.cloud.datacatalog.v1.DataCatalogClient;
24+
import com.google.cloud.datacatalog.v1.Entry;
25+
import com.google.cloud.datacatalog.v1.EntryGroup;
26+
import com.google.cloud.datacatalog.v1.LocationName;
27+
import com.google.cloud.datacatalog.v1.Schema;
28+
import java.io.IOException;
29+
30+
// Sample to create custom entry
31+
public class CreateCustomEntry {
32+
33+
public static void main(String[] args) throws IOException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "my-project";
36+
String entryGroupId = "onprem_entry_group";
37+
String entryId = "onprem_entry_id";
38+
createCustomEntry(projectId, entryGroupId, entryId);
39+
}
40+
41+
public static void createCustomEntry(String projectId, String entryGroupId, String entryId)
42+
throws IOException {
43+
// Currently, Data Catalog stores metadata in the us-central1 region.
44+
String location = "us-central1";
45+
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 (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
50+
// Construct the EntryGroup for the EntryGroup request.
51+
EntryGroup entryGroup =
52+
EntryGroup.newBuilder()
53+
.setDisplayName("My awesome Entry Group")
54+
.setDescription("This Entry Group represents an external system")
55+
.build();
56+
57+
// Construct the EntryGroup request to be sent by the client.
58+
CreateEntryGroupRequest entryGroupRequest =
59+
CreateEntryGroupRequest.newBuilder()
60+
.setParent(LocationName.of(projectId, location).toString())
61+
.setEntryGroupId(entryGroupId)
62+
.setEntryGroup(entryGroup)
63+
.build();
64+
65+
// Use the client to send the API request.
66+
EntryGroup createdEntryGroup = dataCatalogClient.createEntryGroup(entryGroupRequest);
67+
68+
// Construct the Entry for the Entry request.
69+
Entry entry =
70+
Entry.newBuilder()
71+
.setUserSpecifiedSystem("onprem_data_system")
72+
.setUserSpecifiedType("onprem_data_asset")
73+
.setDisplayName("My awesome data asset")
74+
.setDescription("This data asset is managed by an external system.")
75+
.setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset")
76+
.setSchema(
77+
Schema.newBuilder()
78+
.addColumns(
79+
ColumnSchema.newBuilder()
80+
.setColumn("first_column")
81+
.setDescription("This columns consists of ....")
82+
.setMode("NULLABLE")
83+
.setType("DOUBLE")
84+
.build())
85+
.addColumns(
86+
ColumnSchema.newBuilder()
87+
.setColumn("second_column")
88+
.setDescription("This columns consists of ....")
89+
.setMode("REQUIRED")
90+
.setType("STRING")
91+
.build())
92+
.build())
93+
.build();
94+
95+
// Construct the Entry request to be sent by the client.
96+
CreateEntryRequest entryRequest =
97+
CreateEntryRequest.newBuilder()
98+
.setParent(createdEntryGroup.getName())
99+
.setEntryId(entryId)
100+
.setEntry(entry)
101+
.build();
102+
103+
// Use the client to send the API request.
104+
Entry createdEntry = dataCatalogClient.createEntry(entryRequest);
105+
System.out.printf("Custom entry created with name: %s", createdEntry.getName());
106+
}
107+
}
108+
}
109+
// [END data_catalog_create_custom_entry]

datacatalog/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
package com.example.datacatalog;
1818

19-
// [START datacatalog_create_fileset_tag]
20-
21-
import com.google.api.gax.rpc.AlreadyExistsException;
19+
// [START data_catalog_create_fileset]
2220
import com.google.cloud.datacatalog.v1.ColumnSchema;
2321
import com.google.cloud.datacatalog.v1.CreateEntryRequest;
2422
import com.google.cloud.datacatalog.v1.DataCatalogClient;
@@ -29,18 +27,20 @@
2927
import com.google.cloud.datacatalog.v1.Schema;
3028
import java.io.IOException;
3129

30+
// Sample to create file set entry
3231
public class CreateFilesetEntry {
3332

34-
public static void createEntry() {
33+
public static void main(String[] args) throws IOException {
3534
// TODO(developer): Replace these variables before running the sample.
3635
String projectId = "my-project-id";
3736
String entryGroupId = "fileset_entry_group";
3837
String entryId = "fileset_entry_id";
39-
createEntry(projectId, entryGroupId, entryId);
38+
createFilesetEntry(projectId, entryGroupId, entryId);
4039
}
4140

4241
// Create Fileset Entry.
43-
public static void createEntry(String projectId, String entryGroupId, String entryId) {
42+
public static void createFilesetEntry(String projectId, String entryGroupId, String entryId)
43+
throws IOException {
4444
// Currently, Data Catalog stores metadata in the us-central1 region.
4545
String location = "us-central1";
4646

@@ -105,14 +105,9 @@ public static void createEntry(String projectId, String entryGroupId, String ent
105105
.build();
106106

107107
// Use the client to send the API request.
108-
Entry entryResponse = dataCatalogClient.createEntry(entryRequest);
109-
System.out.printf("\nEntry created with name: %s\n", entryResponse.getName());
110-
} catch (AlreadyExistsException | IOException e) {
111-
// AlreadyExistsException's are thrown if EntryGroup or Entry already exists.
112-
// IOException's are thrown when unable to create the DataCatalogClient,
113-
// for example an invalid Service Account path.
114-
System.out.println("Error in create entry process:\n" + e.toString());
108+
Entry entryCreated = dataCatalogClient.createEntry(entryRequest);
109+
System.out.printf("Entry created with name: %s", entryCreated.getName());
115110
}
116111
}
117112
}
118-
// [END datacatalog_create_fileset_tag]
113+
// [END data_catalog_create_fileset]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2020 Google Inc.
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.datacatalog;
18+
19+
// [START data_catalog_grant_tag_template_user_role]
20+
import com.google.cloud.datacatalog.v1.DataCatalogClient;
21+
import com.google.cloud.datacatalog.v1.TagTemplateName;
22+
import com.google.iam.v1.Binding;
23+
import com.google.iam.v1.Policy;
24+
import com.google.iam.v1.SetIamPolicyRequest;
25+
import java.io.IOException;
26+
27+
// Sample to grant tag access on template
28+
public class GrantTagTemplateUserRole {
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";
33+
String tagTemplateId = "my_tag_template";
34+
grantTagTemplateUserRole(projectId, tagTemplateId);
35+
}
36+
37+
public static void grantTagTemplateUserRole(String projectId, String templateId)
38+
throws IOException {
39+
// Currently, Data Catalog stores metadata in the us-central1 region.
40+
String location = "us-central1";
41+
42+
// Format the Template name.
43+
String templateName =
44+
TagTemplateName.newBuilder()
45+
.setProject(projectId)
46+
.setLocation(location)
47+
.setTagTemplate(templateId)
48+
.build()
49+
.toString();
50+
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests. After completing all of your requests, call
53+
// the "close" method on the client to safely clean up any remaining background resources.
54+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
55+
56+
// Create a Binding to add the Tag Template User role and member to the policy.
57+
Binding binding =
58+
Binding.newBuilder()
59+
.setRole("roles/datacatalog.tagTemplateUser")
60+
.addMembers("group:[email protected]")
61+
.build();
62+
63+
// Create a Policy object to update Template's IAM policy by adding the new binding.
64+
Policy policyUpdate = Policy.newBuilder().addBindings(binding).build();
65+
66+
SetIamPolicyRequest request =
67+
SetIamPolicyRequest.newBuilder()
68+
.setPolicy(policyUpdate)
69+
.setResource(templateName)
70+
.build();
71+
72+
// Update Template's policy.
73+
dataCatalogClient.setIamPolicy(request);
74+
System.out.println("Role successfully granted");
75+
}
76+
}
77+
}
78+
// [END data_catalog_grant_tag_template_user_role]

0 commit comments

Comments
 (0)