From c4d4efdda866f7a66112858efa00aaa4ccf0b716 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Tue, 12 Oct 2021 11:07:48 +0200 Subject: [PATCH] return newly created space in response --- changelog/unreleased/create-space-response.md | 6 +++ graph/pkg/service/v0/drives.go | 38 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 changelog/unreleased/create-space-response.md diff --git a/changelog/unreleased/create-space-response.md b/changelog/unreleased/create-space-response.md new file mode 100644 index 00000000000..1fbaa56d7a5 --- /dev/null +++ b/changelog/unreleased/create-space-response.md @@ -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 diff --git a/graph/pkg/service/v0/drives.go b/graph/pkg/service/v0/drives.go index 2cd8f8b9535..9810eac0c02 100644 --- a/graph/pkg/service/v0/drives.go +++ b/graph/pkg/service/v0/drives.go @@ -164,12 +164,12 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { } drive := msgraph.Drive{} 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 } spaceName := *drive.Name if spaceName == "" { - errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, fmt.Errorf("invalid name").Error()) + errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "invalid name") return } @@ -181,7 +181,7 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { case "": driveType = "project" 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 @@ -207,8 +207,24 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { } 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) { @@ -226,19 +242,19 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) { } 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 } drive := msgraph.Drive{} 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 } identifierParts := strings.Split(req.FirstSegment.Identifier.Get(), "!") 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) } @@ -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) if err != nil { w.WriteHeader(http.StatusInternalServerError) } 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)