satellite/admin: add get handler for projects

Change-Id: Ibfb761b1c907b83451570c52eea03ecfa75290ca
This commit is contained in:
Stefan Benten 2020-09-05 21:17:18 +02:00
parent 8b4b44d42b
commit 2111d90684
4 changed files with 75 additions and 13 deletions

View File

@ -174,6 +174,10 @@ Updates rate limit for a project.
Updates bucket 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} ## PUT /api/project/{project-id}
Updates project name or description. Updates project name or description.

View File

@ -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) { func (server *Server) getProjectLimit(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()

View File

@ -39,17 +39,33 @@ func TestAPI(t *testing.T) {
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) { }, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
sat := planet.Satellites[0] sat := planet.Satellites[0]
address := sat.Admin.Admin.Listener.Addr() 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) { 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) { t.Run("UpdateUsage", func(t *testing.T) {
data := url.Values{"usage": []string{"1TiB"}} 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) require.NoError(t, err)
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 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.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close()) 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) require.NoError(t, err)
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) 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.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close()) 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) { 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) require.NoError(t, err)
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) 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.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close()) 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) { 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) require.NoError(t, err)
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) 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.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close()) 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) { 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) require.NoError(t, err)
req.Header.Set("Authorization", planet.Satellites[0].Config.Console.AuthToken) 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.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, response.Body.Close()) 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)
}) })
}) })
} }

View File

@ -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}/usage", server.checkProjectUsage).Methods("GET")
server.mux.HandleFunc("/api/project/{project}/limit", server.getProjectLimit).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}/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.renameProject).Methods("PUT")
server.mux.HandleFunc("/api/project/{project}", server.deleteProject).Methods("DELETE") server.mux.HandleFunc("/api/project/{project}", server.deleteProject).Methods("DELETE")
server.mux.HandleFunc("/api/project", server.addProject).Methods("POST") server.mux.HandleFunc("/api/project", server.addProject).Methods("POST")