diff --git a/cmd/common.go b/cmd/common.go index 8b4e9a727..549c5a2e4 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -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{} @@ -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 +} diff --git a/cmd/diff.go b/cmd/diff.go index 42183f308..948988844 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -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 { diff --git a/cmd/sync.go b/cmd/sync.go index 103a7ace6..37b559af6 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -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 {