Skip to content

Commit

Permalink
satellite/admin: add project rename functionality
Browse files Browse the repository at this point in the history
Change-Id: I4c0f42d4c2c26859279f247f94cef97a8ff630a9
  • Loading branch information
stefanbenten authored and ihaid committed Jul 14, 2020
1 parent eb0f6a0 commit 51d49f0
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
11 changes: 11 additions & 0 deletions satellite/admin/README.md
Expand Up @@ -160,6 +160,17 @@ Updates bandwidth limit for a project.

Updates rate limit for a project.

## PUT /api/project/{project-id}

Updates project name or description.

```json
{
"projectName": "My new Project Name",
"description": "My new awesome description!"
}
```

## DELETE /api/project/{project-id}

Deletes the project.
Expand Down
60 changes: 60 additions & 0 deletions satellite/admin/project.go
Expand Up @@ -4,7 +4,9 @@
package admin

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -220,6 +222,64 @@ func (server *Server) addProject(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
}

func (server *Server) renameProject(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

vars := mux.Vars(r)
projectUUIDString, ok := vars["project"]
if !ok {
http.Error(w, "project-uuid missing", http.StatusBadRequest)
return
}

projectUUID, err := uuid.FromString(projectUUIDString)
if err != nil {
http.Error(w, fmt.Sprintf("invalid project-uuid: %v", err), http.StatusBadRequest)
return
}

project, err := server.db.Console().Projects().Get(ctx, projectUUID)
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, "project with specified uuid does not exist", http.StatusBadRequest)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("error getting project: %v", err), http.StatusInternalServerError)
return
}

body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("failed to read body: %v", err), http.StatusInternalServerError)
return
}

var input struct {
ProjectName string `json:"projectName"`
Description string `json:"description"`
}

err = json.Unmarshal(body, &input)
if err != nil {
http.Error(w, fmt.Sprintf("failed to unmarshal request: %v", err), http.StatusBadRequest)
return
}

if input.ProjectName == "" {
http.Error(w, "ProjectName is not set", http.StatusBadRequest)
return
}

project.Name = input.ProjectName
project.Description = input.Description

err = server.db.Console().Projects().Update(ctx, project)
if err != nil {
http.Error(w, fmt.Sprintf("error renaming project: %v", err), http.StatusInternalServerError)
return
}
}

func (server *Server) deleteProject(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

Expand Down
34 changes: 34 additions & 0 deletions satellite/admin/project_test.go
Expand Up @@ -140,6 +140,40 @@ func TestAddProject(t *testing.T) {
})
}

func TestRenameProject(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1,
StorageNodeCount: 0,
UplinkCount: 1,
Reconfigure: testplanet.Reconfigure{
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
config.Admin.Address = "127.0.0.1:0"
},
},
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
address := planet.Satellites[0].Admin.Admin.Listener.Addr()
userID := planet.Uplinks[0].Projects[0].Owner
oldName, newName := "renameTest", "Test Project"

project, err := planet.Satellites[0].AddProject(ctx, userID.ID, oldName)
require.NoError(t, err)
require.Equal(t, oldName, project.Name)

body := strings.NewReader(fmt.Sprintf(`{"projectName":"%s","description":"This project got renamed"}`, newName))
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("http://"+address.String()+"/api/project/%s", project.ID.String()), body)
require.NoError(t, err)
req.Header.Set("Authorization", "very-secret-token")

response, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode)

project, err = planet.Satellites[0].DB.Console().Projects().Get(ctx, project.ID)
require.NoError(t, err)
require.Equal(t, newName, project.Name)
})
}

func TestDeleteProject(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1,
Expand Down
1 change: 1 addition & 0 deletions satellite/admin/server.go
Expand Up @@ -75,6 +75,7 @@ func NewServer(log *zap.Logger, listener net.Listener, db DB, config Config) *Se
server.mux.HandleFunc("/api/coupon/{couponid}", server.deleteCoupon).Methods("DELETE")
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.renameProject).Methods("PUT")
server.mux.HandleFunc("/api/project/{project}", server.deleteProject).Methods("DELETE")
server.mux.HandleFunc("/api/project", server.addProject).Methods("POST")

Expand Down
2 changes: 1 addition & 1 deletion satellite/satellitedb/dbx/satellitedb.dbx
Expand Up @@ -305,7 +305,7 @@ model project (

field id blob

field name text
field name text ( updatable )
field description text ( updatable )
field usage_limit int64 ( updatable, default 0 )
field bandwidth_limit int64 ( updatable, default 0 )
Expand Down
11 changes: 11 additions & 0 deletions satellite/satellitedb/dbx/satellitedb.dbx.go
Expand Up @@ -5329,6 +5329,7 @@ type Project_Create_Fields struct {
}

type Project_Update_Fields struct {
Name Project_Name_Field
Description Project_Description_Field
UsageLimit Project_UsageLimit_Field
BandwidthLimit Project_BandwidthLimit_Field
Expand Down Expand Up @@ -13767,6 +13768,11 @@ func (obj *pgxImpl) Update_Project_By_Id(ctx context.Context,
var __values []interface{}
var __args []interface{}

if update.Name._set {
__values = append(__values, update.Name.value())
__sets_sql.SQLs = append(__sets_sql.SQLs, __sqlbundle_Literal("name = ?"))
}

if update.Description._set {
__values = append(__values, update.Description.value())
__sets_sql.SQLs = append(__sets_sql.SQLs, __sqlbundle_Literal("description = ?"))
Expand Down Expand Up @@ -20273,6 +20279,11 @@ func (obj *pgxcockroachImpl) Update_Project_By_Id(ctx context.Context,
var __values []interface{}
var __args []interface{}

if update.Name._set {
__values = append(__values, update.Name.value())
__sets_sql.SQLs = append(__sets_sql.SQLs, __sqlbundle_Literal("name = ?"))
}

if update.Description._set {
__values = append(__values, update.Description.value())
__sets_sql.SQLs = append(__sets_sql.SQLs, __sqlbundle_Literal("description = ?"))
Expand Down
1 change: 1 addition & 0 deletions satellite/satellitedb/projects.go
Expand Up @@ -127,6 +127,7 @@ func (projects *projects) Update(ctx context.Context, project *console.Project)
defer mon.Task()(&ctx)(&err)

updateFields := dbx.Project_Update_Fields{
Name: dbx.Project_Name(project.Name),
Description: dbx.Project_Description(project.Description),
RateLimit: dbx.Project_RateLimit_Raw(project.RateLimit),
}
Expand Down

0 comments on commit 51d49f0

Please sign in to comment.