constellation/cli/internal/cloudcmd/terminate.go

66 lines
1.6 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"
2022-10-11 06:24:33 -04:00
"errors"
2022-04-13 07:01:38 -04:00
"github.com/edgelesssys/constellation/v2/cli/internal/libvirt"
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
2022-04-13 07:01:38 -04:00
)
// Terminator deletes cloud provider resources.
type Terminator struct {
2022-10-06 05:52:19 -04:00
newTerraformClient func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error)
newLibvirtRunner func() libvirtRunner
2022-04-13 07:01:38 -04:00
}
// NewTerminator create a new cloud terminator.
func NewTerminator() *Terminator {
return &Terminator{
2022-10-06 05:52:19 -04:00
newTerraformClient: func(ctx context.Context, provider cloudprovider.Provider) (terraformClient, error) {
return terraform.New(ctx, provider)
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 defined in the constellation state.
2022-10-11 06:24:33 -04:00
func (t *Terminator) Terminate(ctx context.Context, provider cloudprovider.Provider) (retErr error) {
2022-10-06 05:52:19 -04:00
if provider == cloudprovider.Unknown {
2022-10-11 06:24:33 -04:00
return errors.New("unknown cloud provider")
}
if provider == cloudprovider.QEMU {
libvirt := t.newLibvirtRunner()
defer func() {
if retErr == nil {
retErr = libvirt.Stop(ctx)
}
}()
2022-10-06 05:52:19 -04:00
}
cl, err := t.newTerraformClient(ctx, provider)
if err != nil {
return err
}
defer cl.RemoveInstaller()
return t.terminateTerraform(ctx, cl)
2022-04-13 07:01:38 -04:00
}
2022-09-27 03:22:29 -04:00
func (t *Terminator) terminateTerraform(ctx context.Context, cl terraformClient) error {
if err := cl.DestroyCluster(ctx); err != nil {
return err
}
return cl.CleanUpWorkspace()
}