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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
package resources
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
"github.com/rebuy-de/aws-nuke/pkg/types"
"github.com/sirupsen/logrus"
)
func init() {
register("CloudFormationType", ListCloudFormationTypes)
}
func ListCloudFormationTypes(sess *session.Session) ([]Resource, error) {
svc := cloudformation.New(sess)
params := &cloudformation.ListTypesInput{}
resources := make([]Resource, 0)
for {
resp, err := svc.ListTypes(params)
if err != nil {
return nil, err
}
for _, typeSummary := range resp.TypeSummaries {
resources = append(resources, &CloudFormationType{
svc: svc,
typeSummary: typeSummary,
})
}
if resp.NextToken == nil {
break
}
params.NextToken = resp.NextToken
}
return resources, nil
}
type CloudFormationType struct {
svc cloudformationiface.CloudFormationAPI
typeSummary *cloudformation.TypeSummary
}
func (cfs *CloudFormationType) findAllVersionSummaries() ([]*cloudformation.TypeVersionSummary, error) {
typeVersionSummaries := make([]*cloudformation.TypeVersionSummary, 0)
page := 0
params := &cloudformation.ListTypeVersionsInput{
Arn: cfs.typeSummary.TypeArn,
}
for {
logrus.Infof("CloudFormationType loading type versions arn=%s page=%d", *cfs.typeSummary.TypeArn, page)
resp, err := cfs.svc.ListTypeVersions(params)
if err != nil {
return nil, err
}
typeVersionSummaries = append(typeVersionSummaries, resp.TypeVersionSummaries...)
if resp.NextToken == nil {
return typeVersionSummaries, nil
}
params.NextToken = resp.NextToken
page = page + 1
}
}
func (cfs *CloudFormationType) Remove() error {
typeVersionSummaries, loadErr := cfs.findAllVersionSummaries()
if loadErr != nil {
return loadErr
}
failed := false
for _, typeVersionSummary := range typeVersionSummaries {
if *typeVersionSummary.IsDefaultVersion {
logrus.Infof("CloudFormationType ignoring default version type=%s version=%s", *cfs.typeSummary.TypeArn, *typeVersionSummary.VersionId)
} else {
logrus.Infof("CloudFormationType removing type=%s version=%s", *cfs.typeSummary.TypeArn, *typeVersionSummary.VersionId)
if _, err := cfs.svc.DeregisterType(&cloudformation.DeregisterTypeInput{
VersionId: typeVersionSummary.VersionId,
TypeName: typeVersionSummary.TypeName,
Type: typeVersionSummary.Type,
}); err != nil {
logrus.Errorf("CloudFormationType failed removing type=%s version=%s type=%s arn=%s error=%s", *cfs.typeSummary.TypeName, *typeVersionSummary.VersionId, *typeVersionSummary.Type, *cfs.typeSummary.TypeArn, err.Error())
failed = true
}
}
}
if failed {
return fmt.Errorf("Unable to remove all CloudFormationType versions arn=%s", *cfs.typeSummary.TypeArn)
}
_, err := cfs.svc.DeregisterType(&cloudformation.DeregisterTypeInput{
Arn: cfs.typeSummary.TypeArn,
})
return err
}
func (cfs *CloudFormationType) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Name", cfs.typeSummary.TypeName)
properties.Set("Type", cfs.typeSummary.Type)
return properties
}
func (cfs *CloudFormationType) String() string {
return *cfs.typeSummary.TypeArn
}
|