-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
63 lines (51 loc) · 1.97 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
// 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"
"namespacelabs.dev/foundation/framework/resources/provider"
"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() {
intent := &s3.BucketIntent{}
ctx, r := provider.MustPrepare(intent)
endpoint, err := resources.LookupServerEndpoint(r, fmt.Sprintf("%s:server", providerPkg), "api")
if err != nil {
log.Fatalf("failed to get localstack server: %v", err)
}
instance := &s3.BucketInstance{
Region: intent.Region,
BucketName: intent.BucketName,
AccessKey: accessKey,
SecretAccessKey: secretAccessKey,
Url: fmt.Sprintf("http://%s", endpoint),
}
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)
}
provider.EmitResult(instance)
}