Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit a13d4f6

Browse files
committed
samples: Add example on creating instance with tags
1 parent 17707be commit a13d4f6

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

samples/snippets/src/main/java/com/example/bigtable/InstanceAdminExample.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public InstanceAdminExample(String projectId, String instanceId, String clusterI
8787

8888
public void run() {
8989
createProdInstance();
90+
/* * OPTIONAL: Testing with Tags
91+
* If you want to test creating an instance with resource tags, comment out
92+
* createProdInstance() above and uncomment createProdInstanceWithTags() below.
93+
*/
94+
// createProdInstanceWithTags();
9095
listInstances();
9196
getInstance();
9297
listClusters();
@@ -141,6 +146,63 @@ public void createProdInstance() {
141146
}
142147
}
143148

149+
/**
150+
* Demonstrates how to create a Production instance within a provided project with tags.
151+
*
152+
* <p>Tags are a way to organize and govern resources across Google Cloud, see:
153+
* https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing
154+
*
155+
*
156+
* NOTE: Unlike Labels, a Tag (Key and Value) must be created before it can be
157+
* attached to a resource.
158+
* See: https://cloud.google.com/resource-manager/docs/tags/tags-creating-and-managing and
159+
* https://docs.cloud.google.com/bigtable/docs/tags for more information.
160+
*/
161+
public void createProdInstanceWithTags() {
162+
// Checks if instance exists, creates instance if does not exists.
163+
if (!adminClient.exists(instanceId)) {
164+
System.out.println("Instance does not exist, creating a PRODUCTION instance with tags");
165+
166+
// These are placeholders. You must create these in your GCP Organization/Project first.
167+
String tagKey = "tagKeys/12345";
168+
String tagValue = "tagValues/6789";
169+
170+
// [START bigtable_create_prod_instance_with_tags]
171+
// Creates a Production Instance with the ID "ssd-instance",
172+
// cluster id "ssd-cluster", 3 nodes and location "us-central1-f".
173+
String parent = "projects/" + projectId;
174+
Instance instanceObj =
175+
Instance.newBuilder()
176+
.setDisplayName(instanceId)
177+
.setType(Instance.Type.PRODUCTION)
178+
.putLabels("department", "accounting")
179+
.putTags(tagKey, tagValue)
180+
.build();
181+
Cluster clusterObj =
182+
Cluster.newBuilder()
183+
.setLocation("projects/" + projectId + "/locations/us-central1-f")
184+
.setServeNodes(3)
185+
.setDefaultStorageType(StorageType.SSD)
186+
.build();
187+
CreateInstanceRequest request =
188+
CreateInstanceRequest.newBuilder()
189+
.setParent(parent)
190+
.setInstanceId(instanceId)
191+
.setInstance(instanceObj)
192+
.putClusters(clusterId, clusterObj)
193+
.build();
194+
// Creates a production instance with the given request.
195+
try {
196+
Instance instance = adminClient.getBaseClient().createInstanceAsync(request).get();
197+
System.out.printf("PRODUCTION type instance %s with tags created successfully%n", instance.getName());
198+
} catch (Exception e) {
199+
System.err.println("Failed to create instance: " + e.getMessage());
200+
throw new RuntimeException(e);
201+
}
202+
// [END bigtable_create_prod_instance_with_tags]
203+
}
204+
}
205+
144206
/** Demonstrates how to list all instances within a project. */
145207
public void listInstances() {
146208
System.out.println("\nListing Instances");

0 commit comments

Comments
 (0)