Skip to content

Commit

Permalink
Add delete workspace support (#15)
Browse files Browse the repository at this point in the history
Adds the ability to delete a workspace.
  • Loading branch information
rgreinho committed Mar 30, 2020
1 parent 0159d39 commit 3fefc1a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
### Added

* Ability to detect HCL variables from a varfile. [#14]
* Ability to delete workspaces. [#15]

## [1.0.0] - 2020-03-25

Expand All @@ -23,3 +24,4 @@ Initial version with support for managing:

[//]: # (Issue/PR links)
[#14]: https://github.com/rgreinho/tfe-cli/pull/14
[#15]: https://github.com/rgreinho/tfe-cli/pull/15
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ Create a new workspace:
tfe-cli workspace create my-new-workspace
```

Setup the VCS Repository:

```bash
tfe-cli workspace create my-new-workspace --vcsrepository ot-8Xc1NTYpjIQZIwIh:organization/repository:master
```

The format of the VCS option is string of colon sperated values: `<OAuthTokenID>:<repository>:<branch>`.

### Variables

Manage variables for a workspace.
Expand Down
39 changes: 35 additions & 4 deletions cmd/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"github.com/spf13/cobra"
)

// workspaceCmd represents the workspace command
// workspaceCmd represents the workspace command.
var workspaceCmd = &cobra.Command{
Use: "workspace",
Short: "Manage TFE workspaces",
Long: `Manage TFE workspaces.`,
}

// createCmd represents the create command
// createCmd represents the create command.
var workspaceCreateCmd = &cobra.Command{
Use: "create [WORKSPACE]",
Short: "Create a TFE workspace",
Expand Down Expand Up @@ -101,6 +101,29 @@ var workspaceCreateCmd = &cobra.Command{
},
}

// workspaceDeleteCmd represents the delete command.
var workspaceDeleteCmd = &cobra.Command{
Use: "delete [WORKSPACE]",
Short: "Delete a TFE workspace",
Long: `Delete a TFE workspace.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Setup the command.
organization, client, err := setup(cmd)
if err != nil {
log.Fatalf("Cannot execute the command: %s.", err)
}

// Read the flags.
name := args[0]

// Delete the workspace.
if err := deleteWorkspace(client, organization, name); err != nil {
log.Fatalf("Cannot delete workspace %q: %s.", name, err)
}
},
}

var workspaceListCmd = &cobra.Command{
Use: "list",
Short: "List TFE workspaces",
Expand Down Expand Up @@ -128,19 +151,20 @@ var workspaceListCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(workspaceCmd)
workspaceCmd.AddCommand(workspaceCreateCmd)
workspaceCmd.AddCommand(workspaceDeleteCmd)
workspaceCmd.AddCommand(workspaceListCmd)

workspaceCreateCmd.Flags().Bool("autoapply", false, "Apply changes automatically")
workspaceCreateCmd.Flags().Bool("filetriggers", false, "Filter runs based on the changed files in a VCS push")
workspaceCreateCmd.Flags().String("terraformversion", "", "Specify the Terraform version")
workspaceCreateCmd.Flags().String("workingdirectory", "", "Specify a relative path that Terraform will execute within")
// colon sperated values: <OAuthTokenID>:<repository>:<branch>
// example: ot-8Xc1NTYpjIQZIwIh:shipstation/mercury-appstack:master
// example: ot-8Xc1NTYpjIQZIwIh:organization/repository:master
workspaceCreateCmd.Flags().String("vcsrepository", "", "Specify a workspace's VCS repository")
workspaceCreateCmd.Flags().BoolP("force", "f", false, "Update workspace if it exists")
}

func readWorkspace(client *tfe.Client, organization string, workspace string) (*tfe.Workspace, error) {
func readWorkspace(client *tfe.Client, organization, workspace string) (*tfe.Workspace, error) {
w, err := client.Workspaces.Read(context.Background(), organization, workspace)
if err != nil {
return nil, err
Expand Down Expand Up @@ -192,3 +216,10 @@ func listWorkspaces(client *tfe.Client, organization string) ([]*tfe.Workspace,

return results, nil
}

func deleteWorkspace(client *tfe.Client, organization, workspace string) error {
if err := client.Workspaces.Delete(context.Background(), organization, workspace); err != nil {
return err
}
return nil
}

0 comments on commit 3fefc1a

Please sign in to comment.