Skip to content

Commit

Permalink
New CLI command to be able to remove variables configured on the work…
Browse files Browse the repository at this point in the history
…space: workspace delete-variables
  • Loading branch information
mvisonneau committed Apr 14, 2020
1 parent 70fa619 commit 962183b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [0ver](https://0ver.org).

### Added

- New CLI command to be able to remove variables configured on the workspace: `workspace delete-variables`
- A couple commands to enable/disable the operations on the workspace from the CLI
- Refactored the CLI

Expand Down
9 changes: 9 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ func NewApp(version string, start time.Time) (app *cli.App) {
Action: cmd.ExecWrapper(cmd.WorkspaceConfigure),
Flags: cli.FlagsByName{dryRun},
},
{
Name: "delete-variables",
Usage: "remove configured workspace variables (default: scoped to variables defined in the config file)",
Action: cmd.ExecWrapper(cmd.WorkspaceDeleteVariables),
Flags: cli.FlagsByName{cli.BoolFlag{
Name: "all, a",
Usage: "delete all variables",
}},
},
{
Name: "status",
Usage: "return the status of the workspace",
Expand Down
25 changes: 25 additions & 0 deletions cmd/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,28 @@ func WorkspaceDisableOperations(ctx *cli.Context) (int, error) {

return 0, nil
}

// WorkspaceDeleteVariables removes managed or all variables on TFC
func WorkspaceDeleteVariables(ctx *cli.Context) (int, error) {
c, cfg, err := configure(ctx)
if err != nil {
return 1, err
}

w, err := c.GetWorkspace(cfg.Runtime.TFC.Organization, cfg.Runtime.TFC.Workspace)
if err != nil {
return 1, err
}

if ctx.Bool("all") {
if err = c.DeleteAllWorkspaceVariables(w); err != nil {
return 1, err
}
} else {
if err = c.DeleteWorkspaceVariables(w, cfg.GetVariables()); err != nil {
return 1, err
}
}

return 0, nil
}
36 changes: 36 additions & 0 deletions lib/client/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,42 @@ func (c *Client) SetWorkspaceOperations(w *tfc.Workspace, operations bool) (err
return
}

// DeleteAllWorkspaceVariables delete the all the variables present on the workspace
func (c *Client) DeleteAllWorkspaceVariables(w *tfc.Workspace) (err error) {
existingVariables, _, _, err := c.listVariables(w)

for _, vars := range existingVariables {
for _, v := range vars {
if err = c.TFC.Variables.Delete(c.Context, w.ID, v.ID); err != nil {
return err
}
log.Infof("deleted variable %s", v.Key)
}
}

return
}

// DeleteWorkspaceVariables delete the variables list passed as an argument if they are present
// on the workspace
func (c *Client) DeleteWorkspaceVariables(w *tfc.Workspace, variables schemas.Variables) (err error) {
existingVariables, _, _, err := c.listVariables(w)

for _, v := range variables {
kind := getCategoryType(v.Kind)
if _, ok := existingVariables[kind]; ok {
if _, ok := existingVariables[kind][v.Name]; ok {
if err = c.TFC.Variables.Delete(c.Context, w.ID, existingVariables[kind][v.Name].ID); err != nil {
return err
}
log.Infof("deleted variable %s", v.Name)
}
}
}

return
}

func (c *Client) updateSSHKey(w *tfc.Workspace, sshKeyName string) error {
if sshKeyName == "-" {
log.Infof("Removing currently configured SSH key")
Expand Down

0 comments on commit 962183b

Please sign in to comment.