From 736bbd4042c015daf4cc6e61e868ab7faf0491fb Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Fri, 25 Sep 2020 13:07:23 +0200 Subject: [PATCH] cli/cmd: make clusterExists return error To remove a dependency on log.Entry to make moving this code around easier and to avoid hiding function complexity from logger. Also, it no longer requires logger, so argument has been removed. Part of #630 Signed-off-by: Mateusz Gozdek --- cli/cmd/cluster-apply.go | 6 +++++- cli/cmd/cluster-destroy.go | 7 ++++++- cli/cmd/cluster.go | 6 +++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cli/cmd/cluster-apply.go b/cli/cmd/cluster-apply.go index d2e8d6f44..5ddfaee3a 100644 --- a/cli/cmd/cluster-apply.go +++ b/cli/cmd/cluster-apply.go @@ -69,7 +69,11 @@ func clusterApply(contextLogger *log.Entry) error { return fmt.Errorf("initializing: %w", err) } - exists := clusterExists(contextLogger, &c.terraformExecutor) + exists, err := clusterExists(c.terraformExecutor) + if err != nil { + return fmt.Errorf("checking if cluster exists: %w", err) + } + if exists && !confirm { // TODO: We could plan to a file and use it when installing. if err := c.terraformExecutor.Plan(); err != nil { diff --git a/cli/cmd/cluster-destroy.go b/cli/cmd/cluster-destroy.go index ab355e499..854d77942 100644 --- a/cli/cmd/cluster-destroy.go +++ b/cli/cmd/cluster-destroy.go @@ -53,7 +53,12 @@ func clusterDestroy(contextLogger *log.Entry) error { return fmt.Errorf("initializing: %w", err) } - if !clusterExists(contextLogger, &c.terraformExecutor) { + exists, err := clusterExists(c.terraformExecutor) + if err != nil { + return fmt.Errorf("checking if cluster exists: %w", err) + } + + if !exists { contextLogger.Println("Cluster already destroyed, nothing to do") return nil diff --git a/cli/cmd/cluster.go b/cli/cmd/cluster.go index c1f684d3d..769ae244c 100644 --- a/cli/cmd/cluster.go +++ b/cli/cmd/cluster.go @@ -148,14 +148,14 @@ func initializeTerraform(p platform.Platform, b backend.Backend) (*terraform.Exe // clusterExists determines if cluster has already been created by getting all // outputs from the Terraform. If there is any output defined, it means 'terraform apply' // run at least once. -func clusterExists(contextLogger *log.Entry, ex *terraform.Executor) bool { +func clusterExists(ex terraform.Executor) (bool, error) { o := map[string]interface{}{} if err := ex.Output("", &o); err != nil { - contextLogger.Fatalf("Failed to check if cluster exists: %v", err) + return false, fmt.Errorf("getting Terraform output: %w", err) } - return len(o) != 0 + return len(o) != 0, nil } type controlplaneUpdater struct {