|
25 | 25 | import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; |
26 | 26 | import com.google.api.gax.rpc.ApiException; |
27 | 27 | import com.google.api.gax.rpc.ServerStream; |
28 | | -import com.google.auth.oauth2.ServiceAccountJwtAccessCredentials; |
| 28 | +import com.google.auth.oauth2.GoogleCredentials; |
29 | 29 | import com.google.auto.value.AutoValue; |
30 | 30 | import com.google.bigtable.v2.Column; |
31 | 31 | import com.google.bigtable.v2.Family; |
|
42 | 42 | import com.google.cloud.bigtable.data.v2.models.RowMutation; |
43 | 43 | import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings; |
44 | 44 | import com.google.cloud.bigtable.testproxy.CloudBigtableV2TestProxyGrpc.CloudBigtableV2TestProxyImplBase; |
| 45 | +import com.google.common.base.Preconditions; |
45 | 46 | import com.google.protobuf.ByteString; |
46 | 47 | import com.google.protobuf.util.Durations; |
47 | 48 | import com.google.rpc.Code; |
@@ -201,18 +202,12 @@ private CbtClient getClient(String id) throws StatusException { |
201 | 202 | @Override |
202 | 203 | public synchronized void createClient( |
203 | 204 | CreateClientRequest request, StreamObserver<CreateClientResponse> responseObserver) { |
204 | | - if (request.getClientId().isEmpty() |
205 | | - || request.getProjectId().isEmpty() |
206 | | - || request.getInstanceId().isEmpty() |
207 | | - || request.getDataTarget().isEmpty()) { |
208 | | - responseObserver.onError( |
209 | | - Status.INVALID_ARGUMENT |
210 | | - .withDescription("clientId, projectId, instanceId, and dataTarget must be provided.") |
211 | | - .asException()); |
212 | | - return; |
213 | | - } |
| 205 | + Preconditions.checkArgument(!request.getClientId().isEmpty(), "client id must be provided"); |
| 206 | + Preconditions.checkArgument(!request.getProjectId().isEmpty(), "project id must be provided"); |
| 207 | + Preconditions.checkArgument(!request.getInstanceId().isEmpty(), "instance id must be provided"); |
| 208 | + Preconditions.checkArgument(!request.getDataTarget().isEmpty(), "data target must be provided"); |
214 | 209 |
|
215 | | - if (idClientMap.get(request.getClientId()) != null) { |
| 210 | + if (idClientMap.contains(request.getClientId())) { |
216 | 211 | responseObserver.onError( |
217 | 212 | Status.ALREADY_EXISTS |
218 | 213 | .withDescription("Client " + request.getClientId() + " already exists.") |
@@ -257,12 +252,11 @@ public synchronized void createClient( |
257 | 252 | @Override |
258 | 253 | public void closeClient( |
259 | 254 | CloseClientRequest request, StreamObserver<CloseClientResponse> responseObserver) { |
260 | | - CbtClient client = idClientMap.get(request.getClientId()); |
261 | | - if (client == null) { |
262 | | - responseObserver.onError( |
263 | | - Status.NOT_FOUND |
264 | | - .withDescription("Client " + request.getClientId() + " not found.") |
265 | | - .asException()); |
| 255 | + CbtClient client; |
| 256 | + try { |
| 257 | + client = getClient(request.getClientId()); |
| 258 | + } catch (StatusException e) { |
| 259 | + responseObserver.onError(e); |
266 | 260 | return; |
267 | 261 | } |
268 | 262 |
|
@@ -291,12 +285,11 @@ public void removeClient( |
291 | 285 | @Override |
292 | 286 | public void mutateRow( |
293 | 287 | MutateRowRequest request, StreamObserver<MutateRowResult> responseObserver) { |
294 | | - CbtClient client = idClientMap.get(request.getClientId()); |
295 | | - if (client == null) { |
296 | | - responseObserver.onError( |
297 | | - Status.NOT_FOUND |
298 | | - .withDescription("Client " + request.getClientId() + " not found.") |
299 | | - .asException()); |
| 288 | + CbtClient client; |
| 289 | + try { |
| 290 | + client = getClient(request.getClientId()); |
| 291 | + } catch (StatusException e) { |
| 292 | + responseObserver.onError(e); |
300 | 293 | return; |
301 | 294 | } |
302 | 295 |
|
@@ -327,12 +320,11 @@ public void mutateRow( |
327 | 320 | @Override |
328 | 321 | public void bulkMutateRows( |
329 | 322 | MutateRowsRequest request, StreamObserver<MutateRowsResult> responseObserver) { |
330 | | - CbtClient client = idClientMap.get(request.getClientId()); |
331 | | - if (client == null) { |
332 | | - responseObserver.onError( |
333 | | - Status.NOT_FOUND |
334 | | - .withDescription("Client " + request.getClientId() + " not found.") |
335 | | - .asException()); |
| 323 | + CbtClient client; |
| 324 | + try { |
| 325 | + client = getClient(request.getClientId()); |
| 326 | + } catch (StatusException e) { |
| 327 | + responseObserver.onError(e); |
336 | 328 | return; |
337 | 329 | } |
338 | 330 |
|
@@ -377,12 +369,11 @@ public void bulkMutateRows( |
377 | 369 |
|
378 | 370 | @Override |
379 | 371 | public void readRow(ReadRowRequest request, StreamObserver<RowResult> responseObserver) { |
380 | | - CbtClient client = idClientMap.get(request.getClientId()); |
381 | | - if (client == null) { |
382 | | - responseObserver.onError( |
383 | | - Status.NOT_FOUND |
384 | | - .withDescription("Client " + request.getClientId() + " not found.") |
385 | | - .asException()); |
| 372 | + CbtClient client; |
| 373 | + try { |
| 374 | + client = getClient(request.getClientId()); |
| 375 | + } catch (StatusException e) { |
| 376 | + responseObserver.onError(e); |
386 | 377 | return; |
387 | 378 | } |
388 | 379 |
|
@@ -442,12 +433,11 @@ public void readRow(ReadRowRequest request, StreamObserver<RowResult> responseOb |
442 | 433 |
|
443 | 434 | @Override |
444 | 435 | public void readRows(ReadRowsRequest request, StreamObserver<RowsResult> responseObserver) { |
445 | | - CbtClient client = idClientMap.get(request.getClientId()); |
446 | | - if (client == null) { |
447 | | - responseObserver.onError( |
448 | | - Status.NOT_FOUND |
449 | | - .withDescription("Client " + request.getClientId() + " not found.") |
450 | | - .asException()); |
| 436 | + CbtClient client; |
| 437 | + try { |
| 438 | + client = getClient(request.getClientId()); |
| 439 | + } catch (StatusException e) { |
| 440 | + responseObserver.onError(e); |
451 | 441 | return; |
452 | 442 | } |
453 | 443 |
|
@@ -558,12 +548,11 @@ private static RowsResult.Builder convertRowsResult( |
558 | 548 | @Override |
559 | 549 | public void sampleRowKeys( |
560 | 550 | SampleRowKeysRequest request, StreamObserver<SampleRowKeysResult> responseObserver) { |
561 | | - CbtClient client = idClientMap.get(request.getClientId()); |
562 | | - if (client == null) { |
563 | | - responseObserver.onError( |
564 | | - Status.NOT_FOUND |
565 | | - .withDescription("Client " + request.getClientId() + " not found.") |
566 | | - .asException()); |
| 551 | + CbtClient client; |
| 552 | + try { |
| 553 | + client = getClient(request.getClientId()); |
| 554 | + } catch (StatusException e) { |
| 555 | + responseObserver.onError(e); |
567 | 556 | return; |
568 | 557 | } |
569 | 558 |
|
@@ -607,12 +596,11 @@ public void sampleRowKeys( |
607 | 596 | @Override |
608 | 597 | public void checkAndMutateRow( |
609 | 598 | CheckAndMutateRowRequest request, StreamObserver<CheckAndMutateRowResult> responseObserver) { |
610 | | - CbtClient client = idClientMap.get(request.getClientId()); |
611 | | - if (client == null) { |
612 | | - responseObserver.onError( |
613 | | - Status.NOT_FOUND |
614 | | - .withDescription("Client " + request.getClientId() + " not found.") |
615 | | - .asException()); |
| 599 | + CbtClient client; |
| 600 | + try { |
| 601 | + client = getClient(request.getClientId()); |
| 602 | + } catch (StatusException e) { |
| 603 | + responseObserver.onError(e); |
616 | 604 | return; |
617 | 605 | } |
618 | 606 |
|
@@ -643,12 +631,11 @@ public void checkAndMutateRow( |
643 | 631 | @Override |
644 | 632 | public void readModifyWriteRow( |
645 | 633 | ReadModifyWriteRowRequest request, StreamObserver<RowResult> responseObserver) { |
646 | | - CbtClient client = idClientMap.get(request.getClientId()); |
647 | | - if (client == null) { |
648 | | - responseObserver.onError( |
649 | | - Status.NOT_FOUND |
650 | | - .withDescription("Client " + request.getClientId() + " not found.") |
651 | | - .asException()); |
| 634 | + CbtClient client; |
| 635 | + try { |
| 636 | + client = getClient(request.getClientId()); |
| 637 | + } catch (StatusException e) { |
| 638 | + responseObserver.onError(e); |
652 | 639 | return; |
653 | 640 | } |
654 | 641 |
|
@@ -749,9 +736,9 @@ private CredentialsProvider getCredentialsProvider() throws IOException { |
749 | 736 | return NoCredentialsProvider.create(); |
750 | 737 | } |
751 | 738 |
|
752 | | - final ServiceAccountJwtAccessCredentials creds = |
753 | | - ServiceAccountJwtAccessCredentials.fromStream( |
754 | | - new ByteArrayInputStream(credential.getBytes(UTF_8))); |
| 739 | + final GoogleCredentials creds = |
| 740 | + GoogleCredentials.fromStream(new ByteArrayInputStream(credential.getBytes(UTF_8))); |
| 741 | + |
755 | 742 | return FixedCredentialsProvider.create(creds); |
756 | 743 | } |
757 | 744 |
|
|
0 commit comments