-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
ecr.go
73 lines (59 loc) · 1.9 KB
/
ecr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package resources
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/go-commons/errors"
)
func (registry *ECR) getAll(c context.Context, configObj config.Config) ([]*string, error) {
repositoryNames := []*string{}
paginator := func(output *ecr.DescribeRepositoriesOutput, lastPage bool) bool {
for _, repository := range output.Repositories {
if configObj.ECRRepository.ShouldInclude(config.ResourceValue{
Time: repository.CreatedAt,
Name: repository.RepositoryName,
}) {
repositoryNames = append(repositoryNames, repository.RepositoryName)
}
}
return !lastPage
}
param := &ecr.DescribeRepositoriesInput{}
err := registry.Client.DescribeRepositoriesPages(param, paginator)
if err != nil {
return nil, errors.WithStackTrace(err)
}
return repositoryNames, nil
}
func (registry *ECR) nukeAll(repositoryNames []string) error {
if len(repositoryNames) == 0 {
logging.Debugf("No ECR repositories to nuke in region %s", registry.Region)
return nil
}
var deletedNames []*string
for _, repositoryName := range repositoryNames {
params := &ecr.DeleteRepositoryInput{
Force: aws.Bool(true),
RepositoryName: aws.String(repositoryName),
}
_, err := registry.Client.DeleteRepository(params)
// Record status of this resource
e := report.Entry{
Identifier: repositoryName,
ResourceType: "ECR Repository",
Error: err,
}
report.Record(e)
if err != nil {
logging.Debugf("[Failed] %s", err)
} else {
deletedNames = append(deletedNames, aws.String(repositoryName))
logging.Debugf("Deleted ECR Repository: %s", repositoryName)
}
}
logging.Debugf("[OK] %d ECR Repositories deleted in %s", len(deletedNames), registry.Region)
return nil
}