Skip to content

Commit

Permalink
Fix config format
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Pichard <7628998+w3st3ry@users.noreply.github.com>
  • Loading branch information
w3st3ry committed Nov 19, 2019
1 parent 510573d commit b3c6aa2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
20 changes: 10 additions & 10 deletions pkg/plugins/builtin/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ An action of type `http` requires the following kind of configuration:
action:
type: http
configuration:
# mandatory
# mandatory, string
url: http://example.org/user
# mandatory
# mandatory, string
method: POST
# optional
timeout_seconds: 5
# optional
# optional, string as uint16
timeout_seconds: "5"
# optional, object of user and password fields
basic_auth:
user: {{.config.basicAuth.user}}
password: {{.config.basicAuth.password}}
# optional
deny_redirects: false
# optional
# optional, string as boolean
deny_redirects: "false"
# optional, array of key and value fields
parameters:
- key: foo
value: bar
# optional
# optional, array of name and value fields
headers:
- name: x-request-id
value: xxx-yyy-zzz
# optional
# optional, string
body: |
{
"name": "pablo"
Expand Down
36 changes: 24 additions & 12 deletions pkg/plugins/builtin/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package pluginhttp
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/ovh/utask"
Expand All @@ -23,13 +23,13 @@ var (

// HTTPConfig is the configuration needed to perform an HTTP call
type HTTPConfig struct {
URL string `json:"url"`
Method string `json:"method"`
Body string `json:"body,omitempty"`
Headers []Header `json:"headers,omitempty"`
TimeoutSeconds int `json:"timeout_seconds,omitempty"`
HTTPBasicAuth BasicAuth `json:"basic_auth,omitempty"`
DenyRedirects bool `json:"deny_redirects,omitempty"`
URL string `json:"url"`
Method string `json:"method"`
Body string `json:"body,omitempty"`
Headers []Header `json:"headers,omitempty"`
TimeoutSeconds string `json:"timeout_seconds,omitempty"`
HTTPBasicAuth BasicAuth `json:"basic_auth,omitempty"`
DenyRedirects string `json:"deny_redirects,omitempty"`
Parameters []Parameter `json:"parameters,omitempty"`
}

Expand Down Expand Up @@ -59,8 +59,16 @@ func validConfig(config interface{}) error {
return fmt.Errorf("Unknown method for HTTP runner: %s", cfg.Method)
}

if cfg.TimeoutSeconds < 0 {
return errors.New("timeout_seconds field can't be < 0")
if cfg.TimeoutSeconds != "" {
if _, err := strconv.ParseUint(cfg.TimeoutSeconds, 10, 16); err != nil {
return fmt.Errorf("timeout_seconds is wrong %s", err.Error())
}
}

if cfg.DenyRedirects != "" {
if _, err := strconv.ParseBool(cfg.DenyRedirects); err != nil {
return fmt.Errorf("deny_redirects is wrong %s", err.Error())
}
}

return nil
Expand Down Expand Up @@ -104,12 +112,16 @@ func exec(stepName string, config interface{}, ctx interface{}) (interface{}, in
req.Header.Set(h.Name, h.Value)
}

ts, _ := strconv.ParseUint(cfg.TimeoutSeconds, 10, 16)

httpClient := &http.Client{
// 0 by default
Timeout: time.Duration(cfg.TimeoutSeconds) * time.Second,
Timeout: time.Duration(ts) * time.Second,
}

if cfg.DenyRedirects {
dr, _ := strconv.ParseBool(cfg.DenyRedirects)

if dr {
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
Expand Down

0 comments on commit b3c6aa2

Please sign in to comment.