Skip to content

Commit

Permalink
feat(cmd) check for workspace and warn user if one does not exist
Browse files Browse the repository at this point in the history
decK returns an unhelpful 'Not Found' error if a workspace doesn't
exist.
It now looks up the workspace before a diff or a sync command and
notifies user if one doesn't exist.

decK is not going to automatically create a workspace for three reasons:
- workspace provisioning via an API call vs via UI has different
behavior
- decK is not responsible for managing workspaces themselves but content
inside them
- diff command is a dry-run, creating a workspace inside it is a
violation of that rule
  • Loading branch information
hbagdi committed Oct 20, 2019
1 parent 34ce45a commit 102ed5d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
37 changes: 36 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package cmd

import "github.com/hbagdi/deck/dump"
import (
"net/http"

"github.com/hbagdi/deck/dump"
"github.com/hbagdi/deck/utils"
"github.com/pkg/errors"
)

var stopChannel chan struct{}

Expand All @@ -12,3 +18,32 @@ func SetStopCh(stopCh chan struct{}) {
}

var dumpConfig dump.Config

// checkWorkspace checks if workspace exists in Kong.
func checkWorkspace(config utils.KongClientConfig) error {

workspace := config.Workspace
if workspace == "" {
return nil
}

client, err := utils.GetKongClient(config)
if err != nil {
return err
}

req, err := http.NewRequest("GET", config.Address+"/workspace/"+workspace,
nil)
if err != nil {
return err
}
resp, err := client.Do(nil, req, nil)
if resp.StatusCode == 404 {
return errors.Errorf("workspace '%v' does not exist in Kong, "+
"please create it before running decK.", workspace)
}
if err != nil {
return errors.Wrapf(err, "checking workspace '%v' in Kong", workspace)
}
return nil
}
6 changes: 6 additions & 0 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ that will be created or updated or deleted.
if err != nil {
return err
}

config.Workspace = workspace
if err := checkWorkspace(config); err != nil {
return err
}

client, err := utils.GetKongClient(config)
if err != nil {
return err
}

dumpConfig.SelectorTags = selectTags
currentState, err := dump.GetState(client, dumpConfig)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ to get Kong's state in sync with the input state.`,
return err
}
config.Workspace = workspace
if err := checkWorkspace(config); err != nil {
return err
}

client, err := utils.GetKongClient(config)
if err != nil {
return err
}

dumpConfig.SelectorTags = selectTags
currentState, err := dump.GetState(client, dumpConfig)
if err != nil {
Expand Down

0 comments on commit 102ed5d

Please sign in to comment.