constellation/cli/internal/cloudcmd/terminate.go

57 lines
1.4 KiB
Go
Raw Normal View History

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
2022-04-13 07:01:38 -04:00
package cloudcmd
import (
"context"
"github.com/edgelesssys/constellation/v2/cli/internal/libvirt"
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
2022-04-13 07:01:38 -04:00
)
// Terminator deletes cloud provider resources.
type Terminator struct {
newTerraformClient func(ctx context.Context, tfWorkspace string) (tfResourceClient, error)
newLibvirtRunner func() libvirtRunner
2022-04-13 07:01:38 -04:00
}
// NewTerminator create a new cloud terminator.
func NewTerminator() *Terminator {
return &Terminator{
newTerraformClient: func(ctx context.Context, tfWorkspace string) (tfResourceClient, error) {
return terraform.New(ctx, tfWorkspace)
2022-04-13 07:01:38 -04:00
},
newLibvirtRunner: func() libvirtRunner {
return libvirt.New()
},
2022-04-13 07:01:38 -04:00
}
}
// Terminate deletes the could provider resources.
func (t *Terminator) Terminate(ctx context.Context, tfWorkspace string, logLevel terraform.LogLevel) (retErr error) {
defer func() {
if retErr == nil {
retErr = t.newLibvirtRunner().Stop(ctx)
}
}()
cl, err := t.newTerraformClient(ctx, tfWorkspace)
2022-10-06 05:52:19 -04:00
if err != nil {
return err
}
defer cl.RemoveInstaller()
return t.terminateTerraform(ctx, cl, logLevel)
2022-04-13 07:01:38 -04:00
}
func (t *Terminator) terminateTerraform(ctx context.Context, cl tfResourceClient, logLevel terraform.LogLevel) error {
if err := cl.Destroy(ctx, logLevel); err != nil {
return err
}
return cl.CleanUpWorkspace()
}