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

Commit 5a45afa

Browse files
authored
samples: Add example on creating instance with tags (#2837)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigtable/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) - [ ] Rollback plan is reviewed and LGTMed - [ ] All new data plane features have a completed end to end testing plan Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md). This is dependent on 2.75.0 https://togithub.com/googleapis/java-bigtable/pull/2832
1 parent f4a9843 commit 5a45afa

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

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

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Google Inc.
2+
* Copyright 2026 Google Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,7 @@
4141
* delete Cloud Bigtable Instances and Clusters.
4242
*
4343
* <ul>
44-
* <li>creates production instance
44+
* <li>creates production instance (optionally with tags)
4545
* <li>lists instances
4646
* <li>gets instance
4747
* <li>lists clusters
@@ -60,15 +60,22 @@ public class InstanceAdminExample {
6060

6161
public static void main(String[] args) throws IOException {
6262

63-
if (args.length != 1) {
64-
System.out.println("Missing required project id");
63+
if (args.length < 1 || args.length > 2) {
64+
System.out.println("Usage: java InstanceAdminExample <project-id> [createWithTags]");
65+
System.out.println(" <project-id>: The Google Cloud project ID");
66+
System.out.println(
67+
" [createWithTags]: Optional boolean (true/false) to enable resource tags on creation");
6568
return;
6669
}
6770
String projectId = args[0];
71+
boolean createWithTags = false;
72+
if (args.length == 2) {
73+
createWithTags = Boolean.parseBoolean(args[1]);
74+
}
6875

6976
InstanceAdminExample instanceAdmin =
7077
new InstanceAdminExample(projectId, "ssd-instance", "ssd-cluster");
71-
instanceAdmin.run();
78+
instanceAdmin.run(createWithTags);
7279
}
7380

7481
public InstanceAdminExample(String projectId, String instanceId, String clusterId)
@@ -85,8 +92,8 @@ public InstanceAdminExample(String projectId, String instanceId, String clusterI
8592
adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings);
8693
}
8794

88-
public void run() {
89-
createProdInstance();
95+
public void run(boolean createWithTags) {
96+
createProdInstance(createWithTags);
9097
listInstances();
9198
getInstance();
9299
listClusters();
@@ -101,21 +108,40 @@ void close() {
101108
adminClient.close();
102109
}
103110

104-
/** Demonstrates how to create a Production instance within a provided project. */
105-
public void createProdInstance() {
111+
/**
112+
* Demonstrates how to create an instance within a provided project.
113+
*
114+
* @param createWithTags If true, adds placeholder tags to the instance.
115+
* <p>Tags are a way to organize and govern resources across Google Cloud, see [Creating and
116+
* managing tags](https://docs.cloud.google.com/resource-manager/docs/tags/tags-overview)
117+
* <p>NOTE: Unlike labels, a tag (key and value) must be created before it can be attached to
118+
* a resource. See [Creating and managing
119+
* tags](https://docs.cloud.google.com/resource-manager/docs/tags/tags-overview) and [Tags
120+
* overview](https://docs.cloud.google.com/bigtable/docs/tags) for more information.
121+
*/
122+
public void createProdInstance(boolean createWithTags) {
106123
// Checks if instance exists, creates instance if does not exists.
107124
if (!adminClient.exists(instanceId)) {
108125
System.out.println("Instance does not exist, creating a PRODUCTION instance");
109126
// [START bigtable_create_prod_instance]
110127
// Creates a Production Instance with the ID "ssd-instance",
111128
// cluster id "ssd-cluster", 3 nodes and location "us-central1-f".
112129
String parent = "projects/" + projectId;
113-
Instance instanceObj =
130+
Instance.Builder instanceObjBuilder =
114131
Instance.newBuilder()
115132
.setDisplayName(instanceId)
116133
.setType(Instance.Type.PRODUCTION)
117-
.putLabels("department", "accounting")
118-
.build();
134+
.putLabels("department", "accounting");
135+
136+
if (createWithTags) {
137+
System.out.println("Enabling tags for instance creation.");
138+
// These are placeholders. You must create these in your GCP Organization/Project first.
139+
String tagKey = "tagKeys/12345";
140+
String tagValue = "tagValues/6789";
141+
instanceObjBuilder.putTags(tagKey, tagValue);
142+
}
143+
Instance instanceObj = instanceObjBuilder.build();
144+
119145
Cluster clusterObj =
120146
Cluster.newBuilder()
121147
.setLocation("projects/" + projectId + "/locations/us-central1-f")

samples/snippets/src/test/java/com/example/bigtable/InstanceAdminExampleTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void testCreateAndDeleteInstance() throws IOException {
8888
String testCluster = generateId();
8989
InstanceAdminExample testInstanceAdmin =
9090
new InstanceAdminExample(projectId, testInstance, testCluster);
91-
testInstanceAdmin.createProdInstance();
91+
testInstanceAdmin.createProdInstance(false);
9292
assertTrue(adminClient.exists(testInstance));
9393

9494
// Deletes an instance.
@@ -121,7 +121,7 @@ public void testAddAndDeleteCluster() {
121121

122122
@Test
123123
public void testRunDoesNotFail() {
124-
instanceAdmin.run();
124+
instanceAdmin.run(false);
125125
}
126126

127127
private static String generateId() {

0 commit comments

Comments
 (0)