-
Notifications
You must be signed in to change notification settings - Fork 281
Add runtime per-bucket pub/sub notification configuration API #2157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8fe2275
add runtime per-bucket pub/sub notification configuration API
drehelis 5492c62
chore: lint fix
drehelis b7055ab
chore(fix) goroutine leak, close all clients on stop and some nitpicks
drehelis ef0568a
cr: always set standard GCS attributes regardless of PayloadFormat
drehelis ce366c6
cr: use t.Cleanup(srv.Stop) before returning
drehelis 490dd0d
cr: extract event trigger helper
drehelis efea8bc
empty commit, re-trigger build
drehelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,65 @@ | ||
| package fakestorage | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
|
|
||
| "github.com/fsouza/fake-gcs-server/internal/notification" | ||
| "github.com/gorilla/mux" | ||
| ) | ||
|
|
||
| func (s *Server) insertNotification(r *http.Request) jsonResponse { | ||
| bucketName := unescapeMuxVars(mux.Vars(r))["bucketName"] | ||
|
|
||
| if _, err := s.backend.GetBucket(bucketName); err != nil { | ||
| return jsonResponse{status: http.StatusNotFound} | ||
| } | ||
|
|
||
| var cfg notification.NotificationConfig | ||
| if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil { | ||
| return jsonResponse{status: http.StatusBadRequest, errorMessage: err.Error()} | ||
| } | ||
| if cfg.Topic == "" { | ||
| return jsonResponse{status: http.StatusBadRequest, errorMessage: "topic is required"} | ||
| } | ||
|
|
||
| created := s.notificationRegistry.Insert(bucketName, cfg) | ||
| return jsonResponse{status: http.StatusCreated, data: created} | ||
| } | ||
|
|
||
| func (s *Server) getNotification(r *http.Request) jsonResponse { | ||
| vars := unescapeMuxVars(mux.Vars(r)) | ||
| bucketName := vars["bucketName"] | ||
| notificationID := vars["notificationId"] | ||
|
|
||
| cfg, ok := s.notificationRegistry.Get(bucketName, notificationID) | ||
| if !ok { | ||
| return jsonResponse{status: http.StatusNotFound} | ||
| } | ||
| return jsonResponse{data: cfg} | ||
| } | ||
|
|
||
| func (s *Server) listNotifications(r *http.Request) jsonResponse { | ||
| bucketName := unescapeMuxVars(mux.Vars(r))["bucketName"] | ||
|
|
||
| if _, err := s.backend.GetBucket(bucketName); err != nil { | ||
| return jsonResponse{status: http.StatusNotFound} | ||
| } | ||
|
|
||
| cfgs := s.notificationRegistry.List(bucketName) | ||
| if cfgs == nil { | ||
| cfgs = []notification.NotificationConfig{} | ||
| } | ||
| return jsonResponse{data: map[string]interface{}{"kind": "storage#notifications", "items": cfgs}} | ||
| } | ||
|
|
||
| func (s *Server) deleteNotification(r *http.Request) jsonResponse { | ||
| vars := unescapeMuxVars(mux.Vars(r)) | ||
| bucketName := vars["bucketName"] | ||
| notificationID := vars["notificationId"] | ||
|
|
||
| if !s.notificationRegistry.Delete(bucketName, notificationID) { | ||
| return jsonResponse{status: http.StatusNotFound} | ||
| } | ||
| return jsonResponse{status: http.StatusNoContent} | ||
| } |
This file contains hidden or 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,145 @@ | ||
| package fakestorage | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/fsouza/fake-gcs-server/internal/notification" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func newNotificationServer(t *testing.T) *Server { | ||
| t.Helper() | ||
| srv, err := NewServerWithOptions(Options{NoListener: true}) | ||
| require.NoError(t, err) | ||
| srv.CreateBucketWithOpts(CreateBucketOpts{Name: "test-bucket"}) | ||
| return srv | ||
| } | ||
|
|
||
| func postNotification(t *testing.T, client *http.Client, bucket string, cfg notification.NotificationConfig) (*http.Response, notification.NotificationConfig) { | ||
| t.Helper() | ||
| body, _ := json.Marshal(cfg) | ||
| resp, err := client.Post( | ||
| fmt.Sprintf("https://storage.googleapis.com/storage/v1/b/%s/notificationConfigs", bucket), | ||
| "application/json", | ||
| bytes.NewReader(body), | ||
| ) | ||
| require.NoError(t, err) | ||
| defer resp.Body.Close() | ||
| var created notification.NotificationConfig | ||
| if resp.StatusCode == http.StatusCreated { | ||
| require.NoError(t, json.NewDecoder(resp.Body).Decode(&created)) | ||
| } | ||
| return resp, created | ||
| } | ||
|
|
||
| func TestInsertNotification(t *testing.T) { | ||
| srv := newNotificationServer(t) | ||
| cfg := notification.NotificationConfig{Topic: "projects/p/topics/t", PayloadFormat: "JSON_API_V1"} | ||
|
|
||
| resp, created := postNotification(t, srv.HTTPClient(), "test-bucket", cfg) | ||
| assert.Equal(t, http.StatusCreated, resp.StatusCode) | ||
| assert.NotEmpty(t, created.ID) | ||
| assert.Equal(t, "storage#notification", created.Kind) | ||
| assert.Equal(t, cfg.Topic, created.Topic) | ||
| } | ||
|
|
||
| func TestInsertNotification_BucketNotFound(t *testing.T) { | ||
| srv := newNotificationServer(t) | ||
| cfg := notification.NotificationConfig{Topic: "projects/p/topics/t"} | ||
| resp, _ := postNotification(t, srv.HTTPClient(), "no-such-bucket", cfg) | ||
| assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
| } | ||
|
|
||
| func TestInsertNotification_MissingTopic(t *testing.T) { | ||
| srv := newNotificationServer(t) | ||
| resp, _ := postNotification(t, srv.HTTPClient(), "test-bucket", notification.NotificationConfig{}) | ||
| assert.Equal(t, http.StatusBadRequest, resp.StatusCode) | ||
| } | ||
|
|
||
| func TestGetNotification(t *testing.T) { | ||
| srv := newNotificationServer(t) | ||
| cfg := notification.NotificationConfig{Topic: "projects/p/topics/t"} | ||
| _, inserted := postNotification(t, srv.HTTPClient(), "test-bucket", cfg) | ||
|
|
||
| resp, err := srv.HTTPClient().Get(fmt.Sprintf( | ||
| "https://storage.googleapis.com/storage/v1/b/test-bucket/notificationConfigs/%s", | ||
| inserted.ID, | ||
| )) | ||
| require.NoError(t, err) | ||
| defer resp.Body.Close() | ||
| assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
|
||
| var got notification.NotificationConfig | ||
| require.NoError(t, json.NewDecoder(resp.Body).Decode(&got)) | ||
| assert.Equal(t, inserted.ID, got.ID) | ||
| assert.Equal(t, cfg.Topic, got.Topic) | ||
| } | ||
|
|
||
| func TestGetNotification_NotFound(t *testing.T) { | ||
| srv := newNotificationServer(t) | ||
| resp, err := srv.HTTPClient().Get( | ||
| "https://storage.googleapis.com/storage/v1/b/test-bucket/notificationConfigs/9999", | ||
| ) | ||
| require.NoError(t, err) | ||
| defer resp.Body.Close() | ||
| assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
| } | ||
|
|
||
| func TestListNotifications(t *testing.T) { | ||
| srv := newNotificationServer(t) | ||
| client := srv.HTTPClient() | ||
| listURL := "https://storage.googleapis.com/storage/v1/b/test-bucket/notificationConfigs" | ||
| cfg := notification.NotificationConfig{Topic: "projects/p/topics/t"} | ||
|
|
||
| // empty list | ||
| resp, err := client.Get(listURL) | ||
| require.NoError(t, err) | ||
| defer resp.Body.Close() | ||
| assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
| var listResp struct { | ||
| Items []notification.NotificationConfig `json:"items"` | ||
| } | ||
| require.NoError(t, json.NewDecoder(resp.Body).Decode(&listResp)) | ||
| assert.Empty(t, listResp.Items) | ||
|
|
||
| // insert two | ||
| for i := 0; i < 2; i++ { | ||
| postNotification(t, client, "test-bucket", cfg) | ||
| } | ||
|
|
||
| resp2, err := client.Get(listURL) | ||
| require.NoError(t, err) | ||
| defer resp2.Body.Close() | ||
| require.NoError(t, json.NewDecoder(resp2.Body).Decode(&listResp)) | ||
| assert.Len(t, listResp.Items, 2) | ||
| } | ||
|
|
||
| func TestDeleteNotification(t *testing.T) { | ||
| srv := newNotificationServer(t) | ||
| client := srv.HTTPClient() | ||
| cfg := notification.NotificationConfig{Topic: "projects/p/topics/t"} | ||
| _, inserted := postNotification(t, client, "test-bucket", cfg) | ||
|
|
||
| deleteURL := fmt.Sprintf( | ||
| "https://storage.googleapis.com/storage/v1/b/test-bucket/notificationConfigs/%s", | ||
| inserted.ID, | ||
| ) | ||
|
|
||
| req, _ := http.NewRequest(http.MethodDelete, deleteURL, nil) | ||
| resp, err := client.Do(req) | ||
| require.NoError(t, err) | ||
| resp.Body.Close() | ||
| assert.Equal(t, http.StatusNoContent, resp.StatusCode) | ||
|
|
||
| // second delete → 404 | ||
| req2, _ := http.NewRequest(http.MethodDelete, deleteURL, nil) | ||
| resp2, err := client.Do(req2) | ||
| require.NoError(t, err) | ||
| resp2.Body.Close() | ||
| assert.Equal(t, http.StatusNotFound, resp2.StatusCode) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.