This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
/
arn.go
69 lines (62 loc) · 2.17 KB
/
arn.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
package model
import (
"encoding/json"
"fmt"
)
type ARN struct {
Arn string `yaml:"arn,omitempty"`
ArnFromStackOutput string `yaml:"arnFromStackOutput,omitempty"`
ArnFromFn string `yaml:"arnFromFn,omitempty"`
}
// HasArn returns true when the id of a resource i.e. either `arn` or `arnFromStackOutput` is specified
func (i ARN) HasArn() bool {
return i.Arn != "" || i.ArnFromStackOutput != ""
}
func (i ARN) Validate() error {
if i.ArnFromFn != "" {
var jsonHolder map[string]interface{}
if err := json.Unmarshal([]byte(i.ArnFromFn), &jsonHolder); err != nil {
return fmt.Errorf("arnFromFn must be a valid json expression but was not: %s", i.ArnFromFn)
}
}
return nil
}
func (i ARN) OrGetAttArn(logicalNameProvider func() (string, error)) (string, error) {
return i.OrExpr(func() (string, error) {
logicalName, err := logicalNameProvider()
if err != nil {
// So that kube-aws can print a more useful error message with
// the line number for the stack-template.json when there's an error
return "", fmt.Errorf("failed to get arn: %v", err)
}
return fmt.Sprintf(`{ "Fn::GetAtt": [ %q, "Arn" ] }`, logicalName), nil
})
}
func (i ARN) OrRef(logicalNameProvider func() (string, error)) (string, error) {
return i.OrExpr(func() (string, error) {
logicalName, err := logicalNameProvider()
if err != nil {
// So that kube-aws can print a more useful error message with
// the line number for the stack-template.json when there's an error
return "", fmt.Errorf("failed to get arn: %v", err)
}
return fmt.Sprintf(`{ "Ref": %q }`, logicalName), nil
})
}
func (i ARN) OrExpr(exprProvider func() (string, error)) (string, error) {
if i.ArnFromStackOutput != "" {
return fmt.Sprintf(`{ "Fn::ImportValue" : %q }`, i.ArnFromStackOutput), nil
} else if i.Arn != "" {
return fmt.Sprintf(`"%s"`, i.Arn), nil
} else if i.ArnFromFn != "" {
return i.ArnFromFn, nil
} else {
expr, err := exprProvider()
if err != nil {
// So that kube-aws can print a more useful error message with
// the line number for the stack-template.json when there's an error
return "", fmt.Errorf("failed to get arn: %v", err)
}
return expr, nil
}
}