-
Notifications
You must be signed in to change notification settings - Fork 402
/
bucket.go
132 lines (110 loc) · 3.49 KB
/
bucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package admin
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
"storj.io/common/storj"
"storj.io/common/uuid"
"storj.io/storj/satellite/buckets"
)
func validateBucketPathParameters(vars map[string]string) (project uuid.NullUUID, bucket []byte, err error) {
projectUUIDString, ok := vars["project"]
if !ok {
return project, bucket, fmt.Errorf("project-uuid missing")
}
project.UUID, err = uuid.FromString(projectUUIDString)
if err != nil {
return project, bucket, fmt.Errorf("project-uuid is not a valid uuid")
}
project.Valid = true
bucketName := vars["bucket"]
if len(bucketName) == 0 {
return project, bucket, fmt.Errorf("bucket name is missing")
}
bucket = []byte(bucketName)
return
}
func parsePlacementConstraint(regionCode string) (storj.PlacementConstraint, error) {
switch regionCode {
case "EU":
return storj.EU, nil
case "EEA":
return storj.EEA, nil
case "US":
return storj.US, nil
case "DE":
return storj.DE, nil
case "":
return storj.EveryCountry, fmt.Errorf("missing region parameter")
default:
return storj.EveryCountry, fmt.Errorf("unrecognized region parameter: %s", regionCode)
}
}
func (server *Server) updateBucket(w http.ResponseWriter, r *http.Request, placement storj.PlacementConstraint) {
ctx := r.Context()
project, bucket, err := validateBucketPathParameters(mux.Vars(r))
if err != nil {
sendJSONError(w, err.Error(), "", http.StatusBadRequest)
return
}
b, err := server.buckets.GetBucket(ctx, bucket, project.UUID)
if err != nil {
if storj.ErrBucketNotFound.Has(err) {
sendJSONError(w, "bucket does not exist", "", http.StatusBadRequest)
} else {
sendJSONError(w, "unable to create geofence for bucket", err.Error(), http.StatusInternalServerError)
}
return
}
b.Placement = placement
_, err = server.buckets.UpdateBucket(ctx, b)
if err != nil {
switch {
case storj.ErrBucketNotFound.Has(err):
sendJSONError(w, "bucket does not exist", "", http.StatusBadRequest)
case buckets.ErrBucketNotEmpty.Has(err):
sendJSONError(w, "bucket must be empty", "", http.StatusBadRequest)
default:
sendJSONError(w, "unable to create geofence for bucket", err.Error(), http.StatusInternalServerError)
}
return
}
w.WriteHeader(http.StatusOK)
}
func (server *Server) createGeofenceForBucket(w http.ResponseWriter, r *http.Request) {
placement, err := parsePlacementConstraint(r.URL.Query().Get("region"))
if err != nil {
sendJSONError(w, err.Error(), "available: EU, EEA, US, DE", http.StatusBadRequest)
return
}
server.updateBucket(w, r, placement)
}
func (server *Server) deleteGeofenceForBucket(w http.ResponseWriter, r *http.Request) {
server.updateBucket(w, r, storj.EveryCountry)
}
func (server *Server) getBucketInfo(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
project, bucket, err := validateBucketPathParameters(mux.Vars(r))
if err != nil {
sendJSONError(w, err.Error(), "", http.StatusBadRequest)
return
}
b, err := server.buckets.GetBucket(ctx, bucket, project.UUID)
if err != nil {
if storj.ErrBucketNotFound.Has(err) {
sendJSONError(w, "bucket does not exist", "", http.StatusBadRequest)
} else {
sendJSONError(w, "unable to check bucket", err.Error(), http.StatusInternalServerError)
}
return
}
data, err := json.Marshal(b)
if err != nil {
sendJSONError(w, "failed to marshal bucket", err.Error(), http.StatusInternalServerError)
} else {
sendJSONData(w, http.StatusOK, data)
}
}