Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource stack: support destroying stacks #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading