Skip to content

Commit

Permalink
updated doc and added the ability to update/delete multiple variables…
Browse files Browse the repository at this point in the history
… concurrently
  • Loading branch information
eRaMvn committed Jan 1, 2021
1 parent 690646b commit 00269e2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
7 changes: 4 additions & 3 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"sync"
"tfc-helper/helper"

"github.com/hashicorp/go-tfe"
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
},
}

Expand Down
41 changes: 28 additions & 13 deletions helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"strings"
"sync"

"github.com/hashicorp/go-tfe"
"github.com/hashicorp/hcl/v2/hclsimple"
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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() {
Expand Down

0 comments on commit 00269e2

Please sign in to comment.