github.com/hairyhenderson/gomplate/v3@v3.11.7/funcs/aws.go (about) 1 package funcs 2 3 import ( 4 "context" 5 "sync" 6 7 "github.com/hairyhenderson/gomplate/v3/aws" 8 "github.com/hairyhenderson/gomplate/v3/conv" 9 ) 10 11 // AWSNS - the aws namespace 12 // Deprecated: don't use 13 // 14 //nolint:golint 15 func AWSNS() *Funcs { 16 return &Funcs{} 17 } 18 19 // AWSFuncs - 20 // Deprecated: use CreateAWSFuncs instead 21 func AWSFuncs(f map[string]interface{}) { 22 f2 := CreateAWSFuncs(context.Background()) 23 for k, v := range f2 { 24 f[k] = v 25 } 26 } 27 28 // CreateAWSFuncs - 29 func CreateAWSFuncs(ctx context.Context) map[string]interface{} { 30 f := map[string]interface{}{} 31 32 ns := &Funcs{ 33 ctx: ctx, 34 awsopts: aws.GetClientOptions(), 35 } 36 37 f["aws"] = func() interface{} { return ns } 38 39 // global aliases - for backwards compatibility 40 f["ec2meta"] = ns.EC2Meta 41 f["ec2dynamic"] = ns.EC2Dynamic 42 f["ec2tag"] = ns.EC2Tag 43 f["ec2tags"] = ns.EC2Tags 44 f["ec2region"] = ns.EC2Region 45 return f 46 } 47 48 // Funcs - 49 type Funcs struct { 50 ctx context.Context 51 52 meta *aws.Ec2Meta 53 info *aws.Ec2Info 54 kms *aws.KMS 55 sts *aws.STS 56 metaInit sync.Once 57 infoInit sync.Once 58 kmsInit sync.Once 59 stsInit sync.Once 60 awsopts aws.ClientOptions 61 } 62 63 // EC2Region - 64 func (a *Funcs) EC2Region(def ...string) (string, error) { 65 a.metaInit.Do(a.initMeta) 66 return a.meta.Region(def...) 67 } 68 69 // EC2Meta - 70 func (a *Funcs) EC2Meta(key string, def ...string) (string, error) { 71 a.metaInit.Do(a.initMeta) 72 return a.meta.Meta(key, def...) 73 } 74 75 // EC2Dynamic - 76 func (a *Funcs) EC2Dynamic(key string, def ...string) (string, error) { 77 a.metaInit.Do(a.initMeta) 78 return a.meta.Dynamic(key, def...) 79 } 80 81 // EC2Tag - 82 func (a *Funcs) EC2Tag(tag string, def ...string) (string, error) { 83 a.infoInit.Do(a.initInfo) 84 return a.info.Tag(tag, def...) 85 } 86 87 // EC2Tag - 88 func (a *Funcs) EC2Tags() (map[string]string, error) { 89 a.infoInit.Do(a.initInfo) 90 return a.info.Tags() 91 } 92 93 // KMSEncrypt - 94 func (a *Funcs) KMSEncrypt(keyID, plaintext interface{}) (string, error) { 95 a.kmsInit.Do(a.initKMS) 96 return a.kms.Encrypt(conv.ToString(keyID), conv.ToString(plaintext)) 97 } 98 99 // KMSDecrypt - 100 func (a *Funcs) KMSDecrypt(ciphertext interface{}) (string, error) { 101 a.kmsInit.Do(a.initKMS) 102 return a.kms.Decrypt(conv.ToString(ciphertext)) 103 } 104 105 // UserID - Gets the unique identifier of the calling entity. The exact value 106 // depends on the type of entity making the call. The values returned are those 107 // listed in the aws:userid column in the Principal table 108 // (http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_variables.html#principaltable) 109 // found on the Policy Variables reference page in the IAM User Guide. 110 func (a *Funcs) UserID() (string, error) { 111 a.stsInit.Do(a.initSTS) 112 return a.sts.UserID() 113 } 114 115 // Account - Gets the AWS account ID number of the account that owns or 116 // contains the calling entity. 117 func (a *Funcs) Account() (string, error) { 118 a.stsInit.Do(a.initSTS) 119 return a.sts.Account() 120 } 121 122 // ARN - Gets the AWS ARN associated with the calling entity 123 func (a *Funcs) ARN() (string, error) { 124 a.stsInit.Do(a.initSTS) 125 return a.sts.Arn() 126 } 127 128 func (a *Funcs) initMeta() { 129 if a.meta == nil { 130 a.meta = aws.NewEc2Meta(a.awsopts) 131 } 132 } 133 134 func (a *Funcs) initInfo() { 135 if a.info == nil { 136 a.info = aws.NewEc2Info(a.awsopts) 137 } 138 } 139 140 func (a *Funcs) initKMS() { 141 if a.kms == nil { 142 a.kms = aws.NewKMS(a.awsopts) 143 } 144 } 145 146 func (a *Funcs) initSTS() { 147 if a.sts == nil { 148 a.sts = aws.NewSTS(a.awsopts) 149 } 150 }