Skip to content

Commit

Permalink
Include bucket location in get-bucket and list-buckets response
Browse files Browse the repository at this point in the history
It's still a globally-configured value in the server, we won't store the
location provided to create-bucket. We can address that later (I'm sure
someone is going to ask for it :D).

Closes #590.
  • Loading branch information
fsouza committed Jan 1, 2022
1 parent 56dd454 commit 07e7a87
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
6 changes: 3 additions & 3 deletions fakestorage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func (s *Server) createBucketByPost(r *http.Request) jsonResponse {
if err != nil {
return jsonResponse{errorMessage: err.Error()}
}
return jsonResponse{data: newBucketResponse(bucket)}
return jsonResponse{data: newBucketResponse(bucket, s.options.BucketsLocation)}
}

func (s *Server) listBuckets(r *http.Request) jsonResponse {
buckets, err := s.backend.ListBuckets()
if err != nil {
return jsonResponse{errorMessage: err.Error()}
}
return jsonResponse{data: newListBucketsResponse(buckets)}
return jsonResponse{data: newListBucketsResponse(buckets, s.options.BucketsLocation)}
}

func (s *Server) getBucket(r *http.Request) jsonResponse {
Expand All @@ -97,7 +97,7 @@ func (s *Server) getBucket(r *http.Request) jsonResponse {
if err != nil {
return jsonResponse{status: http.StatusNotFound}
}
return jsonResponse{data: newBucketResponse(bucket)}
return jsonResponse{data: newBucketResponse(bucket, s.options.BucketsLocation)}
}

func (s *Server) deleteBucket(r *http.Request) jsonResponse {
Expand Down
8 changes: 5 additions & 3 deletions fakestorage/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ type listResponse struct {
Prefixes []string `json:"prefixes,omitempty"`
}

func newListBucketsResponse(buckets []backend.Bucket) listResponse {
func newListBucketsResponse(buckets []backend.Bucket, location string) listResponse {
resp := listResponse{
Kind: "storage#buckets",
Items: make([]interface{}, len(buckets)),
}
for i, bucket := range buckets {
resp.Items[i] = newBucketResponse(bucket)
resp.Items[i] = newBucketResponse(bucket, location)
}
return resp
}
Expand All @@ -31,19 +31,21 @@ type bucketResponse struct {
Name string `json:"name"`
Versioning *bucketVersioning `json:"versioning,omitempty"`
TimeCreated string `json:"timeCreated,omitempty"`
Location string `json:"location,omitempty"`
}

type bucketVersioning struct {
Enabled bool `json:"enabled,omitempty"`
}

func newBucketResponse(bucket backend.Bucket) bucketResponse {
func newBucketResponse(bucket backend.Bucket, location string) bucketResponse {
return bucketResponse{
Kind: "storage#bucket",
ID: bucket.Name,
Name: bucket.Name,
Versioning: &bucketVersioning{bucket.VersioningEnabled},
TimeCreated: bucket.TimeCreated.Format(timestampFormat),
Location: location,
}
}

Expand Down
3 changes: 3 additions & 0 deletions fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ type Options struct {
// EventOptions contains the events that should be published and the URL
// of the Google cloud function such events should be published to.
EventOptions notification.EventManagerOptions

// Location used for buckets in the server.
BucketsLocation string
}

// NewServerWithOptions creates a new server configured according to the
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Config struct {
backend string
fsRoot string
event EventConfig
bucketLocation string
}

type EventConfig struct {
Expand Down Expand Up @@ -67,6 +68,7 @@ func Load(args []string) (Config, error) {
fs.StringVar(&cfg.event.pubsubTopic, "event.pubsub-topic", "", "pubsub topic name to publish events on")
fs.StringVar(&cfg.event.prefix, "event.object-prefix", "", "if not empty, only objects having this prefix will generate trigger events")
fs.StringVar(&eventList, "event.list", eventFinalize, "comma separated list of events to publish on cloud function URl. Options are: finalize, delete, and metadataUpdate")
fs.StringVar(&cfg.bucketLocation, "location", "US-CENTRAL1", "location for buckets")

err := fs.Parse(args)
if err != nil {
Expand Down Expand Up @@ -162,5 +164,6 @@ func (c *Config) ToFakeGcsOptions() fakestorage.Options {
AllowedCORSHeaders: c.allowedCORSHeaders,
Writer: logrus.New().Writer(),
EventOptions: eventOptions,
BucketsLocation: c.bucketLocation,
}
}
5 changes: 5 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestLoadConfig(t *testing.T) {
"-event.pubsub-topic", "gcs-events",
"-event.object-prefix", "uploads/",
"-event.list", "finalize,delete,metadataUpdate,archive",
"-location", "US-EAST1",
},
expectedConfig: Config{
Seed: "/var/gcs",
Expand All @@ -54,6 +55,7 @@ func TestLoadConfig(t *testing.T) {
prefix: "uploads/",
list: []string{"finalize", "delete", "metadataUpdate", "archive"},
},
bucketLocation: "US-EAST1",
},
},
{
Expand All @@ -71,6 +73,7 @@ func TestLoadConfig(t *testing.T) {
event: EventConfig{
list: []string{"finalize"},
},
bucketLocation: "US-CENTRAL1",
},
},
{
Expand Down Expand Up @@ -148,6 +151,7 @@ func TestToFakeGcsOptions(t *testing.T) {
prefix: "uploads/",
list: []string{"finalize", "delete"},
},
bucketLocation: "US-EAST1",
},
fakestorage.Options{
StorageRoot: "/tmp/something",
Expand All @@ -165,6 +169,7 @@ func TestToFakeGcsOptions(t *testing.T) {
MetadataUpdate: false,
},
},
BucketsLocation: "US-EAST1",
},
},
{
Expand Down

0 comments on commit 07e7a87

Please sign in to comment.