diff --git a/samples/snippets/src/main/java/com/example/datacatalog/CreateCustomEntry.java b/samples/snippets/src/main/java/com/example/datacatalog/CreateCustomEntry.java new file mode 100644 index 00000000..57a57c91 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/CreateCustomEntry.java @@ -0,0 +1,109 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +// [START data_catalog_create_custom_entry] +import com.google.cloud.datacatalog.v1.ColumnSchema; +import com.google.cloud.datacatalog.v1.CreateEntryGroupRequest; +import com.google.cloud.datacatalog.v1.CreateEntryRequest; +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroup; +import com.google.cloud.datacatalog.v1.LocationName; +import com.google.cloud.datacatalog.v1.Schema; +import java.io.IOException; + +// Sample to create custom entry +public class CreateCustomEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String entryGroupId = "onprem_entry_group"; + String entryId = "onprem_entry_id"; + createCustomEntry(projectId, entryGroupId, entryId); + } + + public static void createCustomEntry(String projectId, String entryGroupId, String entryId) + throws IOException { + // Currently, Data Catalog stores metadata in the us-central1 region. + String location = "us-central1"; + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + // Construct the EntryGroup for the EntryGroup request. + EntryGroup entryGroup = + EntryGroup.newBuilder() + .setDisplayName("My awesome Entry Group") + .setDescription("This Entry Group represents an external system") + .build(); + + // Construct the EntryGroup request to be sent by the client. + CreateEntryGroupRequest entryGroupRequest = + CreateEntryGroupRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .setEntryGroupId(entryGroupId) + .setEntryGroup(entryGroup) + .build(); + + // Use the client to send the API request. + EntryGroup createdEntryGroup = dataCatalogClient.createEntryGroup(entryGroupRequest); + + // Construct the Entry for the Entry request. + Entry entry = + Entry.newBuilder() + .setUserSpecifiedSystem("onprem_data_system") + .setUserSpecifiedType("onprem_data_asset") + .setDisplayName("My awesome data asset") + .setDescription("This data asset is managed by an external system.") + .setLinkedResource("//my-onprem-server.com/dataAssets/my-awesome-data-asset") + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_column") + .setDescription("This columns consists of ....") + .setMode("NULLABLE") + .setType("DOUBLE") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("second_column") + .setDescription("This columns consists of ....") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .build()) + .build(); + + // Construct the Entry request to be sent by the client. + CreateEntryRequest entryRequest = + CreateEntryRequest.newBuilder() + .setParent(createdEntryGroup.getName()) + .setEntryId(entryId) + .setEntry(entry) + .build(); + + // Use the client to send the API request. + Entry createdEntry = dataCatalogClient.createEntry(entryRequest); + System.out.printf("Custom entry created with name: %s", createdEntry.getName()); + } + } +} +// [END data_catalog_create_custom_entry] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/samples/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java index 579c18bd..c6e15771 100644 --- a/samples/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java +++ b/samples/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -16,9 +16,7 @@ package com.example.datacatalog; -// [START datacatalog_create_fileset_tag] - -import com.google.api.gax.rpc.AlreadyExistsException; +// [START data_catalog_create_fileset] import com.google.cloud.datacatalog.v1.ColumnSchema; import com.google.cloud.datacatalog.v1.CreateEntryRequest; import com.google.cloud.datacatalog.v1.DataCatalogClient; @@ -29,18 +27,20 @@ import com.google.cloud.datacatalog.v1.Schema; import java.io.IOException; +// Sample to create file set entry public class CreateFilesetEntry { - public static void createEntry() { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project-id"; String entryGroupId = "fileset_entry_group"; String entryId = "fileset_entry_id"; - createEntry(projectId, entryGroupId, entryId); + createFilesetEntry(projectId, entryGroupId, entryId); } // Create Fileset Entry. - public static void createEntry(String projectId, String entryGroupId, String entryId) { + public static void createFilesetEntry(String projectId, String entryGroupId, String entryId) + throws IOException { // Currently, Data Catalog stores metadata in the us-central1 region. String location = "us-central1"; @@ -105,14 +105,9 @@ public static void createEntry(String projectId, String entryGroupId, String ent .build(); // Use the client to send the API request. - Entry entryResponse = dataCatalogClient.createEntry(entryRequest); - System.out.printf("\nEntry created with name: %s\n", entryResponse.getName()); - } catch (AlreadyExistsException | IOException e) { - // AlreadyExistsException's are thrown if EntryGroup or Entry already exists. - // IOException's are thrown when unable to create the DataCatalogClient, - // for example an invalid Service Account path. - System.out.println("Error in create entry process:\n" + e.toString()); + Entry entryCreated = dataCatalogClient.createEntry(entryRequest); + System.out.printf("Entry created with name: %s", entryCreated.getName()); } } } -// [END datacatalog_create_fileset_tag] +// [END data_catalog_create_fileset] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/GrantTagTemplateUserRole.java b/samples/snippets/src/main/java/com/example/datacatalog/GrantTagTemplateUserRole.java new file mode 100644 index 00000000..72af7d79 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/GrantTagTemplateUserRole.java @@ -0,0 +1,78 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +// [START data_catalog_grant_tag_template_user_role] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import com.google.iam.v1.Binding; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import java.io.IOException; + +// Sample to grant tag access on template +public class GrantTagTemplateUserRole { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String tagTemplateId = "my_tag_template"; + grantTagTemplateUserRole(projectId, tagTemplateId); + } + + public static void grantTagTemplateUserRole(String projectId, String templateId) + throws IOException { + // Currently, Data Catalog stores metadata in the us-central1 region. + String location = "us-central1"; + + // Format the Template name. + String templateName = + TagTemplateName.newBuilder() + .setProject(projectId) + .setLocation(location) + .setTagTemplate(templateId) + .build() + .toString(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + + // Create a Binding to add the Tag Template User role and member to the policy. + Binding binding = + Binding.newBuilder() + .setRole("roles/datacatalog.tagTemplateUser") + .addMembers("group:example-analyst-group@google.com") + .build(); + + // Create a Policy object to update Template's IAM policy by adding the new binding. + Policy policyUpdate = Policy.newBuilder().addBindings(binding).build(); + + SetIamPolicyRequest request = + SetIamPolicyRequest.newBuilder() + .setPolicy(policyUpdate) + .setResource(templateName) + .build(); + + // Update Template's policy. + dataCatalogClient.setIamPolicy(request); + System.out.println("Role successfully granted"); + } + } +} +// [END data_catalog_grant_tag_template_user_role] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/Quickstart.java b/samples/snippets/src/main/java/com/example/datacatalog/Quickstart.java new file mode 100644 index 00000000..fcd8ff48 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/Quickstart.java @@ -0,0 +1,158 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +// [START data_catalog_quickstart] +import com.google.cloud.datacatalog.v1.CreateTagRequest; +import com.google.cloud.datacatalog.v1.CreateTagTemplateRequest; +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.FieldType; +import com.google.cloud.datacatalog.v1.FieldType.EnumType; +import com.google.cloud.datacatalog.v1.FieldType.EnumType.EnumValue; +import com.google.cloud.datacatalog.v1.FieldType.PrimitiveType; +import com.google.cloud.datacatalog.v1.LocationName; +import com.google.cloud.datacatalog.v1.LookupEntryRequest; +import com.google.cloud.datacatalog.v1.Tag; +import com.google.cloud.datacatalog.v1.TagField; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateField; +import java.io.IOException; + +public class Quickstart { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String tagTemplateId = "my_tag_template"; + createTags(projectId, tagTemplateId); + } + + public static void createTags(String projectId, String tagTemplateId) throws IOException { + // Currently, Data Catalog stores metadata in the us-central1 region. + String location = "us-central1"; + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + + // ------------------------------- + // Create a Tag Template. + // ------------------------------- + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Source of data asset") + .setType(FieldType.newBuilder().setPrimitiveType(PrimitiveType.STRING).build()) + .build(); + + TagTemplateField numRowsField = + TagTemplateField.newBuilder() + .setDisplayName("Number of rows in data asset") + .setType(FieldType.newBuilder().setPrimitiveType(PrimitiveType.DOUBLE).build()) + .build(); + + TagTemplateField hasPiiField = + TagTemplateField.newBuilder() + .setDisplayName("Has PII") + .setType(FieldType.newBuilder().setPrimitiveType(PrimitiveType.BOOL).build()) + .build(); + + TagTemplateField piiTypeField = + TagTemplateField.newBuilder() + .setDisplayName("PII type") + .setType( + FieldType.newBuilder() + .setEnumType( + EnumType.newBuilder() + .addAllowedValues( + EnumValue.newBuilder().setDisplayName("EMAIL").build()) + .addAllowedValues( + EnumValue.newBuilder() + .setDisplayName("SOCIAL SECURITY NUMBER") + .build()) + .addAllowedValues( + EnumValue.newBuilder().setDisplayName("NONE").build()) + .build()) + .build()) + .build(); + + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Demo Tag Template") + .putFields("source", sourceField) + .putFields("num_rows", numRowsField) + .putFields("has_pii", hasPiiField) + .putFields("pii_type", piiTypeField) + .build(); + + CreateTagTemplateRequest createTagTemplateRequest = + CreateTagTemplateRequest.newBuilder() + .setParent( + LocationName.newBuilder() + .setProject(projectId) + .setLocation(location) + .build() + .toString()) + .setTagTemplateId(tagTemplateId) + .setTagTemplate(tagTemplate) + .build(); + + // Create the Tag Template. + tagTemplate = dataCatalogClient.createTagTemplate(createTagTemplateRequest); + + // ------------------------------- + // Lookup Data Catalog's Entry referring to the table. + // ------------------------------- + String linkedResource = + String.format( + "//bigquery.googleapis.com/projects/%s/datasets/test_dataset/tables/test_table", + projectId); + LookupEntryRequest lookupEntryRequest = + LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); + Entry tableEntry = dataCatalogClient.lookupEntry(lookupEntryRequest); + + // ------------------------------- + // Attach a Tag to the table. + // ------------------------------- + TagField sourceValue = + TagField.newBuilder().setStringValue("Copied from tlc_yellow_trips_2017").build(); + TagField numRowsValue = TagField.newBuilder().setDoubleValue(113496874).build(); + TagField hasPiiValue = TagField.newBuilder().setBoolValue(false).build(); + TagField piiTypeValue = + TagField.newBuilder() + .setEnumValue(TagField.EnumValue.newBuilder().setDisplayName("NONE").build()) + .build(); + + Tag tag = + Tag.newBuilder() + .setTemplate(tagTemplate.getName()) + .putFields("source", sourceValue) + .putFields("num_rows", numRowsValue) + .putFields("has_pii", hasPiiValue) + .putFields("pii_type", piiTypeValue) + .build(); + + CreateTagRequest createTagRequest = + CreateTagRequest.newBuilder().setParent(tableEntry.getName()).setTag(tag).build(); + + dataCatalogClient.createTag(createTagRequest); + System.out.printf("Tag created successfully"); + } + } +} +// [END data_catalog_quickstart] diff --git a/samples/snippets/src/main/java/com/example/datacatalog/SearchAssets.java b/samples/snippets/src/main/java/com/example/datacatalog/SearchAssets.java new file mode 100644 index 00000000..98d8d247 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datacatalog/SearchAssets.java @@ -0,0 +1,60 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +// [START data_catalog_search_assets] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DataCatalogClient.SearchCatalogPagedResponse; +import com.google.cloud.datacatalog.v1.SearchCatalogRequest; +import com.google.cloud.datacatalog.v1.SearchCatalogRequest.Scope; +import com.google.cloud.datacatalog.v1.SearchCatalogResult; +import java.io.IOException; + +// Sample to search catalog +public class SearchAssets { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String query = "type=dataset"; + searchCatalog(projectId, query); + } + + public static void searchCatalog(String projectId, String query) throws IOException { + // Create a scope object setting search boundaries to the given organization. + // Scope scope = Scope.newBuilder().addIncludeOrgIds(orgId).build(); + + // Alternatively, search using project scopes. + Scope scope = Scope.newBuilder().addIncludeProjectIds(projectId).build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + // Search the catalog. + SearchCatalogRequest searchCatalogRequest = + SearchCatalogRequest.newBuilder().setScope(scope).setQuery(query).build(); + SearchCatalogPagedResponse response = dataCatalogClient.searchCatalog(searchCatalogRequest); + + System.out.println("Search results:"); + for (SearchCatalogResult result : response.iterateAll()) { + System.out.println(result); + } + } + } +} +// [END data_catalog_search_assets] diff --git a/samples/snippets/src/test/java/com/example/datacatalog/CreateCustomEntryIT.java b/samples/snippets/src/test/java/com/example/datacatalog/CreateCustomEntryIT.java new file mode 100644 index 00000000..5a81d7e5 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/CreateCustomEntryIT.java @@ -0,0 +1,98 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteEntryGroupRequest; +import com.google.cloud.datacatalog.v1.DeleteEntryRequest; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateCustomEntryIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entry; + private String entryGroup; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + entry = "CREATE_CUSTOM_ENTRY_TEST_" + ID; + entryGroup = "CREATE_CUSTOME_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Clean up + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroup, entry); + DeleteEntryRequest entryRequest = + DeleteEntryRequest.newBuilder().setName(entryName.toString()).build(); + dataCatalogClient.deleteEntry(entryRequest); + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroupRequest request = + DeleteEntryGroupRequest.newBuilder().setName(name.toString()).build(); + dataCatalogClient.deleteEntryGroup(request); + } + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testCreateCustomEntry() throws IOException { + CreateCustomEntry.createCustomEntry(PROJECT_ID, entryGroup, entry); + assertThat(bout.toString()).contains("Custom entry created with name:"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java index 2b55edeb..af023f3f 100644 --- a/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java +++ b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java @@ -19,6 +19,8 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteEntryGroupRequest; import com.google.cloud.datacatalog.v1.EntryGroupName; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -68,8 +70,12 @@ public void setUp() { @After public void tearDown() throws IOException { // Clean up - EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); - DeleteEntryGroup.deleteEntryGroup(name); + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroupRequest request = + DeleteEntryGroupRequest.newBuilder().setName(name.toString()).build(); + dataCatalogClient.deleteEntryGroup(request); + } // restores print statements in the original method System.out.flush(); System.setOut(originalPrintStream); diff --git a/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java index b09cca57..ede7e254 100644 --- a/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java +++ b/samples/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java @@ -16,7 +16,7 @@ package com.example.datacatalog; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.fail; import com.google.cloud.datacatalog.v1.DataCatalogClient; @@ -87,7 +87,7 @@ public void testCreateFilesetEntry() throws IOException { // Must create a Entry Group before creating the entry. CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); - CreateFilesetEntry.createEntry(PROJECT_ID, entryGroupId, entryId); + CreateFilesetEntry.createFilesetEntry(PROJECT_ID, entryGroupId, entryId); // Store names for clean up on teardown String expectedEntryGroupName = diff --git a/samples/snippets/src/test/java/com/example/datacatalog/CreateFilesetEntryIT.java b/samples/snippets/src/test/java/com/example/datacatalog/CreateFilesetEntryIT.java new file mode 100644 index 00000000..2b8147f6 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/CreateFilesetEntryIT.java @@ -0,0 +1,99 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteEntryGroupRequest; +import com.google.cloud.datacatalog.v1.DeleteEntryRequest; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateFilesetEntryIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String entryGroup; + private String entry; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + entryGroup = "CREATE_ENTRY_GROUP_TEST_" + ID; + entry = "CREATE_ENTRY_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @After + public void tearDown() throws IOException { + // Clean up + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroup, entry); + DeleteEntryRequest entryRequest = + DeleteEntryRequest.newBuilder().setName(entryName.toString()).build(); + dataCatalogClient.deleteEntry(entryRequest); + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroupRequest request = + DeleteEntryGroupRequest.newBuilder().setName(name.toString()).build(); + dataCatalogClient.deleteEntryGroup(request); + } + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testCreateFilesetEntry() throws IOException { + CreateFilesetEntry.createFilesetEntry(PROJECT_ID, entryGroup, entry); + assertThat(bout.toString()).contains("Entry created with name:"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/GrantTagTemplateUserRoleIT.java b/samples/snippets/src/test/java/com/example/datacatalog/GrantTagTemplateUserRoleIT.java new file mode 100644 index 00000000..0837ab16 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/GrantTagTemplateUserRoleIT.java @@ -0,0 +1,117 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.CreateTagTemplateRequest; +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteTagTemplateRequest; +import com.google.cloud.datacatalog.v1.FieldType; +import com.google.cloud.datacatalog.v1.LocationName; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateField; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GrantTagTemplateUserRoleIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String tagTemplateId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException { + tagTemplateId = "create_tag_template_id_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + LocationName parent = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Source of data asset") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Demo Tag Template") + .putFields("source", sourceField) + .build(); + CreateTagTemplateRequest request = + CreateTagTemplateRequest.newBuilder() + .setParent(parent.toString()) + .setTagTemplateId(tagTemplateId) + .setTagTemplate(tagTemplate) + .build(); + dataCatalogClient.createTagTemplate(request); + } + } + + @After + public void tearDown() throws IOException { + // Clean up + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + boolean force = true; + DeleteTagTemplateRequest request = + DeleteTagTemplateRequest.newBuilder().setName(name.toString()).setForce(force).build(); + dataCatalogClient.deleteTagTemplate(request); + } + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGrantTagTemplateUserRole() throws IOException { + GrantTagTemplateUserRole.grantTagTemplateUserRole(PROJECT_ID, tagTemplateId); + assertThat(bout.toString()).contains("Role successfully granted"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/QuickstartIT.java b/samples/snippets/src/test/java/com/example/datacatalog/QuickstartIT.java new file mode 100644 index 00000000..e48d3e41 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/QuickstartIT.java @@ -0,0 +1,91 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteTagTemplateRequest; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class QuickstartIT { + + private static final String ID = UUID.randomUUID().toString().substring(0, 8); + private static final String LOCATION = "us-central1"; + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String tagTemplateId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + tagTemplateId = "quickstart_tag_template_id_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Clean up + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + boolean force = true; + DeleteTagTemplateRequest request = + DeleteTagTemplateRequest.newBuilder().setName(name.toString()).setForce(force).build(); + dataCatalogClient.deleteTagTemplate(request); + } + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testQuickstart() throws IOException { + Quickstart.createTags(PROJECT_ID, tagTemplateId); + assertThat(bout.toString()).contains("Tag created successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/datacatalog/SearchAssetsIT.java b/samples/snippets/src/test/java/com/example/datacatalog/SearchAssetsIT.java new file mode 100644 index 00000000..d0090243 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datacatalog/SearchAssetsIT.java @@ -0,0 +1,75 @@ +/* + * Copyright 2020 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datacatalog; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class SearchAssetsIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testSearchAssets() throws IOException { + SearchAssets.searchCatalog(PROJECT_ID, "type=dataset"); + assertThat(bout.toString()).contains("Search results:"); + } +}