Skip to content

Commit 806464b

Browse files
committed
refactor createProdInstance into one function with boolean flag
1 parent 3604444 commit 806464b

1 file changed

Lines changed: 37 additions & 70 deletions

File tree

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

Lines changed: 37 additions & 70 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,21 @@ 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(" [createWithTags]: Optional boolean (true/false) to enable resource tags on creation");
6567
return;
6668
}
6769
String projectId = args[0];
70+
boolean createWithTags = false;
71+
if (args.length == 2) {
72+
createWithTags = Boolean.parseBoolean(args[1]);
73+
}
6874

6975
InstanceAdminExample instanceAdmin =
7076
new InstanceAdminExample(projectId, "ssd-instance", "ssd-cluster");
71-
instanceAdmin.run();
77+
instanceAdmin.run(createWithTags);
7278
}
7379

7480
public InstanceAdminExample(String projectId, String instanceId, String clusterId)
@@ -85,13 +91,8 @@ public InstanceAdminExample(String projectId, String instanceId, String clusterI
8591
adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings);
8692
}
8793

88-
public void run() {
89-
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();
94+
public void run(boolean createWithTags) {
95+
createProdInstance(createWithTags);
9596
listInstances();
9697
getInstance();
9798
listClusters();
@@ -106,48 +107,10 @@ void close() {
106107
adminClient.close();
107108
}
108109

109-
/** Demonstrates how to create an instance within a provided project. */
110-
public void createProdInstance() {
111-
// Checks if instance exists, creates instance if does not exists.
112-
if (!adminClient.exists(instanceId)) {
113-
System.out.println("Instance does not exist, creating a PRODUCTION instance");
114-
// [START bigtable_create_prod_instance]
115-
// Creates a Production Instance with the ID "ssd-instance",
116-
// cluster id "ssd-cluster", 3 nodes and location "us-central1-f".
117-
String parent = "projects/" + projectId;
118-
Instance instanceObj =
119-
Instance.newBuilder()
120-
.setDisplayName(instanceId)
121-
.setType(Instance.Type.PRODUCTION)
122-
.putLabels("department", "accounting")
123-
.build();
124-
Cluster clusterObj =
125-
Cluster.newBuilder()
126-
.setLocation("projects/" + projectId + "/locations/us-central1-f")
127-
.setServeNodes(3)
128-
.setDefaultStorageType(StorageType.SSD)
129-
.build();
130-
CreateInstanceRequest request =
131-
CreateInstanceRequest.newBuilder()
132-
.setParent(parent)
133-
.setInstanceId(instanceId)
134-
.setInstance(instanceObj)
135-
.putClusters(clusterId, clusterObj)
136-
.build();
137-
// Creates a production instance with the given request.
138-
try {
139-
Instance instance = adminClient.getBaseClient().createInstanceAsync(request).get();
140-
System.out.printf("PRODUCTION type instance %s created successfully%n", instance.getName());
141-
} catch (Exception e) {
142-
System.err.println("Failed to create instance: " + e.getMessage());
143-
throw new RuntimeException(e);
144-
}
145-
// [END bigtable_create_prod_instance]
146-
}
147-
}
148-
149110
/**
150-
* Demonstrates how to create an instance within a provided project with tags.
111+
* Demonstrates how to create an instance within a provided project.
112+
*
113+
* @param createWithTags If true, adds placeholder tags to the instance.
151114
*
152115
* <p>Tags are a way to organize and govern resources across Google Cloud, see
153116
* [Creating and managing tags](https://docs.cloud.google.com/resource-manager/docs/tags/tags-overview)
@@ -157,27 +120,31 @@ public void createProdInstance() {
157120
* attached to a resource.
158121
* See [Creating and managing tags](https://docs.cloud.google.com/resource-manager/docs/tags/tags-overview)
159122
* and [Tags overview](https://docs.cloud.google.com/bigtable/docs/tags) for more information.
160-
*/
161-
public void createProdInstanceWithTags() {
162-
// Creates an instance if it doesn't exist.
123+
*
124+
**/
125+
public void createProdInstance(boolean createWithTags) {
126+
// Checks if instance exists, creates instance if does not exists.
163127
if (!adminClient.exists(instanceId)) {
164-
System.out.println("Instance does not exist, creating an 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 an instance with the ID "ssd-instance",
128+
System.out.println("Instance does not exist, creating a PRODUCTION instance");
129+
// [START bigtable_create_prod_instance]
130+
// Creates a Production Instance with the ID "ssd-instance",
172131
// cluster id "ssd-cluster", 3 nodes and location "us-central1-f".
173132
String parent = "projects/" + projectId;
174-
Instance instanceObj =
133+
Instance.Builder instanceObjBuilder =
175134
Instance.newBuilder()
176135
.setDisplayName(instanceId)
177136
.setType(Instance.Type.PRODUCTION)
178-
.putLabels("department", "accounting")
179-
.putTags(tagKey, tagValue)
180-
.build();
137+
.putLabels("department", "accounting");
138+
139+
if (createWithTags) {
140+
System.out.println("Enabling tags for instance creation.");
141+
// These are placeholders. You must create these in your GCP Organization/Project first.
142+
String tagKey = "tagKeys/12345";
143+
String tagValue = "tagValues/6789";
144+
instanceObjBuilder.putTags(tagKey, tagValue);
145+
}
146+
Instance instanceObj = instanceObjBuilder.build();
147+
181148
Cluster clusterObj =
182149
Cluster.newBuilder()
183150
.setLocation("projects/" + projectId + "/locations/us-central1-f")
@@ -194,12 +161,12 @@ public void createProdInstanceWithTags() {
194161
// Creates a production instance with the given request.
195162
try {
196163
Instance instance = adminClient.getBaseClient().createInstanceAsync(request).get();
197-
System.out.printf("Instance %s with tags created successfully%n", instance.getName());
164+
System.out.printf("PRODUCTION type instance %s created successfully%n", instance.getName());
198165
} catch (Exception e) {
199166
System.err.println("Failed to create instance: " + e.getMessage());
200167
throw new RuntimeException(e);
201168
}
202-
// [END bigtable_create_prod_instance_with_tags]
169+
// [END bigtable_create_prod_instance]
203170
}
204171
}
205172

0 commit comments

Comments
 (0)