-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
layer.go
84 lines (77 loc) · 2.16 KB
/
layer.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
package lambroll
import (
"context"
"fmt"
"strconv"
"text/template"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/ast"
)
type LayerArnResolver struct {
svc *lambda.Client
}
func newLayerArnResolver(cfg aws.Config) *LayerArnResolver {
return &LayerArnResolver{
svc: lambda.NewFromConfig(cfg),
}
}
func (r *LayerArnResolver) resolve(ctx context.Context, name, version string) (string, error) {
switch version {
case "latest", "":
out, err := r.svc.ListLayerVersions(ctx, &lambda.ListLayerVersionsInput{
LayerName: &name,
MaxItems: aws.Int32(1),
})
if err != nil {
return "", fmt.Errorf("failed to list layer versions: %w", err)
}
if len(out.LayerVersions) == 0 {
return "", fmt.Errorf("layer_arn: layer %s not found", name)
}
return *out.LayerVersions[0].LayerVersionArn, nil
default:
v, err := strconv.ParseInt(version, 10, 64)
if err != nil {
return "", fmt.Errorf("layer_arn: version must be a string of number or 'latest'")
}
out, err := r.svc.GetLayerVersion(ctx, &lambda.GetLayerVersionInput{
LayerName: &name,
VersionNumber: &v,
})
if err != nil {
return "", fmt.Errorf("failed to get layer version %s:%s %w", name, version, err)
}
return *out.LayerVersionArn, nil
}
}
func (r *LayerArnResolver) JsonnetNativeFuncs(ctx context.Context) []*jsonnet.NativeFunction {
return []*jsonnet.NativeFunction{
{
Name: "layer_arn",
Params: []ast.Identifier{"name", "version"},
Func: func(params []any) (any, error) {
if len(params) != 2 {
return nil, fmt.Errorf("layer_arn: invalid number of arguments")
}
name, ok := params[0].(string)
if !ok {
return nil, fmt.Errorf("layer_arn: name must be a string")
}
version, ok := params[1].(string)
if !ok {
return nil, fmt.Errorf("layer_arn: version must be a string")
}
return r.resolve(ctx, name, version)
},
},
}
}
func (r *LayerArnResolver) FuncMap(ctx context.Context) template.FuncMap {
return template.FuncMap{
"layer_arn": func(name, version string) (string, error) {
return r.resolve(ctx, name, version)
},
}
}