From 2111d90684f8c40c7f382afe44ec12df5172347e Mon Sep 17 00:00:00 2001 From: Stefan Benten Date: Sat, 5 Sep 2020 21:17:18 +0200 Subject: [PATCH] satellite/admin: add get handler for projects Change-Id: Ibfb761b1c907b83451570c52eea03ecfa75290ca --- satellite/admin/README.md | 4 ++++ satellite/admin/project.go | 41 ++++++++++++++++++++++++++++++++ satellite/admin/project_test.go | 42 +++++++++++++++++++++++---------- satellite/admin/server.go | 1 + 4 files changed, 75 insertions(+), 13 deletions(-) diff --git a/satellite/admin/README.md b/satellite/admin/README.md index 6b12a3d9d..cdd6c241b 100644 --- a/satellite/admin/README.md +++ b/satellite/admin/README.md @@ -174,6 +174,10 @@ Updates rate limit for a project. Updates bucket limit for a project. +## GET /api/project/{project-id} + +Gets the common information about a project. + ## PUT /api/project/{project-id} Updates project name or description. diff --git a/satellite/admin/project.go b/satellite/admin/project.go index 19d16175b..6b380c53e 100644 --- a/satellite/admin/project.go +++ b/satellite/admin/project.go @@ -49,6 +49,47 @@ func (server *Server) checkProjectUsage(w http.ResponseWriter, r *http.Request) } } +func (server *Server) getProject(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + vars := mux.Vars(r) + projectUUIDString, ok := vars["project"] + if !ok { + httpJSONError(w, "project-uuid missing", + "", http.StatusBadRequest) + return + } + + projectUUID, err := uuid.FromString(projectUUIDString) + if err != nil { + httpJSONError(w, "invalid project-uuid", + err.Error(), http.StatusBadRequest) + return + } + + if err := r.ParseForm(); err != nil { + httpJSONError(w, "invalid form", + err.Error(), http.StatusBadRequest) + return + } + + project, err := server.db.Console().Projects().Get(ctx, projectUUID) + if err != nil { + httpJSONError(w, "unable to fetch project details", + err.Error(), http.StatusInternalServerError) + } + + data, err := json.Marshal(project) + if err != nil { + httpJSONError(w, "json encoding failed", + err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared +} + func (server *Server) getProjectLimit(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/satellite/admin/project_test.go b/satellite/admin/project_test.go index 4616c4d68..455158502 100644 --- a/satellite/admin/project_test.go +++ b/satellite/admin/project_test.go @@ -39,17 +39,33 @@ func TestAPI(t *testing.T) { }, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) { sat := planet.Satellites[0] address := sat.Admin.Admin.Listener.Addr() - project := planet.Uplinks[0].Projects[0] + project, err := sat.DB.Console().Projects().Get(ctx, planet.Uplinks[0].Projects[0].ID) + require.NoError(t, err) - link := "http://" + address.String() + "/api/project/" + project.ID.String() + "/limit" + link := "http://" + address.String() + "/api/project/" + project.ID.String() + linkLimit := link + "/limit" t.Run("GetProject", func(t *testing.T) { - assertGet(t, link, `{"usage":{"amount":"0 B","bytes":0},"bandwidth":{"amount":"0 B","bytes":0},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken) + require.NoError(t, err) + expected := fmt.Sprintf( + `{"id":"%s","name":"%s","description":"%s","partnerId":"%s","ownerId":"%s","rateLimit":null,"maxBuckets":0,"createdAt":"%s"}`, + project.ID.String(), + project.Name, + project.Description, + project.PartnerID.String(), + project.OwnerID.String(), + project.CreatedAt.Format(time.RFC3339Nano), + ) + assertGet(t, link, expected, planet.Satellites[0].Config.Console.AuthToken) + }) + + t.Run("GetProjectLimits", func(t *testing.T) { + assertGet(t, linkLimit, `{"usage":{"amount":"0 B","bytes":0},"bandwidth":{"amount":"0 B","bytes":0},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken) }) t.Run("UpdateUsage", func(t *testing.T) { data := url.Values{"usage": []string{"1TiB"}} - req, err := http.NewRequest(http.MethodPost, link, strings.NewReader(data.Encode())) + req, err := http.NewRequest(http.MethodPost, linkLimit, strings.NewReader(data.Encode())) require.NoError(t, err) req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") @@ -59,9 +75,9 @@ func TestAPI(t *testing.T) { require.Equal(t, http.StatusOK, response.StatusCode) require.NoError(t, response.Body.Close()) - assertGet(t, link, `{"usage":{"amount":"1.0 TiB","bytes":1099511627776},"bandwidth":{"amount":"0 B","bytes":0},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken) + assertGet(t, linkLimit, `{"usage":{"amount":"1.0 TiB","bytes":1099511627776},"bandwidth":{"amount":"0 B","bytes":0},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken) - req, err = http.NewRequest(http.MethodPut, link+"?usage=1GB", nil) + req, err = http.NewRequest(http.MethodPut, linkLimit+"?usage=1GB", nil) require.NoError(t, err) req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) @@ -70,11 +86,11 @@ func TestAPI(t *testing.T) { require.Equal(t, http.StatusOK, response.StatusCode) require.NoError(t, response.Body.Close()) - assertGet(t, link, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"0 B","bytes":0},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken) + assertGet(t, linkLimit, `{"usage":{"amount":"1.00 GB","bytes":1000000000},"bandwidth":{"amount":"0 B","bytes":0},"rate":{"rps":0},"maxBuckets":0}`, planet.Satellites[0].Config.Console.AuthToken) }) t.Run("UpdateBandwidth", func(t *testing.T) { - req, err := http.NewRequest(http.MethodPut, link+"?bandwidth=1MB", nil) + req, err := http.NewRequest(http.MethodPut, linkLimit+"?bandwidth=1MB", nil) require.NoError(t, err) req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) @@ -83,11 +99,11 @@ func TestAPI(t *testing.T) { require.Equal(t, http.StatusOK, response.StatusCode) require.NoError(t, response.Body.Close()) - assertGet(t, link, `{"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(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) }) t.Run("UpdateRate", func(t *testing.T) { - req, err := http.NewRequest(http.MethodPut, link+"?rate=100", nil) + req, err := http.NewRequest(http.MethodPut, linkLimit+"?rate=100", nil) require.NoError(t, err) req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) @@ -96,10 +112,10 @@ func TestAPI(t *testing.T) { require.Equal(t, http.StatusOK, response.StatusCode) require.NoError(t, response.Body.Close()) - assertGet(t, link, `{"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(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) }) t.Run("UpdateBuckets", func(t *testing.T) { - req, err := http.NewRequest(http.MethodPut, link+"?buckets=2000", nil) + req, err := http.NewRequest(http.MethodPut, linkLimit+"?buckets=2000", nil) require.NoError(t, err) req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) @@ -108,7 +124,7 @@ func TestAPI(t *testing.T) { require.Equal(t, http.StatusOK, response.StatusCode) require.NoError(t, response.Body.Close()) - assertGet(t, link, `{"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(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) }) }) } diff --git a/satellite/admin/server.go b/satellite/admin/server.go index 6e3120d77..32e5da663 100644 --- a/satellite/admin/server.go +++ b/satellite/admin/server.go @@ -82,6 +82,7 @@ func NewServer(log *zap.Logger, listener net.Listener, db DB, accounts payments. server.mux.HandleFunc("/api/project/{project}/usage", server.checkProjectUsage).Methods("GET") server.mux.HandleFunc("/api/project/{project}/limit", server.getProjectLimit).Methods("GET") server.mux.HandleFunc("/api/project/{project}/limit", server.putProjectLimit).Methods("PUT", "POST") + server.mux.HandleFunc("/api/project/{project}", server.getProject).Methods("GET") server.mux.HandleFunc("/api/project/{project}", server.renameProject).Methods("PUT") server.mux.HandleFunc("/api/project/{project}", server.deleteProject).Methods("DELETE") server.mux.HandleFunc("/api/project", server.addProject).Methods("POST")