SDK error revamp (#432)

SDK methods now return 1 error.  To differentiate between Client errors and Server errors I've also implemented the AsServerError function which returns an *api.Error if it is an error originating from the remote server or nil otherwise.
This commit is contained in:
Todd Knight authored and GitHub committed 2020-09-24 14:34:58 -07:00
1 parent 94f9d952be
commit fd0da998e8
53 files changed
+1512 -1635

No files matched your search

+11 -13
View File
@@ -13,7 +13,6 @@ import (
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/hashicorp/vault/sdk/helper/password"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -264,23 +263,22 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "read":
result, apiErr, err = accountClient.Read(c.Context, c.FlagId, opts...)
result, err = accountClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = accountClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = accountClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = accountClient.List(c.Context, c.flagAuthMethodId, opts...)
listResult, err = accountClient.List(c.Context, c.flagAuthMethodId, opts...)
case "set-password":
result, apiErr, err = accountClient.SetPassword(c.Context, c.FlagId, c.flagPassword, version, opts...)
result, err = accountClient.SetPassword(c.Context, c.FlagId, c.flagPassword, version, opts...)
case "change-password":
result, apiErr, err = accountClient.ChangePassword(c.Context, c.FlagId, c.flagCurrentPassword, c.flagNewPassword, version, opts...)
result, err = accountClient.ChangePassword(c.Context, c.FlagId, c.flagCurrentPassword, c.flagNewPassword, version, opts...)
}
plural := "account"
@@ -288,13 +286,13 @@ func (c *Command) Run(args []string) int {
plural = "accounts"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
+6 -8
View File
@@ -12,7 +12,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/hashicorp/vault/sdk/helper/password"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -207,24 +206,23 @@ func (c *PasswordCommand) Run(args []string) int {
}
var result api.GenericResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = accountClient.Create(c.Context, c.flagAuthMethodId, opts...)
result, err = accountClient.Create(c.Context, c.flagAuthMethodId, opts...)
case "update":
result, apiErr, err = accountClient.Update(c.Context, c.FlagId, version, opts...)
result, err = accountClient.Update(c.Context, c.FlagId, version, opts...)
}
plural := "password-type account"
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
account := result.GetItem().(*accounts.Account)
switch base.Format(c.UI) {
@@ -8,10 +8,10 @@ import (
"time"
"github.com/btcsuite/btcutil/base58"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/authmethods"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/vault/sdk/helper/password"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/mitchellh/go-wordwrap"
"github.com/posener/complete"
@@ -121,19 +121,19 @@ func (c *PasswordCommand) Run(args []string) int {
// note: Authenticate() calls SetToken() under the hood to set the
// auth bearer on the client so we do not need to do anything with the
// returned token after this call, so we ignore it
result, apiErr, err := authmethods.NewClient(client).Authenticate(c.Context, c.flagAuthMethodId,
result, err := authmethods.NewClient(client).Authenticate(c.Context, c.flagAuthMethodId,
map[string]interface{}{
"login_name": c.flagLoginName,
"password": c.flagPassword,
})
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authentication: %s", err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to perform authentication: %s", err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authentication: %s", pretty.Sprint(apiErr)))
return 1
}
token := result.Item
switch base.Format(c.UI) {
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -157,19 +156,18 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "read":
result, apiErr, err = authmethodClient.Read(c.Context, c.FlagId, opts...)
result, err = authmethodClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = authmethodClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = authmethodClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = authmethodClient.List(c.Context, c.FlagScopeId, opts...)
listResult, err = authmethodClient.List(c.Context, c.FlagScopeId, opts...)
}
plural := "auth method"
@@ -177,13 +175,13 @@ func (c *Command) Run(args []string) int {
plural = "auth methods"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -185,24 +184,23 @@ func (c *PasswordCommand) Run(args []string) int {
}
var result api.GenericResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = authmethodClient.Create(c.Context, "password", c.FlagScopeId, opts...)
result, err = authmethodClient.Create(c.Context, "password", c.FlagScopeId, opts...)
case "update":
result, apiErr, err = authmethodClient.Update(c.Context, c.FlagId, version, opts...)
result, err = authmethodClient.Update(c.Context, c.FlagId, version, opts...)
}
plural := "password-type auth-method"
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
method := result.GetItem().(*authmethods.AuthMethod)
switch base.Format(c.UI) {
+9 -11
View File
@@ -11,7 +11,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -94,19 +93,18 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "read":
result, apiErr, err = authtokenClient.Read(c.Context, c.FlagId)
result, err = authtokenClient.Read(c.Context, c.FlagId)
case "delete":
_, apiErr, err = authtokenClient.Delete(c.Context, c.FlagId)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = authtokenClient.Delete(c.Context, c.FlagId)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = authtokenClient.List(c.Context, c.FlagScopeId)
listResult, err = authtokenClient.List(c.Context, c.FlagScopeId)
}
plural := "auth token"
@@ -114,13 +112,13 @@ func (c *Command) Run(args []string) int {
plural = "auth tokens"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
+14 -16
View File
@@ -9,7 +9,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -165,29 +164,28 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = groupClient.Create(c.Context, c.FlagScopeId, opts...)
result, err = groupClient.Create(c.Context, c.FlagScopeId, opts...)
case "update":
result, apiErr, err = groupClient.Update(c.Context, c.FlagId, version, opts...)
result, err = groupClient.Update(c.Context, c.FlagId, version, opts...)
case "read":
result, apiErr, err = groupClient.Read(c.Context, c.FlagId, opts...)
result, err = groupClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = groupClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = groupClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = groupClient.List(c.Context, c.FlagScopeId, opts...)
listResult, err = groupClient.List(c.Context, c.FlagScopeId, opts...)
case "add-members":
result, apiErr, err = groupClient.AddMembers(c.Context, c.FlagId, version, members, opts...)
result, err = groupClient.AddMembers(c.Context, c.FlagId, version, members, opts...)
case "set-members":
result, apiErr, err = groupClient.SetMembers(c.Context, c.FlagId, version, members, opts...)
result, err = groupClient.SetMembers(c.Context, c.FlagId, version, members, opts...)
case "remove-members":
result, apiErr, err = groupClient.RemoveMembers(c.Context, c.FlagId, version, members, opts...)
result, err = groupClient.RemoveMembers(c.Context, c.FlagId, version, members, opts...)
}
plural := "group"
@@ -195,13 +193,13 @@ func (c *Command) Run(args []string) int {
plural = "groups"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -157,19 +156,18 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "read":
result, apiErr, err = hostcatalogClient.Read(c.Context, c.FlagId, opts...)
result, err = hostcatalogClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = hostcatalogClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = hostcatalogClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = hostcatalogClient.List(c.Context, c.FlagScopeId, opts...)
listResult, err = hostcatalogClient.List(c.Context, c.FlagScopeId, opts...)
}
plural := "host catalog"
@@ -177,13 +175,13 @@ func (c *Command) Run(args []string) int {
plural = "host catalogs"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
+6 -8
View File
@@ -9,7 +9,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -141,24 +140,23 @@ func (c *StaticCommand) Run(args []string) int {
}
var result api.GenericResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = hostcatalogClient.Create(c.Context, "static", c.FlagScopeId, opts...)
result, err = hostcatalogClient.Create(c.Context, "static", c.FlagScopeId, opts...)
case "update":
result, apiErr, err = hostcatalogClient.Update(c.Context, c.FlagId, version, opts...)
result, err = hostcatalogClient.Update(c.Context, c.FlagId, version, opts...)
}
plural := "static-type host-catalog"
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
catalog := result.GetItem().(*hostcatalogs.HostCatalog)
switch base.Format(c.UI) {
+9 -11
View File
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -168,19 +167,18 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "read":
result, apiErr, err = hostClient.Read(c.Context, c.FlagId, opts...)
result, err = hostClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = hostClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = hostClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = hostClient.List(c.Context, c.flagHostCatalogId, opts...)
listResult, err = hostClient.List(c.Context, c.flagHostCatalogId, opts...)
}
plural := "host"
@@ -188,13 +186,13 @@ func (c *Command) Run(args []string) int {
plural = "hosts"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
+6 -8
View File
@@ -9,7 +9,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -181,24 +180,23 @@ func (c *StaticCommand) Run(args []string) int {
}
var result api.GenericResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = hostClient.Create(c.Context, c.flagHostCatalogId, opts...)
result, err = hostClient.Create(c.Context, c.flagHostCatalogId, opts...)
case "update":
result, apiErr, err = hostClient.Update(c.Context, c.FlagId, version, opts...)
result, err = hostClient.Update(c.Context, c.FlagId, version, opts...)
}
plural := "static-type host"
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
host := result.GetItem().(*hosts.Host)
switch base.Format(c.UI) {
+12 -14
View File
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -248,25 +247,24 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "read":
result, apiErr, err = hostsetClient.Read(c.Context, c.FlagId, opts...)
result, err = hostsetClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = hostsetClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = hostsetClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = hostsetClient.List(c.Context, c.flagHostCatalogId, opts...)
listResult, err = hostsetClient.List(c.Context, c.flagHostCatalogId, opts...)
case "add-hosts":
result, apiErr, err = hostsetClient.AddHosts(c.Context, c.FlagId, version, hosts, opts...)
result, err = hostsetClient.AddHosts(c.Context, c.FlagId, version, hosts, opts...)
case "remove-hosts":
result, apiErr, err = hostsetClient.RemoveHosts(c.Context, c.FlagId, version, hosts, opts...)
result, err = hostsetClient.RemoveHosts(c.Context, c.FlagId, version, hosts, opts...)
case "set-hosts":
result, apiErr, err = hostsetClient.SetHosts(c.Context, c.FlagId, version, hosts, opts...)
result, err = hostsetClient.SetHosts(c.Context, c.FlagId, version, hosts, opts...)
}
plural := "host set"
@@ -274,13 +272,13 @@ func (c *Command) Run(args []string) int {
plural = "host sets"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
+6 -8
View File
@@ -9,7 +9,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -152,24 +151,23 @@ func (c *StaticCommand) Run(args []string) int {
}
var result api.GenericResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = hostsetClient.Create(c.Context, c.flagHostCatalogId, opts...)
result, err = hostsetClient.Create(c.Context, c.flagHostCatalogId, opts...)
case "update":
result, apiErr, err = hostsetClient.Update(c.Context, c.FlagId, version, opts...)
result, err = hostsetClient.Update(c.Context, c.FlagId, version, opts...)
}
plural := "static-type host-set"
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
set := result.GetItem().(*hostsets.HostSet)
switch base.Format(c.UI) {
+6 -6
View File
@@ -21,6 +21,7 @@ import (
"time"
"github.com/btcsuite/btcutil/base58"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/targets"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/cmd/base"
@@ -28,7 +29,6 @@ import (
"github.com/hashicorp/boundary/internal/proxy"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/vault/sdk/helper/base62"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"go.uber.org/atomic"
@@ -318,15 +318,15 @@ func (c *Command) Run(args []string) (retCode int) {
opts = append(opts, targets.WithHostId(c.flagHostId))
}
sar, apiErr, err := targetClient.Authorize(c.Context, c.flagTargetId, opts...)
sar, err := targetClient.Authorize(c.Context, c.flagTargetId, opts...)
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authorize on a session against target: %s", err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to authorize a session against target: %s", err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authorize on a session against target: %s", pretty.Sprint(apiErr)))
return 1
}
sa := sar.GetItem().(*targets.SessionAuthorization)
authzString = sa.AuthorizationToken
}
+17 -19
View File
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/perms"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -210,35 +209,34 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = roleClient.Create(c.Context, c.FlagScopeId, opts...)
result, err = roleClient.Create(c.Context, c.FlagScopeId, opts...)
case "update":
result, apiErr, err = roleClient.Update(c.Context, c.FlagId, version, opts...)
result, err = roleClient.Update(c.Context, c.FlagId, version, opts...)
case "read":
result, apiErr, err = roleClient.Read(c.Context, c.FlagId, opts...)
result, err = roleClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = roleClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = roleClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = roleClient.List(c.Context, c.FlagScopeId, opts...)
listResult, err = roleClient.List(c.Context, c.FlagScopeId, opts...)
case "add-principals":
result, apiErr, err = roleClient.AddPrincipals(c.Context, c.FlagId, version, principals, opts...)
result, err = roleClient.AddPrincipals(c.Context, c.FlagId, version, principals, opts...)
case "set-principals":
result, apiErr, err = roleClient.SetPrincipals(c.Context, c.FlagId, version, principals, opts...)
result, err = roleClient.SetPrincipals(c.Context, c.FlagId, version, principals, opts...)
case "remove-principals":
result, apiErr, err = roleClient.RemovePrincipals(c.Context, c.FlagId, version, principals, opts...)
result, err = roleClient.RemovePrincipals(c.Context, c.FlagId, version, principals, opts...)
case "add-grants":
result, apiErr, err = roleClient.AddGrants(c.Context, c.FlagId, version, grants, opts...)
result, err = roleClient.AddGrants(c.Context, c.FlagId, version, grants, opts...)
case "set-grants":
result, apiErr, err = roleClient.SetGrants(c.Context, c.FlagId, version, grants, opts...)
result, err = roleClient.SetGrants(c.Context, c.FlagId, version, grants, opts...)
case "remove-grants":
result, apiErr, err = roleClient.RemoveGrants(c.Context, c.FlagId, version, grants, opts...)
result, err = roleClient.RemoveGrants(c.Context, c.FlagId, version, grants, opts...)
}
plural := "role"
@@ -246,13 +244,13 @@ func (c *Command) Run(args []string) int {
plural = "roles"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
+11 -13
View File
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -140,23 +139,22 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = scopeClient.Create(c.Context, c.FlagScopeId, opts...)
result, err = scopeClient.Create(c.Context, c.FlagScopeId, opts...)
case "update":
result, apiErr, err = scopeClient.Update(c.Context, c.FlagId, version, opts...)
result, err = scopeClient.Update(c.Context, c.FlagId, version, opts...)
case "read":
result, apiErr, err = scopeClient.Read(c.Context, c.FlagId, opts...)
result, err = scopeClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = scopeClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = scopeClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = scopeClient.List(c.Context, c.FlagScopeId, opts...)
listResult, err = scopeClient.List(c.Context, c.FlagScopeId, opts...)
}
plural := "scope"
@@ -164,13 +162,13 @@ func (c *Command) Run(args []string) int {
plural = "scopes"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
+7 -9
View File
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -116,15 +115,14 @@ func (c *Command) Run(args []string) int {
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "read":
result, apiErr, err = sessionClient.Read(c.Context, c.FlagId)
result, err = sessionClient.Read(c.Context, c.FlagId)
case "cancel":
result, apiErr, err = sessionClient.Cancel(c.Context, c.FlagId, 0, sessions.WithAutomaticVersioning(true))
result, err = sessionClient.Cancel(c.Context, c.FlagId, 0, sessions.WithAutomaticVersioning(true))
case "list":
listResult, apiErr, err = sessionClient.List(c.Context, c.FlagScopeId)
listResult, err = sessionClient.List(c.Context, c.FlagScopeId)
}
plural := "session"
@@ -132,13 +130,13 @@ func (c *Command) Run(args []string) int {
plural = "sessions"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "list":
+13 -15
View File
@@ -11,7 +11,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -264,28 +263,27 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
var sar *targets.SessionAuthorizationResult
switch c.Func {
case "read":
result, apiErr, err = targetClient.Read(c.Context, c.FlagId, opts...)
result, err = targetClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = targetClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = targetClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = targetClient.List(c.Context, c.FlagScopeId, opts...)
listResult, err = targetClient.List(c.Context, c.FlagScopeId, opts...)
case "add-host-sets":
result, apiErr, err = targetClient.AddHostSets(c.Context, c.FlagId, version, hostSets, opts...)
result, err = targetClient.AddHostSets(c.Context, c.FlagId, version, hostSets, opts...)
case "remove-host-sets":
result, apiErr, err = targetClient.RemoveHostSets(c.Context, c.FlagId, version, hostSets, opts...)
result, err = targetClient.RemoveHostSets(c.Context, c.FlagId, version, hostSets, opts...)
case "set-host-sets":
result, apiErr, err = targetClient.SetHostSets(c.Context, c.FlagId, version, hostSets, opts...)
result, err = targetClient.SetHostSets(c.Context, c.FlagId, version, hostSets, opts...)
case "authorize":
sar, apiErr, err = targetClient.Authorize(c.Context, c.FlagId, opts...)
sar, err = targetClient.Authorize(c.Context, c.FlagId, opts...)
}
plural := "target"
@@ -296,13 +294,13 @@ func (c *Command) Run(args []string) int {
plural = "a session against target"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":
+6 -8
View File
@@ -11,7 +11,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -213,24 +212,23 @@ func (c *TcpCommand) Run(args []string) int {
}
var result api.GenericResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = targetClient.Create(c.Context, "tcp", c.FlagScopeId, opts...)
result, err = targetClient.Create(c.Context, "tcp", c.FlagScopeId, opts...)
case "update":
result, apiErr, err = targetClient.Update(c.Context, c.FlagId, version, opts...)
result, err = targetClient.Update(c.Context, c.FlagId, version, opts...)
}
plural := "tcp-type target"
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
target := result.GetItem().(*targets.Target)
switch base.Format(c.UI) {
+11 -13
View File
@@ -10,7 +10,6 @@ import (
"github.com/hashicorp/boundary/internal/cmd/common"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
@@ -127,23 +126,22 @@ func (c *Command) Run(args []string) int {
existed := true
var result api.GenericResult
var listResult api.GenericListResult
var apiErr *api.Error
switch c.Func {
case "create":
result, apiErr, err = userClient.Create(c.Context, c.FlagScopeId, opts...)
result, err = userClient.Create(c.Context, c.FlagScopeId, opts...)
case "update":
result, apiErr, err = userClient.Update(c.Context, c.FlagId, version, opts...)
result, err = userClient.Update(c.Context, c.FlagId, version, opts...)
case "read":
result, apiErr, err = userClient.Read(c.Context, c.FlagId, opts...)
result, err = userClient.Read(c.Context, c.FlagId, opts...)
case "delete":
_, apiErr, err = userClient.Delete(c.Context, c.FlagId, opts...)
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
_, err = userClient.Delete(c.Context, c.FlagId, opts...)
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
existed = false
apiErr = nil
err = nil
}
case "list":
listResult, apiErr, err = userClient.List(c.Context, c.FlagScopeId, opts...)
listResult, err = userClient.List(c.Context, c.FlagScopeId, opts...)
}
plural := "user"
@@ -151,13 +149,13 @@ func (c *Command) Run(args []string) int {
plural = "users"
}
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
return 2
}
if apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
return 1
}
switch c.Func {
case "delete":