forked from googleapis/java-bigtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigtableInstanceAdminClient.java
1414 lines (1315 loc) · 49.6 KB
/
BigtableInstanceAdminClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.admin.v2;
import com.google.api.core.ApiAsyncFunction;
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.gax.rpc.ApiExceptions;
import com.google.api.gax.rpc.NotFoundException;
import com.google.bigtable.admin.v2.DeleteAppProfileRequest;
import com.google.bigtable.admin.v2.GetAppProfileRequest;
import com.google.bigtable.admin.v2.ListAppProfilesRequest;
import com.google.bigtable.admin.v2.PartialUpdateClusterRequest;
import com.google.cloud.Policy;
import com.google.cloud.Policy.DefaultMarshaller;
import com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage;
import com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPagedResponse;
import com.google.cloud.bigtable.admin.v2.internal.NameUtil;
import com.google.cloud.bigtable.admin.v2.models.AppProfile;
import com.google.cloud.bigtable.admin.v2.models.Cluster;
import com.google.cloud.bigtable.admin.v2.models.ClusterAutoscalingConfig;
import com.google.cloud.bigtable.admin.v2.models.CreateAppProfileRequest;
import com.google.cloud.bigtable.admin.v2.models.CreateClusterRequest;
import com.google.cloud.bigtable.admin.v2.models.CreateInstanceRequest;
import com.google.cloud.bigtable.admin.v2.models.Instance;
import com.google.cloud.bigtable.admin.v2.models.PartialListClustersException;
import com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException;
import com.google.cloud.bigtable.admin.v2.models.UpdateAppProfileRequest;
import com.google.cloud.bigtable.admin.v2.models.UpdateInstanceRequest;
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStub;
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
import com.google.protobuf.Empty;
import com.google.protobuf.util.FieldMaskUtil;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;
/**
* Client for creating, configuring and deleting Cloud Bigtable instances, app profiles, and
* clusters.
*
* <p>See the individual methods for example code.
*
* <pre>{@code
* // One instance per application.
* BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create("my-project");
* CreateInstanceRequest request = CreateInstanceRequest.of("my-instance")
* .addCluster("my-cluster", "us-east1-c", 3, StorageType.SSD);
*
* Instance instance = client.createInstance(request);
*
* // Cleanup during application shutdown.
* client.close();
* }</pre>
*
* <p>Creating a new client is a very expensive operation and should only be done once and shared in
* an application. However, close() needs to be called on the client object to clean up resources
* such as threads during application shutdown.
*
* <p>This class can be customized by passing in a custom instance of BigtableInstanceAdminSettings
* to create(). For example:
*
* <p>To customize credentials:
*
* <pre>{@code
* BigtableInstanceAdminSettings settings = BigtableInstanceAdminSettings.newBuilder()
* .setProjectId("my-project")
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
* .build();
*
* BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(settings);
* }</pre>
*
* To customize the endpoint:
*
* <pre>{@code
* BigtableInstanceAdminSettings.Builder settingsBuilder = BigtableInstanceAdminSettings.newBuilder()
* .setProjectId("my-project");
*
* settingsBuilder.stubSettings()
* .setEndpoint(myEndpoint);
*
* BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(settingsBuilder.build());
* }</pre>
*/
public final class BigtableInstanceAdminClient implements AutoCloseable {
private final String projectId;
private final BigtableInstanceAdminStub stub;
/** Constructs an instance of BigtableInstanceAdminClient with the given project ID. */
public static BigtableInstanceAdminClient create(@Nonnull String projectId) throws IOException {
return create(BigtableInstanceAdminSettings.newBuilder().setProjectId(projectId).build());
}
/** Constructs an instance of BigtableInstanceAdminClient with the given settings. */
public static BigtableInstanceAdminClient create(@Nonnull BigtableInstanceAdminSettings settings)
throws IOException {
return create(settings.getProjectId(), settings.getStubSettings().createStub());
}
/** Constructs an instance of BigtableInstanceAdminClient with the given project ID and stub. */
public static BigtableInstanceAdminClient create(
@Nonnull String projectId, @Nonnull BigtableInstanceAdminStub stub) {
return new BigtableInstanceAdminClient(projectId, stub);
}
private BigtableInstanceAdminClient(
@Nonnull String projectId, @Nonnull BigtableInstanceAdminStub stub) {
this.projectId = projectId;
this.stub = stub;
}
/** Gets the project ID this client is associated with. */
public String getProjectId() {
return projectId;
}
/** Closes the client and frees all resources associated with it (like thread pools). */
@Override
public void close() {
stub.close();
}
/**
* Creates a new instance and returns its representation.
*
* <p>Sample code:
*
* <pre>{@code
* Instance instance = client.createInstance(
* CreateInstanceRequest.of("my-instance")
* .addCluster("my-cluster", "us-east1-c", 3, StorageType.SSD)
* );
* }</pre>
*
* @see CreateInstanceRequest for details.
*/
@SuppressWarnings("WeakerAccess")
public Instance createInstance(CreateInstanceRequest request) {
return ApiExceptions.callAndTranslateApiException(createInstanceAsync(request));
}
/**
* Asynchronously creates a new instance and returns its representation wrapped in a future.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Instance> instanceFuture = client.createInstanceAsync(
* CreateInstanceRequest.of("my-instance")
* .addCluster("my-cluster", "us-east1-c", 3, StorageType.SSD)
* );
*
* Instance instance = instanceFuture.get();
* }</pre>
*
* @see CreateInstanceRequest for details.
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Instance> createInstanceAsync(CreateInstanceRequest request) {
return ApiFutures.transform(
stub.createInstanceOperationCallable().futureCall(request.toProto(projectId)),
new ApiFunction<com.google.bigtable.admin.v2.Instance, Instance>() {
@Override
public Instance apply(com.google.bigtable.admin.v2.Instance proto) {
return Instance.fromProto(proto);
}
},
MoreExecutors.directExecutor());
}
/**
* Updates a new instance and returns its representation.
*
* <p>Sample code:
*
* <pre>{@code
* Instance instance = client.updateInstance(
* UpdateInstanceRequest.of("my-instance")
* .setProductionType()
* );
* }</pre>
*
* @see UpdateInstanceRequest for details.
*/
@SuppressWarnings("WeakerAccess")
public Instance updateInstance(UpdateInstanceRequest request) {
return ApiExceptions.callAndTranslateApiException(updateInstanceAsync(request));
}
/**
* Asynchronously updates a new instance and returns its representation wrapped in a future.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Instance> instanceFuture = client.updateInstanceAsync(
* UpdateInstanceRequest.of("my-instance")
* .setProductionType()
* );
*
* Instance instance = instanceFuture.get();
* }</pre>
*
* @see UpdateInstanceRequest for details.
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Instance> updateInstanceAsync(UpdateInstanceRequest request) {
return ApiFutures.transform(
stub.partialUpdateInstanceOperationCallable().futureCall(request.toProto(projectId)),
new ApiFunction<com.google.bigtable.admin.v2.Instance, Instance>() {
@Override
public Instance apply(com.google.bigtable.admin.v2.Instance proto) {
return Instance.fromProto(proto);
}
},
MoreExecutors.directExecutor());
}
/**
* Get the instance representation by ID.
*
* <p>Sample code:
*
* <pre>{@code
* Instance instance = client.getInstance("my-instance");
* }</pre>
*/
public Instance getInstance(String id) {
return ApiExceptions.callAndTranslateApiException(getInstanceAsync(id));
}
/**
* Asynchronously gets the instance representation by ID wrapped in a future.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Instance> instanceFuture = client.getInstanceAsync("my-instance");
* Instance instance = instanceFuture.get();
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Instance> getInstanceAsync(String instanceId) {
String name = NameUtil.formatInstanceName(projectId, instanceId);
com.google.bigtable.admin.v2.GetInstanceRequest request =
com.google.bigtable.admin.v2.GetInstanceRequest.newBuilder().setName(name).build();
return ApiFutures.transform(
stub.getInstanceCallable().futureCall(request),
new ApiFunction<com.google.bigtable.admin.v2.Instance, Instance>() {
@Override
public Instance apply(com.google.bigtable.admin.v2.Instance proto) {
return Instance.fromProto(proto);
}
},
MoreExecutors.directExecutor());
}
/**
* Lists all of the instances in the current project.
*
* <p>This method will throw a {@link PartialListInstancesException} when any zone is unavailable.
* If a partial list is OK, the exception can be caught and inspected.
*
* <p>Sample code:
*
* <pre>{@code
* try {
* List<Instance> instances = client.listInstances();
* } catch (PartialListInstancesException e) {
* System.out.println("The following zones are unavailable: " + e.getUnavailableZones());
* System.out.println("But the following instances are reachable: " + e.getInstances());
* }
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public List<Instance> listInstances() {
return ApiExceptions.callAndTranslateApiException(listInstancesAsync());
}
/**
* Asynchronously lists all of the instances in the current project.
*
* <p>This method will throw a {@link PartialListInstancesException} when any zone is unavailable.
* If a partial list is OK, the exception can be caught and inspected.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Instance> instancesFuture = client.listInstancesAsync();
*
* ApiFutures.addCallback(instancesFuture, new ApiFutureCallback<List<Instance>>() {
* public void onFailure(Throwable t) {
* if (t instanceof PartialListInstancesException) {
* PartialListInstancesException partialError = (PartialListInstancesException)t;
* System.out.println("The following zones are unavailable: " + partialError.getUnavailableZones());
* System.out.println("But the following instances are reachable: " + partialError.getInstances());
* } else {
* t.printStackTrace();
* }
* }
*
* public void onSuccess(List<Instance> result) {
* System.out.println("Found a complete set of instances: " + result);
* }
* }, MoreExecutors.directExecutor());
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<Instance>> listInstancesAsync() {
com.google.bigtable.admin.v2.ListInstancesRequest request =
com.google.bigtable.admin.v2.ListInstancesRequest.newBuilder()
.setParent(NameUtil.formatProjectName(projectId))
.build();
ApiFuture<com.google.bigtable.admin.v2.ListInstancesResponse> responseFuture =
stub.listInstancesCallable().futureCall(request);
return ApiFutures.transform(
responseFuture,
new ApiFunction<com.google.bigtable.admin.v2.ListInstancesResponse, List<Instance>>() {
@Override
public List<Instance> apply(com.google.bigtable.admin.v2.ListInstancesResponse proto) {
// NOTE: Pagination is intentionally ignored. The server does not implement it and never
// will.
Verify.verify(
proto.getNextPageToken().isEmpty(),
"Server returned an unexpected paginated response");
ImmutableList.Builder<Instance> instances = ImmutableList.builder();
for (com.google.bigtable.admin.v2.Instance protoInstance : proto.getInstancesList()) {
instances.add(Instance.fromProto(protoInstance));
}
ImmutableList.Builder<String> failedZones = ImmutableList.builder();
for (String locationStr : proto.getFailedLocationsList()) {
failedZones.add(NameUtil.extractZoneIdFromLocationName(locationStr));
}
if (!failedZones.build().isEmpty()) {
throw new PartialListInstancesException(failedZones.build(), instances.build());
}
return instances.build();
}
},
MoreExecutors.directExecutor());
}
/**
* Deletes the specified instance.
*
* <p>Sample code:
*
* <pre>{@code
* client.deleteInstance("my-instance");
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public void deleteInstance(String instanceId) {
ApiExceptions.callAndTranslateApiException(deleteInstanceAsync(instanceId));
}
/**
* Asynchronously deletes the specified instance.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Void> deleteFuture = client.deleteInstanceAsync("my-instance");
* deleteFuture.get();
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Void> deleteInstanceAsync(String instanceId) {
String instanceName = NameUtil.formatInstanceName(projectId, instanceId);
com.google.bigtable.admin.v2.DeleteInstanceRequest request =
com.google.bigtable.admin.v2.DeleteInstanceRequest.newBuilder()
.setName(instanceName)
.build();
return ApiFutures.transform(
stub.deleteInstanceCallable().futureCall(request),
new ApiFunction<Empty, Void>() {
@Override
public Void apply(Empty input) {
return null;
}
},
MoreExecutors.directExecutor());
}
/**
* Checks if the instance specified by the instance ID exists.
*
* <p>Sample code:
*
* <pre>{@code
* if(client.exists("my-instance")) {
* System.out.println("Instance exists");
* }
* }</pre>
*/
public boolean exists(String instanceId) {
return ApiExceptions.callAndTranslateApiException(existsAsync(instanceId));
}
/**
* Asynchronously checks if the instance specified by the instance ID exists.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Boolean> found = client.existsAsync("my-instance");
*
* ApiFutures.addCallback(
* found,
* new ApiFutureCallback<Boolean>() {
* public void onSuccess(Boolean found) {
* if (found) {
* System.out.println("Instance exists");
* } else {
* System.out.println("Instance not found");
* }
* }
*
* public void onFailure(Throwable t) {
* t.printStackTrace();
* }
* },
* MoreExecutors.directExecutor()
* );
* }</pre>
*/
public ApiFuture<Boolean> existsAsync(String instanceId) {
ApiFuture<Instance> protoFuture = getInstanceAsync(instanceId);
ApiFuture<Boolean> existsFuture =
ApiFutures.transform(
protoFuture,
new ApiFunction<Instance, Boolean>() {
@Override
public Boolean apply(Instance ignored) {
return true;
}
},
MoreExecutors.directExecutor());
return ApiFutures.catching(
existsFuture,
NotFoundException.class,
new ApiFunction<NotFoundException, Boolean>() {
@Override
public Boolean apply(NotFoundException ignored) {
return false;
}
},
MoreExecutors.directExecutor());
}
/**
* Creates a new cluster in the specified instance.
*
* <p>Sample code:
*
* <pre>{@code
* Cluster cluster = client.createCluster(
* CreateClusterRequest.of("my-instance", "my-new-cluster")
* .setZone("us-east1-c")
* .setServeNodes(3)
* .setStorageType(StorageType.SSD)
* );
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public Cluster createCluster(CreateClusterRequest request) {
return ApiExceptions.callAndTranslateApiException(createClusterAsync(request));
}
/**
* Asynchronously creates a new cluster in the specified instance.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Cluster> clusterFuture = client.createClusterAsync(
* CreateClusterRequest.of("my-instance", "my-new-cluster")
* .setZone("us-east1-c")
* .setServeNodes(3)
* .setStorageType(StorageType.SSD)
* );
*
* Cluster cluster = clusterFuture.get();
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Cluster> createClusterAsync(CreateClusterRequest request) {
return ApiFutures.transform(
stub.createClusterOperationCallable().futureCall(request.toProto(projectId)),
new ApiFunction<com.google.bigtable.admin.v2.Cluster, Cluster>() {
@Override
public Cluster apply(com.google.bigtable.admin.v2.Cluster proto) {
return Cluster.fromProto(proto);
}
},
MoreExecutors.directExecutor());
}
/**
* Gets the cluster representation by ID.
*
* <p>Sample code:
*
* <pre>{@code
* Cluster cluster = client.getCluster("my-instance", "my-cluster");
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public Cluster getCluster(String instanceId, String clusterId) {
return ApiExceptions.callAndTranslateApiException(getClusterAsync(instanceId, clusterId));
}
/**
* Asynchronously gets the cluster representation by ID.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Cluster> clusterFuture = client.getClusterAsync("my-instance", "my-cluster");
* Cluster cluster = clusterFuture.get();
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Cluster> getClusterAsync(String instanceId, String clusterId) {
String name = NameUtil.formatClusterName(projectId, instanceId, clusterId);
com.google.bigtable.admin.v2.GetClusterRequest request =
com.google.bigtable.admin.v2.GetClusterRequest.newBuilder().setName(name).build();
return ApiFutures.transform(
stub.getClusterCallable().futureCall(request),
new ApiFunction<com.google.bigtable.admin.v2.Cluster, Cluster>() {
@Override
public Cluster apply(com.google.bigtable.admin.v2.Cluster proto) {
return Cluster.fromProto(proto);
}
},
MoreExecutors.directExecutor());
}
/**
* Lists all clusters in the specified instance.
*
* <p>This method will throw a {@link PartialListClustersException} when any zone is unavailable.
* If a partial list is OK, the exception can be caught and inspected.
*
* <p>Sample code:
*
* <pre>{@code
* try {
* List<Cluster> clusters = client.listClusters("my-instance");
* } catch (PartialListClustersException e) {
* System.out.println("The following zones are unavailable: " + e.getUnavailableZones());
* System.out.println("But the following clusters are reachable: " + e.getClusters())
* }
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public List<Cluster> listClusters(String instanceId) {
return ApiExceptions.callAndTranslateApiException(listClustersAsync(instanceId));
}
/**
* Asynchronously lists all clusters in the specified instance.
*
* <p>This method will throw a {@link PartialListClustersException} when any zone is unavailable.
* If a partial list is OK, the exception can be caught and inspected.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Cluster> clustersFuture = client.listClustersAsync("my-instance");
*
* ApiFutures.addCallback(clustersFuture, new ApiFutureCallback<List<Cluster>>() {
* public void onFailure(Throwable t) {
* if (t instanceof PartialListClustersException) {
* PartialListClustersException partialError = (PartialListClustersException)t;
* System.out.println("The following zones are unavailable: " + partialError.getUnavailableZones());
* System.out.println("But the following clusters are reachable: " + partialError.getClusters());
* } else {
* t.printStackTrace();
* }
* }
*
* public void onSuccess(List<Cluster> result) {
* System.out.println("Found a complete set of instances: " + result);
* }
* }, MoreExecutors.directExecutor());
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<Cluster>> listClustersAsync(String instanceId) {
String name = NameUtil.formatInstanceName(projectId, instanceId);
com.google.bigtable.admin.v2.ListClustersRequest request =
com.google.bigtable.admin.v2.ListClustersRequest.newBuilder().setParent(name).build();
return ApiFutures.transform(
stub.listClustersCallable().futureCall(request),
new ApiFunction<com.google.bigtable.admin.v2.ListClustersResponse, List<Cluster>>() {
@Override
public List<Cluster> apply(com.google.bigtable.admin.v2.ListClustersResponse proto) {
// NOTE: Server-side pagination is not and will not be implemented, so remaining pages
// are not fetched. However, if that assumption turns out to be wrong, fail fast to
// avoid returning partial data.
Verify.verify(
proto.getNextPageToken().isEmpty(),
"Server returned an unexpected paginated response");
ImmutableList.Builder<Cluster> clusters = ImmutableList.builder();
for (com.google.bigtable.admin.v2.Cluster cluster : proto.getClustersList()) {
clusters.add(Cluster.fromProto(cluster));
}
ImmutableList.Builder<String> failedZones = ImmutableList.builder();
for (String locationStr : proto.getFailedLocationsList()) {
failedZones.add(NameUtil.extractZoneIdFromLocationName(locationStr));
}
if (!failedZones.build().isEmpty()) {
throw new PartialListClustersException(failedZones.build(), clusters.build());
}
return clusters.build();
}
},
MoreExecutors.directExecutor());
}
/**
* Modifies the cluster's node count for manual scaling. If autoscaling is already enabled, manual
* scaling will be silently ignored. If you wish to disable autoscaling and enable manual scaling,
* please use {@link BigtableInstanceAdminClient#disableClusterAutoscaling(String, String, int)}
* instead. Please note that only clusters that belong to a production instance can be resized.
*
* <p>Sample code:
*
* <pre>{@code
* Cluster cluster = client.resizeCluster("my-instance", "my-cluster", 30);
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public Cluster resizeCluster(String instanceId, String clusterId, int numServeNodes) {
return ApiExceptions.callAndTranslateApiException(
resizeClusterAsync(instanceId, clusterId, numServeNodes));
}
/**
* Asynchronously modifies the cluster's node count for manual scaling. If autoscaling is already
* enabled, manual scaling will be silently ignored. If you wish to disable autoscaling and enable
* manual scaling, please use {@link BigtableInstanceAdminClient#disableClusterAutoscaling(String,
* String, int)} instead. Please note that only clusters that belong to a production instance can
* be resized.
*
* <pre>{@code
* ApiFuture<Cluster> clusterFuture = client.resizeCluster("my-instance", "my-cluster", 30);
* Cluster cluster = clusterFuture.get();
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Cluster> resizeClusterAsync(
String instanceId, String clusterId, int numServeNodes) {
String name = NameUtil.formatClusterName(projectId, instanceId, clusterId);
com.google.bigtable.admin.v2.Cluster request =
com.google.bigtable.admin.v2.Cluster.newBuilder()
.setName(name)
.setServeNodes(numServeNodes)
.build();
return ApiFutures.transform(
stub.updateClusterOperationCallable().futureCall(request),
new ApiFunction<com.google.bigtable.admin.v2.Cluster, Cluster>() {
@Override
public Cluster apply(com.google.bigtable.admin.v2.Cluster proto) {
return Cluster.fromProto(proto);
}
},
MoreExecutors.directExecutor());
}
/**
* Modifies the cluster's autoscaling config. This will enable autoscaling and disable manual
* scaling if the cluster is manually scaled. Please note that only clusters that belong to a
* production instance can enable autoscaling.
*
* <p>Sample code:
*
* <pre>{@code
* ClusterAutoscalingConfig clusterAutoscalingConfig =
* ClusterAutoscalingConfig.of("my-instance", "my-cluster")
* .setMinNodes(1)
* .setMaxNodes(4)
* .setCpuUtilizationTargetPercent(40);
* Cluster cluster = client.updateClusterAutoscalingConfig(clusterAutoscalingConfig);
* }</pre>
*/
public Cluster updateClusterAutoscalingConfig(ClusterAutoscalingConfig clusterAutoscalingConfig) {
return ApiExceptions.callAndTranslateApiException(
updateClusterAutoscalingConfigAsync(clusterAutoscalingConfig));
}
/**
* Asynchronously modifies the cluster's autoscaling config. This will enable autoscaling and
* disable manual scaling if the cluster is manually scaled. Please note that only clusters that
* belong to a production instance can enable autoscaling.
*
* <p>Sample code:
*
* <pre>{@code
* ClusterAutoscalingConfig clusterAutoscalingConfig =
* ClusterAutoscalingConfig.of(targetInstanceId, targetClusterId)
* .setMinNodes(1)
* .setMaxNodes(4)
* .setCpuUtilizationTargetPercent(40);
*
* ApiFuture<Cluster> clusterApiFuture = client.updateClusterAutoscalingConfigAsync(clusterAutoscalingConfig);
* Cluster cluster = clusterApiFuture.get();
* }</pre>
*/
public ApiFuture<Cluster> updateClusterAutoscalingConfigAsync(
ClusterAutoscalingConfig clusterAutoscalingConfig) {
PartialUpdateClusterRequest proto = clusterAutoscalingConfig.toProto(projectId);
return ApiFutures.transform(
stub.partialUpdateClusterOperationCallable().futureCall(proto),
Cluster::fromProto,
MoreExecutors.directExecutor());
}
/**
* Disables autoscaling and enables manual scaling by setting a static node count for the cluster.
* Please note that only clusters that belong to a production instance can be resized.
*
* <p>Sample code:
*
* <pre>{@code
* Cluster cluster = client.disableClusterAutoscaling("my-instance", "my-cluster", 3);
* }</pre>
*/
public Cluster disableClusterAutoscaling(String instanceId, String clusterId, int staticSize) {
return ApiExceptions.callAndTranslateApiException(
disableClusterAutoscalingAsync(instanceId, clusterId, staticSize));
}
/**
* Asynchronously disables autoscaling and enables manual scaling by setting a static node count
* for the cluster. Please note that only clusters that belong to a production instance can be
* resized.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Cluster> clusterApiFuture = client.disableClusterAutoscalingAsync("my-instance", "my-cluster", 3);
* Cluster cluster = clusterApiFuture.get();
* }</pre>
*/
public ApiFuture<Cluster> disableClusterAutoscalingAsync(
String instanceId, String clusterId, int staticSize) {
String name = NameUtil.formatClusterName(projectId, instanceId, clusterId);
com.google.bigtable.admin.v2.Cluster request =
com.google.bigtable.admin.v2.Cluster.newBuilder()
.setName(name)
.setServeNodes(staticSize)
.setClusterConfig(
com.google.bigtable.admin.v2.Cluster.ClusterConfig.getDefaultInstance())
.build();
PartialUpdateClusterRequest partialUpdateClusterRequest =
PartialUpdateClusterRequest.newBuilder()
.setUpdateMask(
FieldMaskUtil.fromStringList(
com.google.bigtable.admin.v2.Cluster.class,
Lists.newArrayList("cluster_config.cluster_autoscaling_config", "serve_nodes")))
.setCluster(request)
.build();
return ApiFutures.transform(
stub.partialUpdateClusterOperationCallable().futureCall(partialUpdateClusterRequest),
Cluster::fromProto,
MoreExecutors.directExecutor());
}
/**
* Deletes the specified cluster. Please note that an instance must have at least 1 cluster. To
* remove the last cluster, please use {@link BigtableInstanceAdminClient#deleteInstance(String)}.
*
* <p>Sample code:
*
* <pre>{@code
* client.deleteCluster("my-instance", "my-cluster");
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public void deleteCluster(String instanceId, String clusterId) {
ApiExceptions.callAndTranslateApiException(deleteClusterAsync(instanceId, clusterId));
}
/**
* Asynchronously deletes the specified cluster. Please note that an instance must have at least 1
* cluster. To remove the last cluster, please use {@link
* BigtableInstanceAdminClient#deleteInstanceAsync(String)}.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Void> future = client.deleteClusterAsync("my-instance", "my-cluster");
* future.get();
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<Void> deleteClusterAsync(String instanceId, String clusterId) {
String name = NameUtil.formatClusterName(projectId, instanceId, clusterId);
com.google.bigtable.admin.v2.DeleteClusterRequest request =
com.google.bigtable.admin.v2.DeleteClusterRequest.newBuilder().setName(name).build();
return ApiFutures.transform(
stub.deleteClusterCallable().futureCall(request),
new ApiFunction<Empty, Void>() {
@Override
public Void apply(Empty input) {
return null;
}
},
MoreExecutors.directExecutor());
}
/**
* Creates a new app profile.
*
* <p>Sample code:
*
* <pre>{@code
* AppProfile appProfile = client.createAppProfile(
* CreateAppProfileRequest.of("my-instance", "my-new-app-profile")
* .setRoutingPolicy(SingleClusterRoutingPolicy.of("my-cluster"))
* );
* }</pre>
*
* @see CreateAppProfileRequest
*/
@SuppressWarnings("WeakerAccess")
public AppProfile createAppProfile(CreateAppProfileRequest request) {
return ApiExceptions.callAndTranslateApiException(createAppProfileAsync(request));
}
/**
* Asynchronously creates a new app profile.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<AppProfile> appProfileFuture = client.createAppProfileAsync(
* CreateAppProfileRequest.of("my-instance", "my-new-app-profile")
* .setRoutingPolicy(SingleClusterRoutingPolicy.of("my-cluster"))
* );
*
* AppProfile appProfile = appProfileFuture.get();
* }</pre>
*
* @see CreateAppProfileRequest
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<AppProfile> createAppProfileAsync(CreateAppProfileRequest request) {
return ApiFutures.transform(
stub.createAppProfileCallable().futureCall(request.toProto(projectId)),
new ApiFunction<com.google.bigtable.admin.v2.AppProfile, AppProfile>() {
@Override
public AppProfile apply(com.google.bigtable.admin.v2.AppProfile proto) {
return AppProfile.fromProto(proto);
}
},
MoreExecutors.directExecutor());
}
/**
* Gets the app profile by ID.
*
* <p>Sample code:
*
* <pre>{@code
* AppProfile appProfile = client.getAppProfile("my-instance", "my-app-profile");
* }</pre>
*
* @see AppProfile
*/
public AppProfile getAppProfile(String instanceId, String appProfileId) {
return ApiExceptions.callAndTranslateApiException(getAppProfileAsync(instanceId, appProfileId));
}
/**
* Asynchronously gets the app profile by ID.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<AppProfile> appProfileFuture = client.getAppProfileAsync("my-instance", "my-app-profile");
*
* AppProfile appProfile = appProfileFuture.get();
* }</pre>
*
* @see AppProfile
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<AppProfile> getAppProfileAsync(String instanceId, String appProfileId) {
String name = NameUtil.formatAppProfileName(projectId, instanceId, appProfileId);
GetAppProfileRequest request = GetAppProfileRequest.newBuilder().setName(name).build();
return ApiFutures.transform(
stub.getAppProfileCallable().futureCall(request),
new ApiFunction<com.google.bigtable.admin.v2.AppProfile, AppProfile>() {
@Override
public AppProfile apply(com.google.bigtable.admin.v2.AppProfile proto) {
return AppProfile.fromProto(proto);
}
},
MoreExecutors.directExecutor());
}
/**
* Lists all app profiles of the specified instance.
*
* <p>Sample code:
*
* <pre>{@code
* List<AppProfile> appProfiles = client.listAppProfiles("my-instance");
* }</pre>
*
* @see AppProfile
*/
@SuppressWarnings("WeakerAccess")
public List<AppProfile> listAppProfiles(String instanceId) {
return ApiExceptions.callAndTranslateApiException(listAppProfilesAsync(instanceId));
}
/**
* Asynchronously lists all app profiles of the specified instance.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<List<AppProfile>> appProfilesFuture = client.listAppProfilesAsync("my-instance");
*
* List<AppProfile> appProfiles = appProfileFuture.get();
* }</pre>
*
* @see AppProfile
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<AppProfile>> listAppProfilesAsync(String instanceId) {
String instanceName = NameUtil.formatInstanceName(projectId, instanceId);
ListAppProfilesRequest request =
ListAppProfilesRequest.newBuilder().setParent(instanceName).build();
// TODO(igorbernstein2): try to upstream pagination spooling or figure out a way to expose the
// paginated responses while maintaining the wrapper facade.
// Fetches the first page.
ApiFuture<ListAppProfilesPage> firstPageFuture =
ApiFutures.transform(
stub.listAppProfilesPagedCallable().futureCall(request),