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()
|
|
|
|
|
2024-06-24 07:17:09 +00:00
|
|
|
kr := garage.NewAddKeyRequest()
|
|
|
|
kr.SetName(name)
|
|
|
|
resp, _, err := client.KeyApi.AddKey(ctx).AddKeyRequest(*kr).Execute()
|
2023-04-19 13:07:46 +00:00
|
|
|
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()
|
|
|
|
|
2024-06-24 07:17:09 +00:00
|
|
|
resp, _, err := client.KeyApi.GetKey(ctx).Id(accessKey).ShowSecretKey("true").Execute()
|
2023-04-19 13:07:46 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%+v\n", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
2023-04-19 09:36:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:22:17 +00:00
|
|
|
func grgSearchKey(name string) (*garage.KeyInfo, error) {
|
|
|
|
client, ctx := gadmin()
|
|
|
|
|
|
|
|
resp, _, err := client.KeyApi.GetKey(ctx).Search(name).ShowSecretKey("true").Execute()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%+v\n", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2024-06-24 08:43:11 +00:00
|
|
|
func grgDelKey(accessKey string) error {
|
|
|
|
client, ctx := gadmin()
|
|
|
|
|
|
|
|
_, err := client.KeyApi.DeleteKey(ctx).Id(accessKey).Execute()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%+v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-24 08:22:17 +00:00
|
|
|
func grgAllowKeyOnBucket(bid, gkey string, read, write, owner bool) (*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,
|
2024-06-24 08:22:17 +00:00
|
|
|
Permissions: *garage.NewAllowBucketKeyRequestPermissions(read, write, owner),
|
2023-04-19 13:07:46 +00:00
|
|
|
}
|
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
|
|
|
|
2024-06-24 07:17:09 +00:00
|
|
|
binfo, _, err := client.BucketApi.UpdateBucket(ctx).Id(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
|
|
|
}
|
|
|
|
|
2023-09-15 16:25:37 +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
|
|
|
|
2024-06-24 07:17:09 +00:00
|
|
|
resp, _, err := client.BucketApi.GetBucketInfo(ctx).Id(bid).Execute()
|
2023-04-19 13:07:46 +00:00
|
|
|
if err != nil {
|
2023-09-15 16:25:37 +00:00
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-25 20:00:46 +00:00
|
|
|
func grgDeleteBucket(bid string) error {
|
|
|
|
client, ctx := gadmin()
|
|
|
|
|
2024-06-24 07:17:09 +00:00
|
|
|
_, err := client.BucketApi.DeleteBucket(ctx).Id(bid).Execute()
|
2023-09-26 06:40:41 +00:00
|
|
|
if err != nil {
|
2023-09-25 20:00:46 +00:00
|
|
|
log.Println(err)
|
2023-09-26 06:40:41 +00:00
|
|
|
}
|
2023-09-25 20:00:46 +00:00
|
|
|
return err
|
|
|
|
}
|