Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Harvest should include a user agent #2224

Merged
merged 1 commit into from
Jul 21, 2023
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
5 changes: 3 additions & 2 deletions cmd/collectors/storagegrid/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/logging"
"github.com/netapp/harvest/v2/pkg/requests"
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/tidwall/gjson"
"io"
Expand Down Expand Up @@ -230,7 +231,7 @@ func (c *Client) getRest(request string) ([]byte, error) {
return nil, fmt.Errorf("failed to unescape %s err: %w", request, err)
}

c.request, err = http.NewRequest("GET", u, nil)
c.request, err = requests.New("GET", u, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -392,7 +393,7 @@ func (c *Client) fetchTokenWithAuthRetry() error {
return err
}

req, err = http.NewRequest("POST", u, bytes.NewBuffer(postBody))
req, err = requests.New("POST", u, bytes.NewBuffer(postBody))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/exporters/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/netapp/harvest/v2/pkg/color"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/requests"
"io"
"net/http"
url2 "net/url"
Expand Down Expand Up @@ -204,7 +205,7 @@ func (e *InfluxDB) Emit(data [][]byte) error {

buffer = bytes.NewBuffer(bytes.Join(data, []byte("\n")))

if request, err = http.NewRequest("POST", e.url, buffer); err != nil {
if request, err = requests.New("POST", e.url, buffer); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/logging"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/requests"
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -1029,7 +1030,7 @@ func (p *Poller) publishDetails() {
if heartBeatURL == "" {
heartBeatURL = defaultURL
}
req, err := http.NewRequest("PUT", heartBeatURL, bytes.NewBuffer(payload))
req, err := requests.New("PUT", heartBeatURL, bytes.NewBuffer(payload))
if err != nil {
logger.Err(err).Msg("failed to connect to admin")
return
Expand Down
5 changes: 3 additions & 2 deletions cmd/tools/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
goversion "github.com/hashicorp/go-version"
"github.com/netapp/harvest/v2/cmd/harvest/version"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/requests"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -958,9 +959,9 @@ func doRequest(opts *options, method, url string, query map[string]interface{})
return nil, status, code, err
}
buf = bytes.NewBuffer(data)
request, err = http.NewRequest(method, opts.addr+url, buf)
request, err = requests.New(method, opts.addr+url, buf)
} else {
request, err = http.NewRequest(method, opts.addr+url, nil)
request, err = requests.New(method, opts.addr+url, nil)
}

if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/tools/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/logging"
"github.com/netapp/harvest/v2/pkg/requests"
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -185,7 +186,7 @@ func (c *Client) GetRest(request string) ([]byte, error) {
return nil, err
}
u := c.baseURL + request
c.request, err = http.NewRequest("GET", u, nil)
c.request, err = requests.New("GET", u, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -308,7 +309,7 @@ func downloadSwagger(poller *conf.Poller, path string, url string, verbose bool)
return 0, fmt.Errorf("unable to create %s to save swagger.yaml", path)
}
defer func(out *os.File) { _ = out.Close() }(out)
request, err := http.NewRequest("GET", url, nil)
request, err := requests.New("GET", url, nil)
if err != nil {
return 0, err
}
Expand Down
27 changes: 2 additions & 25 deletions integration/test/utils/http_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package utils

import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/netapp/harvest/v2/pkg/requests"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -40,7 +40,7 @@ func GetResponseBody(url string) ([]byte, error) {
func SendReqAndGetRes(url string, method string,
buf []byte) map[string]interface{} {
client := &http.Client{}
req, err := http.NewRequest(method, url, bytes.NewBuffer(buf))
req, err := requests.New(method, url, bytes.NewBuffer(buf))
if err != nil {
fmt.Println(err)
panic(err)
Expand All @@ -57,26 +57,3 @@ func SendReqAndGetRes(url string, method string,
PanicIfNotNil(err)
return data
}

func SendPostReqAndGetRes(url string, method string, buf []byte, user string, pass string) map[string]interface{} {
tlsConfig := &tls.Config{InsecureSkipVerify: true} //nolint:gosec
client := &http.Client{
Transport: &http.Transport{TLSClientConfig: tlsConfig},
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(buf))
if err != nil {
fmt.Println(err)
panic(err)
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(user, pass)
res, err := client.Do(req)
PanicIfNotNil(err)
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
PanicIfNotNil(err)
var data map[string]interface{}
err = json.Unmarshal(body, &data)
PanicIfNotNil(err)
return data
}
3 changes: 2 additions & 1 deletion pkg/api/ontapi/zapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/logging"
"github.com/netapp/harvest/v2/pkg/requests"
"github.com/netapp/harvest/v2/pkg/tree"
"github.com/netapp/harvest/v2/pkg/tree/node"
"io"
Expand Down Expand Up @@ -90,7 +91,7 @@ func New(poller *conf.Poller, c *auth.Credentials) (*Client, error) {
}

// create a request object that will be used for later requests
if request, err = http.NewRequest("POST", url, nil); err != nil {
if request, err = requests.New("POST", url, nil); err != nil {
return nil, err
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/requests/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package requests

import (
"github.com/netapp/harvest/v2/cmd/harvest/version"
"io"
"net/http"
)

func New(method, url string, body io.Reader) (*http.Request, error) {
request, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}

harvestUserAgent := "Harvest/" + version.VERSION
request.Header.Set("User-Agent", harvestUserAgent)
return request, nil
}
Loading