satellite/admin: add project limit on segment count

Added possibility to set custom value of segments
per project for user/customer.

Change-Id: Ic6e1abe25269755e9e8819fbef065a87de359fce
This commit is contained in:
Qweder93 2021-12-08 15:18:40 +02:00 committed by Nikolai Siedov
parent f87ce0cadf
commit b7b010adc9
2 changed files with 41 additions and 8 deletions

View File

@ -130,7 +130,8 @@ func (server *Server) getProjectLimit(w http.ResponseWriter, r *http.Request) {
Rate struct {
RPS int `json:"rps"`
} `json:"rate"`
Buckets int `json:"maxBuckets"`
Buckets int `json:"maxBuckets"`
Segments int64 `json:"maxSegments"`
}
if project.StorageLimit != nil {
output.Usage.Amount = *project.StorageLimit
@ -146,6 +147,9 @@ func (server *Server) getProjectLimit(w http.ResponseWriter, r *http.Request) {
if project.RateLimit != nil {
output.Rate.RPS = *project.RateLimit
}
if project.SegmentLimit != nil {
output.Segments = *project.SegmentLimit
}
data, err := json.Marshal(output)
if err != nil {
@ -181,6 +185,7 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
Rate *int `schema:"rate"`
Burst *int `schema:"burst"`
Buckets *int `schema:"buckets"`
Segments *int64 `schema:"segments"`
}
if err := r.ParseForm(); err != nil {
@ -272,7 +277,7 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
if arguments.Buckets != nil {
if *arguments.Buckets < 0 {
sendJSONError(w, "negative bucket coun",
sendJSONError(w, "negative bucket count",
fmt.Sprintf("t: %v", arguments.Buckets), http.StatusBadRequest)
return
}
@ -284,6 +289,21 @@ func (server *Server) putProjectLimit(w http.ResponseWriter, r *http.Request) {
return
}
}
if arguments.Segments != nil {
if *arguments.Segments < 0 {
sendJSONError(w, "negative segments count",
fmt.Sprintf("t: %v", arguments.Buckets), http.StatusBadRequest)
return
}
err = server.db.ProjectAccounting().UpdateProjectSegmentLimit(ctx, projectUUID, *arguments.Segments)
if err != nil {
sendJSONError(w, "failed to update segments limit",
err.Error(), http.StatusInternalServerError)
return
}
}
}
func (server *Server) addProject(w http.ResponseWriter, r *http.Request) {

View File

@ -86,7 +86,7 @@ func TestProjectLimit(t *testing.T) {
linkLimit := "http://" + address.String() + "/api/projects/" + project.ID.String() + "/limit"
t.Run("Get OK", func(t *testing.T) {
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"25.00 GB","bytes":25000000000},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken)
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"25.00 GB","bytes":25000000000},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0,"maxSegments":140000}`, planet.Satellites[0].Config.Console.AuthToken)
})
t.Run("Get Not Found", func(t *testing.T) {
@ -134,7 +134,7 @@ func TestProjectLimit(t *testing.T) {
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close())
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.0 TiB","bytes":1099511627776},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken)
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.0 TiB","bytes":1099511627776},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0,"maxSegments":140000}`, planet.Satellites[0].Config.Console.AuthToken)
req, err = http.NewRequestWithContext(ctx, http.MethodPut, linkLimit+"?usage=1GB", nil)
require.NoError(t, err)
@ -145,7 +145,7 @@ func TestProjectLimit(t *testing.T) {
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close())
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken)
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"25.00 GB","bytes":25000000000},"rate":{"rps":0},"maxBuckets":0,"maxSegments":140000}`, planet.Satellites[0].Config.Console.AuthToken)
})
t.Run("Update Bandwidth", func(t *testing.T) {
@ -158,7 +158,7 @@ func TestProjectLimit(t *testing.T) {
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close())
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken)
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":0},"maxBuckets":0,"maxSegments":140000}`, planet.Satellites[0].Config.Console.AuthToken)
})
t.Run("Update Rate", func(t *testing.T) {
@ -171,7 +171,7 @@ func TestProjectLimit(t *testing.T) {
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close())
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":100},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken)
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":100},"maxBuckets":0,"maxSegments":140000}`, planet.Satellites[0].Config.Console.AuthToken)
})
t.Run("Update Buckets", func(t *testing.T) {
@ -184,7 +184,20 @@ func TestProjectLimit(t *testing.T) {
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close())
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":100},"maxBuckets":2000}`, planet.Satellites[0].Config.Console.AuthToken)
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":100},"maxBuckets":2000,"maxSegments":140000}`, planet.Satellites[0].Config.Console.AuthToken)
})
t.Run("Update Segment Limit", func(t *testing.T) {
req, err := http.NewRequestWithContext(ctx, http.MethodPut, linkLimit+"?segments=500", nil)
require.NoError(t, err)
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
response, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close())
assertGet(ctx, t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"1.00 MB","bytes":1000000},"rate":{"rps":100},"maxBuckets":2000,"maxSegments":500}`, planet.Satellites[0].Config.Console.AuthToken)
})
})
}