From 0532e1f1c8815b1c2990aca63e2c479e02ed6131 Mon Sep 17 00:00:00 2001 From: Muvaffak Onus Date: Wed, 29 Sep 2021 23:35:28 +0300 Subject: [PATCH] client: add synchronous destroy operation for short-living destroy cases Signed-off-by: Muvaffak Onus --- pkg/client/store.go | 54 ++++++++++++++++++++--------------------- pkg/client/workspace.go | 20 ++++++++++++++- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/pkg/client/store.go b/pkg/client/store.go index 07296e48..c9b8333b 100644 --- a/pkg/client/store.go +++ b/pkg/client/store.go @@ -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} } @@ -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 -} diff --git a/pkg/client/workspace.go b/pkg/client/workspace.go index ed303ddb..18f5f6ee 100644 --- a/pkg/client/workspace.go +++ b/pkg/client/workspace.go @@ -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": @@ -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