From 9c21c2e799449ae303ed764ba1677366d0571e8d Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 15 Sep 2023 14:38:46 +0200 Subject: split LDAP and S3 --- garage.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'garage.go') diff --git a/garage.go b/garage.go index 1ae02e4..61d1b93 100644 --- a/garage.go +++ b/garage.go @@ -109,17 +109,15 @@ func grgGetBucket(bid string) (*garage.BucketInfo, error) { } -func checkLoginAndS3(w http.ResponseWriter, r *http.Request) (*LoginStatus, *garage.KeyInfo, error) { - login := checkLogin(w, r) +func checkS3(login *LoginStatus) (*garage.KeyInfo, error) { if login == nil { - return nil, nil, errors.New("LDAP login failed") + return nil, errors.New("Login can't be nil") } - keyID := login.UserEntry.GetAttributeValue("garage_s3_access_key") if keyID == "" { keyPair, err := grgCreateKey(login.Info.Username) if err != nil { - return login, nil, err + return nil, err } modify_request := ldap.NewModifyRequest(login.Info.DN, nil) modify_request.Replace("garage_s3_access_key", []string{*keyPair.AccessKeyId}) @@ -128,11 +126,20 @@ func checkLoginAndS3(w http.ResponseWriter, r *http.Request) (*LoginStatus, *gar // or when bottin will be able to dynamically fetch it. modify_request.Replace("garage_s3_secret_key", []string{*keyPair.SecretAccessKey}) err = login.conn.Modify(modify_request) - return login, keyPair, err + return keyPair, err } // Note: we could simply return the login info, but LX asked we do not // store the secrets in LDAP in the future. keyPair, err := grgGetKey(keyID) + return keyPair, err +} + +func checkLoginAndS3(w http.ResponseWriter, r *http.Request) (*LoginStatus, *garage.KeyInfo, error) { + login := checkLogin(w, r) + if login == nil { + return nil, nil, errors.New("LDAP login failed") + } + keyPair, err := checkS3(login) return login, keyPair, err } -- cgit v1.2.3 From 5b246ec86bc3eee768da2347f031b349d1e1553d Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 15 Sep 2023 18:25:37 +0200 Subject: basic logic for switching alias from local/global --- garage.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'garage.go') diff --git a/garage.go b/garage.go index 61d1b93..db35366 100644 --- a/garage.go +++ b/garage.go @@ -97,12 +97,56 @@ func grgCreateWebsite(gkey, bucket string) (*garage.BucketInfo, error) { return binfo, nil } +func grgAddGlobalAlias(bid, alias string) (*garage.BucketInfo, error) { + client, ctx := gadmin() + + resp, _, err := client.BucketApi.PutBucketGlobalAlias(ctx).Id(bid).Alias(alias).Execute() + if err != nil { + log.Println(err) + return nil, err + } + return resp, nil +} + +func grgAddLocalAlias(bid, key, alias string) (*garage.BucketInfo, error) { + client, ctx := gadmin() + + resp, _, err := client.BucketApi.PutBucketLocalAlias(ctx).Id(bid).AccessKeyId(key).Alias(alias).Execute() + if err != nil { + log.Println(err) + return nil, err + } + return resp, nil +} + +func grgDelGlobalAlias(bid, alias string) (*garage.BucketInfo, error) { + client, ctx := gadmin() + + resp, _, err := client.BucketApi.DeleteBucketGlobalAlias(ctx).Id(bid).Alias(alias).Execute() + if err != nil { + log.Println(err) + return nil, err + } + return resp, nil +} + +func grgDelLocalAlias(bid, key, alias string) (*garage.BucketInfo, error) { + client, ctx := gadmin() + + resp, _, err := client.BucketApi.DeleteBucketLocalAlias(ctx).Id(bid).AccessKeyId(key).Alias(alias).Execute() + if err != nil { + log.Println(err) + return nil, err + } + return resp, nil +} + func grgGetBucket(bid string) (*garage.BucketInfo, error) { client, ctx := gadmin() resp, _, err := client.BucketApi.GetBucketInfo(ctx, bid).Execute() if err != nil { - fmt.Printf("%+v\n", err) + log.Println(err) return nil, err } return resp, nil -- cgit v1.2.3 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) --- garage.go | 100 ++++++++++++++++++-------------------------------------------- 1 file changed, 29 insertions(+), 71 deletions(-) (limited to 'garage.go') diff --git a/garage.go b/garage.go index db35366..4586e26 100644 --- a/garage.go +++ b/garage.go @@ -2,16 +2,15 @@ package main import ( "context" - "errors" "fmt" garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang" - "github.com/go-ldap/ldap/v3" "github.com/gorilla/mux" "log" "net/http" "strings" ) + func gadmin() (*garage.APIClient, context.Context) { // Set Host and other parameters configuration := garage.NewConfiguration() @@ -48,7 +47,9 @@ func grgGetKey(accessKey string) (*garage.KeyInfo, error) { return resp, nil } -func grgCreateWebsite(gkey, bucket string) (*garage.BucketInfo, error) { + + +func grgCreateWebsite(gkey, bucket string, quotas *UserQuota) (*garage.BucketInfo, error) { client, ctx := gadmin() br := garage.NewCreateBucketRequest() @@ -79,9 +80,7 @@ func grgCreateWebsite(gkey, bucket string) (*garage.BucketInfo, error) { wr.SetIndexDocument("index.html") wr.SetErrorDocument("error.html") - qr := garage.NewUpdateBucketRequestQuotas() - qr.SetMaxSize(1024 * 1024 * 50) // 50MB - qr.SetMaxObjects(10000) //10k objects + qr := quotas.DefaultWebsiteQuota() ur := garage.NewUpdateBucketRequest() ur.SetWebsiteAccess(*wr) @@ -153,85 +152,37 @@ func grgGetBucket(bid string) (*garage.BucketInfo, error) { } -func checkS3(login *LoginStatus) (*garage.KeyInfo, error) { - if login == nil { - return nil, errors.New("Login can't be nil") - } - keyID := login.UserEntry.GetAttributeValue("garage_s3_access_key") - if keyID == "" { - keyPair, err := grgCreateKey(login.Info.Username) - if err != nil { - return nil, err - } - modify_request := ldap.NewModifyRequest(login.Info.DN, nil) - modify_request.Replace("garage_s3_access_key", []string{*keyPair.AccessKeyId}) - // @FIXME compatibility feature for bagage (SFTP+webdav) - // you can remove it once bagage will be updated to fetch the key from garage directly - // or when bottin will be able to dynamically fetch it. - modify_request.Replace("garage_s3_secret_key", []string{*keyPair.SecretAccessKey}) - err = login.conn.Modify(modify_request) - return keyPair, err - } - // Note: we could simply return the login info, but LX asked we do not - // store the secrets in LDAP in the future. - keyPair, err := grgGetKey(keyID) - return keyPair, err -} - -func checkLoginAndS3(w http.ResponseWriter, r *http.Request) (*LoginStatus, *garage.KeyInfo, error) { - login := checkLogin(w, r) - if login == nil { - return nil, nil, errors.New("LDAP login failed") - } - keyPair, err := checkS3(login) - return login, keyPair, err -} - -type keyView struct { - Status *LoginStatus - Key *garage.KeyInfo -} +// --- Start page rendering functions func handleGarageKey(w http.ResponseWriter, r *http.Request) { - login, s3key, err := checkLoginAndS3(w, r) - if err != nil { - log.Println(err) + user := RequireUserHtml(w, r) + if user == nil { return } - view := keyView{Status: login, Key: s3key} tKey := getTemplate("garage_key.html") - tKey.Execute(w, &view) -} - -type webListView struct { - Status *LoginStatus - Key *garage.KeyInfo + tKey.Execute(w, user) } func handleGarageWebsiteList(w http.ResponseWriter, r *http.Request) { - login, s3key, err := checkLoginAndS3(w, r) - if err != nil { - log.Println(err) + user := RequireUserHtml(w, r) + if user == nil { return } - view := webListView{Status: login, Key: s3key} tWebsiteList := getTemplate("garage_website_list.html") - tWebsiteList.Execute(w, &view) + tWebsiteList.Execute(w, user) } func handleGarageWebsiteNew(w http.ResponseWriter, r *http.Request) { - _, s3key, err := checkLoginAndS3(w, r) - if err != nil { - log.Println(err) + user := RequireUserHtml(w, r) + if user == nil { return } tWebsiteNew := getTemplate("garage_website_new.html") if r.Method == "POST" { r.ParseForm() - log.Println(r.Form) bucket := strings.Join(r.Form["bucket"], "") if bucket == "" { @@ -244,7 +195,15 @@ func handleGarageWebsiteNew(w http.ResponseWriter, r *http.Request) { return } - binfo, err := grgCreateWebsite(*s3key.AccessKeyId, bucket) + keyInfo, err := user.S3KeyInfo() + if err != nil { + log.Println(err) + // @FIXME we need to return the error to the user + tWebsiteNew.Execute(w, nil) + return + } + + binfo, err := grgCreateWebsite(*keyInfo.AccessKeyId, bucket, user.Quota) if err != nil { log.Println(err) // @FIXME we need to return the error to the user @@ -260,8 +219,7 @@ func handleGarageWebsiteNew(w http.ResponseWriter, r *http.Request) { } type webInspectView struct { - Status *LoginStatus - Key *garage.KeyInfo + User *LoggedUser Bucket *garage.BucketInfo IndexDoc string ErrorDoc string @@ -271,13 +229,14 @@ type webInspectView struct { } func handleGarageWebsiteInspect(w http.ResponseWriter, r *http.Request) { - login, s3key, err := checkLoginAndS3(w, r) - if err != nil { - log.Println(err) + user := RequireUserHtml(w, r) + if user == nil { return } bucketId := mux.Vars(r)["bucket"] + // @FIXME check that user owns the bucket.... + binfo, err := grgGetBucket(bucketId) if err != nil { log.Println(err) @@ -288,8 +247,7 @@ func handleGarageWebsiteInspect(w http.ResponseWriter, r *http.Request) { q := binfo.GetQuotas() view := webInspectView{ - Status: login, - Key: s3key, + User: user, Bucket: binfo, IndexDoc: (&wc).GetIndexDocument(), ErrorDoc: (&wc).GetErrorDocument(), -- cgit v1.2.3 From bc368943a4c0853718b8a53b1caadc297412ef32 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 25 Sep 2023 19:07:07 +0200 Subject: heavy refactor in progress --- garage.go | 128 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 68 insertions(+), 60 deletions(-) (limited to 'garage.go') diff --git a/garage.go b/garage.go index 4586e26..8c8633f 100644 --- a/garage.go +++ b/garage.go @@ -49,7 +49,7 @@ func grgGetKey(accessKey string) (*garage.KeyInfo, error) { -func grgCreateWebsite(gkey, bucket string, quotas *UserQuota) (*garage.BucketInfo, error) { +func grgCreateBucket(bucket string) (*garage.BucketInfo, error) { client, ctx := gadmin() br := garage.NewCreateBucketRequest() @@ -61,32 +61,40 @@ func grgCreateWebsite(gkey, bucket string, quotas *UserQuota) (*garage.BucketInf fmt.Printf("%+v\n", err) return nil, err } + return binfo, nil +} + +func grgAllowKeyOnBucket(bid, gkey string) (*garage.BucketInfo, error) { + client, ctx := gadmin() // Allow user's key ar := garage.AllowBucketKeyRequest{ - BucketId: *binfo.Id, + BucketId: bid, AccessKeyId: gkey, Permissions: *garage.NewAllowBucketKeyRequestPermissions(true, true, true), } - binfo, _, err = client.BucketApi.AllowBucketKey(ctx).AllowBucketKeyRequest(ar).Execute() + binfo, _, err := client.BucketApi.AllowBucketKey(ctx).AllowBucketKeyRequest(ar).Execute() if err != nil { fmt.Printf("%+v\n", err) return nil, err } - // Expose website and set quota + return binfo, nil +} + +func allowWebsiteDefault() *garage.UpdateBucketRequestWebsiteAccess { wr := garage.NewUpdateBucketRequestWebsiteAccess() wr.SetEnabled(true) wr.SetIndexDocument("index.html") wr.SetErrorDocument("error.html") - qr := quotas.DefaultWebsiteQuota() + return wr +} - ur := garage.NewUpdateBucketRequest() - ur.SetWebsiteAccess(*wr) - ur.SetQuotas(*qr) +func grgUpdateBucket(bid string, ur *garage.UpdateBucketRequest) (*garage.BucketInfo, error) { + client, ctx := gadmin() - binfo, _, err = client.BucketApi.UpdateBucket(ctx, *binfo.Id).UpdateBucketRequest(*ur).Execute() + binfo, _, err := client.BucketApi.UpdateBucket(ctx, bid).UpdateBucketRequest(*ur).Execute() if err != nil { fmt.Printf("%+v\n", err) return nil, err @@ -154,7 +162,7 @@ func grgGetBucket(bid string) (*garage.BucketInfo, error) { // --- Start page rendering functions -func handleGarageKey(w http.ResponseWriter, r *http.Request) { +func handleWebsiteConfigure(w http.ResponseWriter, r *http.Request) { user := RequireUserHtml(w, r) if user == nil { return @@ -164,22 +172,48 @@ func handleGarageKey(w http.ResponseWriter, r *http.Request) { tKey.Execute(w, user) } -func handleGarageWebsiteList(w http.ResponseWriter, r *http.Request) { +func handleWebsiteList(w http.ResponseWriter, r *http.Request) { user := RequireUserHtml(w, r) if user == nil { return } - tWebsiteList := getTemplate("garage_website_list.html") - tWebsiteList.Execute(w, user) + ctrl, err := NewWebsiteController(user) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + list := ctrl.List() + if len(list) > 0 { + http.Redirect(w, r, "/website/inspect/"+list[0].Pretty, http.StatusFound) + } else { + http.Redirect(w, r, "/website/new", http.StatusFound) + } +} + +type WebsiteNewTpl struct { + ctrl *WebsiteController + err error } -func handleGarageWebsiteNew(w http.ResponseWriter, r *http.Request) { +func handleWebsiteNew(w http.ResponseWriter, r *http.Request) { user := RequireUserHtml(w, r) if user == nil { return } + ctrl, err := NewWebsiteController(user) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tpl := &WebsiteNewTpl{ + ctrl: ctrl, + err: nil, + } + tWebsiteNew := getTemplate("garage_website_new.html") if r.Method == "POST" { r.ParseForm() @@ -188,73 +222,47 @@ func handleGarageWebsiteNew(w http.ResponseWriter, r *http.Request) { if bucket == "" { bucket = strings.Join(r.Form["bucket2"], "") } - if bucket == "" { - log.Println("Form empty") - // @FIXME we need to return the error to the user - tWebsiteNew.Execute(w, nil) - return - } - - keyInfo, err := user.S3KeyInfo() - if err != nil { - log.Println(err) - // @FIXME we need to return the error to the user - tWebsiteNew.Execute(w, nil) - return - } - binfo, err := grgCreateWebsite(*keyInfo.AccessKeyId, bucket, user.Quota) + view, err := ctrl.Create(bucket) if err != nil { - log.Println(err) - // @FIXME we need to return the error to the user - tWebsiteNew.Execute(w, nil) - return + tpl.err = err + tWebsiteNew.Execute(w, tpl) } - http.Redirect(w, r, "/garage/website/b/"+*binfo.Id, http.StatusFound) + http.Redirect(w, r, "/website/inspect/"+view.Name.Pretty, http.StatusFound) return } tWebsiteNew.Execute(w, nil) } -type webInspectView struct { - User *LoggedUser - Bucket *garage.BucketInfo - IndexDoc string - ErrorDoc string - MaxObjects int64 - MaxSize int64 - UsedSizePct float64 +type WebsiteInspectTpl struct { + Ctrl *WebsiteController + View *WebsiteView } -func handleGarageWebsiteInspect(w http.ResponseWriter, r *http.Request) { +func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) { user := RequireUserHtml(w, r) if user == nil { return } - bucketId := mux.Vars(r)["bucket"] - // @FIXME check that user owns the bucket.... + ctrl, err := NewWebsiteController(user) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } - binfo, err := grgGetBucket(bucketId) + bucketName := mux.Vars(r)["bucket"] + + view, err := ctrl.Inspect(bucketName) if err != nil { - log.Println(err) - return + http.Error(w, err.Error(), http.StatusInternalServerError) + return } - wc := binfo.GetWebsiteConfig() - q := binfo.GetQuotas() - - view := webInspectView{ - User: user, - Bucket: binfo, - IndexDoc: (&wc).GetIndexDocument(), - ErrorDoc: (&wc).GetErrorDocument(), - MaxObjects: (&q).GetMaxObjects(), - MaxSize: (&q).GetMaxSize(), - } + tpl := &WebsiteInspectTpl{ ctrl, view } tWebsiteInspect := getTemplate("garage_website_inspect.html") - tWebsiteInspect.Execute(w, &view) + tWebsiteInspect.Execute(w, &tpl) } -- 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 --- garage.go | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'garage.go') diff --git a/garage.go b/garage.go index 8c8633f..d3d4d38 100644 --- a/garage.go +++ b/garage.go @@ -160,6 +160,16 @@ func grgGetBucket(bid string) (*garage.BucketInfo, error) { } +func grgDeleteBucket(bid string) error { + client, ctx := gadmin() + + _, err := client.BucketApi.DeleteBucket(ctx, bid).Execute() + if err != nil { + log.Println(err) + } + return err +} + // --- Start page rendering functions func handleWebsiteConfigure(w http.ResponseWriter, r *http.Request) { @@ -193,8 +203,8 @@ func handleWebsiteList(w http.ResponseWriter, r *http.Request) { } type WebsiteNewTpl struct { - ctrl *WebsiteController - err error + Ctrl *WebsiteController + Err error } func handleWebsiteNew(w http.ResponseWriter, r *http.Request) { @@ -209,10 +219,7 @@ func handleWebsiteNew(w http.ResponseWriter, r *http.Request) { return } - tpl := &WebsiteNewTpl{ - ctrl: ctrl, - err: nil, - } + tpl := &WebsiteNewTpl{ctrl, nil} tWebsiteNew := getTemplate("garage_website_new.html") if r.Method == "POST" { @@ -225,23 +232,27 @@ func handleWebsiteNew(w http.ResponseWriter, r *http.Request) { view, err := ctrl.Create(bucket) if err != nil { - tpl.err = err + tpl.Err = err tWebsiteNew.Execute(w, tpl) + return } http.Redirect(w, r, "/website/inspect/"+view.Name.Pretty, http.StatusFound) return } - tWebsiteNew.Execute(w, nil) + tWebsiteNew.Execute(w, tpl) } type WebsiteInspectTpl struct { Ctrl *WebsiteController View *WebsiteView + Err error } func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) { + var processErr error + user := RequireUserHtml(w, r) if user == nil { return @@ -254,6 +265,22 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) { } bucketName := mux.Vars(r)["bucket"] + + if r.Method == "POST" { + r.ParseForm() + action := strings.Join(r.Form["action"],"") + switch action { + case "increase_quota": + _, processErr = ctrl.Patch(bucketName, &WebsitePatch { size: &user.Quota.WebsiteSizeBursted }) + case "delete_bucket": + processErr = ctrl.Delete(bucketName) + http.Redirect(w, r, "/website", http.StatusFound) + return + default: + processErr = fmt.Errorf("Unknown action") + } + + } view, err := ctrl.Inspect(bucketName) if err != nil { @@ -261,7 +288,7 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) { return } - tpl := &WebsiteInspectTpl{ ctrl, view } + tpl := &WebsiteInspectTpl{ ctrl, view, processErr } tWebsiteInspect := getTemplate("garage_website_inspect.html") tWebsiteInspect.Execute(w, &tpl) -- 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 --- garage.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'garage.go') diff --git a/garage.go b/garage.go index d3d4d38..b3ca836 100644 --- a/garage.go +++ b/garage.go @@ -194,9 +194,9 @@ func handleWebsiteList(w http.ResponseWriter, r *http.Request) { return } - list := ctrl.List() - if len(list) > 0 { - http.Redirect(w, r, "/website/inspect/"+list[0].Pretty, http.StatusFound) + desc := ctrl.Describe() + if len(desc.Websites) > 0 { + http.Redirect(w, r, "/website/inspect/"+desc.Websites[0].Pretty, http.StatusFound) } else { http.Redirect(w, r, "/website/new", http.StatusFound) } @@ -271,7 +271,7 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) { action := strings.Join(r.Form["action"],"") switch action { case "increase_quota": - _, processErr = ctrl.Patch(bucketName, &WebsitePatch { size: &user.Quota.WebsiteSizeBursted }) + _, processErr = ctrl.Patch(bucketName, &WebsitePatch { Size: &user.Quota.WebsiteSizeBursted }) case "delete_bucket": processErr = ctrl.Delete(bucketName) http.Redirect(w, r, "/website", http.StatusFound) -- cgit v1.2.3 From d8633d7fb89b55996f25e1a67180408c805ba15f Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 26 Sep 2023 08:40:30 +0200 Subject: final --- garage.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'garage.go') diff --git a/garage.go b/garage.go index b3ca836..236dcbd 100644 --- a/garage.go +++ b/garage.go @@ -194,9 +194,8 @@ func handleWebsiteList(w http.ResponseWriter, r *http.Request) { return } - desc := ctrl.Describe() - if len(desc.Websites) > 0 { - http.Redirect(w, r, "/website/inspect/"+desc.Websites[0].Pretty, http.StatusFound) + if len(ctrl.PrettyList) > 0 { + http.Redirect(w, r, "/website/inspect/"+ctrl.PrettyList[0], http.StatusFound) } else { http.Redirect(w, r, "/website/new", http.StatusFound) } @@ -245,7 +244,7 @@ func handleWebsiteNew(w http.ResponseWriter, r *http.Request) { } type WebsiteInspectTpl struct { - Ctrl *WebsiteController + Describe *WebsiteDescribe View *WebsiteView Err error } @@ -287,8 +286,14 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + + describe, err := ctrl.Describe() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } - tpl := &WebsiteInspectTpl{ ctrl, view, processErr } + tpl := &WebsiteInspectTpl{ describe, view, processErr } tWebsiteInspect := getTemplate("garage_website_inspect.html") tWebsiteInspect.Execute(w, &tpl) -- 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 --- garage.go | 57 +++++++++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 30 deletions(-) (limited to 'garage.go') diff --git a/garage.go b/garage.go index 236dcbd..c43fd5f 100644 --- a/garage.go +++ b/garage.go @@ -10,7 +10,6 @@ import ( "strings" ) - func gadmin() (*garage.APIClient, context.Context) { // Set Host and other parameters configuration := garage.NewConfiguration() @@ -47,8 +46,6 @@ func grgGetKey(accessKey string) (*garage.KeyInfo, error) { return resp, nil } - - func grgCreateBucket(bucket string) (*garage.BucketInfo, error) { client, ctx := gadmin() @@ -64,7 +61,7 @@ func grgCreateBucket(bucket string) (*garage.BucketInfo, error) { return binfo, nil } -func grgAllowKeyOnBucket(bid, gkey string) (*garage.BucketInfo, error) { +func grgAllowKeyOnBucket(bid, gkey string) (*garage.BucketInfo, error) { client, ctx := gadmin() // Allow user's key @@ -163,10 +160,10 @@ func grgGetBucket(bid string) (*garage.BucketInfo, error) { func grgDeleteBucket(bid string) error { client, ctx := gadmin() - _, err := client.BucketApi.DeleteBucket(ctx, bid).Execute() - if err != nil { + _, err := client.BucketApi.DeleteBucket(ctx, bid).Execute() + if err != nil { log.Println(err) - } + } return err } @@ -189,9 +186,9 @@ func handleWebsiteList(w http.ResponseWriter, r *http.Request) { } ctrl, err := NewWebsiteController(user) - if err != nil { + if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) - return + return } if len(ctrl.PrettyList) > 0 { @@ -203,7 +200,7 @@ func handleWebsiteList(w http.ResponseWriter, r *http.Request) { type WebsiteNewTpl struct { Ctrl *WebsiteController - Err error + Err error } func handleWebsiteNew(w http.ResponseWriter, r *http.Request) { @@ -213,9 +210,9 @@ func handleWebsiteNew(w http.ResponseWriter, r *http.Request) { } ctrl, err := NewWebsiteController(user) - if err != nil { + if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) - return + return } tpl := &WebsiteNewTpl{ctrl, nil} @@ -245,8 +242,8 @@ func handleWebsiteNew(w http.ResponseWriter, r *http.Request) { type WebsiteInspectTpl struct { Describe *WebsiteDescribe - View *WebsiteView - Err error + View *WebsiteView + Err error } func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) { @@ -258,42 +255,42 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) { } ctrl, err := NewWebsiteController(user) - if err != nil { + if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) - return + return } bucketName := mux.Vars(r)["bucket"] if r.Method == "POST" { r.ParseForm() - action := strings.Join(r.Form["action"],"") + action := strings.Join(r.Form["action"], "") switch action { - case "increase_quota": - _, processErr = ctrl.Patch(bucketName, &WebsitePatch { Size: &user.Quota.WebsiteSizeBursted }) - case "delete_bucket": - processErr = ctrl.Delete(bucketName) - http.Redirect(w, r, "/website", http.StatusFound) - return - default: - processErr = fmt.Errorf("Unknown action") + case "increase_quota": + _, processErr = ctrl.Patch(bucketName, &WebsitePatch{Size: &user.Quota.WebsiteSizeBursted}) + case "delete_bucket": + processErr = ctrl.Delete(bucketName) + http.Redirect(w, r, "/website", http.StatusFound) + return + default: + processErr = fmt.Errorf("Unknown action") } } - + view, err := ctrl.Inspect(bucketName) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) - return + return } - + describe, err := ctrl.Describe() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) - return + return } - tpl := &WebsiteInspectTpl{ describe, view, processErr } + tpl := &WebsiteInspectTpl{describe, view, processErr} tWebsiteInspect := getTemplate("garage_website_inspect.html") tWebsiteInspect.Execute(w, &tpl) -- cgit v1.2.3