elsloo closed pull request #2304: Add TO Go cdns/id/queue_updates URL: https://github.com/apache/incubator-trafficcontrol/pull/2304
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/traffic_ops/traffic_ops_golang/cdn/queue.go b/traffic_ops/traffic_ops_golang/cdn/queue.go new file mode 100644 index 000000000..d3e0a36f0 --- /dev/null +++ b/traffic_ops/traffic_ops_golang/cdn/queue.go @@ -0,0 +1,79 @@ +package cdn + +/* + * 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" + "net/http" + + "github.com/apache/incubator-trafficcontrol/lib/go-log" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth" +) + +type QueueReq struct { + Action string `json:"action"` +} + +type QueueResp struct { + Action string `json:"action"` + CDNID int64 `json:"cdnId"` +} + +func Queue(db *sql.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + user, err := auth.GetCurrentUser(r.Context()) + if err != nil { + api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("getting user: "+err.Error())) + return + } + params, intParams, userErr, sysErr, errCode := api.AllParams(r, []string{"id"}) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, errCode, userErr, sysErr) + return + } + reqObj := QueueReq{} + if err := json.NewDecoder(r.Body).Decode(&reqObj); err != nil { + api.HandleErr(w, r, http.StatusBadRequest, errors.New("malformed JSON: "+err.Error()), nil) + return + } + if reqObj.Action != "queue" && reqObj.Action != "dequeue" { + api.HandleErr(w, r, http.StatusBadRequest, errors.New("action must be 'queue' or 'dequeue'"), nil) + return + } + if err := queueUpdates(db, int64(intParams["id"]), reqObj.Action == "queue"); err != nil { + api.HandleErr(w, r, http.StatusInternalServerError, nil, errors.New("CDN queueing updates: "+err.Error())) + return + } + api.WriteResp(w, r, QueueResp{Action: reqObj.Action, CDNID: int64(intParams["id"])}) + if err := api.CreateChangeLogRaw(api.ApiChange, "Server updates "+reqObj.Action+"d for cdn "+params["id"], *user, db); err != nil { + log.Errorln("creating cdn queue updates changelog: " + err.Error()) + } + } +} + +func queueUpdates(db *sql.DB, cdnID int64, queue bool) error { + if _, err := db.Exec(`UPDATE server SET upd_pending = $1 WHERE server.cdn_id = $2`, queue, cdnID); err != nil { + return errors.New("querying queue updates: " + err.Error()) + } + return nil +} diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go index 9a9f43a66..45fcce42e 100644 --- a/traffic_ops/traffic_ops_golang/routes.go +++ b/traffic_ops/traffic_ops_golang/routes.go @@ -110,8 +110,8 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) { {1.1, http.MethodPut, `cdns/{id}$`, api.UpdateHandler(cdn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, {1.1, http.MethodPost, `cdns/?$`, api.CreateHandler(cdn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, {1.1, http.MethodDelete, `cdns/{id}$`, api.DeleteHandler(cdn.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, - {1.1, http.MethodDelete, `cdns/name/{name}$`, cdn.DeleteName(d.DB.DB), auth.PrivLevelOperations, Authenticated, nil}, + {1.1, http.MethodPost, `cdns/{id}/queue_update$`, cdn.Queue(d.DB.DB), auth.PrivLevelOperations, Authenticated, nil}, //CDN: Monitoring: Traffic Monitor {1.1, http.MethodGet, `cdns/{name}/configs/monitoring(\.json)?$`, monitoringHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services