Skip to content

Commit

Permalink
Merge pull request #123 from ekristen/additional-tags
Browse files Browse the repository at this point in the history
feat: adding additional tags to various resources
  • Loading branch information
ekristen authored Apr 27, 2024
2 parents 98f6c24 + 53967b2 commit a51e1a9
Show file tree
Hide file tree
Showing 20 changed files with 11,478 additions and 29 deletions.
3,266 changes: 3,266 additions & 0 deletions mocks/mock_ecsiface/mock.go

Large diffs are not rendered by default.

4,546 changes: 4,546 additions & 0 deletions mocks/mock_elasticacheiface/mock.go

Large diffs are not rendered by default.

1,552 changes: 1,552 additions & 0 deletions mocks/mock_servicediscoveryiface/mock.go

Large diffs are not rendered by default.

1,253 changes: 1,253 additions & 0 deletions mocks/mock_sqsiface/mock.go

Large diffs are not rendered by default.

51 changes: 43 additions & 8 deletions resources/ecs-clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package resources
import (
"context"

"github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/slices"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/pkg/nuke"
)
Expand All @@ -22,12 +27,20 @@ func init() {
})
}

type ECSClusterLister struct{}
type ECSClusterLister struct {
mockSvc ecsiface.ECSAPI
}

func (l *ECSClusterLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := ecs.New(opts.Session)
var svc ecsiface.ECSAPI
if l.mockSvc != nil {
svc = l.mockSvc
} else {
svc = ecs.New(opts.Session)
}

resources := make([]resource.Resource, 0)

params := &ecs.ListClustersInput{
Expand All @@ -40,11 +53,21 @@ func (l *ECSClusterLister) List(_ context.Context, o interface{}) ([]resource.Re
return nil, err
}

for _, clusterArn := range output.ClusterArns {
resources = append(resources, &ECSCluster{
svc: svc,
ARN: clusterArn,
for _, clusterChunk := range slices.Chunk(output.ClusterArns, 100) {
clusters, err := svc.DescribeClusters(&ecs.DescribeClustersInput{
Clusters: clusterChunk,
})
if err != nil {
logrus.WithError(err).Error("unable to retrieve clusters")
}

for _, cluster := range clusters.Clusters {
resources = append(resources, &ECSCluster{
svc: svc,
ARN: cluster.ClusterArn,
tags: cluster.Tags,
})
}
}

if output.NextToken == nil {
Expand All @@ -58,8 +81,9 @@ func (l *ECSClusterLister) List(_ context.Context, o interface{}) ([]resource.Re
}

type ECSCluster struct {
svc *ecs.ECS
ARN *string
svc ecsiface.ECSAPI
ARN *string
tags []*ecs.Tag
}

func (f *ECSCluster) Remove(_ context.Context) error {
Expand All @@ -73,3 +97,14 @@ func (f *ECSCluster) Remove(_ context.Context) error {
func (f *ECSCluster) String() string {
return *f.ARN
}

func (f *ECSCluster) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("ARN", f.ARN)

for _, tag := range f.tags {
properties.SetTag(tag.Key, tag.Value)
}

return properties
}
84 changes: 84 additions & 0 deletions resources/ecs-clusters_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package resources

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/gotidy/ptr"
"github.com/stretchr/testify/assert"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"

"github.com/ekristen/aws-nuke/mocks/mock_ecsiface"

"github.com/ekristen/aws-nuke/pkg/nuke"
)

func Test_Mock_ECSCluster_List(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockECS := mock_ecsiface.NewMockECSAPI(ctrl)

ecsClusterLister := ECSClusterLister{
mockSvc: mockECS,
}

mockECS.EXPECT().ListClusters(gomock.Any()).Return(&ecs.ListClustersOutput{
ClusterArns: []*string{
aws.String("foobar"),
},
}, nil)

mockECS.EXPECT().DescribeClusters(gomock.Any()).Return(&ecs.DescribeClustersOutput{
Clusters: []*ecs.Cluster{
{
ClusterArn: aws.String("foobar"),
Tags: []*ecs.Tag{
{
Key: aws.String("Name"),
Value: aws.String("foobar"),
},
{
Key: aws.String("aws-nuke"),
Value: aws.String("test"),
},
},
},
},
}, nil)

resources, err := ecsClusterLister.List(context.TODO(), &nuke.ListerOpts{})
a.Nil(err)
a.Len(resources, 1)

ecsCluster := resources[0].(*ECSCluster)
a.Equal("foobar", ecsCluster.String())
a.Equal("foobar", ecsCluster.Properties().Get("tag:Name"))
a.Equal("test", ecsCluster.Properties().Get("tag:aws-nuke"))
}

func Test_Mock_ECSCluster_Remove(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockECS := mock_ecsiface.NewMockECSAPI(ctrl)

ecsCluster := ECSCluster{
svc: mockECS,
ARN: ptr.String("foobar"),
}

a.Equal("foobar", ecsCluster.String())

mockECS.EXPECT().DeleteCluster(gomock.Eq(&ecs.DeleteClusterInput{
Cluster: aws.String("foobar"),
})).Return(&ecs.DeleteClusterOutput{}, nil)

err := ecsCluster.Remove(context.TODO())
a.Nil(err)
}
2 changes: 2 additions & 0 deletions resources/ecs-mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//go:generate ../mocks/generate_mocks.sh ecs ecsiface
package resources
40 changes: 37 additions & 3 deletions resources/elasticache-memcacheclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package resources
import (
"context"

"github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/pkg/nuke"
)
Expand All @@ -27,12 +31,19 @@ func init() {
})
}

type ElasticacheCacheClusterLister struct{}
type ElasticacheCacheClusterLister struct {
mockSvc elasticacheiface.ElastiCacheAPI
}

func (l *ElasticacheCacheClusterLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := elasticache.New(opts.Session)
var svc elasticacheiface.ElastiCacheAPI
if l.mockSvc != nil {
svc = l.mockSvc
} else {
svc = elasticache.New(opts.Session)
}

params := &elasticache.DescribeCacheClustersInput{MaxRecords: aws.Int64(100)}
resp, err := svc.DescribeCacheClusters(params)
Expand All @@ -42,20 +53,43 @@ func (l *ElasticacheCacheClusterLister) List(_ context.Context, o interface{}) (

var resources []resource.Resource
for _, cacheCluster := range resp.CacheClusters {
tags, err := svc.ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: cacheCluster.CacheClusterId,
})
if err != nil {
logrus.WithError(err).Error("unable to retrieve tags")
continue
}

resources = append(resources, &ElasticacheCacheCluster{
svc: svc,
clusterID: cacheCluster.CacheClusterId,
status: cacheCluster.CacheClusterStatus,
Tags: tags.TagList,
})
}

return resources, nil
}

type ElasticacheCacheCluster struct {
svc *elasticache.ElastiCache
svc elasticacheiface.ElastiCacheAPI
clusterID *string
status *string
Tags []*elasticache.Tag
}

func (i *ElasticacheCacheCluster) Properties() types.Properties {
properties := types.NewProperties()

properties.Set("ClusterID", i.clusterID)
properties.Set("Status", i.status)

for _, tag := range i.Tags {
properties.SetTag(tag.Key, tag.Value)
}

return properties
}

func (i *ElasticacheCacheCluster) Remove(_ context.Context) error {
Expand Down
112 changes: 112 additions & 0 deletions resources/elasticache-memcacheclusters_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package resources

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"

"github.com/ekristen/aws-nuke/mocks/mock_elasticacheiface"
"github.com/ekristen/aws-nuke/pkg/nuke"
)

func Test_Mock_ElastiCache_CacheCluster_Remove(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

cacheCluster := ElasticacheCacheCluster{
svc: mockElastiCache,
clusterID: aws.String("foobar"),
}

mockElastiCache.EXPECT().DeleteCacheCluster(&elasticache.DeleteCacheClusterInput{
CacheClusterId: aws.String("foobar"),
}).Return(&elasticache.DeleteCacheClusterOutput{}, nil)

err := cacheCluster.Remove(context.TODO())
a.Nil(err)
}

func Test_Mock_ElastiCache_CacheCluster_List_NoTags(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

cacheClusterLister := ElasticacheCacheClusterLister{
mockSvc: mockElastiCache,
}

mockElastiCache.EXPECT().DescribeCacheClusters(gomock.Any()).Return(&elasticache.DescribeCacheClustersOutput{
CacheClusters: []*elasticache.CacheCluster{
{
CacheClusterId: aws.String("foobar"),
CacheClusterStatus: aws.String("available"),
},
},
}, nil)

mockElastiCache.EXPECT().ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: aws.String("foobar"),
}).Return(&elasticache.TagListMessage{}, nil)

resources, err := cacheClusterLister.List(context.TODO(), &nuke.ListerOpts{})
a.Nil(err)
a.Len(resources, 1)

resource := resources[0].(*ElasticacheCacheCluster)
a.Equal("foobar", resource.String())
}

func Test_Mock_ElastiCache_CacheCluster_List_WithTags(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockElastiCache := mock_elasticacheiface.NewMockElastiCacheAPI(ctrl)

cacheClusterLister := ElasticacheCacheClusterLister{
mockSvc: mockElastiCache,
}

mockElastiCache.EXPECT().DescribeCacheClusters(gomock.Any()).Return(&elasticache.DescribeCacheClustersOutput{
CacheClusters: []*elasticache.CacheCluster{
{
CacheClusterId: aws.String("foobar"),
},
},
}, nil)

mockElastiCache.EXPECT().ListTagsForResource(&elasticache.ListTagsForResourceInput{
ResourceName: aws.String("foobar"),
}).Return(&elasticache.TagListMessage{
TagList: []*elasticache.Tag{
{
Key: aws.String("Name"),
Value: aws.String("foobar"),
},
{
Key: aws.String("aws-nuke"),
Value: aws.String("test"),
},
},
}, nil)

resources, err := cacheClusterLister.List(context.TODO(), &nuke.ListerOpts{})
a.Nil(err)
a.Len(resources, 1)

resource := resources[0].(*ElasticacheCacheCluster)
a.Len(resource.Tags, 2)
a.Equal("foobar", resource.String())
a.Equal("foobar", resource.Properties().Get("tag:Name"))
a.Equal("test", resource.Properties().Get("tag:aws-nuke"))
}
2 changes: 2 additions & 0 deletions resources/elasticache-mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//go:generate ../mocks/generate_mocks.sh elasticache elasticacheiface
package resources
Loading

0 comments on commit a51e1a9

Please sign in to comment.