Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
client: add synchronous destroy operation for short-living destroy cases
Browse files Browse the repository at this point in the history
Signed-off-by: Muvaffak Onus <me@muvaf.com>
  • Loading branch information
muvaf committed Sep 29, 2021
1 parent 300b3e5 commit 0532e1f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
54 changes: 27 additions & 27 deletions pkg/client/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ import (
"github.com/crossplane-contrib/terrajet/pkg/json"
)

// ProviderRequirement holds values for the Terraform HCL setup requirements
type ProviderRequirement struct {
Source string
Version string
}

// ProviderConfiguration holds the setup configuration body
type ProviderConfiguration map[string]interface{}

// TerraformSetup holds values for the Terraform version and setup
// requirements and configuration body
type TerraformSetup struct {
Version string
Requirement ProviderRequirement
Configuration ProviderConfiguration
}

func (p TerraformSetup) validate() error {
if p.Version == "" {
return errors.New(fmtErrValidationVersion)
}
if p.Requirement.Source == "" || p.Requirement.Version == "" {
return errors.Errorf(fmtErrValidationProvider, p.Requirement.Source, p.Requirement.Version)
}
return nil
}

func NewWorkspaceStore(setup TerraformSetup) *WorkspaceStore {
return &WorkspaceStore{setup: setup}
}
Expand Down Expand Up @@ -106,30 +133,3 @@ func (ws *WorkspaceStore) Remove(obj xpresource.Object) error {
ws.store.Delete(obj.GetUID())
return nil
}

// ProviderRequirement holds values for the Terraform HCL setup requirements
type ProviderRequirement struct {
Source string
Version string
}

// ProviderConfiguration holds the setup configuration body
type ProviderConfiguration map[string]interface{}

// TerraformSetup holds values for the Terraform version and setup
// requirements and configuration body
type TerraformSetup struct {
Version string
Requirement ProviderRequirement
Configuration ProviderConfiguration
}

func (p TerraformSetup) validate() error {
if p.Version == "" {
return errors.New(fmtErrValidationVersion)
}
if p.Requirement.Source == "" || p.Requirement.Version == "" {
return errors.Errorf(fmtErrValidationProvider, p.Requirement.Source, p.Requirement.Version)
}
return nil
}
20 changes: 19 additions & 1 deletion pkg/client/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (w *Workspace) Apply(ctx context.Context) (ApplyResult, error) {
return ApplyResult{State: s}, nil
}

func (w *Workspace) Destroy(_ context.Context) error {
func (w *Workspace) DestroyAsync(_ context.Context) error {
switch {
// Destroy call is idempotent and can be called repeatedly.
case w.LastOperation.Type == "destroy":
Expand Down Expand Up @@ -159,6 +159,24 @@ func (w *Workspace) Destroy(_ context.Context) error {
return nil
}

func (w *Workspace) Destroy(ctx context.Context) error {
if w.LastOperation.EndTime == nil {
return errors.Errorf("%s operation that started at %s is still running", w.LastOperation.Type, w.LastOperation.StartTime.String())
}
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd := exec.CommandContext(ctx, "terraform", "destroy", "-auto-approve", "-input=false", "-json")
cmd.Dir = w.dir
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
w.LastOperation.Err = errors.Wrapf(err, "cannot destroy: %s", stderr.String())
}
w.logger.Debug("destroy completed", "stdout", stdout.String())
w.logger.Debug("destroy completed", "stderr", stderr.String())
return nil
}

type RefreshResult struct {
IsApplying bool
IsDestroying bool
Expand Down

0 comments on commit 0532e1f

Please sign in to comment.