From 00269e248a3b1b4cf4afd97074fb5395829e9096 Mon Sep 17 00:00:00 2001 From: eRaMvn Date: Fri, 1 Jan 2021 00:35:18 -0800 Subject: [PATCH] updated doc and added the ability to update/delete multiple variables concurrently --- cmd/delete.go | 7 ++++--- cmd/root.go | 1 - cmd/update.go | 16 +++++++++++----- helper/main.go | 41 ++++++++++++++++++++++++++++------------- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index a75f6fe..dc0934f 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -45,8 +45,9 @@ tfc-help delete -a -w ws-K33Rp -o big-corp`, workspaceID := helper.GetWorkspaceID(organizationName, workspaceName) if allVar { + emptyID := "" // Delete all variables in the workspace - helper.DeleteVariable(workspaceID, "", allVar) + helper.DeleteVariables(workspaceID, emptyID, allVar) } else { valueToSend := helper.GetCommandValues(keyPairs) // Loop through all values passed from the command line @@ -56,11 +57,11 @@ tfc-help delete -a -w ws-K33Rp -o big-corp`, /* If error, meaning the variable does not exist, print out error message */ if error != nil { - fmt.Println("Variable does not exist. Please use tfc-help delete command to delete the variable!") + fmt.Println("Variable does not exist. Cannot delete the variable!") os.Exit(1) // When variable already exists, proceed to delete the variable } else { - helper.DeleteVariable(workspaceID, variable.ID, false) + helper.DeleteVariables(workspaceID, variable.ID, false) } } } diff --git a/cmd/root.go b/cmd/root.go index fc69a2b..9f5c627 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -44,7 +44,6 @@ var rootCmd = &cobra.Command{ credentials for the provider in order to run. tfc-help was created to help automate that process. Example: -tfc-help create --var some_variable=some_value -w ws-K33Rp -o big-corp tfc-help update --var some_variable=some_value -d "This variable is just an example" -w ws-K33Rp -o big-corp tfc-help delete --var some_variable -w ws-K33Rp -o big-corp diff --git a/cmd/update.go b/cmd/update.go index 966bcdb..904d947 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "sync" "tfc-helper/helper" "github.com/hashicorp/go-tfe" @@ -94,8 +95,12 @@ tfc-help update --env -w ws-K33Rp -o big-corp`, valueToSend = helper.GetCommandValues(keyPairs) } + var wg sync.WaitGroup + // Loop through all values passed from the command line for newVariableName, newVariableValue := range valueToSend { + wg.Add(1) + // Try to get the variable ID variable, error := helper.GetVar(workspaceID, newVariableName) @@ -113,7 +118,7 @@ tfc-help update --env -w ws-K33Rp -o big-corp`, Sensitive: sensitive, } - helper.CreateVariable(workspaceID, newVariable) + go helper.CreateVariable(workspaceID, newVariable, &wg) // When variable already exists, proceed to update the variable } else { // originalVariable keeps the value and description given to a variable but changes the hcl and encryption type @@ -141,9 +146,9 @@ tfc-help update --env -w ws-K33Rp -o big-corp`, // If -r flag is set, proceed to recreate the variable if shouldReplace { if keepValue { - helper.RecreateVariable(workspaceID, originalVariable) + go helper.RecreateVariable(workspaceID, originalVariable, &wg) } else { - helper.RecreateVariable(workspaceID, newVariable) + go helper.RecreateVariable(workspaceID, newVariable, &wg) } } else { @@ -161,14 +166,15 @@ Example commands: // If -k flag is set, keep the original value of the variable if keepValue { - helper.UpdateVariable(workspaceID, originalVariable) + go helper.UpdateVariable(workspaceID, originalVariable, &wg) } else { // TODO: Create a way to keep the description the same but the value can be different - helper.UpdateVariable(workspaceID, newVariable) + go helper.UpdateVariable(workspaceID, newVariable, &wg) } } } } + wg.Wait() }, } diff --git a/helper/main.go b/helper/main.go index b79f2c7..bb7d26f 100644 --- a/helper/main.go +++ b/helper/main.go @@ -8,6 +8,7 @@ import ( "log" "os" "strings" + "sync" "github.com/hashicorp/go-tfe" "github.com/hashicorp/hcl/v2/hclsimple" @@ -120,7 +121,9 @@ func GetVar(workspaceID string, varName string) (variable *tfe.Variable, err err } // CreateVariable creates a variable -func CreateVariable(workspaceID string, newVariable NewVariable) { +func CreateVariable(workspaceID string, newVariable NewVariable, wg *sync.WaitGroup) { + defer wg.Done() + _, err := client.Variables.Create(ctx, workspaceID, tfe.VariableCreateOptions{ Key: tfe.String(newVariable.Key), Value: tfe.String(newVariable.Value), @@ -135,7 +138,9 @@ func CreateVariable(workspaceID string, newVariable NewVariable) { } // UpdateVariable updates a variable given the variable id -func UpdateVariable(workspaceID string, newVariable NewVariable) { +func UpdateVariable(workspaceID string, newVariable NewVariable, wg *sync.WaitGroup) { + defer wg.Done() + _, err := client.Variables.Update(ctx, workspaceID, newVariable.ID, tfe.VariableUpdateOptions{ Value: tfe.String(newVariable.Value), Description: tfe.String(newVariable.Description), @@ -148,26 +153,36 @@ func UpdateVariable(workspaceID string, newVariable NewVariable) { } } -// DeleteVariable deletes a variable or all variables -func DeleteVariable(workspaceID string, variableID string, all bool) { +// DeleteVar deletes a single variable +func DeleteVar(workspaceID string, variableID string, wg *sync.WaitGroup) { + defer wg.Done() + message := fmt.Sprintf("Error deleting variable id %s. Please try again!", variableID) + if err := client.Variables.Delete(ctx, workspaceID, variableID); err != nil { + fmt.Println(message) + } +} + +// DeleteVariables deletes a variable or all variables +func DeleteVariables(workspaceID string, variableID string, all bool) { + var wg sync.WaitGroup + if all { for _, variable := range ListAllVariables(workspaceID) { - if err := client.Variables.Delete(ctx, workspaceID, variable.ID); err != nil { - fmt.Println("Error deleting variable. Please try again!") - } + wg.Add(1) + go DeleteVar(workspaceID, variable.ID, &wg) } return } - if err := client.Variables.Delete(ctx, workspaceID, variableID); err != nil { - fmt.Println("Error deleting variable. Please try again!") - } + wg.Add(1) + go DeleteVar(workspaceID, variableID, &wg) + wg.Wait() } // RecreateVariable deletes a variable and create it again -func RecreateVariable(workspaceID string, variable NewVariable) { - DeleteVariable(workspaceID, variable.ID, false) - CreateVariable(workspaceID, variable) +func RecreateVariable(workspaceID string, variable NewVariable, wg *sync.WaitGroup) { + DeleteVariables(workspaceID, variable.ID, false) + CreateVariable(workspaceID, variable, wg) } func init() {