This is an automated email from the ASF dual-hosted git repository.

dangogh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git

commit f4ff48e895751d377f9f4d58f5395a94913d4032
Author: Dewayne Richardson <dewr...@apache.org>
AuthorDate: Mon Mar 5 11:53:54 2018 -0700

    added support for logout
---
 traffic_ops/client/v13/session.go | 61 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/traffic_ops/client/v13/session.go 
b/traffic_ops/client/v13/session.go
index c117d82..79b0368 100644
--- a/traffic_ops/client/v13/session.go
+++ b/traffic_ops/client/v13/session.go
@@ -142,6 +142,41 @@ func (to *Session) login() (net.Addr, error) {
        return remoteAddr, nil
 }
 
+// logout of Traffic Ops
+func (to *Session) logout() (net.Addr, error) {
+       credentials, err := loginCreds(to.UserName, to.Password)
+       if err != nil {
+               return nil, errors.New("creating login credentials: " + 
err.Error())
+       }
+
+       path := "/api/1.2/user/logout"
+       resp, remoteAddr, err := to.rawRequest("POST", path, credentials)
+       resp, remoteAddr, err = to.ErrUnlessOK(resp, remoteAddr, err, path)
+       if err != nil {
+               return remoteAddr, errors.New("requesting: " + err.Error())
+       }
+       defer resp.Body.Close()
+
+       var alerts tc.Alerts
+       if err := json.NewDecoder(resp.Body).Decode(&alerts); err != nil {
+               return remoteAddr, errors.New("decoding response JSON: " + 
err.Error())
+       }
+
+       success := false
+       for _, alert := range alerts.Alerts {
+               if alert.Level == "success" && alert.Text == "Successfully 
logged in." {
+                       success = true
+                       break
+               }
+       }
+
+       if !success {
+               return remoteAddr, fmt.Errorf("Logout failed, alerts string: 
%+v", alerts)
+       }
+
+       return remoteAddr, nil
+}
+
 // Login to traffic_ops, the response should set the cookie for this session
 // automatically. Start with
 //     to := traffic_ops.Login("user", "passwd", true)
@@ -172,6 +207,32 @@ func LoginWithAgent(toURL string, toUser string, toPasswd 
string, insecure bool,
        return to, remoteAddr, nil
 }
 
+// Logout of traffic_ops
+func LogoutWithAgent(toURL string, toUser string, toPasswd string, insecure 
bool, userAgent string, useCache bool, requestTimeout time.Duration) (*Session, 
net.Addr, error) {
+       options := cookiejar.Options{
+               PublicSuffixList: publicsuffix.List,
+       }
+
+       jar, err := cookiejar.New(&options)
+       if err != nil {
+               return nil, nil, err
+       }
+
+       to := NewSession(toUser, toPasswd, toURL, userAgent, &http.Client{
+               Timeout: requestTimeout,
+               Transport: &http.Transport{
+                       TLSClientConfig: &tls.Config{InsecureSkipVerify: 
insecure},
+               },
+               Jar: jar,
+       }, useCache)
+
+       remoteAddr, err := to.logout()
+       if err != nil {
+               return nil, remoteAddr, errors.New("logging out: " + 
err.Error())
+       }
+       return to, remoteAddr, nil
+}
+
 // ErrUnlessOk returns nil and an error if the given Response's status code is 
anything but 200 OK. This includes reading the Response.Body and Closing it. 
Otherwise, the given response and error are returned unchanged.
 func (to *Session) ErrUnlessOK(resp *http.Response, remoteAddr net.Addr, err 
error, path string) (*http.Response, net.Addr, error) {
        if err != nil {

-- 
To stop receiving notification emails like this one, please contact
dang...@apache.org.

Reply via email to