Skip to content

Commit

Permalink
resource stack: support destroying stacks
Browse files Browse the repository at this point in the history
With this implementation someone could add Destroy to a stack, then
deploy and the stack would be destroyed in all accounts.
  • Loading branch information
dschofie committed Aug 15, 2024
1 parent 8b9b15d commit 214d616
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions resource/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Stack struct {

CloudformationParameters []string `yaml:"CloudformationParameters,omitempty"`
CloudformationCapabilities []string `yaml:"CloudformationCapabilities,omitempty"`

Destroy bool `yaml:"Destroy,omitempty"`
}

func (s Stack) NewForRegion(region string) Stack {
Expand Down
4 changes: 4 additions & 0 deletions resourceoperation/cdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (co *cdkOperation) Call(ctx context.Context) error {
}
} else if co.Operation == Deploy {
cdkArgs = []string{"deploy", "--require-approval", "never"}

if co.Stack.Destroy {
cdkArgs = []string{"destroy", "--require-approval", "never"}
}
}

cdkArgs = append(cdkArgs, cdkDefaultArgs(*co.Account, co.Stack)...)
Expand Down
18 changes: 18 additions & 0 deletions resourceoperation/cloudformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func (co *cloudformationOp) Call(ctx context.Context) error {
return nil
}

if co.Stack.Destroy {
err := co.DeleteStack(ctx)
if err != nil {
return oops.Wrapf(err, "DeleteStack")
}
return nil
}

_, err = co.executeChangeSet(ctx, cs.ChangeSetId)
if err != nil {
return oops.Wrapf(err, "executing change set")
Expand Down Expand Up @@ -202,6 +210,16 @@ func (co *cloudformationOp) executeChangeSet(ctx context.Context, changeSetID *s
}
}

func (co *cloudformationOp) DeleteStack(ctx context.Context) error {
_, err := co.CloudformationClient.DeleteStackWithContext(ctx, &cloudformation.DeleteStackInput{
StackName: co.Stack.CloudformationStackName(),
})
if err != nil {
return oops.Wrapf(err, "DeleteStackWithContext stack: %s", *co.Stack.CloudformationStackName())
}
return nil
}

func (co *cloudformationOp) ToString() string {
return ""
}
4 changes: 4 additions & 0 deletions resourceoperation/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (so *scpOperation) Call(ctx context.Context) error {
args = []string{
"apply", "-auto-approve",
}

if so.Stack.Destroy {
args = append(args, "-destroy")
}
}

workingPath := so.tmpPath()
Expand Down
4 changes: 4 additions & 0 deletions resourceoperation/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (to *tfOperation) Call(ctx context.Context) error {
}
}

if to.Stack.Destroy {
args = append(args, "-destroy")
}

workingPath := terraform.TmpPath(*to.Account, to.Stack.Path)
cmd := exec.Command(localstack.TfCmd(), args...)
cmd.Dir = workingPath
Expand Down

0 comments on commit 214d616

Please sign in to comment.