Skip to content

Commit

Permalink
Adding PubSub Notification Samples
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-munro committed Mar 28, 2022
1 parent d5ed72c commit 9d1c73a
Show file tree
Hide file tree
Showing 12 changed files with 554 additions and 0 deletions.
6 changes: 6 additions & 0 deletions samples/install-without-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<version>4.5.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.116.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<!-- compile and run all snippet tests -->
Expand Down
6 changes: 6 additions & 0 deletions samples/native-image-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<version>1.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.116.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
6 changes: 6 additions & 0 deletions samples/snapshot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<version>4.5.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.116.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<!-- compile and run all snippet tests -->
Expand Down
6 changes: 6 additions & 0 deletions samples/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@
<version>1.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.106.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 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
*
* http://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.example.storage.bucket;

// [START storage_create_bucket_notifications]

import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.Notification;
import com.google.cloud.storage.NotificationInfo;


public class CreateBucketPubSubNotification {

public static void createBucketPubSubNotification(String bucketName, NotificationInfo notificationInfo) {
// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The NotificationInfo for the notification you would like to create
// See: https://cloud.google.com/java/docs/reference/google-cloud-notification/latest/com.google.cloud.notification.NotificationInfo
Storage storage = StorageOptions.newBuilder().build().getService();
Notification notification = storage.createNotification(bucketName, notificationInfo);
System.out.println("Successfully created notification for topic " + notification.getTopic());
}
}
// [END storage_create_bucket_notifications]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 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
*
* http://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.example.storage.bucket;

// [START storage_delete_bucket_notification]

import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;


public class DeleteBucketPubSubNotification {

public static void deleteBucketPubSubNotification(String bucketName, String notificationId) {
// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The NotificationId for the notification you would like to delete\
// String notificationId = "your-unique-notification-id"
Storage storage = StorageOptions.newBuilder().build().getService();
boolean success = storage.deleteNotification(bucketName, notificationId);
if(success) {
System.out.println("Successfully deleted notification");
} else {
System.out.println("Failed to find notification");
}
}
}
// [END storage_delete_bucket_notification]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 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
*
* http://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.example.storage.bucket;

// [START storage_list_bucket_notifications]

import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.Notification;
import java.util.List;


public class ListPubSubNotifications {

public static void listPubSubNotifications(String bucketName) {
// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";
Storage storage = StorageOptions.newBuilder().build().getService();
List<Notification> notificationList = storage.listNotifications(bucketName);
for(Notification notification : notificationList) {
System.out.println("Found notification " + notification.getTopic() + " for bucket "+ bucketName);
}
}
}
// [END storage_list_bucket_notifications]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 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
*
* http://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.example.storage.bucket;

// [START storage_print_pubsub_bucket_notification]

import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.Notification;
import java.util.List;


public class PrintPubSubNotification {

public static void printPubSubNotification(String bucketName, String notificationId) {
// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The Pub/Sub topic you would like to find
// String notificationId = "your-unique-notification-id"

Storage storage = StorageOptions.newBuilder().build().getService();
Notification notification = storage.getNotification(bucketName, notificationId);
System.out.println("Found notification " + notification.getTopic() + " for bucket "+ bucketName);
}
}
// [END storage_print_pubsub_bucket_notification]
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2022 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
*
* http://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.example.storage.bucket;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;

import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.example.storage.TestBase;
import com.google.cloud.storage.Notification;
import com.google.cloud.storage.NotificationInfo;
import com.google.common.collect.ImmutableMap;
import com.google.iam.v1.Binding;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.SetIamPolicyRequest;
import java.io.IOException;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class CreateBucketPubSubNotificationTest extends TestBase {
private static final Notification.PayloadFormat PAYLOAD_FORMAT =
Notification.PayloadFormat.JSON_API_V1.JSON_API_V1;
private static final Map<String, String> CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1");
private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String TOPIC = String.format("projects/%s/topics/new-topic", PROJECT);
private static TopicAdminClient topicAdminClient;

@BeforeClass
public static void configureTopicAdminClient() throws IOException {
if(PROJECT != null) {
topicAdminClient = TopicAdminClient.create();
topicAdminClient.createTopic(TOPIC);
GetIamPolicyRequest getIamPolicyRequest =
GetIamPolicyRequest.newBuilder().setResource(TOPIC).build();
com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(getIamPolicyRequest);
Binding binding =
Binding.newBuilder().setRole("roles/owner").addMembers("allAuthenticatedUsers").build();
SetIamPolicyRequest setIamPolicyRequest =
SetIamPolicyRequest.newBuilder()
.setResource(TOPIC)
.setPolicy(policy.toBuilder().addBindings(binding).build())
.build();
topicAdminClient.setIamPolicy(setIamPolicyRequest);
}
}

@AfterClass
public static void deleteTopicAndClient() {
/* Delete the Pub/Sub topic */
if (topicAdminClient != null) {
topicAdminClient.deleteTopic(TOPIC);
topicAdminClient.close();
}
}

@Test
public void testCreateBucketPubSubNotification() {
// Check that we can access project value and that topic admin client came up successfully
assertNotNull("Unable to determine project", PROJECT);
assertNotNull("Topic Admin Client did not start up", topicAdminClient);

NotificationInfo notificationInfo =
NotificationInfo.newBuilder(TOPIC)
.setCustomAttributes(CUSTOM_ATTRIBUTES)
.setPayloadFormat(PAYLOAD_FORMAT)
.build();
CreateBucketPubSubNotification.createBucketPubSubNotification(bucketName, notificationInfo);
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(TOPIC);
}

}

0 comments on commit 9d1c73a

Please sign in to comment.