Skip to content

Commit

Permalink
Merge pull request #931 from surki/master
Browse files Browse the repository at this point in the history
Add EC2Tags function
  • Loading branch information
hairyhenderson authored Aug 27, 2020
2 parents 8e69d13 + bace187 commit f6bb489
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
22 changes: 22 additions & 0 deletions aws/ec2info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions aws/ec2info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions docs-src/content/functions/aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
26 changes: 26 additions & 0 deletions docs/content/functions/aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 7 additions & 0 deletions funcs/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f6bb489

Please sign in to comment.