srijeet0406 commented on a change in pull request #5016: URL: https://github.com/apache/trafficcontrol/pull/5016#discussion_r488873569
########## File path: traffic_ops/testing/api/v3/topologies_queue_update_test.go ########## @@ -0,0 +1,127 @@ +package v3 + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "github.com/apache/trafficcontrol/lib/go-tc" Review comment: This line should go below all the other imports, with a blank line in between ########## File path: CHANGELOG.md ########## @@ -9,7 +9,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Traffic Ops API v3 - Added an optional readiness check service to cdn-in-a-box that exits successfully when it is able to get a `200 OK` from all delivery services - [Flexible Topologies (in progress)](https://github.com/apache/trafficcontrol/blob/master/blueprints/flexible-topologies.md) - - Traffic Ops: Added an API 3.0 endpoint, /api/3.0/topologies, to create, read, update and delete flexible topologies. + - Traffic Ops: Added an API 3.0 endpoint, `GET /api/3.0/topologies`, to create, read, update and delete flexible topologies. + - Traffic Ops: Added an API 3.0 endpoint, `POST /api/3.0/topologyes/{name}/queue_update`, to queue or dequeue updates for all servers assigned to the Cachegroups in a given Topology. Review comment: typo: topologies ########## File path: traffic_ops/traffic_ops_golang/topology/queue_update.go ########## @@ -0,0 +1,100 @@ +package topology + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "database/sql" + "encoding/json" + "errors" + "fmt" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/lib/go-tc/tovalidate" + "github.com/apache/trafficcontrol/lib/go-util" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers" + validation "github.com/go-ozzo/ozzo-validation" + "net/http" +) + +func Validate(reqObj tc.TopologiesQueueUpdateRequest, topologyName tc.TopologyName, tx *sql.Tx) error { + errorMap := validation.Errors{} + + if reqObj.Action != "queue" && reqObj.Action != "dequeue" { + errorMap["action"] = errors.New("action must be 'queue' or 'dequeue'") + } + + if _, cdnExists, err := dbhelpers.GetCDNNameFromID(tx, reqObj.CDNID); err != nil { + errorMap["cdnId"] = fmt.Errorf("could not check whether cdn exists for id %d", reqObj.CDNID) + } else if !cdnExists { + errorMap["cdnId"] = fmt.Errorf("no cdn exists with id %d", reqObj.CDNID) + } + + if topologyExists, err := dbhelpers.TopologyExists(tx, topologyName); err != nil { + errorMap["topology"] = fmt.Errorf("could not check whether topology %s exists", topologyName) + } else if !topologyExists { + errorMap["topology"] = fmt.Errorf("no topology exists by the name of %s", topologyName) + } + + return util.JoinErrs(tovalidate.ToErrors(errorMap)) +} + +func QueueUpdateHandler(w http.ResponseWriter, r *http.Request) { Review comment: Might be good to add a description comment here ########## File path: traffic_ops/traffic_ops_golang/topology/queue_update.go ########## @@ -0,0 +1,100 @@ +package topology + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "database/sql" + "encoding/json" + "errors" + "fmt" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/lib/go-tc/tovalidate" + "github.com/apache/trafficcontrol/lib/go-util" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers" + validation "github.com/go-ozzo/ozzo-validation" + "net/http" +) + +func Validate(reqObj tc.TopologiesQueueUpdateRequest, topologyName tc.TopologyName, tx *sql.Tx) error { + errorMap := validation.Errors{} + + if reqObj.Action != "queue" && reqObj.Action != "dequeue" { + errorMap["action"] = errors.New("action must be 'queue' or 'dequeue'") + } + + if _, cdnExists, err := dbhelpers.GetCDNNameFromID(tx, reqObj.CDNID); err != nil { + errorMap["cdnId"] = fmt.Errorf("could not check whether cdn exists for id %d", reqObj.CDNID) + } else if !cdnExists { + errorMap["cdnId"] = fmt.Errorf("no cdn exists with id %d", reqObj.CDNID) + } + + if topologyExists, err := dbhelpers.TopologyExists(tx, topologyName); err != nil { + errorMap["topology"] = fmt.Errorf("could not check whether topology %s exists", topologyName) + } else if !topologyExists { + errorMap["topology"] = fmt.Errorf("no topology exists by the name of %s", topologyName) + } + + return util.JoinErrs(tovalidate.ToErrors(errorMap)) +} + +func QueueUpdateHandler(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"name"}, []string{}) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + reqObj := tc.TopologiesQueueUpdateRequest{} + if err := json.NewDecoder(r.Body).Decode(&reqObj); err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, errors.New("malformed JSON: "+err.Error()), nil) + return + } + topologyName := tc.TopologyName(inf.Params["name"]) + if err := Validate(reqObj, topologyName, inf.Tx.Tx); err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, fmt.Errorf("invalid request to queue updates: %s", err), nil) + return + } + if err := queueUpdates(inf.Tx.Tx, topologyName, reqObj.CDNID, reqObj.Action == "queue"); err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("Topology queueing updates: "+err.Error())) + return + } + + message := fmt.Sprintf("TOPOLOGY: %s, ACTION: Topology server updates %sd", topologyName, reqObj.Action) + api.CreateChangeLogRawTx(api.ApiChange, message, inf.User, inf.Tx.Tx) + api.WriteResp(w, r, tc.TopologiesQueueUpdate{Action: reqObj.Action, CDNID: reqObj.CDNID, Topology: topologyName}) +} + +func queueUpdates(tx *sql.Tx, topologyName tc.TopologyName, cdnId int64, queue bool) error { + query := ` +UPDATE server s +SET upd_pending = $1 +FROM cachegroup c, topology_cachegroup tc, cdn +WHERE s.cachegroup = c.id +AND c."name" = tc.cachegroup +AND tc.topology = $2 +AND s.cdn_id = $3 +` + var err error + if _, err = tx.Exec(query, queue, topologyName, cdnId); err != nil { + err = fmt.Errorf("queueing updates: %s", err) Review comment: Should this be `err.Error()` instead? ########## File path: traffic_ops/traffic_ops_golang/topology/queue_update.go ########## @@ -0,0 +1,100 @@ +package topology + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "database/sql" + "encoding/json" + "errors" + "fmt" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/lib/go-tc/tovalidate" + "github.com/apache/trafficcontrol/lib/go-util" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers" + validation "github.com/go-ozzo/ozzo-validation" + "net/http" +) + +func Validate(reqObj tc.TopologiesQueueUpdateRequest, topologyName tc.TopologyName, tx *sql.Tx) error { + errorMap := validation.Errors{} + + if reqObj.Action != "queue" && reqObj.Action != "dequeue" { + errorMap["action"] = errors.New("action must be 'queue' or 'dequeue'") + } + + if _, cdnExists, err := dbhelpers.GetCDNNameFromID(tx, reqObj.CDNID); err != nil { + errorMap["cdnId"] = fmt.Errorf("could not check whether cdn exists for id %d", reqObj.CDNID) + } else if !cdnExists { + errorMap["cdnId"] = fmt.Errorf("no cdn exists with id %d", reqObj.CDNID) + } + + if topologyExists, err := dbhelpers.TopologyExists(tx, topologyName); err != nil { + errorMap["topology"] = fmt.Errorf("could not check whether topology %s exists", topologyName) + } else if !topologyExists { + errorMap["topology"] = fmt.Errorf("no topology exists by the name of %s", topologyName) + } + + return util.JoinErrs(tovalidate.ToErrors(errorMap)) +} + +func QueueUpdateHandler(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"name"}, []string{}) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + reqObj := tc.TopologiesQueueUpdateRequest{} + if err := json.NewDecoder(r.Body).Decode(&reqObj); err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, errors.New("malformed JSON: "+err.Error()), nil) + return + } + topologyName := tc.TopologyName(inf.Params["name"]) + if err := Validate(reqObj, topologyName, inf.Tx.Tx); err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, fmt.Errorf("invalid request to queue updates: %s", err), nil) + return + } + if err := queueUpdates(inf.Tx.Tx, topologyName, reqObj.CDNID, reqObj.Action == "queue"); err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("Topology queueing updates: "+err.Error())) + return + } + + message := fmt.Sprintf("TOPOLOGY: %s, ACTION: Topology server updates %sd", topologyName, reqObj.Action) + api.CreateChangeLogRawTx(api.ApiChange, message, inf.User, inf.Tx.Tx) + api.WriteResp(w, r, tc.TopologiesQueueUpdate{Action: reqObj.Action, CDNID: reqObj.CDNID, Topology: topologyName}) +} + +func queueUpdates(tx *sql.Tx, topologyName tc.TopologyName, cdnId int64, queue bool) error { + query := ` +UPDATE server s +SET upd_pending = $1 +FROM cachegroup c, topology_cachegroup tc, cdn +WHERE s.cachegroup = c.id +AND c."name" = tc.cachegroup Review comment: Does this need to be quoted? ########## File path: lib/go-tc/topologies.go ########## @@ -47,3 +47,25 @@ type TopologiesResponse struct { Response []Topology `json:"response"` Alerts } + +// TopologiesQueueUpdateRequest encodes the request data for the POST +// topologies/{{name}}/queue_update endpoint. +type TopologiesQueueUpdateRequest struct { + Action string `json:"action"` + CDNID int64 `json:"cdnId"` +} + +// TopologiesQueueUpdate encodes the response data for the POST Review comment: Should be `TopologiesQueueUpdateResponse` ########## File path: traffic_ops/traffic_ops_golang/topology/queue_update.go ########## @@ -0,0 +1,100 @@ +package topology + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "database/sql" + "encoding/json" + "errors" + "fmt" + "github.com/apache/trafficcontrol/lib/go-tc" Review comment: These imports are not ordered correctly ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
