From a13d4f60e757cea5fb2bc88c7dc71139c9ad7b38 Mon Sep 17 00:00:00 2001 From: Dohun Kim Date: Tue, 10 Mar 2026 00:41:47 +0000 Subject: [PATCH 1/3] samples: Add example on creating instance with tags --- .../bigtable/InstanceAdminExample.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java b/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java index 07ae1fe007..523b3f55de 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java +++ b/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java @@ -87,6 +87,11 @@ public InstanceAdminExample(String projectId, String instanceId, String clusterI public void run() { createProdInstance(); + /* * OPTIONAL: Testing with Tags + * If you want to test creating an instance with resource tags, comment out + * createProdInstance() above and uncomment createProdInstanceWithTags() below. + */ + // createProdInstanceWithTags(); listInstances(); getInstance(); listClusters(); @@ -141,6 +146,63 @@ public void createProdInstance() { } } + /** + * Demonstrates how to create a Production instance within a provided project with tags. + * + *

Tags are a way to organize and govern resources across Google Cloud, see: + * https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing + * + * + * NOTE: Unlike Labels, a Tag (Key and Value) must be created before it can be + * attached to a resource. + * See: https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing and + * https://docs.cloud.google.com/bigtable/docs/tags for more information. + */ + public void createProdInstanceWithTags() { + // Checks if instance exists, creates instance if does not exists. + if (!adminClient.exists(instanceId)) { + System.out.println("Instance does not exist, creating a PRODUCTION instance with tags"); + + // These are placeholders. You must create these in your GCP Organization/Project first. + String tagKey = "tagKeys/12345"; + String tagValue = "tagValues/6789"; + + // [START bigtable_create_prod_instance_with_tags] + // Creates a Production Instance with the ID "ssd-instance", + // cluster id "ssd-cluster", 3 nodes and location "us-central1-f". + String parent = "projects/" + projectId; + Instance instanceObj = + Instance.newBuilder() + .setDisplayName(instanceId) + .setType(Instance.Type.PRODUCTION) + .putLabels("department", "accounting") + .putTags(tagKey, tagValue) + .build(); + Cluster clusterObj = + Cluster.newBuilder() + .setLocation("projects/" + projectId + "/locations/us-central1-f") + .setServeNodes(3) + .setDefaultStorageType(StorageType.SSD) + .build(); + CreateInstanceRequest request = + CreateInstanceRequest.newBuilder() + .setParent(parent) + .setInstanceId(instanceId) + .setInstance(instanceObj) + .putClusters(clusterId, clusterObj) + .build(); + // Creates a production instance with the given request. + try { + Instance instance = adminClient.getBaseClient().createInstanceAsync(request).get(); + System.out.printf("PRODUCTION type instance %s with tags created successfully%n", instance.getName()); + } catch (Exception e) { + System.err.println("Failed to create instance: " + e.getMessage()); + throw new RuntimeException(e); + } + // [END bigtable_create_prod_instance_with_tags] + } + } + /** Demonstrates how to list all instances within a project. */ public void listInstances() { System.out.println("\nListing Instances"); From 49ba8da33584ca099d3a87076bbf3dc92fa4d5d6 Mon Sep 17 00:00:00 2001 From: Dohun Kim Date: Tue, 10 Mar 2026 17:46:07 +0000 Subject: [PATCH 2/3] Grammar fixes --- .../com/example/bigtable/InstanceAdminExample.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java b/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java index 523b3f55de..784cea0eaa 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java +++ b/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java @@ -106,7 +106,7 @@ void close() { adminClient.close(); } - /** Demonstrates how to create a Production instance within a provided project. */ + /** Demonstrates how to create an instance within a provided project. */ public void createProdInstance() { // Checks if instance exists, creates instance if does not exists. if (!adminClient.exists(instanceId)) { @@ -149,17 +149,17 @@ public void createProdInstance() { /** * Demonstrates how to create a Production instance within a provided project with tags. * - *

Tags are a way to organize and govern resources across Google Cloud, see: - * https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing + *

Tags are a way to organize and govern resources across Google Cloud, see + * [Creating and managing tags](https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing) * * - * NOTE: Unlike Labels, a Tag (Key and Value) must be created before it can be + * NOTE: Unlike labels, a tag (Key and Value) must be created before it can be * attached to a resource. - * See: https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing and - * https://docs.cloud.google.com/bigtable/docs/tags for more information. + * See [Creating and managing tags](https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing) + * and [Tags overview](https://docs.cloud.google.com/bigtable/docs/tags) for more information. */ public void createProdInstanceWithTags() { - // Checks if instance exists, creates instance if does not exists. + // Creates an instance if it doesn't exist. if (!adminClient.exists(instanceId)) { System.out.println("Instance does not exist, creating a PRODUCTION instance with tags"); From fafd517fdd6022b4ac0e39e4be349e56d2d728d0 Mon Sep 17 00:00:00 2001 From: Dohun Kim Date: Tue, 10 Mar 2026 17:46:52 +0000 Subject: [PATCH 3/3] lower case tags --- .../main/java/com/example/bigtable/InstanceAdminExample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java b/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java index 784cea0eaa..1ad46874c5 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java +++ b/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java @@ -87,7 +87,7 @@ public InstanceAdminExample(String projectId, String instanceId, String clusterI public void run() { createProdInstance(); - /* * OPTIONAL: Testing with Tags + /* * OPTIONAL: Testing with tags * If you want to test creating an instance with resource tags, comment out * createProdInstance() above and uncomment createProdInstanceWithTags() below. */