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..05a5422479 100644 --- a/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java +++ b/samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Google Inc. + * Copyright 2026 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,7 +41,7 @@ * delete Cloud Bigtable Instances and Clusters. * *
Tags are a way to organize and govern resources across Google Cloud, see [Creating and + * managing tags](https://docs.cloud.google.com/resource-manager/docs/tags/tags-overview) + *
NOTE: Unlike labels, a tag (key and value) must be created before it can be attached to + * a resource. See [Creating and managing + * tags](https://docs.cloud.google.com/resource-manager/docs/tags/tags-overview) and [Tags + * overview](https://docs.cloud.google.com/bigtable/docs/tags) for more information. + */ + public void createProdInstance(boolean createWithTags) { // 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"); @@ -110,12 +127,21 @@ public void createProdInstance() { // 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.Builder instanceObjBuilder = Instance.newBuilder() .setDisplayName(instanceId) .setType(Instance.Type.PRODUCTION) - .putLabels("department", "accounting") - .build(); + .putLabels("department", "accounting"); + + if (createWithTags) { + System.out.println("Enabling tags for instance creation."); + // These are placeholders. You must create these in your GCP Organization/Project first. + String tagKey = "tagKeys/12345"; + String tagValue = "tagValues/6789"; + instanceObjBuilder.putTags(tagKey, tagValue); + } + Instance instanceObj = instanceObjBuilder.build(); + Cluster clusterObj = Cluster.newBuilder() .setLocation("projects/" + projectId + "/locations/us-central1-f") diff --git a/samples/snippets/src/test/java/com/example/bigtable/InstanceAdminExampleTest.java b/samples/snippets/src/test/java/com/example/bigtable/InstanceAdminExampleTest.java index acab4fca74..9539a27bf7 100644 --- a/samples/snippets/src/test/java/com/example/bigtable/InstanceAdminExampleTest.java +++ b/samples/snippets/src/test/java/com/example/bigtable/InstanceAdminExampleTest.java @@ -88,7 +88,7 @@ public void testCreateAndDeleteInstance() throws IOException { String testCluster = generateId(); InstanceAdminExample testInstanceAdmin = new InstanceAdminExample(projectId, testInstance, testCluster); - testInstanceAdmin.createProdInstance(); + testInstanceAdmin.createProdInstance(false); assertTrue(adminClient.exists(testInstance)); // Deletes an instance. @@ -121,7 +121,7 @@ public void testAddAndDeleteCluster() { @Test public void testRunDoesNotFail() { - instanceAdmin.run(); + instanceAdmin.run(false); } private static String generateId() {