Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Storage Spaces through the Graph API #2471

Merged
merged 13 commits into from
Sep 9, 2021
43 changes: 43 additions & 0 deletions graph/pkg/service/v0/drives.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package svc

import (
"fmt"
"math"
"net/http"
"net/url"
"path"
"time"

ctxpkg "github.com/cs3org/reva/pkg/ctx"

cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
v1beta11 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/owncloud/ocis/graph/pkg/service/v0/errorcode"
msgraph "github.com/owncloud/open-graph-api-go"
Expand Down Expand Up @@ -128,6 +134,43 @@ func (g Graph) GetRootDriveChildren(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, &listResponse{Value: files})
}

// CreateDrive creates a storage drive (space).
func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
us, ok := ctxpkg.ContextGetUser(r.Context())
if !ok {
errorcode.GeneralException.Render(w, r, http.StatusUnauthorized, "invalid user")
return
}

client, err := g.GetClient()
if err != nil {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}

spaceName := chi.URLParam(r, "drive-name")

csr := provider.CreateStorageSpaceRequest{
Owner: us,
Type: "share",
Name: spaceName,
Quota: &provider.Quota{
QuotaMaxBytes: 65536,
refs marked this conversation as resolved.
Show resolved Hide resolved
QuotaMaxFiles: 20,
},
}

resp, err := client.CreateStorageSpace(r.Context(), &csr)
if err != nil {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}

if resp.GetStatus().GetCode() != v1beta11.Code_CODE_OK {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, fmt.Errorf("").Error())
}
}

func cs3TimestampToTime(t *types.Timestamp) time.Time {
return time.Unix(int64(t.Seconds), int64(t.Nanos))
}
Expand Down
5 changes: 5 additions & 0 deletions graph/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func NewService(opts ...Option) Service {
r.Get("/", svc.GetGroup)
})
})
r.Route("/drives", func(r chi.Router) {
// This route is non-compliant with MS Graph implementation; creating a drive is not supported. There
// is no official MS SDK support for this method.
refs marked this conversation as resolved.
Show resolved Hide resolved
r.Post("/{drive-name}", svc.CreateDrive)
})
})
})

Expand Down