diff --git a/datacatalog/snippets/pom.xml b/datacatalog/snippets/pom.xml new file mode 100644 index 00000000000..b421fe41d33 --- /dev/null +++ b/datacatalog/snippets/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + com.example.datacatalog + datacatalog-snippets + jar + Google Data Catalog Snippets + https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/datacatalog + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + 1.8 + 1.8 + UTF-8 + + + + + + + + com.google.cloud + libraries-bom + 26.1.2 + pom + import + + + + + + + com.google.cloud + google-cloud-datacatalog + + + + + com.google.protobuf + protobuf-java-util + 3.21.7 + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.1.3 + test + + + diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateCustomEntry.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateCustomEntry.java new file mode 100644 index 00000000000..57a57c91b54 --- /dev/null +++ b/datacatalog/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/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateEntry.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateEntry.java new file mode 100644 index 00000000000..2cbc7e40a01 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateEntry.java @@ -0,0 +1,57 @@ +/* + * Copyright 2020 Google LLC + * + * 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_entry] +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.EntryGroupName; +import java.io.IOException; + +// Sample to create an entry +public class CreateEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String entryId = "MY_ENTRY_ID"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + Entry entry = Entry.newBuilder().build(); + createEntry(entryGroupName, entryId, entry); + } + + public static void createEntry(EntryGroupName entryGroupName, String entryId, Entry entry) + throws IOException { + // 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 client = DataCatalogClient.create()) { + CreateEntryRequest request = + CreateEntryRequest.newBuilder() + .setParent(entryGroupName.toString()) + .setEntryId(entryId) + .setEntry(entry) + .build(); + client.createEntry(request); + System.out.println("Entry created successfully"); + } + } +} +// [END data_catalog_create_entry] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateEntryGroup.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateEntryGroup.java new file mode 100644 index 00000000000..64133657f8a --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateEntryGroup.java @@ -0,0 +1,62 @@ +/* + * 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_entry_group] +import com.google.cloud.datacatalog.v1.CreateEntryGroupRequest; +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.EntryGroup; +import com.google.cloud.datacatalog.v1.LocationName; +import java.io.IOException; + +// Sample to create an entry group +public class CreateEntryGroup { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "us-central1"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + createEntryGroup(projectId, location, entryGroupId); + } + + // Create Entry Group. + public static void createEntryGroup(String projectId, String location, String entryGroupId) + throws IOException { + // 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()) { + EntryGroup entryGroup = + EntryGroup.newBuilder() + .setDisplayName("MY Entry Group") + .setDescription("This Entry Group consists of ....") + .build(); + + CreateEntryGroupRequest entryGroupRequest = + CreateEntryGroupRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .setEntryGroupId(entryGroupId) + .setEntryGroup(entryGroup) + .build(); + + dataCatalogClient.createEntryGroup(entryGroupRequest); + System.out.println("Entry Group created successfully"); + } + } +} +// [END data_catalog_create_entry_group] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java new file mode 100644 index 00000000000..c6e157711bb --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateFilesetEntry.java @@ -0,0 +1,113 @@ +/* + * Copyright 2019 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_fileset] +import com.google.cloud.datacatalog.v1.ColumnSchema; +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.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryType; +import com.google.cloud.datacatalog.v1.GcsFilesetSpec; +import com.google.cloud.datacatalog.v1.Schema; +import java.io.IOException; + +// Sample to create file set entry +public class CreateFilesetEntry { + + 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"; + createFilesetEntry(projectId, entryGroupId, entryId); + } + + // Create Fileset Entry. + 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"; + + // 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 Entry for the Entry request. + Entry entry = + Entry.newBuilder() + .setDisplayName("My Fileset") + .setDescription("This fileset consists of ....") + .setGcsFilesetSpec( + GcsFilesetSpec.newBuilder().addFilePatterns("gs://cloud-samples-data/*").build()) + .setSchema( + Schema.newBuilder() + .addColumns( + ColumnSchema.newBuilder() + .setColumn("first_name") + .setDescription("First name") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("last_name") + .setDescription("Last name") + .setMode("REQUIRED") + .setType("STRING") + .build()) + .addColumns( + ColumnSchema.newBuilder() + .setColumn("addresses") + .setDescription("Addresses") + .setMode("REPEATED") + .setType("RECORD") + .addSubcolumns( + ColumnSchema.newBuilder() + .setColumn("city") + .setDescription("City") + .setMode("NULLABLE") + .setType("STRING") + .build()) + .addSubcolumns( + ColumnSchema.newBuilder() + .setColumn("state") + .setDescription("State") + .setMode("NULLABLE") + .setType("STRING") + .build()) + .build()) + .build()) + .setType(EntryType.FILESET) + .build(); + + // Construct the Entry request to be sent by the client. + CreateEntryRequest entryRequest = + CreateEntryRequest.newBuilder() + .setParent(EntryGroupName.of(projectId, location, entryGroupId).toString()) + .setEntryId(entryId) + .setEntry(entry) + .build(); + + // Use the client to send the API request. + Entry entryCreated = dataCatalogClient.createEntry(entryRequest); + System.out.printf("Entry created with name: %s", entryCreated.getName()); + } + } +} +// [END data_catalog_create_fileset] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateTagTemplate.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateTagTemplate.java new file mode 100644 index 00000000000..8528185c8b5 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/CreateTagTemplate.java @@ -0,0 +1,68 @@ +/* + * Copyright 2020 Google LLC + * + * 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_tag_template] +import com.google.cloud.datacatalog.v1.CreateTagTemplateRequest; +import com.google.cloud.datacatalog.v1.DataCatalogClient; +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 java.io.IOException; + +// Sample to create tag template +public class CreateTagTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + LocationName locationName = LocationName.of(projectId, location); + String tagTemplateId = "MY_TAG_TEMPLATE_ID"; + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + createTagTemplate(locationName, tagTemplateId, tagTemplate); + } + + public static void createTagTemplate( + LocationName name, String tagTemplateId, TagTemplate template) throws IOException { + // 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 client = DataCatalogClient.create()) { + CreateTagTemplateRequest request = + CreateTagTemplateRequest.newBuilder() + .setParent(name.toString()) + .setTagTemplateId(tagTemplateId) + .setTagTemplate(template) + .build(); + client.createTagTemplate(request); + System.out.println("Tag template created successfully"); + } + } +} +// [END data_catalog_create_tag_template] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteEntry.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteEntry.java new file mode 100644 index 00000000000..8658f7f9cee --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteEntry.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020 Google LLC + * + * 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_delete_entry] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteEntryRequest; +import com.google.cloud.datacatalog.v1.EntryName; +import java.io.IOException; + +// Sample to delete a entry +public class DeleteEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String entryId = "MY_ENTRY_ID"; + EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId); + deleteEntry(entryName); + } + + public static void deleteEntry(EntryName entryName) throws IOException { + // 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 client = DataCatalogClient.create()) { + DeleteEntryRequest request = + DeleteEntryRequest.newBuilder().setName(entryName.toString()).build(); + client.deleteEntry(request); + System.out.println("Entry deleted successfully"); + } + } +} +// [END data_catalog_delete_entry] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteEntryGroup.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteEntryGroup.java new file mode 100644 index 00000000000..07460d3f8ce --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteEntryGroup.java @@ -0,0 +1,49 @@ +/* + * 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_delete_entry_group] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteEntryGroupRequest; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.IOException; + +// Sample to delete a entry group +public class DeleteEntryGroup { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + deleteEntryGroup(entryGroupName); + } + + public static void deleteEntryGroup(EntryGroupName entryGroupName) throws IOException { + // 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 client = DataCatalogClient.create()) { + DeleteEntryGroupRequest request = + DeleteEntryGroupRequest.newBuilder().setName(entryGroupName.toString()).build(); + client.deleteEntryGroup(request); + System.out.println("Entry group deleted successfully"); + } + } +} +// [END data_catalog_delete_entry_group] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteTagTemplate.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteTagTemplate.java new file mode 100644 index 00000000000..fc1a2a04871 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/DeleteTagTemplate.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020 Google LLC + * + * 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_delete_tag_template] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.DeleteTagTemplateRequest; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.IOException; + +// Sample to delete tag template +public class DeleteTagTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String tagTemplateId = "MY_TAG_TEMPLATE_ID"; + TagTemplateName tagTemplate = TagTemplateName.of(projectId, location, tagTemplateId); + deleteTagTemplate(tagTemplate); + } + + public static void deleteTagTemplate(TagTemplateName template) throws IOException { + // 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 client = DataCatalogClient.create()) { + DeleteTagTemplateRequest request = + DeleteTagTemplateRequest.newBuilder().setName(template.toString()).setForce(true).build(); + client.deleteTagTemplate(request); + System.out.println("Tag template deleted successfully"); + } + } +} +// [END data_catalog_delete_tag_template] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/GetEntry.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/GetEntry.java new file mode 100644 index 00000000000..572af94e5d5 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/GetEntry.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020 Google LLC + * + * 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_get_entry] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryName; +import java.io.IOException; + +// Sample to get an entity +public class GetEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String entryId = "MY_ENTRY_ID"; + EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId); + getEntry(entryName); + } + + public static void getEntry(EntryName entryName) throws IOException { + // 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 client = DataCatalogClient.create()) { + Entry entry = client.getEntry(entryName); + System.out.println("Entry retrieved successfully: " + entry.getName()); + } + } +} +// [END data_catalog_get_entry] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/GetEntryGroup.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/GetEntryGroup.java new file mode 100644 index 00000000000..ee73efff8ae --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/GetEntryGroup.java @@ -0,0 +1,47 @@ +/* + * Copyright 2020 Google LLC + * + * 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_get_entry_group] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.EntryGroup; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.IOException; + +// Sample to get an entity group +public class GetEntryGroup { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + getEntryGroup(entryGroupName); + } + + public static void getEntryGroup(EntryGroupName entryGroupName) throws IOException { + // 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 client = DataCatalogClient.create()) { + EntryGroup entryGroup = client.getEntryGroup(entryGroupName); + System.out.println("Entry group retrieved successfully: " + entryGroup.getName()); + } + } +} +// [END data_catalog_get_entry_group] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/GetTagTemplate.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/GetTagTemplate.java new file mode 100644 index 00000000000..8d7e76caa46 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/GetTagTemplate.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020 Google LLC + * + * 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_get_tag_template] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.GetTagTemplateRequest; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import java.io.IOException; + +// Sample to get tag template +public class GetTagTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String tagTemplateId = "MY_TAG_TEMPLATE_ID"; + TagTemplateName tagTemplate = TagTemplateName.of(projectId, location, tagTemplateId); + getTagTemplate(tagTemplate); + } + + public static void getTagTemplate(TagTemplateName template) throws IOException { + // 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 client = DataCatalogClient.create()) { + GetTagTemplateRequest request = + GetTagTemplateRequest.newBuilder().setName(template.toString()).build(); + TagTemplate tagTemplate = client.getTagTemplate(request); + System.out.println("Tag template retrieved successfully :" + tagTemplate.getName()); + } + } +} +// [END data_catalog_get_tag_template] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/GrantTagTemplateUserRole.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/GrantTagTemplateUserRole.java new file mode 100644 index 00000000000..72af7d79bb7 --- /dev/null +++ b/datacatalog/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/datacatalog/snippets/src/main/java/com/example/datacatalog/ListEntries.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/ListEntries.java new file mode 100644 index 00000000000..c635e5917e1 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/ListEntries.java @@ -0,0 +1,51 @@ +/* + * Copyright 2020 Google LLC + * + * 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_list_entries] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import java.io.IOException; + +// Sample to get list of entries +public class ListEntries { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + listEntries(entryGroupName); + } + + public static void listEntries(EntryGroupName entryGroupName) throws IOException { + // 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 client = DataCatalogClient.create()) { + DataCatalogClient.ListEntriesPagedResponse listEntries = client.listEntries(entryGroupName); + listEntries + .iterateAll() + .forEach( + entry -> { + System.out.println("Entry name : " + entry.getName()); + }); + } + } +} +// [END data_catalog_list_entries] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/ListEntryGroups.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/ListEntryGroups.java new file mode 100644 index 00000000000..aa6194e5268 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/ListEntryGroups.java @@ -0,0 +1,55 @@ +/* + * Copyright 2020 Google LLC + * + * 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_list_entry_groups] + +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.ListEntryGroupsRequest; +import com.google.cloud.datacatalog.v1.LocationName; +import java.io.IOException; + +// Sample to get list of entry group +public class ListEntryGroups { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + LocationName name = LocationName.of(projectId, location); + listEntryGroups(name); + } + + public static void listEntryGroups(LocationName name) throws IOException { + // 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 client = DataCatalogClient.create()) { + ListEntryGroupsRequest request = + ListEntryGroupsRequest.newBuilder().setParent(name.toString()).build(); + DataCatalogClient.ListEntryGroupsPagedResponse listEntryGroups = + client.listEntryGroups(request); + listEntryGroups + .iterateAll() + .forEach( + entryGroup -> { + System.out.println("Entry group name : " + entryGroup.getName()); + }); + } + } +} +// [END data_catalog_list_entry_groups] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryBigQueryDataset.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryBigQueryDataset.java new file mode 100644 index 00000000000..09314ea1ebf --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryBigQueryDataset.java @@ -0,0 +1,58 @@ +/* + * Copyright 2019 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_bigquery_lookup_dataset] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.LookupEntryRequest; + +public class LookupEntryBigQueryDataset { + + /** + * Lookup the Data Catalog entry referring to a BigQuery Dataset + * + * @param projectId The project ID to which the Dataset belongs, e.g. 'my-project' + * @param datasetId The dataset ID to which the Catalog Entry refers, e.g. 'my_dataset' + */ + public static void lookupEntry(String projectId, String datasetId) { + // String projectId = "my-project" + // String datasetId = "my_dataset" + + // Get an entry by the resource name from the source Google Cloud Platform service. + String linkedResource = + String.format("//bigquery.googleapis.com/projects/%s/datasets/%s", projectId, datasetId); + LookupEntryRequest request = + LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); + + // Alternatively, lookup by the SQL name of the entry would have the same result: + // String sqlResource = String.format("bigquery.dataset.`%s`.`%s`", projectId, datasetId); + // LookupEntryRequest request = + // LookupEntryRequest.newBuilder().setSqlResource(sqlResource).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()) { + Entry entry = dataCatalogClient.lookupEntry(request); + System.out.printf("Entry name: %s\n", entry.getName()); + } catch (Exception e) { + System.out.print("Error during lookupEntryBigQueryDataset:\n" + e.toString()); + } + } +} +// [END data_catalog_bigquery_lookup_dataset] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryBigQueryTable.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryBigQueryTable.java new file mode 100644 index 00000000000..0cf8b2ff740 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryBigQueryTable.java @@ -0,0 +1,61 @@ +/* + * Copyright 2019 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 com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.LookupEntryRequest; + +public class LookupEntryBigQueryTable { + + /** + * Lookup the Data Catalog entry referring to a BigQuery Table + * + * @param projectId The project ID to which the Dataset belongs, e.g. 'my-project' + * @param datasetId The dataset ID to which the Table belongs, e.g. 'my_dataset' + * @param tableId The table ID to which the Catalog Entry refers, e.g. 'my_table' + */ + public static void lookupEntry(String projectId, String datasetId, String tableId) { + // String projectId = "my-project" + // String datasetId = "my_dataset" + // String tableId = "my_table" + + // Get an entry by the resource name from the source Google Cloud Platform service. + String linkedResource = + String.format( + "//bigquery.googleapis.com/projects/%s/datasets/%s/tables/%s", + projectId, datasetId, tableId); + LookupEntryRequest request = + LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); + + // Alternatively, lookup by the SQL name of the entry would have the same result: + // String sqlResource = String.format("bigquery.table.`%s`.`%s`.`%s`", projectId, datasetId, + // tableId); + // LookupEntryRequest request = + // LookupEntryRequest.newBuilder().setSqlResource(sqlResource).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()) { + Entry entry = dataCatalogClient.lookupEntry(request); + System.out.printf("Entry name: %s\n", entry.getName()); + } catch (Exception e) { + System.out.print("Error during lookupEntryBigQueryTable:\n" + e.toString()); + } + } +} diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryPubSubTopic.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryPubSubTopic.java new file mode 100644 index 00000000000..9db4c4c53c4 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/LookupEntryPubSubTopic.java @@ -0,0 +1,56 @@ +/* + * Copyright 2019 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 com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.LookupEntryRequest; + +public class LookupEntryPubSubTopic { + + /** + * Lookup the Data Catalog entry referring to a BigQuery Dataset + * + * @param projectId The project ID to which the Dataset belongs, e.g. 'my-project' + * @param topicId The topic ID to which the Catalog Entry refers, e.g. 'my-topic' + */ + public static void lookupEntry(String projectId, String topicId) { + // String projectId = "my-project" + // String topicId = "my-topic" + + // Get an entry by the resource name from the source Google Cloud Platform service. + String linkedResource = + String.format("//pubsub.googleapis.com/projects/%s/topics/%s", projectId, topicId); + LookupEntryRequest request = + LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); + + // Alternatively, lookup by the SQL name of the entry would have the same result: + // String sqlResource = String.format("pubsub.topic.`%s`.`%s`", projectId, topicId); + // LookupEntryRequest request = + // LookupEntryRequest.newBuilder().setSqlResource(sqlResource).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()) { + Entry entry = dataCatalogClient.lookupEntry(request); + System.out.printf("Entry name: %s\n", entry.getName()); + } catch (Exception e) { + System.out.print("Error during lookupEntryPubSubTopic:\n" + e.toString()); + } + } +} diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/Quickstart.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/Quickstart.java new file mode 100644 index 00000000000..fcd8ff4854f --- /dev/null +++ b/datacatalog/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/datacatalog/snippets/src/main/java/com/example/datacatalog/SearchAssets.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/SearchAssets.java new file mode 100644 index 00000000000..98d8d247d2f --- /dev/null +++ b/datacatalog/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/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateEntry.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateEntry.java new file mode 100644 index 00000000000..ea965215b7f --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateEntry.java @@ -0,0 +1,57 @@ +/* + * Copyright 2020 Google LLC + * + * 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_update_entry] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.UpdateEntryRequest; +import com.google.protobuf.FieldMask; +import com.google.protobuf.util.FieldMaskUtil; +import java.io.IOException; + +// Sample to update an entity +public class UpdateEntry { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String entryId = "MY_ENTRY_ID"; + String description = "MY_DESCRIPTION"; + EntryName entryName = EntryName.of(projectId, location, entryGroupId, entryId); + Entry entry = + Entry.newBuilder().setName(entryName.toString()).setDescription(description).build(); + updateEntry(entry); + } + + public static void updateEntry(Entry entry) throws IOException { + // 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 client = DataCatalogClient.create()) { + FieldMask fieldMask = FieldMaskUtil.fromString("description"); + UpdateEntryRequest request = + UpdateEntryRequest.newBuilder().setEntry(entry).setUpdateMask(fieldMask).build(); + Entry entryUpdate = client.updateEntry(request); + System.out.println("Entry updated successfully : " + entryUpdate.getDescription()); + } + } +} +// [END data_catalog_update_entry] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateEntryGroup.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateEntryGroup.java new file mode 100644 index 00000000000..5c8bf3f3eb9 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateEntryGroup.java @@ -0,0 +1,62 @@ +/* + * Copyright 2020 Google LLC + * + * 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_update_entry_group] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.EntryGroup; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.UpdateEntryGroupRequest; +import com.google.protobuf.FieldMask; +import com.google.protobuf.util.FieldMaskUtil; +import java.io.IOException; + +// Sample to update an entity group +public class UpdateEntryGroup { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String entryGroupId = "MY_ENTRY_GROUP_ID"; + String description = "MY_DESCRIPTION"; + EntryGroupName entryGroupName = EntryGroupName.of(projectId, location, entryGroupId); + EntryGroup entryGroup = + EntryGroup.newBuilder() + .setName(entryGroupName.toString()) + .setDescription(description) + .build(); + updateEntryGroup(entryGroup); + } + + public static void updateEntryGroup(EntryGroup entryGroup) throws IOException { + // 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 client = DataCatalogClient.create()) { + FieldMask fieldMask = FieldMaskUtil.fromString("description"); + UpdateEntryGroupRequest request = + UpdateEntryGroupRequest.newBuilder() + .setEntryGroup(entryGroup) + .setUpdateMask(fieldMask) + .build(); + EntryGroup entryGroupUpdate = client.updateEntryGroup(request); + System.out.println("Entry group updated successfully : " + entryGroupUpdate.getDescription()); + } + } +} +// [END data_catalog_update_entry_group] diff --git a/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateTagTemplate.java b/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateTagTemplate.java new file mode 100644 index 00000000000..ebef7ea1df9 --- /dev/null +++ b/datacatalog/snippets/src/main/java/com/example/datacatalog/UpdateTagTemplate.java @@ -0,0 +1,63 @@ +/* + * Copyright 2020 Google LLC + * + * 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_update_tag_template] +import com.google.cloud.datacatalog.v1.DataCatalogClient; +import com.google.cloud.datacatalog.v1.TagTemplate; +import com.google.cloud.datacatalog.v1.TagTemplateName; +import com.google.cloud.datacatalog.v1.UpdateTagTemplateRequest; +import com.google.protobuf.FieldMask; +import com.google.protobuf.util.FieldMaskUtil; +import java.io.IOException; + +// Sample to update tag template +public class UpdateTagTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String location = "MY_LOCATION"; + String tagTemplateId = "MY_TAG_TEMPLATE_ID"; + String displayName = "MY_DISPLAY_NAME"; + TagTemplateName tagTemplate = TagTemplateName.of(projectId, location, tagTemplateId); + TagTemplate template = + TagTemplate.newBuilder() + .setName(tagTemplate.toString()) + .setDisplayName(displayName) + .build(); + updateTagTemplate(template); + } + + public static void updateTagTemplate(TagTemplate template) throws IOException { + // 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 client = DataCatalogClient.create()) { + FieldMask fieldMask = FieldMaskUtil.fromString("display_name"); + UpdateTagTemplateRequest request = + UpdateTagTemplateRequest.newBuilder() + .setTagTemplate(template) + .setUpdateMask(fieldMask) + .build(); + TagTemplate tagTemplateUpdate = client.updateTagTemplate(request); + System.out.println( + "Tag template updated successfully : " + tagTemplateUpdate.getDisplayName()); + } + } +} +// [END data_catalog_update_tag_template] diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateCustomEntryIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateCustomEntryIT.java new file mode 100644 index 00000000000..5a81d7e540e --- /dev/null +++ b/datacatalog/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/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java new file mode 100644 index 00000000000..af023f3f8ed --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryGroupIT.java @@ -0,0 +1,90 @@ +/* + * 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.EntryGroupName; +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 CreateEntryGroupIT { + + 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 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() { + entryGroup = "CREATE_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()) { + 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 testCreateEntryGroup() throws IOException { + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + assertThat(bout.toString()).contains("Entry Group created"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryIT.java new file mode 100644 index 00000000000..9260584e64a --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryIT.java @@ -0,0 +1,119 @@ +/* + * 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.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +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 CreateEntryIT { + + 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 entryId; + private String entryGroupId; + 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 { + entryId = "CREATE_ENTRY_TEST_" + ID; + entryGroupId = "CREATE_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testCreateEntry() throws IOException { + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + 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(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + assertThat(bout.toString()).contains("Entry created successfully"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java new file mode 100644 index 00000000000..ede7e254c82 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateEntryTests.java @@ -0,0 +1,129 @@ +/* + * Copyright 2019 Google LLC + * + * 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 org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; + +import com.google.cloud.datacatalog.v1.DataCatalogClient; +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.ArrayList; +import java.util.List; +import java.util.UUID; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link CreateFilesetEntry} and {@link CreateEntryGroup}. */ +@RunWith(JUnit4.class) +public class CreateEntryTests { + + private ByteArrayOutputStream bout; + + private static String LOCATION = "us-central1"; + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + + private static List entryGroupsPendingDeletion = new ArrayList<>(); + private static List entriesPendingDeletion = new ArrayList<>(); + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @After + public void tearDown() { + System.setOut(null); + bout.reset(); + } + + @AfterClass + public static void tearDownClass() { + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { + // Must delete Entries before deleting the Entry Group. + if (entriesPendingDeletion.isEmpty() || entryGroupsPendingDeletion.isEmpty()) { + fail("Something went wrong, no entries were generated"); + } + + for (String entryName : entriesPendingDeletion) { + dataCatalogClient.deleteEntry(entryName); + } + + for (String entryGroupName : entryGroupsPendingDeletion) { + dataCatalogClient.deleteEntryGroup(entryGroupName); + } + } catch (Exception e) { + System.out.println("Error in cleaning up test data:\n" + e.toString()); + } + } + + @Test + public void testCreateFilesetEntry() throws IOException { + String entryGroupId = "fileset_entry_group_parent_" + getUuid8Chars(); + String entryId = "fileset_entry_id_" + getUuid8Chars(); + + // Must create a Entry Group before creating the entry. + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + CreateFilesetEntry.createFilesetEntry(PROJECT_ID, entryGroupId, entryId); + + // Store names for clean up on teardown + String expectedEntryGroupName = + EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId).toString(); + entryGroupsPendingDeletion.add(expectedEntryGroupName); + + String expectedEntryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId).toString(); + entriesPendingDeletion.add(expectedEntryName); + + String output = bout.toString(); + + String entryTemplate = "Entry created with name: %s"; + assertThat( + output, CoreMatchers.containsString(String.format(entryTemplate, expectedEntryName))); + } + + @Test + public void testCreateEntryGroup() throws IOException { + String entryGroupId = "entry_group_no_children_" + getUuid8Chars(); + + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + + // Store names for clean up on teardown + String expectedEntryGroupName = + EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId).toString(); + entryGroupsPendingDeletion.add(expectedEntryGroupName); + + String output = bout.toString(); + + String entryGroupTemplate = "Entry Group created successfully"; + assertThat( + output, + CoreMatchers.containsString(String.format(entryGroupTemplate, expectedEntryGroupName))); + } + + private String getUuid8Chars() { + return UUID.randomUUID().toString().substring(0, 8); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateFilesetEntryIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateFilesetEntryIT.java new file mode 100644 index 00000000000..2b8147f67cb --- /dev/null +++ b/datacatalog/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/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateTagTemplateIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateTagTemplateIT.java new file mode 100644 index 00000000000..037440d1cfa --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/CreateTagTemplateIT.java @@ -0,0 +1,100 @@ +/* + * Copyright 2020 Google LLC + * + * 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.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 CreateTagTemplateIT { + + 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_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Clean up + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + DeleteTagTemplate.deleteTagTemplate(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testCreateTagTemplate() throws IOException { + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + CreateTagTemplate.createTagTemplate(locationName, tagTemplateId, tagTemplate); + assertThat(bout.toString()).contains("Tag template created successfully"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteEntryGroupIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteEntryGroupIT.java new file mode 100644 index 00000000000..6f5fac2ce75 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteEntryGroupIT.java @@ -0,0 +1,84 @@ +/* + * 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.EntryGroupName; +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 DeleteEntryGroupIT { + + 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 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 { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + entryGroup = "DELETE_ENTRY_GROUP_TEST_" + ID; + // create temporary entry group + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @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 testDeleteEntryGroup() throws IOException { + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + assertThat(bout.toString()).contains("Entry group deleted successfully"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteEntryIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteEntryIT.java new file mode 100644 index 00000000000..124c298927e --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteEntryIT.java @@ -0,0 +1,120 @@ +/* + * 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.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +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 DeleteEntryIT { + + 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 entryId; + private String entryGroupId; + 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 { + entryId = "DELETE_ENTRY_TEST_" + ID; + entryGroupId = "DELETE_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + // create a temporary entry group and entry + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + 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(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testDeleteEntry() throws IOException { + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + assertThat(bout.toString()).contains("Entry deleted successfully"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteTagTemplateIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteTagTemplateIT.java new file mode 100644 index 00000000000..d979845fa21 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/DeleteTagTemplateIT.java @@ -0,0 +1,100 @@ +/* + * Copyright 2020 Google LLC + * + * 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.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 DeleteTagTemplateIT { + + 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 = "delete_tag_template_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + // create a tempory tag template + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + CreateTagTemplate.createTagTemplate(locationName, tagTemplateId, tagTemplate); + } + + @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 testDeleteTagTemplate() throws IOException { + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + DeleteTagTemplate.deleteTagTemplate(name); + assertThat(bout.toString()).contains("Tag template deleted successfully"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/GetEntryGroupIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/GetEntryGroupIT.java new file mode 100644 index 00000000000..0034e00aae3 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/GetEntryGroupIT.java @@ -0,0 +1,87 @@ +/* + * 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.EntryGroupName; +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 GetEntryGroupIT { + + 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 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 { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + entryGroup = "GET_ENTRY_GROUP_TEST_" + ID; + // create temporary entry group + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @After + public void tearDown() throws IOException { + // clean up + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGetEntryGroup() throws IOException { + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + GetEntryGroup.getEntryGroup(name); + assertThat(bout.toString()).contains("Entry group retrieved successfully:"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/GetEntryIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/GetEntryIT.java new file mode 100644 index 00000000000..7693ade9db2 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/GetEntryIT.java @@ -0,0 +1,121 @@ +/* + * 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.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +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 GetEntryIT { + + 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 entryId; + private String entryGroupId; + 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 { + entryId = "GET_ENTRY_TEST_" + ID; + entryGroupId = "GET_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + 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(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGetEntry() throws IOException { + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + GetEntry.getEntry(entryName); + assertThat(bout.toString()).contains("Entry retrieved successfully:"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/GetTagTemplateIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/GetTagTemplateIT.java new file mode 100644 index 00000000000..374b896f255 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/GetTagTemplateIT.java @@ -0,0 +1,103 @@ +/* + * Copyright 2020 Google LLC + * + * 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.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 GetTagTemplateIT { + + 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 = "get_tag_template_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + // create a tempory tag template + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + CreateTagTemplate.createTagTemplate(locationName, tagTemplateId, tagTemplate); + } + + @After + public void tearDown() throws IOException { + // clean up + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + DeleteTagTemplate.deleteTagTemplate(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGetTagTemplate() throws IOException { + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + GetTagTemplate.getTagTemplate(name); + assertThat(bout.toString()).contains("Tag template retrieved successfully"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/GrantTagTemplateUserRoleIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/GrantTagTemplateUserRoleIT.java new file mode 100644 index 00000000000..0837ab16fb6 --- /dev/null +++ b/datacatalog/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/datacatalog/snippets/src/test/java/com/example/datacatalog/ListEntriesIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/ListEntriesIT.java new file mode 100644 index 00000000000..1e85fee2131 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/ListEntriesIT.java @@ -0,0 +1,121 @@ +/* + * 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.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +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 ListEntriesIT { + + 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 entryId; + private String entryGroupId; + 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 { + entryId = "LIST_ENTRIES_TEST_" + ID; + entryGroupId = "LIST_ENTRIES_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + 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(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testListEntries() throws IOException { + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + ListEntries.listEntries(entryGroupName); + assertThat(bout.toString()).contains("Entry name :"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/ListEntryGroupsIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/ListEntryGroupsIT.java new file mode 100644 index 00000000000..9426878a4aa --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/ListEntryGroupsIT.java @@ -0,0 +1,87 @@ +/* + * 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.EntryGroupName; +import com.google.cloud.datacatalog.v1.LocationName; +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 ListEntryGroupsIT { + + 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 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 { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + entryGroup = "LIST_ENTRY_GROUPS_TEST_" + ID; + // create temporary entry group + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @After + public void tearDown() throws IOException { + // clean up + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testListEntryGroups() throws IOException { + ListEntryGroups.listEntryGroups(LocationName.of(PROJECT_ID, LOCATION)); + assertThat(bout.toString()).contains("Entry group name :"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/LookupEntryTests.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/LookupEntryTests.java new file mode 100644 index 00000000000..bf254fec42e --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/LookupEntryTests.java @@ -0,0 +1,81 @@ +/* + * Copyright 2019 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 java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class LookupEntryTests { + + private static final String BIGQUERY_PROJECT = "bigquery-public-data"; + private static final String BIGQUERY_DATASET = "new_york_taxi_trips"; + private static final String BIGQUERY_TABLE = "taxi_zone_geom"; + + private static final String PUBSUB_PROJECT = "pubsub-public-data"; + private static final String PUBSUB_TOPIC = "taxirides-realtime"; + + private ByteArrayOutputStream bout; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + bout.close(); + System.setOut(null); + } + + @Test + public void testLookupEntryBigQueryDataset() { + LookupEntryBigQueryDataset.lookupEntry(BIGQUERY_PROJECT, BIGQUERY_DATASET); + String got = bout.toString(); + assertThat(got) + .containsMatch( + "projects/" + BIGQUERY_PROJECT + "/locations/.+?/entryGroups/@bigquery/entries/.+?$"); + } + + @Test + public void testLookupEntryBigQueryTable() { + LookupEntryBigQueryTable.lookupEntry(BIGQUERY_PROJECT, BIGQUERY_DATASET, BIGQUERY_TABLE); + String got = bout.toString(); + assertThat(got) + .containsMatch( + "projects/" + BIGQUERY_PROJECT + "/locations/.+?/entryGroups/@bigquery/entries/.+?$"); + } + + @Test + public void testLookupPubSubTopic() { + LookupEntryPubSubTopic.lookupEntry(PUBSUB_PROJECT, PUBSUB_TOPIC); + String got = bout.toString(); + assertThat(got) + .containsMatch( + "projects/" + PUBSUB_PROJECT + "/locations/.+?/entryGroups/@pubsub/entries/.+?$"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/QuickstartIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/QuickstartIT.java new file mode 100644 index 00000000000..e48d3e41077 --- /dev/null +++ b/datacatalog/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/datacatalog/snippets/src/test/java/com/example/datacatalog/SearchAssetsIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/SearchAssetsIT.java new file mode 100644 index 00000000000..64be8c2df60 --- /dev/null +++ b/datacatalog/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:"); + } +} \ No newline at end of file diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateEntryGroupIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateEntryGroupIT.java new file mode 100644 index 00000000000..aac12d51017 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateEntryGroupIT.java @@ -0,0 +1,90 @@ +/* + * 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.EntryGroup; +import com.google.cloud.datacatalog.v1.EntryGroupName; +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 UpdateEntryGroupIT { + + 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 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 { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + entryGroup = "UPDATE_ENTRY_GROUP_TEST_" + ID; + // create temporary entry group + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroup); + } + + @After + public void tearDown() throws IOException { + // clean up + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + DeleteEntryGroup.deleteEntryGroup(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testUpdateEntryGroup() throws IOException { + EntryGroupName name = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroup); + EntryGroup entryGroup = + EntryGroup.newBuilder().setName(name.toString()).setDescription("test-description").build(); + UpdateEntryGroup.updateEntryGroup(entryGroup); + assertThat(bout.toString()).contains("Entry group updated successfully :"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateEntryIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateEntryIT.java new file mode 100644 index 00000000000..cc289bb219b --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateEntryIT.java @@ -0,0 +1,123 @@ +/* + * 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.ColumnSchema; +import com.google.cloud.datacatalog.v1.Entry; +import com.google.cloud.datacatalog.v1.EntryGroupName; +import com.google.cloud.datacatalog.v1.EntryName; +import com.google.cloud.datacatalog.v1.Schema; +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 UpdateEntryIT { + + 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 entryId; + private String entryGroupId; + 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 { + entryId = "UPDATE_ENTRY_TEST_" + ID; + entryGroupId = "UPDATE_ENTRY_GROUP_TEST_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + 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(); + CreateEntry.createEntry(entryGroupName, entryId, entry); + } + + @After + public void tearDown() throws IOException { + // Clean up + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + DeleteEntry.deleteEntry(entryName); + EntryGroupName entryGroupName = EntryGroupName.of(PROJECT_ID, LOCATION, entryGroupId); + DeleteEntryGroup.deleteEntryGroup(entryGroupName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testUpdateEntry() throws IOException { + EntryName entryName = EntryName.of(PROJECT_ID, LOCATION, entryGroupId, entryId); + Entry entry = + Entry.newBuilder().setName(entryName.toString()).setDescription("test_description").build(); + UpdateEntry.updateEntry(entry); + assertThat(bout.toString()).contains("Entry updated successfully :"); + } +} diff --git a/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateTagTemplateIT.java b/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateTagTemplateIT.java new file mode 100644 index 00000000000..5f351ac1673 --- /dev/null +++ b/datacatalog/snippets/src/test/java/com/example/datacatalog/UpdateTagTemplateIT.java @@ -0,0 +1,108 @@ +/* + * Copyright 2020 Google LLC + * + * 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.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 UpdateTagTemplateIT { + + 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 = "update_tag_template_test_" + ID; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + // create a tempory tag template + LocationName locationName = LocationName.of(PROJECT_ID, LOCATION); + TagTemplateField sourceField = + TagTemplateField.newBuilder() + .setDisplayName("Your display name") + .setType( + FieldType.newBuilder().setPrimitiveType(FieldType.PrimitiveType.STRING).build()) + .build(); + TagTemplate tagTemplate = + TagTemplate.newBuilder() + .setDisplayName("Your display name") + .putFields("sourceField", sourceField) + .build(); + CreateTagTemplate.createTagTemplate(locationName, tagTemplateId, tagTemplate); + } + + @After + public void tearDown() throws IOException { + // clean up + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + DeleteTagTemplate.deleteTagTemplate(name); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testUpdateTagTemplate() throws IOException { + TagTemplateName name = TagTemplateName.of(PROJECT_ID, LOCATION, tagTemplateId); + TagTemplate template = + TagTemplate.newBuilder() + .setName(name.toString()) + .setDisplayName("test_display_name") + .build(); + UpdateTagTemplate.updateTagTemplate(template); + assertThat(bout.toString()).contains("Tag template updated successfully :"); + } +}