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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ exist in your statefiles, and prints the missing ones in a json format for ease
| autoscaling | autoscaling_group |
| lambda | lambda_function |
| dynamodb | dynamodb_table |
| cloudwatchlogs | cloudwatchlogs_log_group |
| cloudwatchlogs | cloudwatch_log_group |
| elasticache | elasticache_cluster |
| ecs | ecs_cluster, ecs_service |

## How to use

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

import (
"context"
"log"
"strings"

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

type ECSClient interface {
ListClusters(ctx context.Context, params *ecs.ListClustersInput, optFns ...func(*ecs.Options)) (*ecs.ListClustersOutput, error)
ListServices(ctx context.Context, params *ecs.ListServicesInput, optFns ...func(*ecs.Options)) (*ecs.ListServicesOutput, error)
}

type NoclickopsECSClient struct {
Client ECSClient
ClientMeta
}

type NoclickopsECSService struct {
Clients []NoclickopsECSClient
common.ServiceMeta
}

func NewECSServiceFromConfigs(cfg []awssdk.Config, meta common.ServiceMeta) NoclickopsECSService {
service := NoclickopsECSService{ServiceMeta: meta}
for _, c := range cfg {
service.Clients = append(service.Clients, NoclickopsECSClient{
Client: ecs.NewFromConfig(c),
ClientMeta: ClientMeta{Region: c.Region},
})
}
return service
}

func (s *NoclickopsECSService) GetAllResources() []common.Resource {
return s.GetClustersAndServices()
}

func (s *NoclickopsECSService) GetClustersAndServices() []common.Resource {
var resources []common.Resource
for _, rc := range s.Clients {
var clusterNextToken *string = nil
for {
clusterRes, err := rc.Client.ListClusters(context.TODO(), &ecs.ListClustersInput{
NextToken: clusterNextToken,
})
if err != nil {
log.Printf("warning: %v", err)
break
}

for _, clusterArn := range clusterRes.ClusterArns {
// ecs cluster arn format
// arn:${Partition}:ecs:${Region}:${Account}:cluster/${ClusterName}
parts := strings.Split(clusterArn, "/")
clusterName := parts[len(parts)-1]
resources = append(resources, common.Resource{Arn: clusterArn, TerraformID: clusterName, ResourceType: common.ECS_cluster, Region: rc.Region})

var nextToken *string = nil
for {
res, err := rc.Client.ListServices(context.TODO(), &ecs.ListServicesInput{
Cluster: awssdk.String(clusterArn),
NextToken: nextToken,
})
if err != nil {
log.Printf("warning: %v", err)
break
}

for _, serviceArn := range res.ServiceArns {
// ecs service arn format
// arn:${Partition}:ecs:${Region}:${Account}:service/${ClusterName}/${ServiceName}
//
// tf import: cluster-name/service-name
tfId := strings.SplitN(serviceArn, ":service/", 2)[1]
resources = append(resources, common.Resource{Arn: serviceArn, TerraformID: tfId, ResourceType: common.ECS_service, Region: rc.Region})
}

if res.NextToken == nil {
break
}
nextToken = res.NextToken
}
}

if clusterRes.NextToken == nil {
break
}
clusterNextToken = clusterRes.NextToken
}
}
return resources
}
118 changes: 118 additions & 0 deletions aws/ecs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package aws_test

import (
"context"
"fmt"
"testing"

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

func getMockedECSService(mock *mockECSClient) aws.NoclickopsECSService {
return aws.NoclickopsECSService{
Clients: []aws.NoclickopsECSClient{
{
Client: mock,
ClientMeta: aws.ClientMeta{Region: "eu-west-1"},
},
},
ServiceMeta: common.ServiceMeta{Global: false, ServiceName: "ecs"},
}
}

func TestGetECSClustersAndServices_PaginationFollowed(t *testing.T) {
listClustersCallCount := 0
listServicesCallCount := 0
mock := &mockECSClient{
listClustersFn: func(_ context.Context, params *ecs.ListClustersInput, _ ...func(*ecs.Options)) (*ecs.ListClustersOutput, error) {
listClustersCallCount++
if listClustersCallCount == 1 {
return &ecs.ListClustersOutput{
NextToken: ptr("next-cluster"),
ClusterArns: []string{"arn:aws:ecs:eu-west-1:123456789012:cluster/cluster-1"},
}, nil
}
if params.NextToken == nil || *params.NextToken != "next-cluster" {
return nil, fmt.Errorf("wrong NextToken, expected 'next-cluster' got '%v'", params.NextToken)
}
return &ecs.ListClustersOutput{
ClusterArns: []string{"arn:aws:ecs:eu-west-1:123456789012:cluster/cluster-2"},
}, nil
},
listServicesFn: func(_ context.Context, params *ecs.ListServicesInput, _ ...func(*ecs.Options)) (*ecs.ListServicesOutput, error) {
listServicesCallCount++
if listServicesCallCount == 1 {
return &ecs.ListServicesOutput{
NextToken: ptr("next-service"),
ServiceArns: []string{"arn:aws:ecs:eu-west-1:123456789012:service/cluster-1/service-1"},
}, nil
}
if listServicesCallCount == 2 {
if params.NextToken == nil || *params.NextToken != "next-service" {
return nil, fmt.Errorf("wrong NextToken, expected 'next-service' got '%v'", params.NextToken)
}
return &ecs.ListServicesOutput{
ServiceArns: []string{"arn:aws:ecs:eu-west-1:123456789012:service/cluster-1/service-2"},
}, nil
}
return &ecs.ListServicesOutput{
ServiceArns: []string{"arn:aws:ecs:eu-west-1:123456789012:service/cluster-2/service-3"},
}, nil
},
}
service := getMockedECSService(mock)
got := service.GetClustersAndServices()
expected := []common.Resource{
{Arn: "arn:aws:ecs:eu-west-1:123456789012:cluster/cluster-1", TerraformID: "cluster-1", ResourceType: common.ECS_cluster, Region: "eu-west-1"},
{Arn: "arn:aws:ecs:eu-west-1:123456789012:service/cluster-1/service-1", TerraformID: "cluster-1/service-1", ResourceType: common.ECS_service, Region: "eu-west-1"},
{Arn: "arn:aws:ecs:eu-west-1:123456789012:service/cluster-1/service-2", TerraformID: "cluster-1/service-2", ResourceType: common.ECS_service, Region: "eu-west-1"},
{Arn: "arn:aws:ecs:eu-west-1:123456789012:cluster/cluster-2", TerraformID: "cluster-2", ResourceType: common.ECS_cluster, Region: "eu-west-1"},
{Arn: "arn:aws:ecs:eu-west-1:123456789012:service/cluster-2/service-3", TerraformID: "cluster-2/service-3", ResourceType: common.ECS_service, Region: "eu-west-1"},
}
if diff := cmp.Diff(got, expected); diff != "" {
t.Errorf("expected %v, got %v", expected, got)
}
if listClustersCallCount != 2 {
t.Errorf("expected 2 calls to ListClusters, got %d", listClustersCallCount)
}
if listServicesCallCount != 3 {
t.Errorf("expected 3 calls to ListServices, got %d", listServicesCallCount)
}
}

func TestGetECSClustersAndServices_ListClustersErrorIsSkipped(t *testing.T) {
mock := &mockECSClient{
listClustersFn: func(_ context.Context, _ *ecs.ListClustersInput, _ ...func(*ecs.Options)) (*ecs.ListClustersOutput, error) {
return nil, fmt.Errorf("some error")
},
}
service := getMockedECSService(mock)
got := service.GetClustersAndServices()
if len(got) != 0 {
t.Errorf("expected no resources on error, got %v", got)
}
}

func TestGetECSClustersAndServices_ListServicesErrorIsSkipped(t *testing.T) {
mock := &mockECSClient{
listClustersFn: func(_ context.Context, _ *ecs.ListClustersInput, _ ...func(*ecs.Options)) (*ecs.ListClustersOutput, error) {
return &ecs.ListClustersOutput{
ClusterArns: []string{"arn:aws:ecs:eu-west-1:123456789012:cluster/cluster-1"},
}, nil
},
listServicesFn: func(_ context.Context, _ *ecs.ListServicesInput, _ ...func(*ecs.Options)) (*ecs.ListServicesOutput, error) {
return nil, fmt.Errorf("some error")
},
}
service := getMockedECSService(mock)
got := service.GetClustersAndServices()
expected := []common.Resource{
{Arn: "arn:aws:ecs:eu-west-1:123456789012:cluster/cluster-1", TerraformID: "cluster-1", ResourceType: common.ECS_cluster, Region: "eu-west-1"},
}
if diff := cmp.Diff(got, expected); diff != "" {
t.Errorf("expected %v, got %v", expected, got)
}
}
14 changes: 14 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/ecs"
"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"
Expand Down Expand Up @@ -216,6 +217,19 @@ func (m *mockDynamodbClient) ListTables(ctx context.Context, params *dynamodb.Li
return m.listTablesFn(ctx, params, optFns...)
}

type mockECSClient struct {
listClustersFn func(ctx context.Context, params *ecs.ListClustersInput, optFns ...func(*ecs.Options)) (*ecs.ListClustersOutput, error)
listServicesFn func(ctx context.Context, params *ecs.ListServicesInput, optFns ...func(*ecs.Options)) (*ecs.ListServicesOutput, error)
}

func (m *mockECSClient) ListClusters(ctx context.Context, params *ecs.ListClustersInput, optFns ...func(*ecs.Options)) (*ecs.ListClustersOutput, error) {
return m.listClustersFn(ctx, params, optFns...)
}

func (m *mockECSClient) ListServices(ctx context.Context, params *ecs.ListServicesInput, optFns ...func(*ecs.Options)) (*ecs.ListServicesOutput, error) {
return m.listServicesFn(ctx, params, optFns...)
}

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

// not included in the switch/case in `NewclickopsServiceFromConfigs`
// because it doesn't follow the same invocation pattern.
Expand Down Expand Up @@ -103,6 +104,9 @@ func NewNoclickopsServiceFromConfigs(service common.AWSServiceName, configs []aw
case common.Elasticache:
c := NewElasticacheServiceFromConfigs(configs, meta)
return &c
case common.ECS:
c := NewECSServiceFromConfigs(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.

6 changes: 4 additions & 2 deletions common/resourcetype_string.go

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

3 changes: 3 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
Cloudwatchlogs_log_group
Dynamodb_table
Elasticache_cluster
ECS_cluster
ECS_service
)

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

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 @@ -11,6 +11,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.74.0
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/ecs v1.81.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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/aws/aws-sdk-go-v2/service/dynamodb v1.57.4 h1:0E3bfw1Va3vfCrmtATvKRnG
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.57.4/go.mod h1:dFPU89qDDGgQbXyzQ5ZY6zcjjKPVW+1M63axOw887JE=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.299.0 h1:qTozRFl2YFFU2HJGl7ZAywlRQvBnAN591gbAFT5bE0s=
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/ecs v1.81.0 h1:2Sp9EwK7giQpJnQ54k0zdUh6aykmmbpEurEEygr104c=
github.com/aws/aws-sdk-go-v2/service/ecs v1.81.0/go.mod h1:TIKZ9zIFS6W2k9FeW+r5sGVnlxp+aUt9oQ/St3Suj1o=
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=
Expand Down