garage-admin-sdk-generator/example/golang/main.go

86 lines
2.9 KiB
Go

package main
import (
"context"
"fmt"
"os"
"strings"
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
)
func main() {
// Set Host and other parameters
configuration := garage.NewConfiguration()
configuration.Host = "127.0.0.1:3903"
// We can now generate a client
client := garage.NewAPIClient(configuration)
// Authentication is handled through the context pattern
ctx := context.WithValue(context.Background(), garage.ContextAccessToken, "s3cr3t")
// Send a request
resp, r, err := client.NodesApi.GetNodes(ctx).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `NodesApi.GetNodes`\n")
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
panic(err)
}
// Process the response
fmt.Fprintf(os.Stdout, "First hostname: %v\n", resp.KnownNodes[0].Hostname)
// Layout
capa := int64(1000000000)
change := []garage.NodeRoleChange{
garage.NodeRoleChange{NodeRoleUpdate: &garage.NodeRoleUpdate {
Id: *resp.KnownNodes[0].Id,
Zone: "dc1",
Capacity: *garage.NewNullableInt64(&capa),
Tags: []string{ "fast", "amd64" },
}},
}
staged, r, err := client.LayoutApi.AddLayout(ctx).NodeRoleChange(change).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `LayoutApi.AddLayout`\n")
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
panic(err)
}
msg, r, err := client.LayoutApi.ApplyLayout(ctx).LayoutVersion(*garage.NewLayoutVersion(staged.Version + 1)).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `LayoutApi.ApplyLayout`\n")
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
panic(err)
}
fmt.Printf(strings.Join(msg.Message, "\n"))
// Key
// -- create
key := "openapi-key"
keyInfo, r, err := client.KeyApi.AddKey(ctx).AddKeyRequest(garage.AddKeyRequest{Name: *garage.NewNullableString(&key) }).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `KeyApi.AddKey`\n")
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
panic(err)
}
fmt.Printf("AWS_ACCESS_KEY_ID=%s\nAWS_SECRET_ACCESS_KEY=%s\n", *keyInfo.AccessKeyId, *keyInfo.SecretAccessKey.Get())
// Bucket
// -- create
global_name := "global-ns-openapi-bucket"
local_name := "local-ns-openapi-bucket"
bucketInfo, r, err := client.BucketApi.CreateBucket(ctx).CreateBucketRequest(garage.CreateBucketRequest{
GlobalAlias: &global_name,
LocalAlias: &garage.CreateBucketRequestLocalAlias {
AccessKeyId: keyInfo.AccessKeyId,
Alias: &local_name,
},
}).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `BucketApi.CreateBucket`\n")
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
panic(err)
}
fmt.Printf("Bucket id: %s\n", *bucketInfo.Id)
}