Skip to content

Commit

Permalink
return newly created space in response
Browse files Browse the repository at this point in the history
  • Loading branch information
David Christofas committed Oct 12, 2021
1 parent e4cc2a5 commit 71dd8ed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/create-space-response.md
Original file line number Diff line number Diff line change
@@ -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
38 changes: 30 additions & 8 deletions graph/pkg/service/v0/drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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)
}

Expand All @@ -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)
Expand Down

0 comments on commit 71dd8ed

Please sign in to comment.