constellation/cli/internal/cloudcmd/terminate.go

77 lines
2.1 KiB
Go
Raw Normal View History

2022-04-13 07:01:38 -04:00
package cloudcmd
import (
"context"
"fmt"
2022-06-07 10:30:41 -04:00
azurecl "github.com/edgelesssys/constellation/cli/internal/azure/client"
2022-06-07 08:52:47 -04:00
gcpcl "github.com/edgelesssys/constellation/cli/internal/gcp/client"
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
2022-04-13 07:01:38 -04:00
"github.com/edgelesssys/constellation/internal/state"
)
// Terminator deletes cloud provider resources.
type Terminator struct {
newGCPClient func(ctx context.Context) (gcpclient, error)
newAzureClient func(subscriptionID, tenantID string) (azureclient, error)
}
// NewTerminator create a new cloud terminator.
func NewTerminator() *Terminator {
return &Terminator{
newGCPClient: func(ctx context.Context) (gcpclient, error) {
return gcpcl.NewFromDefault(ctx)
},
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.GCP:
cl, err := t.newGCPClient(ctx)
if err != nil {
return err
}
defer cl.Close()
return t.terminateGCP(ctx, cl, state)
case cloudprovider.Azure:
cl, err := t.newAzureClient(state.AzureSubscription, state.AzureTenant)
if err != nil {
return err
}
return t.terminateAzure(ctx, cl, state)
default:
return fmt.Errorf("unsupported provider: %s", provider)
}
}
func (t *Terminator) terminateGCP(ctx context.Context, cl gcpclient, state state.ConstellationState) error {
2022-08-01 06:35:35 -04:00
cl.SetState(state)
2022-04-13 07:01:38 -04:00
2022-08-01 10:51:34 -04:00
if err := cl.TerminateLoadBalancers(ctx); err != nil {
2022-06-09 16:26:36 -04:00
return err
}
2022-04-13 07:01:38 -04:00
if err := cl.TerminateInstances(ctx); err != nil {
return err
}
if err := cl.TerminateFirewall(ctx); err != nil {
return err
}
if err := cl.TerminateVPCs(ctx); err != nil {
return err
}
2022-08-23 11:49:55 -04:00
return nil
2022-04-13 07:01:38 -04:00
}
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
}