Skip to content

Commit 8dfc0cb

Browse files
authored
feat: expose all the methods of notification (#141)
* feat: expose all the methods of notification * feat: remove unsed code * feat: fix review changes * build: fix require upper bound dependencies errors * build: fix clirr ignore differences * feat: implement getNotification inside StorageRpcTestBase class and fix the builds checks
1 parent 81472a4 commit 8dfc0cb

14 files changed

+743
-1
lines changed

google-cloud-storage/clirr-ignored-differences.xml

+25
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,29 @@
2626
<method>com.google.cloud.storage.BucketInfo$Builder deleteLifecycleRules()</method>
2727
<differenceType>7013</differenceType>
2828
</difference>
29+
<difference>
30+
<className>com/google/cloud/storage/Storage</className>
31+
<method>com.google.api.services.storage.model.Notification createNotification(java.lang.String, com.google.cloud.storage.NotificationInfo)</method>
32+
<differenceType>7012</differenceType>
33+
</difference>
34+
<difference>
35+
<className>com/google/cloud/storage/Storage</className>
36+
<method>com.google.api.services.storage.model.Notification getNotification(java.lang.String, java.lang.String)</method>
37+
<differenceType>7012</differenceType>
38+
</difference>
39+
<difference>
40+
<className>com/google/cloud/storage/Storage</className>
41+
<method>java.util.List listNotifications(java.lang.String)</method>
42+
<differenceType>7012</differenceType>
43+
</difference>
44+
<difference>
45+
<className>com/google/cloud/storage/Storage</className>
46+
<method>boolean deleteNotification(java.lang.String, java.lang.String)</method>
47+
<differenceType>7012</differenceType>
48+
</difference>
49+
<difference>
50+
<className>com/google/cloud/storage/spi/v1/StorageRpc</className>
51+
<method>com.google.api.services.storage.model.Notification getNotification(java.lang.String, java.lang.String)</method>
52+
<differenceType>7012</differenceType>
53+
</difference>
2954
</differences>

google-cloud-storage/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@
155155
<artifactId>truth</artifactId>
156156
<scope>test</scope>
157157
</dependency>
158+
<dependency>
159+
<groupId>com.google.cloud</groupId>
160+
<artifactId>google-cloud-pubsub</artifactId>
161+
<scope>test</scope>
162+
</dependency>
158163
<dependency>
159164
<groupId>org.easymock</groupId>
160165
<artifactId>easymock</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.storage;
17+
18+
import static com.google.common.base.Preconditions.checkNotNull;
19+
20+
import com.google.api.pathtemplate.PathTemplate;
21+
import com.google.api.services.storage.model.Notification;
22+
import com.google.common.base.Function;
23+
import com.google.common.base.MoreObjects;
24+
import com.google.common.collect.ImmutableList;
25+
import com.google.common.collect.ImmutableMap;
26+
import java.io.Serializable;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Objects;
30+
31+
/**
32+
* Google Storage Notification metadata;
33+
*
34+
* @see <a href="https://cloud.google.com/storage/docs/concepts-techniques#concepts">Concepts and
35+
* Terminology</a>
36+
*/
37+
public class NotificationInfo implements Serializable {
38+
39+
private static final long serialVersionUID = 5725883368559753810L;
40+
private static final PathTemplate PATH_TEMPLATE =
41+
PathTemplate.createWithoutUrlEncoding("projects/{project}/topics/{topic}");
42+
43+
public enum PayloadFormat {
44+
JSON_API_V1,
45+
NONE
46+
}
47+
48+
static final Function<Notification, NotificationInfo> FROM_PB_FUNCTION =
49+
new Function<Notification, NotificationInfo>() {
50+
@Override
51+
public NotificationInfo apply(Notification pb) {
52+
return NotificationInfo.fromPb(pb);
53+
}
54+
};
55+
static final Function<NotificationInfo, Notification> TO_PB_FUNCTION =
56+
new Function<NotificationInfo, Notification>() {
57+
@Override
58+
public Notification apply(NotificationInfo NotificationInfo) {
59+
return NotificationInfo.toPb();
60+
}
61+
};
62+
private final String generatedId;
63+
private final String topic;
64+
private final List<String> eventTypes;
65+
private final Map<String, String> customAttributes;
66+
private final PayloadFormat payloadFormat;
67+
private final String objectNamePrefix;
68+
private final String etag;
69+
private final String selfLink;
70+
71+
public static final class Builder {
72+
73+
private String generatedId;
74+
private String topic;
75+
private List<String> eventTypes;
76+
private Map<String, String> customAttributes;
77+
private PayloadFormat payloadFormat;
78+
private String objectNamePrefix;
79+
private String etag;
80+
private String selfLink;
81+
82+
Builder(String topic) {
83+
this.topic = topic;
84+
}
85+
86+
Builder(NotificationInfo NotificationInfo) {
87+
generatedId = NotificationInfo.generatedId;
88+
etag = NotificationInfo.etag;
89+
selfLink = NotificationInfo.selfLink;
90+
topic = NotificationInfo.topic;
91+
eventTypes = NotificationInfo.eventTypes;
92+
customAttributes = NotificationInfo.customAttributes;
93+
payloadFormat = NotificationInfo.payloadFormat;
94+
objectNamePrefix = NotificationInfo.objectNamePrefix;
95+
}
96+
97+
Builder setGeneratedId(String generatedId) {
98+
this.generatedId = generatedId;
99+
return this;
100+
}
101+
102+
Builder setSelfLink(String selfLink) {
103+
this.selfLink = selfLink;
104+
return this;
105+
}
106+
107+
/** The name of the topic. It must have the format "projects/{project}/topics/{topic}". */
108+
public Builder setTopic(String topic) {
109+
this.topic = topic;
110+
return this;
111+
}
112+
113+
public Builder setPayloadFormat(PayloadFormat payloadFormat) {
114+
this.payloadFormat = payloadFormat;
115+
return this;
116+
}
117+
118+
public Builder setObjectNamePrefix(String objectNamePrefix) {
119+
this.objectNamePrefix = objectNamePrefix;
120+
return this;
121+
}
122+
123+
public Builder setEventTypes(Iterable<String> eventTypes) {
124+
this.eventTypes = eventTypes != null ? ImmutableList.copyOf(eventTypes) : null;
125+
return this;
126+
}
127+
128+
Builder setEtag(String etag) {
129+
this.etag = etag;
130+
return this;
131+
}
132+
133+
public Builder setCustomAttributes(Map<String, String> customAttributes) {
134+
this.customAttributes =
135+
customAttributes != null ? ImmutableMap.copyOf(customAttributes) : null;
136+
return this;
137+
}
138+
139+
public NotificationInfo build() {
140+
checkNotNull(topic);
141+
return new NotificationInfo(this);
142+
}
143+
}
144+
145+
NotificationInfo(Builder builder) {
146+
generatedId = builder.generatedId;
147+
etag = builder.etag;
148+
selfLink = builder.selfLink;
149+
topic = builder.topic;
150+
eventTypes = builder.eventTypes;
151+
customAttributes = builder.customAttributes;
152+
payloadFormat = builder.payloadFormat;
153+
objectNamePrefix = builder.objectNamePrefix;
154+
}
155+
156+
/** Returns the service-generated id for the notification. */
157+
public String getGeneratedId() {
158+
return generatedId;
159+
}
160+
161+
/** Returns the topic to which this subscription publishes. */
162+
public String getTopic() {
163+
return topic;
164+
}
165+
166+
/** Returns the canonical URI of this topic as a string. */
167+
public String getSelfLink() {
168+
return selfLink;
169+
}
170+
171+
/** Returns the desired content of the Payload. */
172+
public PayloadFormat getPayloadFormat() {
173+
return payloadFormat;
174+
}
175+
176+
/** Returns the object name prefix for which this notification configuration applies. */
177+
public String getObjectNamePrefix() {
178+
return objectNamePrefix;
179+
}
180+
181+
/**
182+
* Returns HTTP 1.1 Entity tag for the notification.
183+
*
184+
* @see <a href="http://tools.ietf.org/html/rfc2616#section-3.11">Entity Tags</a>
185+
*/
186+
public String getEtag() {
187+
return etag;
188+
}
189+
190+
/**
191+
* Returns the list of event types that this notification will apply to. If empty, notifications
192+
* will be sent on all event types.
193+
*
194+
* @see <a href="https://cloud.google.com/storage/docs/cross-origin">Cross-Origin Resource Sharing
195+
* (CORS)</a>
196+
*/
197+
public List<String> getEventTypes() {
198+
return eventTypes;
199+
}
200+
201+
/**
202+
* Returns the list of additional attributes to attach to each Cloud PubSub message published for
203+
* this notification subscription.
204+
*
205+
* @see <a href="https://cloud.google.com/storage/docs/access-control#About-Access-Control-Lists">
206+
* About Access Control Lists</a>
207+
*/
208+
public Map<String, String> getCustomAttributes() {
209+
return customAttributes;
210+
}
211+
212+
/** Returns a builder for the current notification. */
213+
public Builder toBuilder() {
214+
return new Builder(this);
215+
}
216+
217+
@Override
218+
public int hashCode() {
219+
return Objects.hash(getTopic());
220+
}
221+
222+
@Override
223+
public boolean equals(Object obj) {
224+
return obj == this
225+
|| obj != null
226+
&& obj.getClass().equals(NotificationInfo.class)
227+
&& Objects.equals(toPb(), ((NotificationInfo) obj).toPb());
228+
}
229+
230+
@Override
231+
public String toString() {
232+
return MoreObjects.toStringHelper(this).add("topic", getTopic()).toString();
233+
}
234+
235+
Notification toPb() {
236+
Notification notificationPb = new Notification();
237+
notificationPb.setId(generatedId);
238+
notificationPb.setEtag(etag);
239+
if (customAttributes != null) {
240+
notificationPb.setCustomAttributes(customAttributes);
241+
}
242+
if (eventTypes != null) {
243+
notificationPb.setEventTypes(eventTypes);
244+
}
245+
if (objectNamePrefix != null) {
246+
notificationPb.setObjectNamePrefix(objectNamePrefix);
247+
}
248+
if (payloadFormat != null) {
249+
notificationPb.setPayloadFormat(payloadFormat.toString());
250+
} else {
251+
notificationPb.setPayloadFormat(PayloadFormat.NONE.toString());
252+
}
253+
notificationPb.setSelfLink(selfLink);
254+
notificationPb.setTopic(topic);
255+
256+
return notificationPb;
257+
}
258+
259+
/**
260+
* Creates a {@code NotificationInfo} object for the provided topic name.
261+
*
262+
* @param topic The name of the topic. It must have the format
263+
* "projects/{project}/topics/{topic}".
264+
*/
265+
public static NotificationInfo of(String topic) {
266+
PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format");
267+
return newBuilder(topic).build();
268+
}
269+
270+
/**
271+
* Returns a {@code NotificationInfo} builder where the topic's name is set to the provided name.
272+
*
273+
* @param topic The name of the topic. It must have the format
274+
* "projects/{project}/topics/{topic}".
275+
*/
276+
public static Builder newBuilder(String topic) {
277+
PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format");
278+
return new Builder(topic);
279+
}
280+
281+
static NotificationInfo fromPb(Notification notificationPb) {
282+
Builder builder = newBuilder(notificationPb.getTopic());
283+
if (notificationPb.getId() != null) {
284+
builder.setGeneratedId(notificationPb.getId());
285+
}
286+
if (notificationPb.getEtag() != null) {
287+
builder.setEtag(notificationPb.getEtag());
288+
}
289+
if (notificationPb.getCustomAttributes() != null) {
290+
builder.setCustomAttributes(notificationPb.getCustomAttributes());
291+
}
292+
if (notificationPb.getSelfLink() != null) {
293+
builder.setSelfLink(notificationPb.getSelfLink());
294+
}
295+
if (notificationPb.getObjectNamePrefix() != null) {
296+
builder.setObjectNamePrefix(notificationPb.getObjectNamePrefix());
297+
}
298+
if (notificationPb.getTopic() != null) {
299+
builder.setTopic(notificationPb.getTopic());
300+
}
301+
if (notificationPb.getEventTypes() != null) {
302+
builder.setEventTypes(notificationPb.getEventTypes());
303+
}
304+
if (notificationPb.getPayloadFormat() != null) {
305+
builder.setPayloadFormat(PayloadFormat.valueOf(notificationPb.getPayloadFormat()));
306+
}
307+
return builder.build();
308+
}
309+
}

google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java

+33
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.api.core.InternalExtensionOnly;
2323
import com.google.api.gax.paging.Page;
24+
import com.google.api.services.storage.model.Notification;
2425
import com.google.auth.ServiceAccountSigner;
2526
import com.google.auth.ServiceAccountSigner.SigningException;
2627
import com.google.cloud.FieldSelector;
@@ -3433,4 +3434,36 @@ List<Boolean> testIamPermissions(
34333434
* @throws StorageException upon failure
34343435
*/
34353436
ServiceAccount getServiceAccount(String projectId);
3437+
3438+
/**
3439+
* Creates a notification with the specified entity on the specified bucket.
3440+
*
3441+
* @return the notification that was created.
3442+
* @throws StorageException upon failure
3443+
*/
3444+
Notification createNotification(String bucket, NotificationInfo notification);
3445+
3446+
/**
3447+
* Get the notification with the specified name on the specified object.
3448+
*
3449+
* @return the notification object that exist on the bucket.
3450+
* @throws StorageException upon failure
3451+
*/
3452+
Notification getNotification(String bucket, String notification);
3453+
3454+
/**
3455+
* List the notifications for the provided bucket.
3456+
*
3457+
* @return a list of {@link Notification} objects that exist on the bucket.
3458+
* @throws StorageException upon failure
3459+
*/
3460+
List<Notification> listNotifications(String bucket);
3461+
3462+
/**
3463+
* Deletes the notification with the specified name on the specified object.
3464+
*
3465+
* @return {@code true} if the notification was deleted, {@code false} if it was not found
3466+
* @throws StorageException upon failure
3467+
*/
3468+
boolean deleteNotification(String bucket, String notification);
34363469
}

0 commit comments

Comments
 (0)