Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ exist in your statefiles, and prints the missing ones in a json format for ease
| lambda | lambda_function |
| dynamodb | dynamodb_table |
| cloudwatchlogs | cloudwatchlogs_log_group |
| elasticache | elasticache_cluster |

## How to use

Expand Down
66 changes: 66 additions & 0 deletions aws/elasticache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package aws

import (
"context"
"log"

awssdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/elasticache"
"github.com/noclickops/common"
)

type ElasticacheClient interface {
DescribeCacheClusters(ctx context.Context, params *elasticache.DescribeCacheClustersInput, optFns ...func(*elasticache.Options)) (*elasticache.DescribeCacheClustersOutput, error)
}

type NoclickopsElasticacheClient struct {
Client ElasticacheClient
ClientMeta
}

type NoclickopsElasticacheService struct {
Clients []NoclickopsElasticacheClient
common.ServiceMeta
}

func NewElasticacheServiceFromConfigs(cfg []awssdk.Config, meta common.ServiceMeta) NoclickopsElasticacheService {
service := NoclickopsElasticacheService{ServiceMeta: meta}
for _, c := range cfg {
service.Clients = append(service.Clients, NoclickopsElasticacheClient{
Client: elasticache.NewFromConfig(c),
ClientMeta: ClientMeta{Region: c.Region},
})
}
return service
}

func (s *NoclickopsElasticacheService) GetAllResources() []common.Resource {
return s.GetClusters()
}

func (s *NoclickopsElasticacheService) GetClusters() []common.Resource {
var resources []common.Resource
for _, rc := range s.Clients {
var marker *string = nil
for {
res, err := rc.Client.DescribeCacheClusters(context.TODO(), &elasticache.DescribeCacheClustersInput{
Marker: marker,
ShowCacheClustersNotInReplicationGroups: awssdk.Bool(true),
})
if err != nil {
log.Printf("warning: %v", err)
break
}

for _, cluster := range res.CacheClusters {
resources = append(resources, common.Resource{Arn: *cluster.ARN, TerraformID: *cluster.CacheClusterId, ResourceType: common.Elasticache_cluster, Region: rc.Region})
}

if res.Marker == nil {
break
}
marker = res.Marker
}
}
return resources
}
71 changes: 71 additions & 0 deletions aws/elasticache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package aws_test

import (
"context"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/service/elasticache"
"github.com/aws/aws-sdk-go-v2/service/elasticache/types"
"github.com/google/go-cmp/cmp"
"github.com/noclickops/aws"
"github.com/noclickops/common"
)

func getMockedElasticacheService(mock *mockElasticacheClient) aws.NoclickopsElasticacheService {
return aws.NoclickopsElasticacheService{
Clients: []aws.NoclickopsElasticacheClient{
{
Client: mock,
ClientMeta: aws.ClientMeta{Region: "eu-west-1"},
},
},
ServiceMeta: common.ServiceMeta{Global: false, ServiceName: "elasticache"},
}
}

func TestGetCacheClusters_PaginationFollowed(t *testing.T) {
callCount := 0
mock := &mockElasticacheClient{
describeCacheClustersFn: func(_ context.Context, params *elasticache.DescribeCacheClustersInput, _ ...func(*elasticache.Options)) (*elasticache.DescribeCacheClustersOutput, error) {
callCount++
if callCount == 1 {
return &elasticache.DescribeCacheClustersOutput{
Marker: ptr("next"),
CacheClusters: []types.CacheCluster{{CacheClusterId: ptr("cluster-1"), ARN: ptr("arn:aws:elasticache:eu-west-1:123456789012:cluster:cluster-1")}},
}, nil
}
if params.Marker == nil || *params.Marker != "next" {
return nil, fmt.Errorf("wrong Marker, expected 'next' got '%v'", params.Marker)
}
return &elasticache.DescribeCacheClustersOutput{
CacheClusters: []types.CacheCluster{{CacheClusterId: ptr("cluster-2"), ARN: ptr("arn:aws:elasticache:eu-west-1:123456789012:cluster:cluster-2")}},
}, nil
},
}
service := getMockedElasticacheService(mock)
got := service.GetClusters()
expected := []common.Resource{
{Arn: "arn:aws:elasticache:eu-west-1:123456789012:cluster:cluster-1", TerraformID: "cluster-1", ResourceType: common.Elasticache_cluster, Region: "eu-west-1"},
{Arn: "arn:aws:elasticache:eu-west-1:123456789012:cluster:cluster-2", TerraformID: "cluster-2", ResourceType: common.Elasticache_cluster, Region: "eu-west-1"},
}
if diff := cmp.Diff(got, expected); diff != "" {
t.Errorf("expected %v, got %v", expected, got)
}
if callCount != 2 {
t.Errorf("expected 2 calls to DescribeCacheClusters, got %d", callCount)
}
}

