-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5ed72c
commit 9d1c73a
Showing
12 changed files
with
554 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...les/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
42 changes: 42 additions & 0 deletions
42
...les/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
40 changes: 40 additions & 0 deletions
40
samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
41 changes: 41 additions & 0 deletions
41
samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
87 changes: 87 additions & 0 deletions
87
...snippets/src/test/java/com/example/storage/bucket/CreateBucketPubSubNotificationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.