-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
87 lines (69 loc) · 2.56 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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/oss/minio"
"namespacelabs.dev/foundation/library/storage/s3"
)
const providerPkg = "namespacelabs.dev/foundation/library/oss/minio"
func main() {
ctx, p := provider.MustPrepare[*minio.BucketIntent]()
instance, err := prepareInstance(p.Resources, p.Intent)
if err != nil {
log.Fatalf("failed to create instance: %v", err)
}
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)
}
func prepareInstance(r *resources.Parsed, intent *minio.BucketIntent) (*s3.BucketInstance, error) {
serverRef := fmt.Sprintf("%s:server", providerPkg)
serviceName := "api"
endpoint, err := resources.LookupServerEndpoint(r, serverRef, serviceName)
if err != nil {
return nil, err
}
accessKeyID, err := resources.ReadSecret(r, fmt.Sprintf("%s:user", providerPkg))
if err != nil {
return nil, err
}
secretAccessKey, err := resources.ReadSecret(r, fmt.Sprintf("%s:password", providerPkg))
if err != nil {
return nil, err
}
ingress, err := resources.LookupServerFirstIngress(r, serverRef, serviceName)
if err != nil {
return nil, err
}
bucket := &s3.BucketInstance{
AccessKey: string(accessKeyID),
SecretAccessKey: string(secretAccessKey),
BucketName: intent.BucketName,
Url: fmt.Sprintf("http://%s", endpoint), // XXX remove.
PrivateEndpointUrl: fmt.Sprintf("http://%s", endpoint),
}
if ingress != nil {
bucket.PublicUrl = *ingress + "/" + intent.BucketName
}
return bucket, nil
}