return newly created space in response

This commit is contained in:
David Christofas
2021-10-12 19:18:50 +02:00
parent 185e11e861
commit c4d4efdda8
2 changed files with 36 additions and 8 deletions
@@ -0,0 +1,6 @@
Enhancement: Return the newly created space
Changed the response of the CreateSpace method to include the newly created space.
https://github.com/owncloud/ocis/pull/2610
https://github.com/cs3org/reva/pull/2158
+30 -8
View File
@@ -164,12 +164,12 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
} }
drive := msgraph.Drive{} drive := msgraph.Drive{}
if err := json.NewDecoder(r.Body).Decode(&drive); err != nil { if err := json.NewDecoder(r.Body).Decode(&drive); err != nil {
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("invalid schema definition").Error()) errorcode.GeneralException.Render(w, r, http.StatusBadRequest, "invalid schema definition")
return return
} }
spaceName := *drive.Name spaceName := *drive.Name
if spaceName == "" { if spaceName == "" {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, fmt.Errorf("invalid name").Error()) errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "invalid name")
return return
} }
@@ -181,7 +181,7 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
case "": case "":
driveType = "project" driveType = "project"
case "share": case "share":
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("drives of type share cannot be created via this api").Error()) errorcode.GeneralException.Render(w, r, http.StatusBadRequest, "drives of type share cannot be created via this api")
} }
var quota uint64 var quota uint64
@@ -207,8 +207,24 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
} }
if resp.GetStatus().GetCode() != v1beta11.Code_CODE_OK { if resp.GetStatus().GetCode() != v1beta11.Code_CODE_OK {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, fmt.Errorf("").Error()) errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "")
} }
wdu, err := url.Parse(g.config.Spaces.WebDavBase)
if err != nil {
g.logger.Error().Err(err).Msg("error parsing url")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
newDrive, err := cs3StorageSpaceToDrive(wdu, resp.StorageSpace)
if err != nil {
g.logger.Error().Err(err).Msg("error parsing url")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
render.Status(r, http.StatusCreated)
render.JSON(w, r, newDrive)
} }
func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) { func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
@@ -226,19 +242,19 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
} }
if req.FirstSegment.Identifier.Get() == "" { if req.FirstSegment.Identifier.Get() == "" {
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("identifier cannot be empty").Error()) errorcode.GeneralException.Render(w, r, http.StatusBadRequest, "identifier cannot be empty")
return return
} }
drive := msgraph.Drive{} drive := msgraph.Drive{}
if err = json.NewDecoder(r.Body).Decode(&drive); err != nil { if err = json.NewDecoder(r.Body).Decode(&drive); err != nil {
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("invalid request body: %v", r.Body).Error()) errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("invalid request body: %v", r.Body))
return return
} }
identifierParts := strings.Split(req.FirstSegment.Identifier.Get(), "!") identifierParts := strings.Split(req.FirstSegment.Identifier.Get(), "!")
if len(identifierParts) != 2 { if len(identifierParts) != 2 {
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Errorf("invalid resource id: %v", req.FirstSegment.Identifier.Get()).Error()) errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("invalid resource id: %v", req.FirstSegment.Identifier.Get()))
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
} }
@@ -262,13 +278,19 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
}, },
} }
if drive.Quota.HasTotal() {
updateSpaceRequest.StorageSpace.Quota = &storageprovider.Quota{
QuotaMaxBytes: uint64(*drive.Quota.Total),
}
}
resp, err := client.UpdateStorageSpace(r.Context(), updateSpaceRequest) resp, err := client.UpdateStorageSpace(r.Context(), updateSpaceRequest)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
} }
if resp.GetStatus().GetCode() != v1beta11.Code_CODE_OK { if resp.GetStatus().GetCode() != v1beta11.Code_CODE_OK {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, fmt.Errorf("").Error()) errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "")
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)