dangogh commented on a change in pull request #1993: [Issue-1617] - Traffic Ops 
Keeps track of configuration differences between database and Traffic Servers
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/1993#discussion_r173950952
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/cfgdiffs.go
 ##########
 @@ -0,0 +1,303 @@
+package main
+
+import (
+       "database/sql"
+       "encoding/json"
+       "errors"
+       "fmt"
+       "net/http"
+
+       "github.com/apache/incubator-trafficcontrol/lib/go-log"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
+
+       "github.com/jmoiron/sqlx"
+)
+
+const CfgDiffsPrivLevel = auth.PrivLevelReadOnly
+const CfgDiffsWritePrivLevel = auth.PrivLevelOperations
+
+type CfgFileDiffs struct {
+       FileName         string   `json:"fileName"`
+       DBLinesMissing   []string `json:"dbLinesMissing"`
+       DiskLinesMissing []string `json:"diskLinesMissing"`
+       ReportTimestamp  string   `json:"timestamp"`
+}
+
+type CfgFileDiffsResponse struct {
+       Response []CfgFileDiffs `json:"response"`
+}
+
+type ServerExistsMethod func(db *sqlx.DB, hostname string) (bool, error)
+type UpdateCfgDiffsMethod func(db *sqlx.DB, hostname string, diffs 
CfgFileDiffs) (bool, error)
+type InsertCfgDiffsMethod func(db *sqlx.DB, hostname string, diffs 
CfgFileDiffs) error
+type GetCfgDiffsMethod func(db *sqlx.DB, hostName string) ([]CfgFileDiffs, 
error)
+
+func getCfgDiffsHandler(db *sqlx.DB) http.HandlerFunc {
+       return func(w http.ResponseWriter, r *http.Request) {
+               handleErr := func(err error, status int) {
+                       log.Errorf("%v %v\n", r.RemoteAddr, err)
+                       w.WriteHeader(status)
+                       fmt.Fprintf(w, http.StatusText(status))
+               }
+               ctx := r.Context()
+               pathParams, err := getPathParams(ctx)
+               if err != nil {
+                       handleErr(err, http.StatusInternalServerError)
+                       return
+               }
+
+               hostName := pathParams["host-name"]
+
+               resp, err := getCfgDiffsJson(hostName, db, getCfgDiffs)
+               if err != nil {
+                       handleErr(err, http.StatusInternalServerError)
+                       return
+               }
+
+               // if the response has a length of zero, no results were found 
for that server
+               if len(resp.Response) == 0 {
+                       w.WriteHeader(http.StatusNotFound)
+                       return
+               }
+
+               respBts, err := json.Marshal(resp)
+               if err != nil {
+                       handleErr(err, http.StatusInternalServerError)
+                       return
+               }
+
+               w.Header().Set("Content-Type", "application/json")
+               fmt.Fprintf(w, "%s", respBts)
+       }
+}
+
+func putCfgDiffsHandler(db *sqlx.DB) http.HandlerFunc {
+       return func(w http.ResponseWriter, r *http.Request) {
+               handleErr := func(err error, status int) {
+                       log.Errorf("%v %v\n", r.RemoteAddr, err)
+                       w.WriteHeader(status)
+                       fmt.Fprintf(w, http.StatusText(status))
+               }
+               ctx := r.Context()
+               pathParams, err := getPathParams(ctx)
+               if err != nil {
+                       handleErr(err, http.StatusInternalServerError)
+                       return
+               }
+
+               hostName := pathParams["host-name"]
+               configName := pathParams["cfg-file-name"]
+
+               decoder := json.NewDecoder(r.Body)
+               var diffs CfgFileDiffs
+               err = decoder.Decode(&diffs)
+               if err != nil {
+                       handleErr(err, http.StatusBadRequest)
+                       return
+               }
+
+               defer r.Body.Close()
+
+               diffs.FileName = configName
+
+               result, err := putCfgDiffs(db, hostName, diffs, serverExists, 
updateCfgDiffs, insertCfgDiffs)
+               if err != nil {
+                       handleErr(err, http.StatusInternalServerError)
+                       return
+               }
+
+               // Not found (invalid hostname)
+               if result == 0 { // This keeps happening
+                       w.WriteHeader(404)
+                       return
+               }
+               // Created (newly added)
+               if result == 1 {
+                       w.WriteHeader(201)
+                       return
+               }
+               // Updated (already existed)
+               if result == 2 {
+                       w.WriteHeader(202)
+                       return
+               }
+       }
+}
+
+func serverExists(db *sqlx.DB, hostName string) (bool, error) {
 
 Review comment:
   shouldn't this also include `domainName` to make sure it's unique?

----------------------------------------------------------------
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

Reply via email to