diff options
| author | Suresh Kumar <sureshkumar.pp@gmail.com> | 2020-08-27 15:56:02 +0530 |
|---|---|---|
| committer | Suresh Kumar <sureshkumar.pp@gmail.com> | 2020-08-27 15:56:02 +0530 |
| commit | 4d6a1bc3c4eb262c643719f65651986023fc01cd (patch) | |
| tree | dc5eaad107db87b8d8716e18a2663429f958a632 /aws | |
| parent | 8e69d1355658f56e0ef962d10f68952077085342 (diff) | |
Add EC2Tags function
Diffstat (limited to 'aws')
| -rw-r--r-- | aws/ec2info.go | 22 | ||||
| -rw-r--r-- | aws/ec2info_test.go | 26 |
2 files changed, 48 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 |
