Skip to content

Commit

Permalink
Merge pull request #15 from safesoftware/fix-healthcheck-json-DEVOPS-…
Browse files Browse the repository at this point in the history
…2907

Fix json output in healthcheck endpoint. Add custom-columns output type for v4.
  • Loading branch information
garnold54 authored Jan 5, 2023
2 parents 65d7995 + 715066d commit 3a28e8e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
54 changes: 49 additions & 5 deletions cmd/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
"io"
"net/http"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type healthcheckFlags struct {
ready bool
outputType string
noHeaders bool
apiVersion apiVersionFlag
}

Expand Down Expand Up @@ -43,19 +46,28 @@ func newHealthcheckCmd() *cobra.Command {
fmeserver healthcheck --ready
# Check if the FME Server is healthy and output in json
fmeserver healthcheck --json`,
fmeserver healthcheck --json
# Check that the FME Server is healthy and output just the status
fmeserver healthcheck --output=custom-columns=STATUS:.status`,
Args: NoArgs,
RunE: healthcheckRun(&f),
}
cmd.Flags().BoolVar(&f.ready, "ready", false, "The health check will report the status of FME Server if it is ready to process jobs.")
cmd.Flags().Var(&f.apiVersion, "api-version", "The api version to use when contacting FME Server. Must be one of v3 or v4")
cmd.Flags().StringVarP(&f.outputType, "output", "o", "table", "Specify the output type. Should be one of table, json, or custom-columns")
cmd.Flags().BoolVar(&f.noHeaders, "no-headers", false, "Don't print column headers")
cmd.Flags().MarkHidden("api-version")
cmd.RegisterFlagCompletionFunc("api-version", apiVersionFlagCompletion)
return cmd
}

func healthcheckRun(f *healthcheckFlags) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
// --json overrides --output
if jsonOutput {
f.outputType = "json"
}

// set up http
client := &http.Client{}
Expand Down Expand Up @@ -100,20 +112,49 @@ func healthcheckRun(f *healthcheckFlags) func(cmd *cobra.Command, args []string)
if err := json.Unmarshal(responseData, &resultV4); err != nil {
return err
}
if !jsonOutput {
if f.outputType == "table" {
t := createTableWithDefaultColumns(resultV4)

if noHeaders {
t.ResetHeaders()
}
fmt.Fprintln(cmd.OutOrStdout(), t.Render())

} else if outputType == "json" {
} else if f.outputType == "json" {
prettyJSON, err := prettyPrintJSON(responseData)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), prettyJSON)
} else if strings.HasPrefix(f.outputType, "custom-columns") {
// parse the columns and json queries
columnsString := ""
if strings.HasPrefix(f.outputType, "custom-columns=") {
columnsString = f.outputType[len("custom-columns="):]
}
if len(columnsString) == 0 {
return errors.New("custom-columns format specified but no custom columns given")
}

// we have to marshal the Items array, then create an array of marshalled items
// to pass to the creation of the table.
marshalledItems := [][]byte{}
mJson, err := json.Marshal(resultV4)
if err != nil {
return err
}
marshalledItems = append(marshalledItems, mJson)

columnsInput := strings.Split(columnsString, ",")
t, err := createTableFromCustomColumns(marshalledItems, columnsInput)
if err != nil {
return err
}
if noHeaders {
t.ResetHeaders()
}
fmt.Fprintln(cmd.OutOrStdout(), t.Render())

} else {
return errors.New("invalid output format specified")
}
Expand Down Expand Up @@ -149,14 +190,17 @@ func healthcheckRun(f *healthcheckFlags) func(cmd *cobra.Command, args []string)
return err
}
status = resultV3.Status
if !jsonOutput {
if f.outputType == "table" {
fmt.Fprintln(cmd.OutOrStdout(), status)
} else if outputType == "json" {
} else if f.outputType == "json" {
prettyJSON, err := prettyPrintJSON(responseData)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), prettyJSON)
} else if strings.HasPrefix(f.outputType, "custom-columns") {
// since V3 only returns a single json parameter, we won't support the custom-columns output type
return errors.New("custom-columns format not valid with V3 API")
} else {
return errors.New("invalid output format specified")
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ func TestHealthcheck(t *testing.T) {
fmeserverBuild: 23200,
args: []string{"healthcheck"},
},
{
name: "json output v4",
statusCode: http.StatusOK,
body: okResponseV4,
wantOutputJson: okResponseV4,
args: []string{"healthcheck", "--json"},
},
{
name: "json output v3",
statusCode: http.StatusOK,
body: okResponseV3,
wantOutputJson: okResponseV3,
args: []string{"healthcheck", "--json", "--api-version", "v3"},
},
}
runTests(cases, t)
}

0 comments on commit 3a28e8e

Please sign in to comment.