summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2020-08-27 08:00:38 -0400
committerGitHub <noreply@github.com>2020-08-27 08:00:38 -0400
commitf6bb4891e871bf515c56e242ff38e653fecad50d (patch)
tree0179a90873b95d342d015dfd966181c466d02d62
parent8e69d1355658f56e0ef962d10f68952077085342 (diff)
parentbace1876784301005b93c36cdd6ad2bdcce0b779 (diff)
Merge pull request #931 from surki/master
Add EC2Tags function
-rw-r--r--aws/ec2info.go22
-rw-r--r--aws/ec2info_test.go26
-rw-r--r--docs-src/content/functions/aws.yml12
-rw-r--r--docs/content/functions/aws.md26
-rw-r--r--funcs/aws.go7
5 files changed, 93 insertions, 0 deletions
diff --git a/aws/ec2info.go b/aws/ec2info.go
index da2ec9c1..30146553 100644
--- a/aws/ec2info.go
+++ b/aws/ec2info.go
@@ -163,6 +163,28 @@ func (e *Ec2Info) Tag(tag string, def ...string) (string, error) {
return returnDefault(def), nil
}
+func (e *Ec2Info) Tags() (map[string]string, error) {
+ tags := map[string]string{}
+
+ output, err := e.describeInstance()
+ if err != nil {
+ return tags, err
+ }
+ if output == nil {
+ return tags, nil
+ }
+
+ if len(output.Reservations) > 0 &&
+ len(output.Reservations[0].Instances) > 0 &&
+ len(output.Reservations[0].Instances[0].Tags) > 0 {
+ for _, v := range output.Reservations[0].Instances[0].Tags {
+ tags[*v.Key] = *v.Value
+ }
+ }
+
+ return tags, nil
+}
+
func (e *Ec2Info) describeInstance() (output *ec2.DescribeInstancesOutput, err error) {
// cache the InstanceDescriber here
d, err := e.describer()
diff --git a/aws/ec2info_test.go b/aws/ec2info_test.go
index 809704ff..b3490ac5 100644
--- a/aws/ec2info_test.go
+++ b/aws/ec2info_test.go
@@ -65,6 +65,32 @@ func TestTag_ValidKey(t *testing.T) {
assert.Equal(t, "bar", must(e.Tag("foo", "default")))
}
+func TestTags(t *testing.T) {
+ server, ec2meta := MockServer(200, `"i-1234"`)
+ defer server.Close()
+ client := DummyInstanceDescriber{
+ tags: []*ec2.Tag{
+ {
+ Key: aws.String("foo"),
+ Value: aws.String("bar"),
+ },
+ {
+ Key: aws.String("baz"),
+ Value: aws.String("qux"),
+ },
+ },
+ }
+ e := &Ec2Info{
+ describer: func() (InstanceDescriber, error) {
+ return client, nil
+ },
+ metaClient: ec2meta,
+ cache: make(map[string]interface{}),
+ }
+
+ assert.Equal(t, map[string]string{"foo": "bar", "baz": "qux"}, must(e.Tags()))
+}
+
func TestTag_NonEC2(t *testing.T) {
server, ec2meta := MockServer(404, "")
ec2meta.nonAWS = true
diff --git a/docs-src/content/functions/aws.yml b/docs-src/content/functions/aws.yml
index 5648b80e..c980ff25 100644
--- a/docs-src/content/functions/aws.yml
+++ b/docs-src/content/functions/aws.yml
@@ -98,6 +98,18 @@ funcs:
- |
$ echo 'I am a {{ aws.EC2Tag "classification" "meat popsicle" }}.' | ./gomplate
I am a meat popsicle.
+ - name: aws.EC2Tags
+ alias: ec2tags
+ description: |
+ Queries the AWS EC2 API to find all the tags/values [user-defined tag](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html).
+ pipeline: false
+ arguments:
+ examples:
+ - |
+ echo '{{ range $key, $value := aws.EC2Tags }}{{(printf "%s=%s\n" $key $value)}}{{ end }}' | ./gomplate
+ Description=foo
+ Name=bar
+ svc:name=foobar
- name: aws.KMSEncrypt
description: |
Encrypt an input string with the AWS Key Management Service (KMS).
diff --git a/docs/content/functions/aws.md b/docs/content/functions/aws.md
index cb59d482..d70d906f 100644
--- a/docs/content/functions/aws.md
+++ b/docs/content/functions/aws.md
@@ -144,6 +144,32 @@ $ echo 'I am a {{ aws.EC2Tag "classification" "meat popsicle" }}.' | ./gomplate
I am a meat popsicle.
```
+## `aws.EC2Tags`
+
+**Alias:** `ec2tags`
+
+Queries the AWS EC2 API to find all the tags/values [user-defined tag](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html).
+
+### Usage
+
+```go
+aws.EC2Tags
+```
+
+### Arguments
+
+| name | description |
+|------|-------------|
+
+### Examples
+
+```console
+echo '{{ range $key, $value := aws.EC2Tags }}{{(printf "%s=%s\n" $key $value)}}{{ end }}' | ./gomplate
+Description=foo
+Name=bar
+svc:name=foobar
+```
+
## `aws.KMSEncrypt`
Encrypt an input string with the AWS Key Management Service (KMS).
diff --git a/funcs/aws.go b/funcs/aws.go
index e7359ce9..8f5f9380 100644
--- a/funcs/aws.go
+++ b/funcs/aws.go
@@ -43,6 +43,7 @@ func CreateAWSFuncs(ctx context.Context) map[string]interface{} {
f["ec2meta"] = ns.EC2Meta
f["ec2dynamic"] = ns.EC2Dynamic
f["ec2tag"] = ns.EC2Tag
+ f["ec2tags"] = ns.EC2Tags
f["ec2region"] = ns.EC2Region
return f
}
@@ -86,6 +87,12 @@ func (a *Funcs) EC2Tag(tag string, def ...string) (string, error) {
return a.info.Tag(tag, def...)
}
+// EC2Tag -
+func (a *Funcs) EC2Tags() (map[string]string, error) {
+ a.infoInit.Do(a.initInfo)
+ return a.info.Tags()
+}
+
// KMSEncrypt -
func (a *Funcs) KMSEncrypt(keyID, plaintext interface{}) (string, error) {
a.kmsInit.Do(a.initKMS)