@@ -472,6 +472,11 @@ func (avisess *AviSession) initiateSession() error {
472472 cred ["password" ] = avisess .password
473473 }
474474
475+ if _ , exists := avisess .user_headers ["Authorization" ]; exists || avisess .CSP_ACCESS_TOKEN != "" {
476+ return nil
477+ }
478+ // Do not add any additional session related logic after this line!
479+
475480 rerror := avisess .Post ("login" , cred , res )
476481 if rerror != nil {
477482 glog .Errorf ("response error: %v " , rerror )
@@ -759,15 +764,19 @@ func (avisess *AviSession) newAviRequest(verb string, url string, payload io.Rea
759764 if tenant == "" {
760765 tenant = avisess .tenant
761766 }
767+
762768 if ! strings .HasSuffix (url , "login" ) && avisess .csrfToken != "" {
763769 req .Header ["X-CSRFToken" ] = []string {avisess .csrfToken }
764770 req .AddCookie (& http.Cookie {Name : "csrftoken" , Value : avisess .csrfToken })
765771 }
772+
766773 if tenant != "" {
767774 req .Header .Set ("X-Avi-Tenant" , tenant )
768775 }
769776
770- if ! strings .HasSuffix (url , "login" ) && avisess .sessionid != "" {
777+ _ , exists := req .Header ["Authorization" ]
778+
779+ if ! strings .HasSuffix (url , "login" ) && avisess .sessionid != "" && ! exists {
771780 req .AddCookie (& http.Cookie {Name : "sessionid" , Value : avisess .sessionid })
772781 req .AddCookie (& http.Cookie {Name : "avi-sessionid" , Value : avisess .sessionid })
773782 }
@@ -839,6 +848,7 @@ func (avisess *AviSession) restRequest(verb string, uri string, payload interfac
839848 return nil , errorResult
840849 }
841850 retryReq := false
851+ clientApiTimedOut := false // adding this flag to skip logins for client timeouts.
842852 resp , err := avisess .client .Do (req )
843853 if err != nil {
844854 // retry until controller status check limits.
@@ -849,6 +859,7 @@ func (avisess *AviSession) restRequest(verb string, uri string, payload interfac
849859 }
850860 debug (dump , dumpErr )
851861 retryReq = true
862+ clientApiTimedOut = true
852863 }
853864 if resp != nil && resp .StatusCode == 500 {
854865 if _ , err = avisess .fetchBody (verb , uri , resp ); err != nil {
@@ -867,7 +878,8 @@ func (avisess *AviSession) restRequest(verb string, uri string, payload interfac
867878 if uri == "login" {
868879 avisess .collectCookiesFromResp (resp )
869880 }
870- if resp .StatusCode == 401 && uri != "login" {
881+ _ , exists := req .Header ["Authorization" ]
882+ if resp .StatusCode == 401 && uri != "login" && ! exists {
871883 resp .Body .Close ()
872884 glog .Infof ("Retrying url %s; retry %d due to Status Code %d" , url , retry , resp .StatusCode )
873885 err := avisess .initiateSession ()
@@ -897,12 +909,14 @@ func (avisess *AviSession) restRequest(verb string, uri string, payload interfac
897909 glog .Errorf ("restRequest Error during checking controller state. Error: %s" , err )
898910 return httpResp , err
899911 }
900- if err := avisess .initiateSession (); err != nil {
901- if resp != nil && resp .Body != nil {
902- glog .Infof ("Body is not nil, close it." )
903- resp .Body .Close ()
912+ if uri != "login" && ! clientApiTimedOut {
913+ if err := avisess .initiateSession (); err != nil {
914+ if resp != nil && resp .Body != nil {
915+ glog .Infof ("Body is not nil, close it." )
916+ resp .Body .Close ()
917+ }
918+ return nil , err
904919 }
905- return nil , err
906920 }
907921 return avisess .restRequest (verb , uri , payload , tenant , errorResult , retry + 1 )
908922 } else {
0 commit comments