guichet/garage.go

166 lines
3.9 KiB
Go
Raw Normal View History

2023-04-18 17:37:51 +00:00
package main
import (
2023-04-19 13:07:46 +00:00
"context"
"fmt"
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
"log"
2023-04-18 17:37:51 +00:00
)
2023-04-18 20:45:04 +00:00
func gadmin() (*garage.APIClient, context.Context) {
2023-04-19 13:07:46 +00:00
// Set Host and other parameters
configuration := garage.NewConfiguration()
configuration.Host = config.S3AdminEndpoint
2023-04-18 20:45:04 +00:00
2023-04-19 13:07:46 +00:00
// We can now generate a client
client := garage.NewAPIClient(configuration)
2023-04-18 20:45:04 +00:00
2023-04-19 13:07:46 +00:00
// Authentication is handled through the context pattern
ctx := context.WithValue(context.Background(), garage.ContextAccessToken, config.S3AdminToken)
return client, ctx
2023-04-18 20:45:04 +00:00
}
2023-04-19 09:36:13 +00:00
func grgCreateKey(name string) (*garage.KeyInfo, error) {
2023-04-19 13:07:46 +00:00
client, ctx := gadmin()
kr := garage.AddKeyRequest{Name: &name}
resp, _, err := client.KeyApi.AddKey(ctx).AddKeyRequest(kr).Execute()
if err != nil {
fmt.Printf("%+v\n", err)
return nil, err
}
return resp, nil
2023-04-19 09:36:13 +00:00
}
func grgGetKey(accessKey string) (*garage.KeyInfo, error) {
2023-04-19 13:07:46 +00:00
client, ctx := gadmin()
resp, _, err := client.KeyApi.GetKey(ctx, accessKey).Execute()
if err != nil {
fmt.Printf("%+v\n", err)
return nil, err
}
return resp, nil
2023-04-19 09:36:13 +00:00
}
2023-09-25 17:07:07 +00:00
func grgCreateBucket(bucket string) (*garage.BucketInfo, error) {
2023-04-19 13:07:46 +00:00
client, ctx := gadmin()
br := garage.NewCreateBucketRequest()
br.SetGlobalAlias(bucket)
// Create Bucket
binfo, _, err := client.BucketApi.CreateBucket(ctx).CreateBucketRequest(*br).Execute()
if err != nil {
fmt.Printf("%+v\n", err)
return nil, err
}
2023-09-25 17:07:07 +00:00
return binfo, nil
}
2023-09-26 06:40:41 +00:00
func grgAllowKeyOnBucket(bid, gkey string) (*garage.BucketInfo, error) {
2023-09-25 17:07:07 +00:00
client, ctx := gadmin()
2023-04-19 13:07:46 +00:00
// Allow user's key
ar := garage.AllowBucketKeyRequest{
2023-09-25 17:07:07 +00:00
BucketId: bid,
2023-04-19 13:07:46 +00:00
AccessKeyId: gkey,
Permissions: *garage.NewAllowBucketKeyRequestPermissions(true, true, true),
}
2023-09-25 17:07:07 +00:00
binfo, _, err := client.BucketApi.AllowBucketKey(ctx).AllowBucketKeyRequest(ar).Execute()
2023-04-19 13:07:46 +00:00
if err != nil {
fmt.Printf("%+v\n", err)
return nil, err
}
2023-09-25 17:07:07 +00:00
return binfo, nil
}
func allowWebsiteDefault() *garage.UpdateBucketRequestWebsiteAccess {
2023-04-19 13:07:46 +00:00
wr := garage.NewUpdateBucketRequestWebsiteAccess()
wr.SetEnabled(true)
wr.SetIndexDocument("index.html")
wr.SetErrorDocument("error.html")
2023-09-25 17:07:07 +00:00
return wr
}
2023-04-19 13:07:46 +00:00
2023-09-25 17:07:07 +00:00
func grgUpdateBucket(bid string, ur *garage.UpdateBucketRequest) (*garage.BucketInfo, error) {
client, ctx := gadmin()
2023-04-19 13:07:46 +00:00
2023-09-25 17:07:07 +00:00
binfo, _, err := client.BucketApi.UpdateBucket(ctx, bid).UpdateBucketRequest(*ur).Execute()
2023-04-19 13:07:46 +00:00
if err != nil {
fmt.Printf("%+v\n", err)
return nil, err
}
// Return updated binfo
return binfo, nil
2023-04-19 12:58:39 +00:00
}
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
}
2023-04-19 12:58:39 +00:00
func grgGetBucket(bid string) (*garage.BucketInfo, error) {
2023-04-19 13:07:46 +00:00
client, ctx := gadmin()
2023-04-19 12:58:39 +00:00
2023-04-19 13:07:46 +00:00
resp, _, err := client.BucketApi.GetBucketInfo(ctx, bid).Execute()
if err != nil {
log.Println(err)
2023-04-19 13:07:46 +00:00
return nil, err
}
return resp, nil
2023-04-19 12:58:39 +00:00
}
func grgDeleteBucket(bid string) error {
client, ctx := gadmin()
2023-09-26 06:40:41 +00:00
_, err := client.BucketApi.DeleteBucket(ctx, bid).Execute()
if err != nil {
log.Println(err)
2023-09-26 06:40:41 +00:00
}
return err
}