Skip to content

Commit

Permalink
Merge pull request #111 from cgascoig/fix91
Browse files Browse the repository at this point in the history
Show API error messages
  • Loading branch information
cgascoig authored Aug 8, 2023
2 parents 586ee1d + 84c5b5b commit 6eb3a01
Show file tree
Hide file tree
Showing 12 changed files with 54,775 additions and 49,094 deletions.
8 changes: 4 additions & 4 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func destroyMOs(client *util.IsctlClient, rawMOs []rawMO) error {
}

getOperation := gen.GetGetOperationForClassID(classID)
res, _, err := getOperation.Execute(client, nil, map[string]string{"filter": fmt.Sprintf("Name eq '%s'", name)})
res, err := getOperation.Execute(client, nil, map[string]string{"filter": fmt.Sprintf("Name eq '%s'", name)})
if err != nil {
return fmt.Errorf("Error checking if MO already exists: %v", err)
}
Expand All @@ -105,7 +105,7 @@ func destroyMOs(client *util.IsctlClient, rawMOs []rawMO) error {

delOperation := gen.GetDeleteOperationForClassID(classID)

_, _, err = delOperation.Execute(client, []string{moid}, nil)
_, err = delOperation.Execute(client, []string{moid}, nil)
if err != nil {
return fmt.Errorf("Error executing operation: %v", err)
}
Expand All @@ -131,7 +131,7 @@ func applyMOs(client *util.IsctlClient, rawMOs []rawMO) error {
}

getOperation := gen.GetGetOperationForClassID(classID)
res, _, err := getOperation.Execute(client, nil, map[string]string{"filter": fmt.Sprintf("Name eq '%s'", name)})
res, err := getOperation.Execute(client, nil, map[string]string{"filter": fmt.Sprintf("Name eq '%s'", name)})
if err != nil {
return fmt.Errorf("Error checking if MO already exists: %v", err)
}
Expand All @@ -155,7 +155,7 @@ func applyMOs(client *util.IsctlClient, rawMOs []rawMO) error {
return fmt.Errorf("Error setting up operation body: %v", err)
}

_, _, err = op.Execute(client, args, nil)
_, err = op.Execute(client, args, nil)
if err != nil {
return fmt.Errorf("Error executing operation: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,14 @@ func validateFlags(cmd *cobra.Command, args []string) error {

// Setup logging
if verbose && os.Getenv(traceEnvName) == "" {
httpTransport = newLoggingTransport()
log.SetLevel(log.DebugLevel)
log.Debug("Logging level set to debug(verbose)")
}

if logLevel := log.GetLevel(); logLevel == log.DebugLevel || logLevel == log.TraceLevel {
httpTransport = newLoggingTransport()
}

var err error

keyID := viper.GetString(keyIDConfigKey)
Expand Down
26 changes: 2 additions & 24 deletions cmd/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"regexp"
Expand All @@ -19,15 +18,7 @@ import (
"github.com/cgascoig/isctl/pkg/util"
)

func logHTTPResponse(httpResponse *http.Response) {
if httpResponse == nil {
return
}

log.Debugf("HTTP Response: %d %v", httpResponse.StatusCode, httpResponse.Status)
}

func resultHandler(result interface{}, httpResponse *http.Response, err error, options ...util.ResultOpt) {
func resultHandler(result interface{}, err error, options ...util.ResultOpt) {
var singleResult bool

for _, opt := range options {
Expand All @@ -36,22 +27,9 @@ func resultHandler(result interface{}, httpResponse *http.Response, err error, o
}
}

if err != nil || verbose {
logHTTPResponse(httpResponse)
}

// if there is an error try to display something helpful
if err != nil {
if httpResponse != nil {
body, err2 := ioutil.ReadAll(httpResponse.Body)
if err2 != nil {
log.Fatalf("ERROR: %v", err)
} else {
log.Fatalf("ERROR: %v: %s", err, string(body))
}
} else {
log.Fatalf("ERROR: %v", err)
}
log.Fatalf("ERROR: %v", err)
}

if jsonPathFilter != "" {
Expand Down
21 changes: 11 additions & 10 deletions generator/cli.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func () *cobra.Command {
Use: "{{ .Token }}",
{{ if .Operation }}
Run: func(cmd *cobra.Command, args []string) {
// client.GetConfig().Debug = verbose
var res any
var err error

// singleResult is true if the query should return a single result - used to decide whether to collapse the result into an object or single entry list for display/jsonpath processing
var singleResult = false
Expand All @@ -32,18 +33,18 @@ func () *cobra.Command {
// Lookup moid from name
getOp := GetGetOperationForClassID("{{ .Operation.ReturnClassID }}")
if getOp == nil {
resultHandler(nil, nil, fmt.Errorf("FATAL: No operation to lookup Moid from '{{ .Operation.ReturnClassID }}'"))
resultHandler(nil, fmt.Errorf("FATAL: No operation to lookup Moid from '{{ .Operation.ReturnClassID }}'"))
return
}
res, _, err := getOp.Execute(client, nil, map[string]string{"filter": fmt.Sprintf("Name eq '%s'", args[0])})
res, err = getOp.Execute(client, nil, map[string]string{"filter": fmt.Sprintf("Name eq '%s'", args[0])})
if err != nil {
resultHandler(nil, nil, fmt.Errorf("Error executing query to lookup Moid from Name: %v", err))
resultHandler(nil, fmt.Errorf("Error executing query to lookup Moid from Name: %v", err))
return
}

moid, ok := util.GetMoid(res)
if !ok {
resultHandler(nil, nil, fmt.Errorf("Error executing query to lookup Moid from Name: number of results must exactly equal 1"))
resultHandler(nil, fmt.Errorf("Error executing query to lookup Moid from Name: number of results must exactly equal 1"))
return
}

Expand All @@ -56,7 +57,7 @@ func () *cobra.Command {
if err == nil && bodyFormat != "" {
err = util.ReadBody(bodyFormat, &bodyParamMap)
if err != nil {
resultHandler(nil, nil, fmt.Errorf("Error reading body from standard input: %v", err))
resultHandler(nil, fmt.Errorf("Error reading body from standard input: %v", err))
return
}
}
Expand All @@ -65,7 +66,7 @@ func () *cobra.Command {

flagSetToMap(cmd.Flags(), &bodyParamMap)
if err = operation.SetBodyParams(client, bodyParamMap); err != nil {
resultHandler(nil, nil, fmt.Errorf("Generating API body: %v", err))
resultHandler(nil, fmt.Errorf("Generating API body: %v", err))
return
}

Expand All @@ -79,8 +80,8 @@ func () *cobra.Command {
{{ end }}
{{ end }}

res, httpResponse, err := operation.Execute(client, args, queryParams)
resultHandler(res, httpResponse, err, util.ResultOpt{SingleResult: &singleResult})
res, err = operation.Execute(client, args, queryParams)
resultHandler(res, err, util.ResultOpt{SingleResult: &singleResult})
},
{{ end }}
{{ if .Parameter }}
Expand Down Expand Up @@ -241,7 +242,7 @@ func runCmd(cmd *cobra.Command, args []string) {
}

// ResultHandler is the function signature to handle API results
type ResultHandler = func(result interface{}, httpResponse *http.Response, err error, opts ...util.ResultOpt)
type ResultHandler = func(result interface{}, err error, opts ...util.ResultOpt)

// GetCommands returns the cobra command tree for the API
func GetCommands(client *util.IsctlClient, resultHandler ResultHandler) *cobra.Command {
Expand Down
10 changes: 5 additions & 5 deletions generator/operations.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,28 @@ import (
{{ else }}return nil{{ end }}
}

func (o *{{ .Operation.OperationID }}) Execute(client *util.IsctlClient, args []string, queryParams map[string]string) (interface{}, *http.Response, error) {
func (o *{{ .Operation.OperationID }}) Execute(client *util.IsctlClient, args []string, queryParams map[string]string) (interface{}, error) {
{{ if ne .BodyParamType "" }}
js, err := json.Marshal(o.body)
if err != nil {
return nil, nil, fmt.Errorf("unable to marshal request body")
return nil, fmt.Errorf("unable to marshal request body")
}
{{ else }}
js := []byte("")
{{ end }}
path, err := ReplaceArgs("{{ .Operation.Path }}", args)
if err != nil {
return nil, nil, err
return nil, err
}
queryString := EncodeQueryParams(queryParams)
if queryString != "" {
path = fmt.Sprintf("%s?%s", path, queryString)
}
res, err := client.IntersightClient.Call("{{ .Operation.HTTPMethod }}", path, js)
{{ if ne .Operation.ReturnType "" }}
return res, nil, err
return res, err
{{ else }}
return res, nil, err
return res, err
{{ end }}
}
{{ end }}{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/PaesslerAG/jsonpath v0.1.1
github.com/bndr/gotabulate v1.1.3-0.20210209140143-7b841ac46153
github.com/cgascoig/intersight-simple-go v0.4.0
github.com/cgascoig/intersight-simple-go v0.5.0
github.com/getkin/kin-openapi v0.85.0
github.com/icza/dyno v0.0.0-20230330125955-09f820a8d9c0
github.com/mitchellh/go-homedir v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ github.com/bndr/gotabulate v1.1.3-0.20210209140143-7b841ac46153/go.mod h1:0+8yUg
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cgascoig/intersight-simple-go v0.4.0 h1:wdXgAvzAp2wwcowcg80SJVmPsVdsTKpl29DDfGQKAco=
github.com/cgascoig/intersight-simple-go v0.4.0/go.mod h1:TlpkJWi4kWZw2rZzwS3vMXdO2Rkkwt7kyt8U2tfpxoY=
github.com/cgascoig/intersight-simple-go v0.5.0 h1:1p94OLrWQR2GbVt9QaiumxkWfgwRM/pDyYUMMlbG5WA=
github.com/cgascoig/intersight-simple-go v0.5.0/go.mod h1:7pA/YNgn2rxO2uXYu4neMMbV/u5uPmPc4Y7mdrLqYfM=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
Loading

0 comments on commit 6eb3a01

Please sign in to comment.