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.
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" )
0 commit comments