constellation/cli/internal/cloudcmd/terminate.go

74 lines
2.1 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"
"fmt"
2022-09-21 07:47:57 -04:00
azurecl "github.com/edgelesssys/constellation/v2/cli/internal/azure/client"
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/state"
2022-04-13 07:01:38 -04:00
)
// Terminator deletes cloud provider resources.
type Terminator struct {
2022-09-27 03:22:29 -04:00
newTerraformClient func(ctx context.Context) (terraformClient, error)
newAzureClient func(subscriptionID, tenantID string) (azureclient, error)
2022-04-13 07:01:38 -04:00
}
// NewTerminator create a new cloud terminator.
func NewTerminator() *Terminator {
return &Terminator{
2022-09-27 03:22:29 -04:00
newTerraformClient: func(ctx context.Context) (terraformClient, error) {
return terraform.New(ctx, cloudprovider.GCP)
2022-04-13 07:01:38 -04:00
},
newAzureClient: func(subscriptionID, tenantID string) (azureclient, error) {
return azurecl.NewFromDefault(subscriptionID, tenantID)
},
}
}
// Terminate deletes the could provider resources defined in the constellation state.
func (t *Terminator) Terminate(ctx context.Context, state state.ConstellationState) error {
provider := cloudprovider.FromString(state.CloudProvider)
switch provider {
case cloudprovider.Azure:
cl, err := t.newAzureClient(state.AzureSubscription, state.AzureTenant)
if err != nil {
return err
}
return t.terminateAzure(ctx, cl, state)
2022-09-27 03:22:29 -04:00
case cloudprovider.GCP:
fallthrough
case cloudprovider.QEMU:
2022-09-27 03:22:29 -04:00
cl, err := t.newTerraformClient(ctx)
if err != nil {
return err
}
defer cl.RemoveInstaller()
2022-09-27 03:22:29 -04:00
return t.terminateTerraform(ctx, cl)
2022-04-13 07:01:38 -04:00
default:
return fmt.Errorf("unsupported provider: %s", provider)
}
}
func (t *Terminator) terminateAzure(ctx context.Context, cl azureclient, state state.ConstellationState) error {
2022-08-01 06:35:35 -04:00
cl.SetState(state)
2022-04-13 07:01:38 -04:00
return cl.TerminateResourceGroupResources(ctx)
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()
}