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