Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tags for volumes in an Instance #35

Merged
merged 6 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions ec2/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ec2

import (
"context"
"strings"

"github.com/YaleSpinup/apierror"
"github.com/YaleSpinup/ec2-api/common"
Expand All @@ -19,6 +20,43 @@ func (e *Ec2) UpdateTags(ctx context.Context, rawTags map[string]string, ids ...
tags = append(tags, &ec2.Tag{Key: aws.String(key), Value: aws.String(val)})
}

instanceIDs := []*string{}
for _, id := range ids {
if strings.HasPrefix(id, "i-") {
instanceIDs = append(instanceIDs, aws.String(id))
}
}

describeVolumesInput := ec2.DescribeVolumesInput{
nvnyale marked this conversation as resolved.
Show resolved Hide resolved
Filters: []*ec2.Filter{
{
Name: aws.String("attachment.instance-id"),
Values: instanceIDs,
},
},
MaxResults: aws.Int64(1000),
}

for {
out, err := e.Service.DescribeVolumesWithContext(ctx, &describeVolumesInput)
if err != nil {
return common.ErrCode("describing volumes for instance", err)
}

log.Debugf("got describe volumes output %+v", out)

for _, v := range out.Volumes {
ids = append(ids, aws.StringValue(v.VolumeId))
}

if out.NextToken != nil {
describeVolumesInput.NextToken = out.NextToken
continue
}

break
}

log.Infof("updating resources: %v with tags %+v", ids, tags)

input := ec2.CreateTagsInput{
Expand Down
9 changes: 8 additions & 1 deletion ec2/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func (m *mockEC2Client) CreateTagsWithContext(ctx context.Context, input *ec2.Cr
return &ec2.CreateTagsOutput{}, nil
}

func (m *mockEC2Client) DescribeVolumesWithContext(aws aws.Context, inp *ec2.DescribeVolumesInput, opt ...request.Option) (*ec2.DescribeVolumesOutput, error) {
if m.err != nil {
return nil, m.err
}
return &ec2.DescribeVolumesOutput{}, nil
}

func TestEc2_UpdateTags(t *testing.T) {
type fields struct {
Service ec2iface.EC2API
Expand Down Expand Up @@ -65,7 +72,7 @@ func TestEc2_UpdateTags(t *testing.T) {
{
name: "no ids",
fields: fields{Service: newmockEC2Client(t, nil)},
args: args{ctx: context.TODO(), tags: inpTags, ids: nil},
args: args{ctx: context.TODO(), tags: inpTags, ids: []string{}},
wantErr: true,
},
}
Expand Down