|
30 | 30 | import com.google.api.core.ApiFuture;
|
31 | 31 | import com.google.api.core.BetaApi;
|
32 | 32 | import com.google.api.gax.grpc.GrpcCallContext;
|
33 |
| -import com.google.api.gax.grpc.GrpcStatusCode; |
34 | 33 | import com.google.api.gax.paging.AbstractPage;
|
35 | 34 | import com.google.api.gax.paging.Page;
|
36 | 35 | import com.google.api.gax.retrying.ResultRetryAlgorithm;
|
|
39 | 38 | import com.google.api.gax.rpc.NotFoundException;
|
40 | 39 | import com.google.api.gax.rpc.StatusCode;
|
41 | 40 | import com.google.api.gax.rpc.UnaryCallable;
|
42 |
| -import com.google.api.gax.rpc.UnimplementedException; |
43 | 41 | import com.google.cloud.BaseService;
|
44 | 42 | import com.google.cloud.Policy;
|
45 | 43 | import com.google.cloud.WriteChannel;
|
|
84 | 82 | import com.google.storage.v2.ComposeObjectRequest.SourceObject;
|
85 | 83 | import com.google.storage.v2.CreateBucketRequest;
|
86 | 84 | import com.google.storage.v2.CreateHmacKeyRequest;
|
| 85 | +import com.google.storage.v2.CreateNotificationConfigRequest; |
87 | 86 | import com.google.storage.v2.DeleteBucketRequest;
|
88 | 87 | import com.google.storage.v2.DeleteHmacKeyRequest;
|
| 88 | +import com.google.storage.v2.DeleteNotificationConfigRequest; |
89 | 89 | import com.google.storage.v2.DeleteObjectRequest;
|
90 | 90 | import com.google.storage.v2.GetBucketRequest;
|
91 | 91 | import com.google.storage.v2.GetHmacKeyRequest;
|
| 92 | +import com.google.storage.v2.GetNotificationConfigRequest; |
92 | 93 | import com.google.storage.v2.GetObjectRequest;
|
93 | 94 | import com.google.storage.v2.GetServiceAccountRequest;
|
94 | 95 | import com.google.storage.v2.ListBucketsRequest;
|
95 | 96 | import com.google.storage.v2.ListHmacKeysRequest;
|
| 97 | +import com.google.storage.v2.ListNotificationConfigsRequest; |
| 98 | +import com.google.storage.v2.ListNotificationConfigsResponse; |
96 | 99 | import com.google.storage.v2.ListObjectsRequest;
|
97 | 100 | import com.google.storage.v2.ListObjectsResponse;
|
98 | 101 | import com.google.storage.v2.LockBucketRetentionPolicyRequest;
|
| 102 | +import com.google.storage.v2.NotificationConfig; |
| 103 | +import com.google.storage.v2.NotificationConfigName; |
99 | 104 | import com.google.storage.v2.Object;
|
100 | 105 | import com.google.storage.v2.ObjectAccessControl;
|
101 | 106 | import com.google.storage.v2.ObjectChecksums;
|
102 | 107 | import com.google.storage.v2.ReadObjectRequest;
|
103 | 108 | import com.google.storage.v2.RewriteObjectRequest;
|
104 | 109 | import com.google.storage.v2.RewriteResponse;
|
105 | 110 | import com.google.storage.v2.StorageClient;
|
| 111 | +import com.google.storage.v2.StorageClient.ListNotificationConfigsPage; |
106 | 112 | import com.google.storage.v2.UpdateBucketRequest;
|
107 | 113 | import com.google.storage.v2.UpdateHmacKeyRequest;
|
108 | 114 | import com.google.storage.v2.UpdateObjectRequest;
|
109 | 115 | import com.google.storage.v2.WriteObjectRequest;
|
110 | 116 | import com.google.storage.v2.WriteObjectResponse;
|
111 | 117 | import com.google.storage.v2.WriteObjectSpec;
|
112 |
| -import io.grpc.Status.Code; |
113 | 118 | import java.io.ByteArrayInputStream;
|
114 | 119 | import java.io.ByteArrayOutputStream;
|
115 | 120 | import java.io.IOException;
|
@@ -1404,23 +1409,92 @@ public ServiceAccount getServiceAccount(String projectId) {
|
1404 | 1409 |
|
1405 | 1410 | @Override
|
1406 | 1411 | public Notification createNotification(String bucket, NotificationInfo notificationInfo) {
|
1407 |
| - return throwNotYetImplemented( |
1408 |
| - fmtMethodName("createNotification", String.class, NotificationInfo.class)); |
| 1412 | + NotificationConfig encode = codecs.notificationInfo().encode(notificationInfo); |
| 1413 | + CreateNotificationConfigRequest req = |
| 1414 | + CreateNotificationConfigRequest.newBuilder() |
| 1415 | + .setParent(bucketNameCodec.encode(bucket)) |
| 1416 | + .setNotificationConfig(encode) |
| 1417 | + .build(); |
| 1418 | + return Retrying.run( |
| 1419 | + getOptions(), |
| 1420 | + retryAlgorithmManager.getFor(req), |
| 1421 | + () -> storageClient.createNotificationConfigCallable().call(req), |
| 1422 | + syntaxDecoders.notificationConfig); |
1409 | 1423 | }
|
1410 | 1424 |
|
1411 | 1425 | @Override
|
1412 | 1426 | public Notification getNotification(String bucket, String notificationId) {
|
1413 |
| - return throwNotYetImplemented(fmtMethodName("getNotification", String.class, String.class)); |
| 1427 | + String name; |
| 1428 | + if (NotificationConfigName.isParsableFrom(notificationId)) { |
| 1429 | + name = notificationId; |
| 1430 | + } else { |
| 1431 | + NotificationConfigName configName = NotificationConfigName.of("_", bucket, notificationId); |
| 1432 | + name = configName.toString(); |
| 1433 | + } |
| 1434 | + GetNotificationConfigRequest req = |
| 1435 | + GetNotificationConfigRequest.newBuilder().setName(name).build(); |
| 1436 | + return Retrying.run( |
| 1437 | + getOptions(), |
| 1438 | + retryAlgorithmManager.getFor(req), |
| 1439 | + () -> { |
| 1440 | + try { |
| 1441 | + return storageClient.getNotificationConfigCallable().call(req); |
| 1442 | + } catch (NotFoundException e) { |
| 1443 | + return null; |
| 1444 | + } |
| 1445 | + }, |
| 1446 | + syntaxDecoders.notificationConfig); |
1414 | 1447 | }
|
1415 | 1448 |
|
1416 | 1449 | @Override
|
1417 | 1450 | public List<Notification> listNotifications(String bucket) {
|
1418 |
| - return throwNotYetImplemented(fmtMethodName("listNotifications", String.class)); |
| 1451 | + ListNotificationConfigsRequest req = |
| 1452 | + ListNotificationConfigsRequest.newBuilder() |
| 1453 | + .setParent(bucketNameCodec.encode(bucket)) |
| 1454 | + .build(); |
| 1455 | + ResultRetryAlgorithm<?> algorithm = retryAlgorithmManager.getFor(req); |
| 1456 | + return Retrying.run( |
| 1457 | + getOptions(), |
| 1458 | + algorithm, |
| 1459 | + () -> storageClient.listNotificationConfigsPagedCallable().call(req), |
| 1460 | + resp -> { |
| 1461 | + TransformingPageDecorator< |
| 1462 | + ListNotificationConfigsRequest, |
| 1463 | + ListNotificationConfigsResponse, |
| 1464 | + NotificationConfig, |
| 1465 | + ListNotificationConfigsPage, |
| 1466 | + Notification> |
| 1467 | + page = |
| 1468 | + new TransformingPageDecorator<>( |
| 1469 | + resp.getPage(), syntaxDecoders.notificationConfig, getOptions(), algorithm); |
| 1470 | + return ImmutableList.copyOf(page.iterateAll()); |
| 1471 | + }); |
1419 | 1472 | }
|
1420 | 1473 |
|
1421 | 1474 | @Override
|
1422 | 1475 | public boolean deleteNotification(String bucket, String notificationId) {
|
1423 |
| - return throwNotYetImplemented(fmtMethodName("deleteNotification", String.class, String.class)); |
| 1476 | + String name; |
| 1477 | + if (NotificationConfigName.isParsableFrom(notificationId)) { |
| 1478 | + name = notificationId; |
| 1479 | + } else { |
| 1480 | + NotificationConfigName configName = NotificationConfigName.of("_", bucket, notificationId); |
| 1481 | + name = configName.toString(); |
| 1482 | + } |
| 1483 | + DeleteNotificationConfigRequest req = |
| 1484 | + DeleteNotificationConfigRequest.newBuilder().setName(name).build(); |
| 1485 | + return Boolean.TRUE.equals( |
| 1486 | + Retrying.run( |
| 1487 | + getOptions(), |
| 1488 | + retryAlgorithmManager.getFor(req), |
| 1489 | + () -> { |
| 1490 | + try { |
| 1491 | + storageClient.deleteNotificationConfigCallable().call(req); |
| 1492 | + return true; |
| 1493 | + } catch (NotFoundException e) { |
| 1494 | + return false; |
| 1495 | + } |
| 1496 | + }, |
| 1497 | + Decoder.identity())); |
1424 | 1498 | }
|
1425 | 1499 |
|
1426 | 1500 | @Override
|
@@ -1448,6 +1522,8 @@ private final class SyntaxDecoders {
|
1448 | 1522 | o -> codecs.blobInfo().decode(o).asBlob(GrpcStorageImpl.this);
|
1449 | 1523 | final Decoder<com.google.storage.v2.Bucket, Bucket> bucket =
|
1450 | 1524 | b -> codecs.bucketInfo().decode(b).asBucket(GrpcStorageImpl.this);
|
| 1525 | + final Decoder<NotificationConfig, Notification> notificationConfig = |
| 1526 | + n -> codecs.notificationInfo().decode(n).asNotification(GrpcStorageImpl.this); |
1451 | 1527 | }
|
1452 | 1528 |
|
1453 | 1529 | /**
|
@@ -1668,14 +1744,6 @@ static <T> T throwHttpJsonOnly(Class<?> clazz, String methodName) {
|
1668 | 1744 | throw new UnsupportedOperationException(message);
|
1669 | 1745 | }
|
1670 | 1746 |
|
1671 |
| - static <T> T throwNotYetImplemented(String methodName) { |
1672 |
| - String message = |
1673 |
| - String.format( |
1674 |
| - "%s#%s is not yet implemented for GRPC transport. Please use StorageOptions.http() to construct a compatible instance in the interim.", |
1675 |
| - Storage.class.getName(), methodName); |
1676 |
| - throw new UnimplementedException(message, null, GrpcStatusCode.of(Code.UNIMPLEMENTED), false); |
1677 |
| - } |
1678 |
| - |
1679 | 1747 | private static String fmtMethodName(String name, Class<?>... args) {
|
1680 | 1748 | return name
|
1681 | 1749 | + "("
|
|
0 commit comments