ocket8888 commented on a change in pull request #5328:
URL: https://github.com/apache/trafficcontrol/pull/5328#discussion_r535733339



##########
File path: traffic_ops/traffic_ops_golang/server/put_status.go
##########
@@ -24,69 +24,94 @@ import (
        "encoding/json"
        "errors"
        "fmt"
-       "github.com/apache/trafficcontrol/lib/go-log"
        "net/http"
        "strings"
        "time"
 
+       "github.com/apache/trafficcontrol/lib/go-log"
        "github.com/apache/trafficcontrol/lib/go-tc"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
        
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 )
 
+// UpdateStatusHandler is the handler for PUT requests to the 
/servers/{{ID}}/status API endpoint.
 func UpdateStatusHandler(w http.ResponseWriter, r *http.Request) {
        inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"id"}, 
[]string{"id"})
+       tx := inf.Tx.Tx
        if userErr != nil || sysErr != nil {
-               api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+               api.HandleErr(w, r, tx, errCode, userErr, sysErr)
                return
        }
        defer inf.Close()
        reqObj := tc.ServerPutStatus{}
        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)
+               api.HandleErr(w, r, tx, http.StatusBadRequest, 
errors.New("malformed JSON: "+err.Error()), nil)
                return
        }
 
-       serverInfo, exists, err := dbhelpers.GetServerInfo(inf.IntParams["id"], 
inf.Tx.Tx)
+       id := inf.IntParams["id"]
+       serverInfo, exists, err := dbhelpers.GetServerInfo(id, tx)
        if err != nil {
-               api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, err)
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
err)
                return
        }
        if !exists {
-               api.HandleErr(w, r, inf.Tx.Tx, http.StatusNotFound, 
fmt.Errorf("server ID %d not found", inf.IntParams["id"]), nil)
+               api.HandleErr(w, r, tx, http.StatusNotFound, fmt.Errorf("server 
ID %d not found", id), nil)
                return
        }
 
        status := tc.StatusNullable{}
        statusExists := false
        if reqObj.Status.Name != nil {
-               status, statusExists, err = 
dbhelpers.GetStatusByName(*reqObj.Status.Name, inf.Tx.Tx)
+               status, statusExists, err = 
dbhelpers.GetStatusByName(*reqObj.Status.Name, tx)
        } else if reqObj.Status.ID != nil {
-               status, statusExists, err = 
dbhelpers.GetStatusByID(*reqObj.Status.ID, inf.Tx.Tx)
+               status, statusExists, err = 
dbhelpers.GetStatusByID(*reqObj.Status.ID, tx)
        } else {
-               api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, 
errors.New("status is required"), nil)
+               api.HandleErr(w, r, tx, http.StatusBadRequest, 
errors.New("status is required"), nil)
                return
        }
        if err != nil {
-               api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, err)
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
err)
                return
        }
        if !statusExists {
-               api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, 
errors.New("invalid status (does not exist)"), nil)
+               api.HandleErr(w, r, tx, http.StatusBadRequest, 
errors.New("invalid status (does not exist)"), nil)
                return
        }
 
        if *status.Name == tc.CacheStatusAdminDown.String() || *status.Name == 
tc.CacheStatusOffline.String() {
                if reqObj.OfflineReason == nil {
-                       api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, 
errors.New("offlineReason is required for "+tc.CacheStatusAdminDown.String()+" 
or "+tc.CacheStatusOffline.String()+" status"), nil)
+                       api.HandleErr(w, r, tx, http.StatusBadRequest, 
errors.New("offlineReason is required for "+tc.CacheStatusAdminDown.String()+" 
or "+tc.CacheStatusOffline.String()+" status"), nil)
                        return
                }
                *reqObj.OfflineReason = inf.User.UserName + ": " + 
*reqObj.OfflineReason
        } else {
                reqObj.OfflineReason = nil
        }
-       if err := updateServerStatusAndOfflineReason(inf.IntParams["id"], 
*status.ID, reqObj.OfflineReason, inf.Tx.Tx); err != nil {
-               api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, err)
+
+       existingStatus, existingStatusUpdatedTime := 
checkExistingStatusInfo(id, tx)
+       if *status.Name != "ONLINE" && *status.Name != "REPORTED" && *status.ID 
!= existingStatus {
+               dsIDs, err := 
getDeliveryServicesThatOnlyHaveThisServerAssigned(id, tx)
+               if err != nil {
+                       sysErr = fmt.Errorf("getting Delivery Services to which 
server #%d is assigned that have no other servers: %v", id, err)
+                       api.HandleErr(w, r, tx, http.StatusInternalServerError, 
nil, sysErr)
+                       return
+               }
+               if len(dsIDs) > 0 {
+                       alerts := make([]tc.Alert, 0, len(dsIDs))

Review comment:
       I guess there's not really a good reason. Plus I'm not sure TP can 
handle that right now.




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


Reply via email to