Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The command offers a cURL-like experience to interact with the Ping platform ser
cmd.Flags().AddFlag(options.RequestHTTPMethodOption.Flag)
cmd.Flags().AddFlag(options.RequestServiceOption.Flag)
cmd.Flags().AddFlag(options.RequestDataOption.Flag)
cmd.Flags().AddFlag(options.RequestFailOption.Flag)

return cmd
}
Expand Down
10 changes: 8 additions & 2 deletions internal/commands/request/request_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func runInternalPingOneRequest(uri string) (err error) {
return err
}

failOption, err := profiles.GetOptionValue(options.RequestFailOption)
if err != nil {
return err
}

apiURL := fmt.Sprintf("https://api.pingone.%s/v1/%s", topLevelDomain, uri)

httpMethod, err := profiles.GetOptionValue(options.RequestHTTPMethodOption)
Expand Down Expand Up @@ -102,9 +107,10 @@ func runInternalPingOneRequest(uri string) (err error) {
}

if res.StatusCode < 200 || res.StatusCode >= 300 {
// Note we don't os.Exit(1) here because pingcli has executed
// without issue, despite a failed response to the custom request
output.UserError("Failed Custom Request", fields)
if failOption == "true" {
os.Exit(1)
}
} else {
output.Success("Custom request successful", fields)
}
Expand Down
26 changes: 26 additions & 0 deletions internal/commands/request/request_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package request_internal
import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/pingidentity/pingcli/internal/configuration/options"
Expand All @@ -21,6 +22,31 @@ func Test_RunInternalRequest(t *testing.T) {
testutils.CheckExpectedError(t, err, nil)
}

// Test RunInternalRequest function with fail
func Test_RunInternalRequestWithFail(t *testing.T) {

if os.Getenv("RUN_INTERNAL_FAIL_TEST") == "true" {
testutils_viper.InitVipers(t)
t.Setenv(options.RequestServiceOption.EnvVar, "pingone")
options.RequestFailOption.Flag.Changed = true
err := options.RequestFailOption.Flag.Value.Set("true")
if err != nil {
t.Fatal(err)
}
_ = RunInternalRequest("environments/failTest")
t.Fatal("This should never run due to internal request resulting in os.Exit(1)")
} else {
cmdName := os.Args[0]
cmd := exec.Command(cmdName, "-test.run=Test_RunInternalRequestWithFail")
cmd.Env = append(os.Environ(), "RUN_INTERNAL_FAIL_TEST=true")
err := cmd.Run()
if exitError, ok := err.(*exec.ExitError); ok && !exitError.Success() {
return
}
t.Fatalf("The process did not exit with a non-zero: %s", err)
}
}

// Test RunInternalRequest function with empty service
func Test_RunInternalRequest_EmptyService(t *testing.T) {
testutils_viper.InitVipers(t)
Expand Down
2 changes: 2 additions & 0 deletions internal/configuration/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Options() []Option {
RequestServiceOption,
RequestAccessTokenOption,
RequestAccessTokenExpiryOption,
RequestFailOption,
}
}

Expand Down Expand Up @@ -145,4 +146,5 @@ var (
RequestServiceOption Option
RequestAccessTokenOption Option
RequestAccessTokenExpiryOption Option
RequestFailOption Option
)
24 changes: 23 additions & 1 deletion internal/configuration/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func InitRequestOptions() {
initServiceOption()
initAccessTokenOption()
initAccessTokenExpiryOption()

initFailOption()
}

func initDataOption() {
Expand Down Expand Up @@ -123,3 +123,25 @@ func initAccessTokenExpiryOption() {
ViperKey: "request.accessTokenExpiry",
}
}

func initFailOption() {
cobraParamName := "fail"
cobraValue := new(customtypes.Bool)
defaultValue := customtypes.Bool(false)

options.RequestFailOption = options.Option{
CobraParamName: cobraParamName,
CobraParamValue: cobraValue,
DefaultValue: &defaultValue,
Flag: &pflag.Flag{
Name: cobraParamName,
NoOptDefVal: "true",
Shorthand: "f",
Usage: "Return non-zero exit code when HTTP custom request returns a failure status code.",
Value: cobraValue,
},

Type: options.ENUM_BOOL,
ViperKey: "request.fail",
}
}