From c06f52837e5b4aab5335e5a66885c48c24a148a2 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 25 Sep 2023 15:35:54 +0200 Subject: WIP refactor (broken templates) --- quotas.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 quotas.go (limited to 'quotas.go') diff --git a/quotas.go b/quotas.go new file mode 100644 index 0000000..e077ac8 --- /dev/null +++ b/quotas.go @@ -0,0 +1,79 @@ +package main + +import ( + "errors" + "fmt" + "strconv" + + "github.com/go-ldap/ldap/v3" + garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang" +) + +const ( + // --- Default Quota Values --- + QUOTA_WEBSITE_SIZE_DEFAULT = 1024 * 1024 * 50 // 50MB + QUOTA_WEBSITE_SIZE_BURSTED = 1024 * 1024 * 200 // 200MB + QUOTA_WEBSITE_OBJECTS = 10000 // 10k objects + QUOTA_WEBSITE_COUNT = 5 // 5 buckets + + // --- Per-user overridable fields --- + FIELD_QUOTA_WEBSITE_SIZE_BURSTED = "quota_website_size_bursted" + FIELD_QUOTA_WEBSITE_COUNT = "quota_website_count" +) + +type UserQuota struct { + WebsiteCount int64 + WebsiteSizeDefault int64 + WebsiteSizeBursted int64 + WebsiteObjects int64 +} + +func NewUserQuota() *UserQuota { + return &UserQuota { + WebsiteCount: QUOTA_WEBSITE_COUNT, + WebsiteSizeDefault: QUOTA_WEBSITE_SIZE_DEFAULT, + WebsiteSizeBursted: QUOTA_WEBSITE_SIZE_BURSTED, + WebsiteObjects: QUOTA_WEBSITE_OBJECTS, + } +} + +var ( + ErrQuotaEmpty = fmt.Errorf("No quota is defined for this entry") + ErrQuotaInvalid = fmt.Errorf("The defined quota can't be parsed") +) + +func entryToQuota(entry *ldap.Entry, field string) (int64, error) { + f := entry.GetAttributeValue(field) + if f == "" { + return -1, ErrQuotaEmpty + } + + q, err := strconv.ParseInt(f, 10, 64) + if err != nil { + return -1, errors.Join(ErrQuotaInvalid, err) + } + return q, nil +} + +func NewUserQuotaFromEntry(entry *ldap.Entry) *UserQuota { + quotas := NewUserQuota() + + if q, err := entryToQuota(entry, FIELD_QUOTA_WEBSITE_COUNT); err != nil { + quotas.WebsiteCount = q + } + + if q, err := entryToQuota(entry, FIELD_QUOTA_WEBSITE_SIZE_BURSTED); err != nil { + quotas.WebsiteSizeBursted = q + } + + return quotas +} + +func (q *UserQuota) DefaultWebsiteQuota() *garage.UpdateBucketRequestQuotas { + qr := garage.NewUpdateBucketRequestQuotas() + + qr.SetMaxSize(q.WebsiteSizeDefault) + qr.SetMaxObjects(q.WebsiteSizeBursted) + + return qr +} -- cgit v1.2.3 From be97a1be587f42a4688825244b025b06172c442a Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 25 Sep 2023 15:57:59 +0200 Subject: fix some templates --- quotas.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'quotas.go') diff --git a/quotas.go b/quotas.go index e077ac8..3eec9b8 100644 --- a/quotas.go +++ b/quotas.go @@ -58,11 +58,11 @@ func entryToQuota(entry *ldap.Entry, field string) (int64, error) { func NewUserQuotaFromEntry(entry *ldap.Entry) *UserQuota { quotas := NewUserQuota() - if q, err := entryToQuota(entry, FIELD_QUOTA_WEBSITE_COUNT); err != nil { + if q, err := entryToQuota(entry, FIELD_QUOTA_WEBSITE_COUNT); err == nil { quotas.WebsiteCount = q } - if q, err := entryToQuota(entry, FIELD_QUOTA_WEBSITE_SIZE_BURSTED); err != nil { + if q, err := entryToQuota(entry, FIELD_QUOTA_WEBSITE_SIZE_BURSTED); err == nil { quotas.WebsiteSizeBursted = q } -- cgit v1.2.3 From 08287375736a0a57c03b4bd6d9c222dc5567db6e Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 25 Sep 2023 22:00:46 +0200 Subject: patch and delete with quota are now implemented --- quotas.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'quotas.go') diff --git a/quotas.go b/quotas.go index 3eec9b8..9a2e426 100644 --- a/quotas.go +++ b/quotas.go @@ -77,3 +77,74 @@ func (q *UserQuota) DefaultWebsiteQuota() *garage.UpdateBucketRequestQuotas { return qr } + +func (q *UserQuota) WebsiteSizeAdjust(sz int64) int64 { + if sz < q.WebsiteSizeDefault { + return q.WebsiteSizeDefault + } else if sz > q.WebsiteSizeBursted { + return q.WebsiteSizeBursted + } else { + return sz + } +} + +func (q *UserQuota) WebsiteObjectAdjust(objs int64) int64 { + if objs > q.WebsiteObjects || objs <= 0 { + return q.WebsiteObjects + } else { + return objs + } +} + +func (q *UserQuota) WebsiteSizeBurstedPretty() string { + return prettyValue(q.WebsiteSizeBursted) +} + +// --- A quota stat we can use +type QuotaStat struct { + Current int64 + Max int64 + Ratio float64 + Burstable bool +} +func NewQuotaStat(current, max int64, burstable bool) QuotaStat { + return QuotaStat { + Current: current, + Max: max, + Ratio: float64(current) / float64(max), + Burstable: burstable, + } +} +func (q *QuotaStat) IsFull() bool { + return q.Current >= q.Max +} +func (q *QuotaStat) Percent() int64 { + return int64(q.Ratio * 100) +} + +func (q *QuotaStat) PrettyCurrent() string { + return prettyValue(q.Current) +} +func (q *QuotaStat) PrettyMax() string { + return prettyValue(q.Max) +} + +func prettyValue(v int64) string { + if v < 1024 { + return fmt.Sprintf("%d octets", v) + } + v = v / 1024 + if v < 1024 { + return fmt.Sprintf("%d kio", v) + } + v = v / 1024 + if v < 1024 { + return fmt.Sprintf("%d Mio", v) + } + v = v / 1024 + if v < 1024 { + return fmt.Sprintf("%d Gio", v) + } + v = v / 1024 + return fmt.Sprintf("%d Tio", v) +} -- cgit v1.2.3 From 982bd8a43c50bb5845b694dbd0b3e0ffbf43dad7 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 25 Sep 2023 23:00:57 +0200 Subject: done with API --- quotas.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'quotas.go') diff --git a/quotas.go b/quotas.go index 9a2e426..e520f5c 100644 --- a/quotas.go +++ b/quotas.go @@ -102,10 +102,10 @@ func (q *UserQuota) WebsiteSizeBurstedPretty() string { // --- A quota stat we can use type QuotaStat struct { - Current int64 - Max int64 - Ratio float64 - Burstable bool + Current int64 `json:"current"` + Max int64 `json:"max"` + Ratio float64 `json:"ratio"` + Burstable bool `json:"burstable"` } func NewQuotaStat(current, max int64, burstable bool) QuotaStat { return QuotaStat { -- cgit v1.2.3 From 706ff58a6f6608719feda15075d50f978df39c5b Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 26 Sep 2023 08:40:41 +0200 Subject: format --- quotas.go | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'quotas.go') diff --git a/quotas.go b/quotas.go index e520f5c..894ea3c 100644 --- a/quotas.go +++ b/quotas.go @@ -5,40 +5,40 @@ import ( "fmt" "strconv" - "github.com/go-ldap/ldap/v3" garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang" + "github.com/go-ldap/ldap/v3" ) const ( // --- Default Quota Values --- - QUOTA_WEBSITE_SIZE_DEFAULT = 1024 * 1024 * 50 // 50MB + QUOTA_WEBSITE_SIZE_DEFAULT = 1024 * 1024 * 50 // 50MB QUOTA_WEBSITE_SIZE_BURSTED = 1024 * 1024 * 200 // 200MB - QUOTA_WEBSITE_OBJECTS = 10000 // 10k objects - QUOTA_WEBSITE_COUNT = 5 // 5 buckets + QUOTA_WEBSITE_OBJECTS = 10000 // 10k objects + QUOTA_WEBSITE_COUNT = 5 // 5 buckets // --- Per-user overridable fields --- FIELD_QUOTA_WEBSITE_SIZE_BURSTED = "quota_website_size_bursted" - FIELD_QUOTA_WEBSITE_COUNT = "quota_website_count" + FIELD_QUOTA_WEBSITE_COUNT = "quota_website_count" ) type UserQuota struct { - WebsiteCount int64 + WebsiteCount int64 WebsiteSizeDefault int64 WebsiteSizeBursted int64 - WebsiteObjects int64 + WebsiteObjects int64 } func NewUserQuota() *UserQuota { - return &UserQuota { - WebsiteCount: QUOTA_WEBSITE_COUNT, + return &UserQuota{ + WebsiteCount: QUOTA_WEBSITE_COUNT, WebsiteSizeDefault: QUOTA_WEBSITE_SIZE_DEFAULT, WebsiteSizeBursted: QUOTA_WEBSITE_SIZE_BURSTED, - WebsiteObjects: QUOTA_WEBSITE_OBJECTS, + WebsiteObjects: QUOTA_WEBSITE_OBJECTS, } } var ( - ErrQuotaEmpty = fmt.Errorf("No quota is defined for this entry") + ErrQuotaEmpty = fmt.Errorf("No quota is defined for this entry") ErrQuotaInvalid = fmt.Errorf("The defined quota can't be parsed") ) @@ -72,7 +72,7 @@ func NewUserQuotaFromEntry(entry *ldap.Entry) *UserQuota { func (q *UserQuota) DefaultWebsiteQuota() *garage.UpdateBucketRequestQuotas { qr := garage.NewUpdateBucketRequestQuotas() - qr.SetMaxSize(q.WebsiteSizeDefault) + qr.SetMaxSize(q.WebsiteSizeDefault) qr.SetMaxObjects(q.WebsiteSizeBursted) return qr @@ -80,7 +80,7 @@ func (q *UserQuota) DefaultWebsiteQuota() *garage.UpdateBucketRequestQuotas { func (q *UserQuota) WebsiteSizeAdjust(sz int64) int64 { if sz < q.WebsiteSizeDefault { - return q.WebsiteSizeDefault + return q.WebsiteSizeDefault } else if sz > q.WebsiteSizeBursted { return q.WebsiteSizeBursted } else { @@ -102,16 +102,17 @@ func (q *UserQuota) WebsiteSizeBurstedPretty() string { // --- A quota stat we can use type QuotaStat struct { - Current int64 `json:"current"` - Max int64 `json:"max"` - Ratio float64 `json:"ratio"` - Burstable bool `json:"burstable"` + Current int64 `json:"current"` + Max int64 `json:"max"` + Ratio float64 `json:"ratio"` + Burstable bool `json:"burstable"` } + func NewQuotaStat(current, max int64, burstable bool) QuotaStat { - return QuotaStat { - Current: current, - Max: max, - Ratio: float64(current) / float64(max), + return QuotaStat{ + Current: current, + Max: max, + Ratio: float64(current) / float64(max), Burstable: burstable, } } @@ -141,7 +142,7 @@ func prettyValue(v int64) string { if v < 1024 { return fmt.Sprintf("%d Mio", v) } - v = v / 1024 + v = v / 1024 if v < 1024 { return fmt.Sprintf("%d Gio", v) } -- cgit v1.2.3