|
15 | 15 | */
|
16 | 16 | package com.google.cloud.bigtable.admin.v2;
|
17 | 17 |
|
| 18 | +import com.google.api.core.ApiFunction; |
| 19 | +import com.google.api.core.ApiFuture; |
| 20 | +import com.google.api.core.ApiFutures; |
| 21 | +import com.google.bigtable.admin.v2.DeleteInstanceRequest; |
| 22 | +import com.google.bigtable.admin.v2.GetInstanceRequest; |
| 23 | +import com.google.bigtable.admin.v2.InstanceName; |
| 24 | +import com.google.bigtable.admin.v2.ListInstancesRequest; |
| 25 | +import com.google.bigtable.admin.v2.ListInstancesResponse; |
| 26 | +import com.google.bigtable.admin.v2.LocationName; |
18 | 27 | import com.google.bigtable.admin.v2.ProjectName;
|
| 28 | +import com.google.cloud.bigtable.admin.v2.models.CreateInstanceRequest; |
| 29 | +import com.google.cloud.bigtable.admin.v2.models.Instance; |
| 30 | +import com.google.cloud.bigtable.admin.v2.models.UpdateInstanceRequest; |
19 | 31 | import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStub;
|
| 32 | +import com.google.common.base.Verify; |
| 33 | +import com.google.common.collect.ImmutableList; |
| 34 | +import com.google.common.util.concurrent.MoreExecutors; |
| 35 | +import com.google.protobuf.Empty; |
20 | 36 | import java.io.IOException;
|
| 37 | +import java.util.List; |
21 | 38 | import javax.annotation.Nonnull;
|
22 | 39 |
|
23 | 40 | /**
|
|
28 | 45 | *
|
29 | 46 | * <pre>{@code
|
30 | 47 | * try(BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(ProjectName.of("[PROJECT]"))) {
|
31 |
| - * CreateInstanceRequest request = CreateInstanceRequest.of(ProjectName) |
32 |
| - * .addFamily("cf1") |
33 |
| - * .addFamily("cf2", GCRULES.maxVersions(10)) |
34 |
| - * .addSplit(ByteString.copyFromUtf8("b")) |
35 |
| - * .addSplit(ByteString.copyFromUtf8("q")); |
| 48 | + * CreateInstanceRequest request = CreateInstanceRequest.of("my-instance") |
| 49 | + * .addCluster("my-cluster", "us-east1-c", 3, StorageType.SSD); |
36 | 50 | *
|
37 |
| - * client.createInstance(request); |
| 51 | + * Instance instance = client.createInstance(request); |
38 | 52 | * }
|
39 | 53 | * }</pre>
|
40 | 54 | *
|
@@ -105,4 +119,168 @@ public ProjectName getProjectName() {
|
105 | 119 | public void close() {
|
106 | 120 | stub.close();
|
107 | 121 | }
|
| 122 | + |
| 123 | + /** |
| 124 | + * Creates a new instance and returns its representation. |
| 125 | + * |
| 126 | + * @see CreateInstanceRequest for details. |
| 127 | + */ |
| 128 | + public Instance createInstance(CreateInstanceRequest request) { |
| 129 | + return awaitFuture(createInstanceAsync(request)); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Asynchronously creates a new instance and returns its representation wrapped in a future. |
| 134 | + * |
| 135 | + * @see CreateInstanceRequest for details. |
| 136 | + */ |
| 137 | + public ApiFuture<Instance> createInstanceAsync(CreateInstanceRequest request) { |
| 138 | + return ApiFutures.transform( |
| 139 | + stub.createInstanceOperationCallable().futureCall(request.toProto(projectName)), |
| 140 | + Instance.PROTO_TRANSFORMER, |
| 141 | + MoreExecutors.directExecutor()); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Updates a new instance and returns its representation. |
| 146 | + * |
| 147 | + * @see UpdateInstanceRequest for details. |
| 148 | + */ |
| 149 | + public Instance updateInstance(UpdateInstanceRequest request) { |
| 150 | + return awaitFuture(updateInstanceAsync(request)); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Asynchronously updates a new instance and returns its representation wrapped in a future. |
| 155 | + * |
| 156 | + * @see UpdateInstanceRequest for details. |
| 157 | + */ |
| 158 | + public ApiFuture<Instance> updateInstanceAsync(UpdateInstanceRequest request) { |
| 159 | + return ApiFutures.transform( |
| 160 | + stub.partialUpdateInstanceOperationCallable().futureCall(request.toProto(projectName)), |
| 161 | + Instance.PROTO_TRANSFORMER, |
| 162 | + MoreExecutors.directExecutor()); |
| 163 | + } |
| 164 | + |
| 165 | + /** Get the instance representation. */ |
| 166 | + public Instance getInstance(String id) { |
| 167 | + return awaitFuture(getInstanceAsync(id)); |
| 168 | + } |
| 169 | + |
| 170 | + /** Asynchronously gets the instance representation wrapped in a future. */ |
| 171 | + public ApiFuture<Instance> getInstanceAsync(String instanceId) { |
| 172 | + |
| 173 | + InstanceName name = InstanceName.of(projectName.getProject(), instanceId); |
| 174 | + |
| 175 | + GetInstanceRequest request = GetInstanceRequest.newBuilder() |
| 176 | + .setName(name.toString()) |
| 177 | + .build(); |
| 178 | + |
| 179 | + return ApiFutures.transform( |
| 180 | + stub.getInstanceCallable().futureCall(request), |
| 181 | + Instance.PROTO_TRANSFORMER, |
| 182 | + MoreExecutors.directExecutor()); |
| 183 | + } |
| 184 | + |
| 185 | + /** Lists all of the instances in the current project. */ |
| 186 | + public List<Instance> listInstances() { |
| 187 | + return awaitFuture(listInstancesAsync()); |
| 188 | + } |
| 189 | + |
| 190 | + /** Asynchronously lists all of the instances in the current project. */ |
| 191 | + public ApiFuture<List<Instance>> listInstancesAsync() { |
| 192 | + ListInstancesRequest request = ListInstancesRequest.newBuilder() |
| 193 | + .setParent(projectName.toString()) |
| 194 | + .build(); |
| 195 | + |
| 196 | + ApiFuture<ListInstancesResponse> responseFuture = stub.listInstancesCallable() |
| 197 | + .futureCall(request); |
| 198 | + |
| 199 | + return ApiFutures.transform(responseFuture, new ApiFunction<ListInstancesResponse, List<Instance>>() { |
| 200 | + @Override |
| 201 | + public List<Instance> apply(ListInstancesResponse proto) { |
| 202 | + // NOTE: pagination is intentionally ignored. The server does not implement it. |
| 203 | + Verify.verify(proto.getNextPageToken().isEmpty(), |
| 204 | + "Server returned an unexpected paginated response"); |
| 205 | + |
| 206 | + ImmutableList.Builder<Instance> instances = ImmutableList.builder(); |
| 207 | + |
| 208 | + for (com.google.bigtable.admin.v2.Instance protoInstance : proto.getInstancesList()) { |
| 209 | + instances.add(Instance.PROTO_TRANSFORMER.apply(protoInstance)); |
| 210 | + } |
| 211 | + |
| 212 | + ImmutableList.Builder<String> failedZones = ImmutableList.builder(); |
| 213 | + for (String locationStr : proto.getFailedLocationsList()) { |
| 214 | + failedZones.add(LocationName.parse(locationStr).getLocation()); |
| 215 | + } |
| 216 | + |
| 217 | + |
| 218 | + if (!failedZones.build().isEmpty()) { |
| 219 | + throw new PartialListInstancesException(failedZones.build(), instances.build()); |
| 220 | + } |
| 221 | + |
| 222 | + return instances.build(); |
| 223 | + } |
| 224 | + }, MoreExecutors.directExecutor()); |
| 225 | + } |
| 226 | + |
| 227 | + /** Deletes the specified instance. */ |
| 228 | + public void deleteInstance(String instanceId) { |
| 229 | + awaitFuture(deleteInstanceAsync(instanceId)); |
| 230 | + } |
| 231 | + |
| 232 | + /** Asynchronously deletes the specified instance. */ |
| 233 | + private ApiFuture<Void> deleteInstanceAsync(String instanceId) { |
| 234 | + InstanceName instanceName = InstanceName.of(projectName.getProject(), instanceId); |
| 235 | + |
| 236 | + DeleteInstanceRequest request = DeleteInstanceRequest.newBuilder() |
| 237 | + .setName(instanceName.toString()) |
| 238 | + .build(); |
| 239 | + |
| 240 | + return ApiFutures.transform(stub.deleteInstanceCallable().futureCall(request), |
| 241 | + new ApiFunction<Empty, Void>() { |
| 242 | + @Override |
| 243 | + public Void apply(Empty input) { |
| 244 | + return null; |
| 245 | + } |
| 246 | + }, |
| 247 | + MoreExecutors.directExecutor() |
| 248 | + ); |
| 249 | + } |
| 250 | + |
| 251 | + |
| 252 | + private <T> T awaitFuture(ApiFuture<T> future) { |
| 253 | + try { |
| 254 | + return future.get(); |
| 255 | + } catch(Throwable t) { |
| 256 | + // TODO(igorbernstein2): figure out a better wrapper exception. |
| 257 | + throw new RuntimeException(t); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * Exception thrown when some zones are unavailable and listInstances is unable to return a full |
| 263 | + * instance list. This exception can be inspected to get a partial list. |
| 264 | + */ |
| 265 | + public static class PartialListInstancesException extends RuntimeException { |
| 266 | + private final List<String> failedZones; |
| 267 | + private final List<Instance> instances; |
| 268 | + |
| 269 | + PartialListInstancesException(List<String> failedZones, List<Instance> instances) { |
| 270 | + super("Failed to list all instances, some zones where unavailable"); |
| 271 | + |
| 272 | + this.failedZones = failedZones; |
| 273 | + this.instances = instances; |
| 274 | + } |
| 275 | + |
| 276 | + /** A list of zones, whose unavailability caused this error. */ |
| 277 | + public List<String> getFailedZones() { |
| 278 | + return failedZones; |
| 279 | + } |
| 280 | + |
| 281 | + /** A partial list of instances that were found in the available zones. */ |
| 282 | + public List<Instance> getInstances() { |
| 283 | + return instances; |
| 284 | + } |
| 285 | + } |
108 | 286 | }
|
0 commit comments