You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by zr...@apache.org on 2022/05/13 22:29:02 UTC

[trafficcontrol] branch master updated: Fix t3c for TO resp with profile or profileNames (#6823)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new c927950f06 Fix t3c for TO resp with profile or profileNames (#6823)
c927950f06 is described below

commit c927950f064030cb9f36db087272c71e75147f0f
Author: Robert O Butts <ro...@users.noreply.github.com>
AuthorDate: Fri May 13 16:28:56 2022 -0600

    Fix t3c for TO resp with profile or profileNames (#6823)
---
 cache-config/t3cutil/toreq/clientfuncs.go |  4 +--
 cache-config/t3cutil/toreq/conversions.go | 49 +++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/cache-config/t3cutil/toreq/clientfuncs.go b/cache-config/t3cutil/toreq/clientfuncs.go
index 4efeef2adb..54ea949607 100644
--- a/cache-config/t3cutil/toreq/clientfuncs.go
+++ b/cache-config/t3cutil/toreq/clientfuncs.go
@@ -130,7 +130,7 @@ func (cl *TOClient) GetServers(reqHdr http.Header) ([]atscfg.Server, toclientlib
 	servers := []atscfg.Server{}
 	reqInf := toclientlib.ReqInf{}
 	err := torequtil.GetRetry(cl.NumRetries, "servers", &servers, func(obj interface{}) error {
-		toServers, toReqInf, err := cl.c.GetServers(*ReqOpts(reqHdr))
+		toServers, toReqInf, err := cl.GetServersCompat(*ReqOpts(reqHdr))
 		if err != nil {
 			return errors.New("getting servers from Traffic Ops '" + torequtil.MaybeIPStr(reqInf.RemoteAddr) + "': " + err.Error())
 		}
@@ -158,7 +158,7 @@ func (cl *TOClient) GetServerByHostName(serverHostName string, reqHdr http.Heade
 	err := torequtil.GetRetry(cl.NumRetries, "server-name-"+serverHostName, &server, func(obj interface{}) error {
 		params := url.Values{}
 		params.Add("hostName", serverHostName)
-		toServers, toReqInf, err := cl.c.GetServers(toclient.RequestOptions{
+		toServers, toReqInf, err := cl.GetServersCompat(toclient.RequestOptions{
 			QueryParameters: params,
 			Header:          reqHdr,
 		})
diff --git a/cache-config/t3cutil/toreq/conversions.go b/cache-config/t3cutil/toreq/conversions.go
index 74c232493a..ab098a5eaa 100644
--- a/cache-config/t3cutil/toreq/conversions.go
+++ b/cache-config/t3cutil/toreq/conversions.go
@@ -192,3 +192,52 @@ func (cl *TOClient) SetServerUpdateStatusCompat(serverName string, configApplyTi
 	reqInf, err := cl.c.TOClient.Req(http.MethodPost, path, nil, opts.Header, &alerts)
 	return alerts, reqInf, err
 }
+
+// GetServersCompat gets servers from any Traffic Ops built from the ATC `master` branch, and converts the different formats to the latest.
+// This makes t3c work with old or new Traffic Ops deployed from `master`,
+// though it doesn't make a version of t3c older than this work with a new TO,
+// which isn't logically possible from the client.
+func (cl *TOClient) GetServersCompat(opts toclient.RequestOptions) (tc.ServersV4Response, toclientlib.ReqInf, error) {
+	path := "/servers"
+	objs := struct {
+		Response []ServerV40PlusLegacy `json:"response"`
+		tc.Alerts
+	}{}
+
+	if len(opts.QueryParameters) > 0 {
+		path += "?" + opts.QueryParameters.Encode()
+	}
+	reqInf, err := cl.c.TOClient.Req(http.MethodGet, path, nil, opts.Header, &objs)
+	if err != nil {
+		return tc.ServersV4Response{}, reqInf, errors.New("request: " + err.Error())
+	}
+
+	resp := tc.ServersV4Response{Alerts: objs.Alerts}
+	for _, sv := range objs.Response {
+		newSv, err := ServerV40FromLegacy(sv)
+		if err != nil {
+			return tc.ServersV4Response{}, reqInf, errors.New("converting server from possible legacy format: " + err.Error())
+		}
+		resp.Response = append(resp.Response, newSv)
+	}
+	return resp, reqInf, nil
+}
+
+type ServerV40PlusLegacy struct {
+	tc.ServerV40
+	Profile     string `json:"profile" db:"profile"`
+	ProfileDesc string `json:"profileDesc" db:"profile_desc"`
+	ProfileID   int    `json:"profileId" db:"profile_id"`
+}
+
+func ServerV40FromLegacy(old ServerV40PlusLegacy) (tc.ServerV40, error) {
+	new := old.ServerV40
+	if len(new.ProfileNames) != 0 {
+		return new, nil
+	}
+	if old.Profile == "" {
+		return tc.ServerV40{}, errors.New("got server with neither profileNames nor profile")
+	}
+	new.ProfileNames = []string{old.Profile}
+	return new, nil
+}