-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for updating image tags (#23)
* Add support for updating image tags * Fixed comments * Added Unit test * Resolved Comments
- Loading branch information
Showing
6 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ec2 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/YaleSpinup/apierror" | ||
"github.com/YaleSpinup/ec2-api/common" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func (e *Ec2) UpdateTags(ctx context.Context, rawTags map[string]string, ids ...string) error { | ||
if len(ids) == 0 || len(rawTags) == 0 { | ||
return apierror.New(apierror.ErrBadRequest, "invalid input", nil) | ||
} | ||
var tags []*ec2.Tag | ||
for key, val := range rawTags { | ||
tags = append(tags, &ec2.Tag{Key: aws.String(key), Value: aws.String(val)}) | ||
} | ||
|
||
log.Infof("updating resources: %v with tags %+v", ids, tags) | ||
|
||
input := ec2.CreateTagsInput{ | ||
Resources: aws.StringSlice(ids), | ||
Tags: tags, | ||
} | ||
|
||
if _, err := e.Service.CreateTagsWithContext(ctx, &input); err != nil { | ||
return common.ErrCode("creating tags", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package ec2 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface" | ||
) | ||
|
||
var ( | ||
inpIds = []string{"id-234"} | ||
inpTags = map[string]string{"foo": "bar"} | ||
expTags = []*ec2.Tag{{Key: aws.String("foo"), Value: aws.String("bar")}} | ||
) | ||
|
||
func (m *mockEC2Client) CreateTagsWithContext(ctx context.Context, input *ec2.CreateTagsInput, opts ...request.Option) (*ec2.CreateTagsOutput, error) { | ||
if m.err != nil { | ||
return nil, m.err | ||
} | ||
if !reflect.DeepEqual(input.Resources, aws.StringSlice(inpIds)) || !reflect.DeepEqual(input.Tags, expTags) { | ||
return nil, errors.New("input does not match") | ||
} | ||
return &ec2.CreateTagsOutput{}, nil | ||
} | ||
|
||
func TestEc2_UpdateTags(t *testing.T) { | ||
type fields struct { | ||
Service ec2iface.EC2API | ||
} | ||
type args struct { | ||
ctx context.Context | ||
tags map[string]string | ||
ids []string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "success case", | ||
args: args{ctx: context.TODO(), tags: inpTags, ids: inpIds}, | ||
fields: fields{Service: newmockEC2Client(t, nil)}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "aws error", | ||
args: args{ctx: context.TODO(), tags: inpTags, ids: inpIds}, | ||
fields: fields{Service: newmockEC2Client(t, awserr.New("Bad Request", "boom.", nil))}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "no tags", | ||
fields: fields{Service: newmockEC2Client(t, nil)}, | ||
args: args{ctx: context.TODO(), tags: nil, ids: inpIds}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "no ids", | ||
fields: fields{Service: newmockEC2Client(t, nil)}, | ||
args: args{ctx: context.TODO(), tags: inpTags, ids: nil}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
e := &Ec2{ | ||
Service: tt.fields.Service, | ||
} | ||
err := e.UpdateTags(tt.args.ctx, tt.args.tags, tt.args.ids...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Ec2.UpdateTags() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
}) | ||
} | ||
} |