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

added client and header timeout. #19

Merged
merged 1 commit into from
Jan 14, 2022
Merged
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
71 changes: 46 additions & 25 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ func NewInsAPICliShowASCIIRequest(s string) *InsAPIRequest {

// Client is an instance of Cisco NX-OS API client.
type Client struct {
host string
port int
protocol string
username string
password string
secure bool
host string
port int
protocol string
username string
password string
secure bool
headerTimeout time.Duration
clientTimeout time.Duration
}

// NewClient returns an instance of Client.
Expand Down Expand Up @@ -153,6 +155,18 @@ func (cli *Client) SetPort(p int) error {
return nil
}

// SetHeaderTimeout sets the response header timeout for the http transport.
func (cli *Client) SetHeaderTimeout(r time.Duration) error {
cli.headerTimeout = r
return nil
}

// SetClientTimeout sets the http client timeout.
func (cli *Client) SetClientTimeout(c time.Duration) error {
cli.clientTimeout = c
return nil
}

// SetUsername sets the username for the API calls.
func (cli *Client) SetUsername(s string) error {
if s == "" {
Expand Down Expand Up @@ -191,21 +205,28 @@ func (cli *Client) SetSecure() error {
return nil
}

func callAPI(contentType string, url string, payload []byte, username, password string, secure bool) ([]byte, error) {
func (cli *Client) callAPI(contentType string, url string, payload []byte) ([]byte, error) {
tr := &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
if !secure {
if cli.headerTimeout > 0 {
tr.ResponseHeaderTimeout = cli.headerTimeout * time.Second
}
if !cli.secure {
tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
cli := &http.Client{
client := &http.Client{
Transport: tr,
Timeout: time.Second * 30,
}
if cli.clientTimeout > 0 {
client.Timeout = cli.clientTimeout * time.Second
} else {
client.Timeout = 30 * time.Second
}
var reqContentType string
switch contentType {
Expand All @@ -222,9 +243,9 @@ func callAPI(contentType string, url string, payload []byte, username, password
}
req.Header.Add("Content-Type", reqContentType)
req.Header.Add("Cache-Control", "no-cache")
req.SetBasicAuth(username, password)
req.SetBasicAuth(cli.username, cli.password)

res, err := cli.Do(req)
res, err := client.Do(req)
if err != nil {
if !strings.HasSuffix(err.Error(), "EOF") {
return nil, err
Expand Down Expand Up @@ -262,7 +283,7 @@ func (cli *Client) GetSystemInfo() (*SysInfo, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -277,7 +298,7 @@ func (cli *Client) GetVlans() ([]*Vlan, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -292,7 +313,7 @@ func (cli *Client) GetInterfaces() ([]*Interface, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -307,7 +328,7 @@ func (cli *Client) GetInterface(name string) (*Interface, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -325,7 +346,7 @@ func (cli *Client) GetSystemResources() (*SystemResources, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -340,7 +361,7 @@ func (cli *Client) GetSystemEnvironment() (*SystemEnvironment, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -355,7 +376,7 @@ func (cli *Client) GetGeneric(s string) ([]byte, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -370,7 +391,7 @@ func (cli *Client) GetBgpSummary() (*BgpSummary, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("json", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("json", url, payload)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -403,7 +424,7 @@ func (cli *Client) getConfiguration(s string) (*Configuration, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("json", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("json", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -419,7 +440,7 @@ func (cli *Client) GetTransceivers() ([]*Transceiver, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -440,7 +461,7 @@ func (cli *Client) GetMacAddressTable(intf string) (*MacAddressTable, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -456,7 +477,7 @@ func (cli *Client) GetCDPNeighbors() (*CDPNeighborTable, error) {
if err != nil {
return nil, err
}
resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand All @@ -477,7 +498,7 @@ func (cli *Client) Configure(cmds []string) ([]JSONRPCResponse, error) {
return nil, err
}

resp, err := callAPI("jsonrpc", url, payload, cli.username, cli.password, cli.secure)
resp, err := cli.callAPI("jsonrpc", url, payload)
if err != nil {
return nil, err
}
Expand Down