diff --git a/go.mod b/go.mod index cb1e733..807b3d6 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/hashicorp/terraform-plugin-log v0.8.0 github.com/hashicorp/terraform-plugin-testing v1.2.0 github.com/stretchr/testify v1.8.4 - gitswarm.f5net.com/terraform-providers/f5osclient v1.0.8 + gitswarm.f5net.com/terraform-providers/f5osclient v1.0.9 golang.org/x/mod v0.17.0 ) diff --git a/go.sum b/go.sum index 182346e..46f5468 100644 --- a/go.sum +++ b/go.sum @@ -217,8 +217,8 @@ github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6e github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zclconf/go-cty v1.13.1 h1:0a6bRwuiSHtAmqCqNOE+c2oHgepv0ctoxU4FUe43kwc= github.com/zclconf/go-cty v1.13.1/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0= -gitswarm.f5net.com/terraform-providers/f5osclient v1.0.8 h1:XP4wzpY6Tziiyc3I5HFD59LY98mK2DG/tvtYkh5b4W0= -gitswarm.f5net.com/terraform-providers/f5osclient v1.0.8/go.mod h1:k+4tg9l6yO3FlFh3dkXGc+Wl+LOB/AXu302hAX6nhDU= +gitswarm.f5net.com/terraform-providers/f5osclient v1.0.9 h1:gp8/pfctubABn5els92mqYbE+n+ZHv1XQh4d37Vw94c= +gitswarm.f5net.com/terraform-providers/f5osclient v1.0.9/go.mod h1:k+4tg9l6yO3FlFh3dkXGc+Wl+LOB/AXu302hAX6nhDU= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= diff --git a/vendor/gitswarm.f5net.com/terraform-providers/f5osclient/f5os.go b/vendor/gitswarm.f5net.com/terraform-providers/f5osclient/f5os.go index 021caaf..9edaa3e 100644 --- a/vendor/gitswarm.f5net.com/terraform-providers/f5osclient/f5os.go +++ b/vendor/gitswarm.f5net.com/terraform-providers/f5osclient/f5os.go @@ -70,13 +70,17 @@ type F5os struct { Token string // if set, will be used instead of User/Password Transport *http.Transport // UserAgent is an optional field that specifies the caller of this request. - UserAgent string - Teem bool - ConfigOptions *ConfigOptions - PlatformType string - Metadata interface{} - PlatformVersion string - UriRoot string + UserAgent string + Teem bool + ConfigOptions *ConfigOptions + PlatformType string + Metadata interface{} + PlatformVersion string + UriRoot string + User string + Password string + DisableSSLVerify bool + Port int } type requestError struct { ErrorType string `json:"error-type,omitempty"` @@ -198,6 +202,11 @@ func NewSession(f5osObj *F5osConfig) (*F5os, error) { f5osSession.Host = urlString f5osSession.Transport = tr f5osSession.ConfigOptions = f5osObj.ConfigOptions + f5osSession.User = f5osObj.User + f5osSession.Password = f5osObj.Password + f5osSession.DisableSSLVerify = f5osObj.DisableSSLVerify + f5osSession.Port = f5osObj.Port + client := &http.Client{ Transport: tr, } @@ -267,38 +276,50 @@ func (p *F5os) doRequest(op, path string, body []byte) ([]byte, error) { if len(body) > 0 { f5osLogger.Debug("[doRequest]", "Request body", hclog.Fmt("%+v", string(body))) } - req, err := http.NewRequest(op, path, bytes.NewBuffer(body)) - if err != nil { - return nil, err - } - req.Header.Set("X-Auth-Token", p.Token) - req.Header.Set("Content-Type", contentTypeHeader) - client := &http.Client{ - Transport: p.Transport, - Timeout: p.ConfigOptions.APICallTimeout, - } - resp, err := client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - f5osLogger.Debug("[doRequest]", "Resp CODE", hclog.Fmt("%+v", resp.StatusCode)) - if resp.StatusCode == 200 || resp.StatusCode == 201 { - return io.ReadAll(resp.Body) - } - if resp.StatusCode == 404 { - // byteData, err := io.ReadAll(resp.Body) - // if err != nil { - // return nil, err - // } - // f5osLogger.Debug("[doRequest]", "Resp CODE", hclog.Fmt("%+v", string(byteData))) - return io.ReadAll(resp.Body) - } - if resp.StatusCode >= 400 { - byteData, _ := io.ReadAll(resp.Body) - var errorNew F5osError - json.Unmarshal(byteData, &errorNew) - return nil, errorNew.Error() + + retries := 3 + delay := 10 * time.Second + for i := 0; i < retries; i++ { + + req, err := http.NewRequest(op, path, bytes.NewBuffer(body)) + if err != nil { + return nil, err + } + req.Header.Set("X-Auth-Token", p.Token) + req.Header.Set("Content-Type", contentTypeHeader) + client := &http.Client{ + Transport: p.Transport, + Timeout: p.ConfigOptions.APICallTimeout, + } + + resp, err := client.Do(req) + if err != nil { + if !strings.Contains(err.Error(), "context deadline exceeded") { + return nil, err + } + } else { + defer resp.Body.Close() + if resp.StatusCode == 200 || resp.StatusCode == 201 || resp.StatusCode == 204 || resp.StatusCode == 404 { + f5osLogger.Debug("[doRequest]", "Resp code :", hclog.Fmt("%+v", resp.StatusCode)) + return io.ReadAll(resp.Body) + } + if resp.StatusCode == 401 && i != retries-1 { + var f5osObj = F5osConfig{Host: p.Host, User: p.User, Password: p.Password, Transport: p.Transport, UserAgent: p.UserAgent, Teem: p.Teem, ConfigOptions: p.ConfigOptions, DisableSSLVerify: p.DisableSSLVerify, Port: p.Port} + f5os, err := NewSession(&f5osObj) + if err != nil { + return nil, err + } + req.Header.Set("X-Auth-Token", f5os.Token) + req.Header.Set("Content-Type", contentTypeHeader) + } + if resp.StatusCode >= 400 && i == retries-1 { + byteData, _ := io.ReadAll(resp.Body) + var errorNew F5osError + json.Unmarshal(byteData, &errorNew) + return nil, errorNew.Error() + } + } + time.Sleep(delay) } return nil, nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index 2e29152..7d6d7c5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -302,7 +302,7 @@ github.com/zclconf/go-cty/cty/function/stdlib github.com/zclconf/go-cty/cty/gocty github.com/zclconf/go-cty/cty/json github.com/zclconf/go-cty/cty/set -# gitswarm.f5net.com/terraform-providers/f5osclient v1.0.8 +# gitswarm.f5net.com/terraform-providers/f5osclient v1.0.9 ## explicit; go 1.21.3 gitswarm.f5net.com/terraform-providers/f5osclient # golang.org/x/crypto v0.25.0