Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
ec2/ec2test: handle "tag:" filter for volumes
Browse files Browse the repository at this point in the history
Handle the "tag:key=value" type filter for
volumes. This is required for Juju to be able
to list volumes belonging to an environment.
  • Loading branch information
axw committed Jul 22, 2015
1 parent f142bcd commit 9728e8c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ec2/ec2test/server.go
Expand Up @@ -2198,6 +2198,10 @@ func (v *volume) matchAttr(attr, value string) (ok bool, err error) {
if strings.HasPrefix(attr, "attachment.") {
return false, fmt.Errorf("%q filter is not implemented", attr)
}
if strings.HasPrefix(attr, "tag:") {
key := attr[len("tag:"):]
return matchTag(v.Tags, key, value), nil
}
switch attr {
case "volume-type":
return v.VolumeType == value, nil
Expand Down Expand Up @@ -2787,3 +2791,12 @@ func fatalf(statusCode int, code string, f string, a ...interface{}) {
Message: fmt.Sprintf(f, a...),
})
}

func matchTag(tags []ec2.Tag, key, value string) bool {
for _, tag := range tags {
if tag.Key == key {
return tag.Value == value
}
}
return false
}
35 changes: 35 additions & 0 deletions ec2/volumes_test.go
Expand Up @@ -223,6 +223,41 @@ func (s *ServerTests) TestVolumes(c *C) {
assertVolume(c, list.Volumes[0], id2, vol2.VolumeType, vol2.AvailZone, 101, vol2.IOPS)
}

func (s *ServerTests) TestVolumesTagFilter(c *C) {
createVolume := func() string {
resp, err := s.ec2.CreateVolume(ec2.CreateVolume{
AvailZone: "us-east-1b",
VolumeType: "standard",
VolumeSize: 1,
})
c.Assert(err, IsNil)
return resp.Id
}
destroyVolume := func(id string) {
_, err := s.ec2.DeleteVolume(id)
c.Assert(err, IsNil)
}

volId1 := createVolume()
defer destroyVolume(volId1)
volId2 := createVolume()
defer destroyVolume(volId2)

filter := ec2.NewFilter()
filter.Add("tag:key", "value")
resp, err := s.ec2.Volumes(nil, filter)
c.Assert(err, IsNil)
c.Assert(resp.Volumes, HasLen, 0)

_, err = s.ec2.CreateTags([]string{volId1}, []ec2.Tag{{"key", "value"}})
c.Assert(err, IsNil)

resp, err = s.ec2.Volumes(nil, filter)
c.Assert(err, IsNil)
c.Assert(resp.Volumes, HasLen, 1)
c.Assert(resp.Volumes[0].Id, Equals, volId1)
}

func assertVolume(c *C, obtained ec2.Volume, expectId, expectType, availZone string, expectSize int, expectIOPS int64) {
if expectId != "" {
c.Check(obtained.Id, Equals, expectId)
Expand Down

0 comments on commit 9728e8c

Please sign in to comment.