An API for Guichet #23

Merged
quentin merged 14 commits from api into main 2023-09-26 06:44:36 +00:00
8 changed files with 139 additions and 147 deletions
Showing only changes of commit 706ff58a6f - Show all commits

2
api.go
View file

@ -1,8 +1,8 @@
package main
import (
"errors"
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
"net/http"

2
cli.go
View file

@ -3,9 +3,9 @@ package main
import (
"flag"
"fmt"
"golang.org/x/term"
"os"
"syscall"
"golang.org/x/term"
)
var fsCli = flag.NewFlagSet("cli", flag.ContinueOnError)

View file

@ -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()
@ -267,10 +264,10 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) {
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 })
_, processErr = ctrl.Patch(bucketName, &WebsitePatch{Size: &user.Quota.WebsiteSizeBursted})
case "delete_bucket":
processErr = ctrl.Delete(bucketName)
http.Redirect(w, r, "/website", http.StatusFound)
@ -293,7 +290,7 @@ func handleWebsiteInspect(w http.ResponseWriter, r *http.Request) {
return
}
tpl := &WebsiteInspectTpl{ describe, view, processErr }
tpl := &WebsiteInspectTpl{describe, view, processErr}
tWebsiteInspect := getTemplate("garage_website_inspect.html")
tWebsiteInspect.Execute(w, &tpl)

View file

@ -112,6 +112,7 @@ type Capabilities struct {
CanAdmin bool
CanInvite bool
}
func NewCapabilities(login *LoginStatus, entry *ldap.Entry) *Capabilities {
// Initialize
canAdmin := false
@ -148,6 +149,7 @@ type LoggedUser struct {
Quota *UserQuota
s3key *garage.KeyInfo
}
func NewLoggedUser(login *LoginStatus) (*LoggedUser, error) {
requestKind := "(objectClass=organizationalPerson)"
if strings.EqualFold(login.Info.DN(), config.AdminAccount) {
@ -184,7 +186,7 @@ func NewLoggedUser(login *LoginStatus) (*LoggedUser, error) {
}
entry := sr.Entries[0]
lu := &LoggedUser {
lu := &LoggedUser{
Login: login,
Entry: entry,
Capabilities: NewCapabilities(login, entry),
@ -251,7 +253,6 @@ func RequireUser(r *http.Request) (*LoggedUser, error) {
return nil, ErrNotAuthenticated
}
loginStatus, err := NewLoginStatus(r, login_info)
if err != nil {
return nil, err

View file

@ -192,7 +192,6 @@ func logRequest(handler http.Handler) http.Handler {
// Page handlers ----
// --- Home Controller
type HomePageData struct {
User *LoggedUser
@ -254,7 +253,7 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
username := strings.Join(r.Form["username"], "")
password := strings.Join(r.Form["password"], "")
loginInfo := LoginInfo { username, password }
loginInfo := LoginInfo{username, password}
l, err := NewLdapCon()
if err != nil {

View file

@ -5,8 +5,8 @@ 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 (
@ -29,7 +29,7 @@ type UserQuota struct {
}
func NewUserQuota() *UserQuota {
return &UserQuota {
return &UserQuota{
WebsiteCount: QUOTA_WEBSITE_COUNT,
WebsiteSizeDefault: QUOTA_WEBSITE_SIZE_DEFAULT,
WebsiteSizeBursted: QUOTA_WEBSITE_SIZE_BURSTED,
@ -107,8 +107,9 @@ type QuotaStat struct {
Ratio float64 `json:"ratio"`
Burstable bool `json:"burstable"`
}
func NewQuotaStat(current, max int64, burstable bool) QuotaStat {
return QuotaStat {
return QuotaStat{
Current: current,
Max: max,
Ratio: float64(current) / float64(max),

View file

@ -2,9 +2,9 @@ package main
import (
"fmt"
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
"sort"
"strings"
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
)
var (
@ -19,16 +19,14 @@ var (
ErrBucketDeleteUnfinishedUpload = fmt.Errorf("You must remove all the unfinished multipart uploads before deleting a bucket")
)
type WebsiteId struct {
Pretty string `json:"name"`
Internal string `json:"-"`
Alt []string `json:"alt_name"`
Expanded bool `json:"expanded"`
Url string `json:"domain"`
}
func NewWebsiteId(id string, aliases []string) *WebsiteId {
pretty := id
var alt []string
@ -43,7 +41,7 @@ func NewWebsiteId(id string, aliases []string) *WebsiteId {
url = fmt.Sprintf("%s.web.deuxfleurs.fr", pretty)
}
return &WebsiteId { pretty, id, alt, expanded, url }
return &WebsiteId{pretty, id, alt, expanded, url}
}
func NewWebsiteIdFromBucketInfo(binfo *garage.BucketInfo) *WebsiteId {
return NewWebsiteId(*binfo.Id, binfo.GlobalAliases)
@ -65,7 +63,7 @@ func NewWebsiteController(user *LoggedUser) (*WebsiteController, error) {
return nil, err
}
for _, bckt := range(keyInfo.Buckets) {
for _, bckt := range keyInfo.Buckets {
if len(bckt.GlobalAliases) > 0 {
wid := NewWebsiteId(*bckt.Id, bckt.GlobalAliases)
idx[wid.Pretty] = wid
@ -77,7 +75,7 @@ func NewWebsiteController(user *LoggedUser) (*WebsiteController, error) {
maxW := user.Quota.WebsiteCount
quota := NewQuotaStat(int64(len(wlist)), maxW, true)
return &WebsiteController { user, idx, wlist, quota }, nil
return &WebsiteController{user, idx, wlist, quota}, nil
}
type WebsiteDescribe struct {
@ -98,12 +96,12 @@ func (w *WebsiteController) Describe() (*WebsiteDescribe, error) {
for _, k := range w.PrettyList {
r = append(r, w.WebsiteIdx[k])
}
return &WebsiteDescribe {
return &WebsiteDescribe{
*s3key.AccessKeyId,
*s3key.SecretAccessKey,
&w.WebsiteCount,
w.User.Quota.WebsiteSizeBurstedPretty(),
r }, nil
r}, nil
}
func (w *WebsiteController) Inspect(pretty string) (*WebsiteView, error) {
@ -183,7 +181,6 @@ func (w *WebsiteController) Create(pretty string) (*WebsiteView, error) {
ur.SetWebsiteAccess(*wr)
ur.SetQuotas(*qr)
binfo, err = grgUpdateBucket(*binfo.Id, ur)
if err != nil {
return nil, ErrCantConfigureBucket
@ -219,9 +216,6 @@ func (w *WebsiteController) Delete(pretty string) error {
return err
}
type WebsiteView struct {
Name *WebsiteId `json:"identified_as"`
Size QuotaStat `json:"quota_size"`
@ -234,7 +228,7 @@ func NewWebsiteView(binfo *garage.BucketInfo) *WebsiteView {
wid := NewWebsiteIdFromBucketInfo(binfo)
size := NewQuotaStat(*binfo.Bytes, (&q).GetMaxSize(), true)
objects := NewQuotaStat(*binfo.Objects, (&q).GetMaxObjects(), false)
return &WebsiteView { wid, size, objects }
return &WebsiteView{wid, size, objects}
}
type WebsitePatch struct {