From d5d33c7941608708a4c9a4910ae7660a66d24f42 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 13 Oct 2021 13:47:57 +0200 Subject: [PATCH 1/5] add default space quota --- graph/pkg/config/config.go | 3 ++- graph/pkg/flagset/flagset.go | 8 ++++++++ graph/pkg/service/v0/drives.go | 25 +++++++++++++++++-------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/graph/pkg/config/config.go b/graph/pkg/config/config.go index e678250f5..402147277 100644 --- a/graph/pkg/config/config.go +++ b/graph/pkg/config/config.go @@ -51,7 +51,8 @@ type TokenManager struct { } type Spaces struct { - WebDavBase string + WebDavBase string + DefaultQuota string } // Config combines all available configuration parts. diff --git a/graph/pkg/flagset/flagset.go b/graph/pkg/flagset/flagset.go index 9ac8ca49e..e691b287f 100644 --- a/graph/pkg/flagset/flagset.go +++ b/graph/pkg/flagset/flagset.go @@ -149,6 +149,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { Destination: &cfg.Spaces.WebDavBase, }, + &cli.StringFlag{ + Name: "default-space-quota", + Value: flags.OverrideDefaultString(cfg.Spaces.DefaultQuota, "1000000000"), + Usage: "default quota used for all spaces if no custom quota was given", + EnvVars: []string{"GRAPH_SPACES_DEFAULT_QUOTA"}, + Destination: &cfg.Spaces.DefaultQuota, + }, + &cli.StringFlag{ Name: "jwt-secret", Value: flags.OverrideDefaultString(cfg.TokenManager.JWTSecret, "Pive-Fumkiu4"), diff --git a/graph/pkg/service/v0/drives.go b/graph/pkg/service/v0/drives.go index c83a5de7d..9be62567d 100644 --- a/graph/pkg/service/v0/drives.go +++ b/graph/pkg/service/v0/drives.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "path" + "strconv" "strings" "time" @@ -184,20 +185,27 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { errorcode.GeneralException.Render(w, r, http.StatusBadRequest, "drives of type share cannot be created via this api") } - var quota uint64 - if drive.Quota != nil && drive.Quota.Total != nil { - quota = uint64(*drive.Quota.Total) - } else { - quota = 65536 // set default quota if no value was sent. + var maxBytes *int64 + switch { + case drive.Quota != nil && drive.Quota.Total != nil && *drive.Quota.Total >= 0: + maxBytes = drive.Quota.Total + case g.config.Spaces.DefaultQuota != "": + q, err := strconv.ParseInt(g.config.Spaces.DefaultQuota, 10, 64) + if err == nil && q >= 0 { + maxBytes = &q + } + } + + var quota provider.Quota + if maxBytes != nil { + quota.QuotaMaxBytes = uint64(*maxBytes) } csr := provider.CreateStorageSpaceRequest{ Owner: us, Type: driveType, Name: spaceName, - Quota: &provider.Quota{ - QuotaMaxBytes: quota, - }, + Quota: "a, } resp, err := client.CreateStorageSpace(r.Context(), &csr) @@ -208,6 +216,7 @@ 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, "") + return } wdu, err := url.Parse(g.config.Spaces.WebDavBase) From 2a6e5454d26151ba5f27707aeec1d1d5bfa472ee Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 13 Oct 2021 14:13:26 +0200 Subject: [PATCH 2/5] send nil quota if not set --- graph/pkg/service/v0/drives.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/graph/pkg/service/v0/drives.go b/graph/pkg/service/v0/drives.go index 9be62567d..26bba71e9 100644 --- a/graph/pkg/service/v0/drives.go +++ b/graph/pkg/service/v0/drives.go @@ -55,6 +55,7 @@ func (g Graph) GetDrives(w http.ResponseWriter, r *http.Request) { } g.logger.Error().Err(err).Msg("error sending list storage spaces grpc request") errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, res.Status.Message) + return } wdu, err := url.Parse(g.config.Spaces.WebDavBase) @@ -179,10 +180,11 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { driveType = *drive.DriveType } switch driveType { - case "": + case "", "project": driveType = "project" - case "share": - errorcode.GeneralException.Render(w, r, http.StatusBadRequest, "drives of type share cannot be created via this api") + default: + errorcode.GeneralException.Render(w, r, http.StatusBadRequest, fmt.Sprintf("drives of type %s cannot be created via this api", driveType)) + return } var maxBytes *int64 @@ -196,16 +198,16 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { } } - var quota provider.Quota + var quota *provider.Quota if maxBytes != nil { - quota.QuotaMaxBytes = uint64(*maxBytes) + quota = &provider.Quota{QuotaMaxBytes: uint64(*maxBytes)} } csr := provider.CreateStorageSpaceRequest{ Owner: us, Type: driveType, Name: spaceName, - Quota: "a, + Quota: quota, } resp, err := client.CreateStorageSpace(r.Context(), &csr) From 492da79b09a932bb730b777776a0712e1f9e8283 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 13 Oct 2021 16:50:28 +0200 Subject: [PATCH 3/5] add changelog item --- changelog/unreleased/create-space-quota.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/unreleased/create-space-quota.md diff --git a/changelog/unreleased/create-space-quota.md b/changelog/unreleased/create-space-quota.md new file mode 100644 index 000000000..31243fc04 --- /dev/null +++ b/changelog/unreleased/create-space-quota.md @@ -0,0 +1,6 @@ +Change: Configurable default quota + +When creating a new space a (configurable) default quota will be used (instead the hardcoded one) +One can set the EnvVar `GRAPH_SPACES_DEFAULT_QUOTA` to configure it + +https://jira.owncloud.com/browse/OCIS-2070 From 94accca55cd95d0747fe8397e0193f5ffd4f5b46 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 13 Oct 2021 17:04:41 +0200 Subject: [PATCH 4/5] use newly created issue --- changelog/unreleased/create-space-quota.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog/unreleased/create-space-quota.md b/changelog/unreleased/create-space-quota.md index 31243fc04..7651cf746 100644 --- a/changelog/unreleased/create-space-quota.md +++ b/changelog/unreleased/create-space-quota.md @@ -3,4 +3,5 @@ Change: Configurable default quota When creating a new space a (configurable) default quota will be used (instead the hardcoded one) One can set the EnvVar `GRAPH_SPACES_DEFAULT_QUOTA` to configure it +https://github.com/owncloud/ocis/issues/2621 https://jira.owncloud.com/browse/OCIS-2070 From 00c663f3b676acdd72c0d7d3870b8ed5a8b99c84 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Thu, 14 Oct 2021 13:50:06 +0200 Subject: [PATCH 5/5] simplify quota creation logic --- graph/pkg/service/v0/drives.go | 36 ++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/graph/pkg/service/v0/drives.go b/graph/pkg/service/v0/drives.go index 26bba71e9..14ea3fe63 100644 --- a/graph/pkg/service/v0/drives.go +++ b/graph/pkg/service/v0/drives.go @@ -187,27 +187,11 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { return } - var maxBytes *int64 - switch { - case drive.Quota != nil && drive.Quota.Total != nil && *drive.Quota.Total >= 0: - maxBytes = drive.Quota.Total - case g.config.Spaces.DefaultQuota != "": - q, err := strconv.ParseInt(g.config.Spaces.DefaultQuota, 10, 64) - if err == nil && q >= 0 { - maxBytes = &q - } - } - - var quota *provider.Quota - if maxBytes != nil { - quota = &provider.Quota{QuotaMaxBytes: uint64(*maxBytes)} - } - csr := provider.CreateStorageSpaceRequest{ Owner: us, Type: driveType, Name: spaceName, - Quota: quota, + Quota: getQuota(drive.Quota, g.config.Spaces.DefaultQuota), } resp, err := client.CreateStorageSpace(r.Context(), &csr) @@ -426,3 +410,21 @@ func formatDrives(baseURL *url.URL, mds []*storageprovider.StorageSpace) ([]*msg return responses, nil } + +func getQuota(quota *msgraph.Quota, defaultQuota string) *provider.Quota { + switch { + case quota != nil && quota.Total != nil: + if q := *quota.Total; q >= 0 { + return &provider.Quota{QuotaMaxBytes: uint64(q)} + } + fallthrough + case defaultQuota != "": + if q, err := strconv.ParseInt(defaultQuota, 10, 64); err == nil && q >= 0 { + return &provider.Quota{QuotaMaxBytes: uint64(q)} + } + fallthrough + default: + return nil + } + +}