func TestGetCacheClusters_ErrorIsSkipped(t *testing.T) {
mock := &mockElasticacheClient{
describeCacheClustersFn: func(_ context.Context, _ *elasticache.DescribeCacheClustersInput, _ ...func(*elasticache.Options)) (*elasticache.DescribeCacheClustersOutput, error) {
return nil, fmt.Errorf("some error")
},
}
service := getMockedElasticacheService(mock)
got := service.GetClusters()
if len(got) != 0 {
t.Errorf("expected no resources on error, got %v", got)
}
}
9 changes: 9 additions & 0 deletions aws/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/elasticache"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/eks"
Expand Down Expand Up @@ -214,3 +215,11 @@ type mockDynamodbClient struct {
func (m *mockDynamodbClient) ListTables(ctx context.Context, params *dynamodb.ListTablesInput, optFns ...func(*dynamodb.Options)) (*dynamodb.ListTablesOutput, error) {
return m.listTablesFn(ctx, params, optFns...)
}

type mockElasticacheClient struct {
describeCacheClustersFn func(ctx context.Context, params *elasticache.DescribeCacheClustersInput, optFns ...func(*elasticache.Options)) (*elasticache.DescribeCacheClustersOutput, error)
}

func (m *mockElasticacheClient) DescribeCacheClusters(ctx context.Context, params *elasticache.DescribeCacheClustersInput, optFns ...func(*elasticache.Options)) (*elasticache.DescribeCacheClustersOutput, error) {
return m.describeCacheClustersFn(ctx, params, optFns...)
}
4 changes: 4 additions & 0 deletions aws/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var SERVICES = map[common.AWSServiceName]common.ServiceMeta{
common.Lambda: {Global: false, ServiceName: "lambda"},
common.CloudwatchLogs: {Global: false, ServiceName: "cloudwatchlogs"},
common.Dynamodb: {Global: false, ServiceName: "dynamodb"},
common.Elasticache: {Global: false, ServiceName: "elasticache"},

// not included in the switch/case in `NewclickopsServiceFromConfigs`
// because it doesn't follow the same invocation pattern.
Expand Down Expand Up @@ -99,6 +100,9 @@ func NewNoclickopsServiceFromConfigs(service common.AWSServiceName, configs []aw
case common.Lambda:
c := NewLambdaServiceFromConfigs(configs, meta)
return &c
case common.Elasticache:
c := NewElasticacheServiceFromConfigs(configs, meta)
return &c
case common.IdentityStore:
ssoMeta := SERVICES[common.SSOAdmin]
ssoClient := NewSSOAdminServiceFromConfigs(configs[:1], ssoMeta)
Expand Down
5 changes: 3 additions & 2 deletions common/awsservicename_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions common/resourcetype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
Autoscaling_group
Cloudwatchlogs_log_group
Dynamodb_table
Elasticache_cluster
)

func (r ResourceType) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -66,6 +67,7 @@ const (
ResourceGroupsTaggingAPI
CloudwatchLogs
Dynamodb
Elasticache
)

type Resource struct {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.57.4
github.com/aws/aws-sdk-go-v2/service/ec2 v1.299.0
github.com/aws/aws-sdk-go-v2/service/eks v1.82.1
github.com/aws/aws-sdk-go-v2/service/elasticache v1.52.2
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.33.24
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.54.11
github.com/aws/aws-sdk-go-v2/service/iam v1.53.8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/aws/aws-sdk-go-v2/service/ec2 v1.299.0 h1:qTozRFl2YFFU2HJGl7ZAywlRQvB
github.com/aws/aws-sdk-go-v2/service/ec2 v1.299.0/go.mod h1:E1pnYwWFZ8N3REmeN9Fe/Zipbpps4HJj8DQGNnLUMYc=
github.com/aws/aws-sdk-go-v2/service/eks v1.82.1 h1:xTzXiQ8Q6U4ACdMNSCm72zd4Ds7QxhgVLqt5x8GXLBM=
github.com/aws/aws-sdk-go-v2/service/eks v1.82.1/go.mod h1:jjcGpziR11RTrr3JIgXg/Nn8GSwK3WOz2z1v/RqEBUI=
github.com/aws/aws-sdk-go-v2/service/elasticache v1.52.2 h1:5wbCUfyxXcjIqesyVfJBBJs0bDMyejthtHyy48mfZCI=
github.com/aws/aws-sdk-go-v2/service/elasticache v1.52.2/go.mod h1:o4vQxDt6oteknUjkXIEskp0ccy+93NRTPKXw3HlVMFE=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.33.24 h1:VoaXlgix9ZgAjRjq86f827UVzm9pgiYX+zkyU1brhZ4=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.33.24/go.mod h1:OtTEw8mOh0CCWVx072DtJ0WOlR3Ulngdqwa36oV6jm4=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.54.11 h1:0iNKMyO0SXuRfl5FF6TQASAHTXnTYZlQS3/oJSfpEbQ=
Expand Down