Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
Merge pull request #96 in STORAGEOS/c2-cli from feature/CP-4409-cli-a…
Browse files Browse the repository at this point in the history
…llow-http-500-response-for-get-diagnostics to master

Squashed commit of the following:

commit 5c316295f95d0b9807448a6604f8849430d2a64c
Author: Dom <[email protected]>
Date:   Thu Aug 13 15:28:49 2020 +0100

    cmd/get/diagnostics: print newlines around warning

    Makes it a little easier to spot

commit d42b0309efee845cacdee07e24b6c7ed2fe6d135
Author: Dom <[email protected]>
Date:   Thu Aug 13 15:28:27 2020 +0100

    apiclient/openapi: handle 502 instead of 500

commit f1b785fb2317efc5b50b617d9eb8eca29178ff0c
Author: Fraser Savage <[email protected]>
Date:   Wed Aug 12 13:50:23 2020 +0100

    Update lint rule

commit f0647eb3ba0fdb4027425be2694c914bf9dadf81
Author: Fraser Savage <[email protected]>
Date:   Wed Aug 12 13:40:32 2020 +0100

    diagnostics: Accept 500 as a potential partial bundle if we cannot get a sensible error out
  • Loading branch information
domodwyer committed Aug 13, 2020
1 parent 4d24a46 commit 225bbff
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ issues:
- linters:
- gosec
text: "G304: Potential file inclusion via variable"
- linters:
- gosec
text: 'G307: Deferring unsafe method "\*os\.File" on type "Close"'

- path: mock_.+\.go
text: "should have comment or be unexported"
Expand Down
27 changes: 26 additions & 1 deletion apiclient/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package apiclient

import "fmt"
import (
"fmt"
"io"
)

// AuthenticationError indicates that the requested operation could not be
// performed for the client due to an issue with the authentication credentials
Expand Down Expand Up @@ -194,3 +197,25 @@ func NewEncodingError(err error, targetType, value interface{}) EncodingError {
value: value,
}
}

// IncompleteDiagnosticsError provides an error type
type IncompleteDiagnosticsError struct {
bundleData io.ReadCloser
}

func (e IncompleteDiagnosticsError) Error() string {
return "received an incomplete diagnostic bundle"
}

// BundleData returns the read closer for the bundle data associated with the error.
func (e IncompleteDiagnosticsError) BundleData() io.ReadCloser {
return e.bundleData
}

// NewIncompleteDiagnosticsError constructs an incomplete diagnostics error for
// the provided bundle data.
func NewIncompleteDiagnosticsError(bundleData io.ReadCloser) IncompleteDiagnosticsError {
return IncompleteDiagnosticsError{
bundleData: bundleData,
}
}
14 changes: 13 additions & 1 deletion apiclient/openapi/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"

"code.storageos.net/storageos/c2-cli/apiclient"
"code.storageos.net/storageos/openapi"
)

Expand Down Expand Up @@ -40,8 +41,19 @@ func (o *OpenAPI) GetDiagnostics(ctx context.Context) (io.ReadCloser, error) {
}

switch resp.StatusCode {
case 200:
case http.StatusOK:
// Carry on.
case http.StatusBadGateway:
// Check if the response content-type indicates a partial bundle. That
// is, it has a gzip content type.
for _, value := range resp.Header["Content-Type"] {
if value == "application/gzip" {
return nil, apiclient.NewIncompleteDiagnosticsError(resp.Body)
}
}

// If not, use the normal error handling code.
fallthrough
default:
defer resp.Body.Close()
// Try to read the response body and unmarshal it into an openapi.Error
Expand Down
17 changes: 14 additions & 3 deletions cmd/get/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/spf13/cobra"

"code.storageos.net/storageos/c2-cli/apiclient"
"code.storageos.net/storageos/c2-cli/cmd/runwrappers"
"code.storageos.net/storageos/c2-cli/pkg/cmdcontext"
)
Expand Down Expand Up @@ -53,9 +54,19 @@ func (c *diagnosticsCommand) runWithCtx(ctx context.Context, cmd *cobra.Command,

bundle, err := c.client.GetDiagnostics(ctx)
if err != nil {
// If we failed to get a bundle then remove the file we created.
os.Remove(outputfile.Name())
return err
switch v := err.(type) {

case apiclient.IncompleteDiagnosticsError:
// If the error is for an incomplete diagnostic bundle, extract
// the data and warn the user.
bundle = v.BundleData()
fmt.Fprintf(os.Stderr, "\nWarning: %v\n\n", v)

default:
// If we failed to get a bundle at all then remove the file we created.
os.Remove(outputfile.Name())
return err
}
}
defer bundle.Close()

Expand Down

0 comments on commit 225bbff

Please sign in to comment.