-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
66 lines (53 loc) · 2.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package main
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"namespacelabs.dev/foundation/framework/resources/provider"
"namespacelabs.dev/foundation/library/oss/localstack"
"namespacelabs.dev/foundation/library/storage/s3"
)
const (
providerPkg = "namespacelabs.dev/foundation/library/oss/localstack"
// Any non-empty access key is valid for localstack.
// https://github.com/localstack/localstack/issues/62#issuecomment-294749459
accessKey = "localstack"
secretAccessKey = "localstack"
)
func main() {
ctx, p := provider.MustPrepare[*localstack.BucketIntent]()
server := &localstack.ClusterInstance{}
if err := p.Resources.Unmarshal(fmt.Sprintf("%s:cluster", providerPkg), server); err != nil {
log.Fatalf("unable to read required resource \"cluster\": %v", err)
}
instance := &s3.BucketInstance{
BucketName: p.Intent.BucketName,
AccessKey: accessKey,
SecretAccessKey: secretAccessKey,
Url: fmt.Sprintf("http://%s", server.Endpoint),
PrivateEndpointUrl: fmt.Sprintf("http://%s", server.Endpoint),
}
if server.PublicBaseUrl != "" {
instance.PublicUrl = server.PublicBaseUrl + "/" + p.Intent.BucketName
}
resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{PartitionID: "aws", URL: instance.Url, SigningRegion: region}, nil
})
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(instance.Region),
config.WithEndpointResolverWithOptions(resolver),
config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(instance.AccessKey, instance.SecretAccessKey, "" /* session */)))
if err != nil {
log.Fatalf("failed to load aws config: %v", err)
}
if err := s3.CreateBucket(ctx, cfg, instance.BucketName); err != nil {
log.Fatalf("failed to create bucket: %v", err)
}
p.EmitResult(instance)
}