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



##########
File path: traffic_ops/traffic_ops_golang/server/servers.go
##########
@@ -1303,13 +1326,51 @@ func Update(w http.ResponseWriter, r *http.Request) {
 
        server.ID = new(int)
        *server.ID = inf.IntParams["id"]
+       status, ok, err := dbhelpers.GetStatusByID(*server.StatusID, tx)
+       if err != nil {
+               sysErr = fmt.Errorf("getting server #%d status (#%d): %v", id, 
*server.StatusID, err)
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
sysErr)
+               return
+       }
+       if !ok {
+               log.Warnf("previously existent status #%d not found when 
fetching later", *server.StatusID)
+               userErr = fmt.Errorf("no such Status: #%d", *server.StatusID)
+               api.HandleErr(w, r, tx, http.StatusBadRequest, userErr, nil)
+               return
+       }
+       if status.Name == nil {
+               sysErr = fmt.Errorf("status #%d had no name", *server.StatusID)
+               api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, 
sysErr)
+               return
+       }
+       if *status.Name != "ONLINE" && *status.Name != "REPORTED" {
+               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))
+                       for _, dsID := range dsIDs {
+                               alert := tc.Alert{
+                                       Level: tc.ErrorLevel.String(),
+                                       Text:  fmt.Sprintf("setting server 
status to '%s' would leave Delivery Service #%d with no 'ONLINE' or 'REPORTED' 
servers", *status.Name, dsID),
+                               }
+                               alerts = append(alerts, alert)
+                       }
+                       api.WriteAlerts(w, r, http.StatusConflict, 
tc.Alerts{Alerts: alerts})
+                       return
+               }
+       }
+       server.ID = &id

Review comment:
       it is not




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