Skip to content

Commit

Permalink
PDI-2101: add fail flag to propagate non-zero exit code on custom req…
Browse files Browse the repository at this point in the history
…uest failure
  • Loading branch information
wesleymccollam committed Nov 21, 2024
1 parent e3454b1 commit 52fd1be
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
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
16 changes: 16 additions & 0 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 := getFailOption()
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 @@ -108,6 +113,9 @@ func runInternalPingOneRequest(uri string) (err error) {
} else {
output.Success("Custom request successful", fields)
}
if failOption == "true" {
return fmt.Errorf("custom request failed with --fail (-f) flag")
}

return nil
}
Expand Down Expand Up @@ -297,3 +305,11 @@ func getData() (data string, err error) {

return data, nil
}

func getFailOption() (fail string, err error) {
fail, err = profiles.GetOptionValue(options.RequestFailOption)
if err != nil {
return "", err
}
return fail, nil
}
12 changes: 12 additions & 0 deletions internal/commands/request/request_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ func Test_RunInternalRequest(t *testing.T) {
testutils.CheckExpectedError(t, err, nil)
}

// Test RunInternalRequest function with fail
func Test_RunInternalRequestWithFail(t *testing.T) {
testutils_viper.InitVipers(t)
t.Setenv(options.RequestServiceOption.EnvVar, "pingone")
options.RequestFailOption.Flag.Changed = true
options.RequestFailOption.Flag.Value.Set("true")

Check failure on line 29 in internal/commands/request/request_internal_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `options.RequestFailOption.Flag.Value.Set` is not checked (errcheck)

err := RunInternalRequest("environments/failTest")
expectedErrorPattern := `^failed to send custom request: custom request failed with --fail \(-f\) flag$`
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}

// 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 exit code when HTTP custom request returns a failure status code.",
Value: cobraValue,
},

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

0 comments on commit 52fd1be

Please sign in to comment.