You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2021/11/17 21:30:27 UTC

[GitHub] [trafficcontrol] rawlinp commented on a change in pull request #6355: Make t3c jobs work with previous TO 4.0

rawlinp commented on a change in pull request #6355:
URL: https://github.com/apache/trafficcontrol/pull/6355#discussion_r751646833



##########
File path: cache-config/t3cutil/toreq/conversions.go
##########
@@ -40,3 +48,93 @@ func dsesToLatest(dses []tc.DeliveryServiceV40) []atscfg.DeliveryService {
 func jobsToLatest(jobs []tc.InvalidationJobV4) []atscfg.InvalidationJob {
 	return atscfg.ToInvalidationJobs(jobs)
 }
+
+// GetJobsCompat gets jobs 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) GetJobsCompat(opts toclient.RequestOptions) (tc.InvalidationJobsResponseV4, toclientlib.ReqInf, error) {
+	path := "/jobs"
+
+	objs := struct {
+		Response []InvalidationJobV4PlusLegacy `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.InvalidationJobsResponseV4{}, reqInf, errors.New("request: " + err.Error())
+	}
+
+	resp := tc.InvalidationJobsResponseV4{Alerts: objs.Alerts}
+	for _, job := range objs.Response {
+		newJob, err := InvalidationJobV4FromLegacy(job) // (InvalidationJobV4, error) {
+		if err != nil {
+			return tc.InvalidationJobsResponseV4{}, reqInf, errors.New("converting job from possible legacy format: " + err.Error())
+		}
+		resp.Response = append(resp.Response, newJob)
+	}
+	return resp, reqInf, nil
+}
+
+// InvalidationJobV4ForLegacy is a type alias to prevent MarshalJSON recursion.
+type InvalidationJobV4ForLegacy tc.InvalidationJobV4
+
+// InvalidationJobV4PlusLegacy has the data to deserialize both the latest and older versions that Traffic Ops could return.
+type InvalidationJobV4PlusLegacy struct {
+	StartTime *string `json:"startTime"`

Review comment:
       Just for my own edification, even though `InvalidationJobV4ForLegacy` contains a `StartTime` field with the same json tag, the JSON unmarshaller will unmarshal the `"startTime"` value into _this_ field because it's top-level rather than in an embedded field? If so, it would be good to comment something along those lines for future reference.

##########
File path: cache-config/t3cutil/toreq/clientfuncs.go
##########
@@ -531,7 +531,7 @@ func (cl *TOClient) GetJobs(reqHdr http.Header, cdnName string) ([]atscfg.Invali
 		opts := *ReqOpts(reqHdr)
 		opts.QueryParameters.Set("maxRevalDurationDays", "") // only get jobs with a start time within the window defined by the GLOBAL parameter 'maxRevalDurationDays'
 		opts.QueryParameters.Set("cdn", cdnName)             // only get jobs for delivery services in this server's CDN
-		toJobs, toReqInf, err := cl.c.GetInvalidationJobs(opts)
+		toJobs, toReqInf, err := cl.GetJobsCompat(opts)

Review comment:
       Should we add a comment along the lines of being able to switch back to `cl.c.GetInvalidationJobs` after the 6.1 release?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org