[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #4030: Rewrite PUT /api/1.1/servers/:id/status to Go

2019-10-29 Thread GitBox
ocket commented on a change in pull request #4030: Rewrite PUT 
/api/1.1/servers/:id/status to Go
URL: https://github.com/apache/trafficcontrol/pull/4030#discussion_r340269064
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/put_status.go
 ##
 @@ -0,0 +1,137 @@
+package server
+
+/*
+ * 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"
+   "net/http"
+   "strings"
+
+   "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"
+)
+
+func UpdateStatusHandler(w http.ResponseWriter, r *http.Request) {
+   inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"id"}, 
[]string{"id"})
+   if userErr != nil || sysErr != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+   return
+   }
+   defer inf.Close()
+   reqObj := tc.ServerPutStatus{}
+   if err := json.NewDecoder(r.Body).Decode(); err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, 
errors.New("malformed JSON: "+err.Error()), nil)
+   return
+   }
+
+   serverInfo, exists, err := dbhelpers.GetServerInfo(inf.IntParams["id"], 
inf.Tx.Tx)
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.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)
+   return
+   }
+
+   status := tc.StatusNullable{}
+   statusExists := false
+   if reqObj.Status.Name != nil {
+   status, statusExists, err = 
dbhelpers.GetStatusByName(*reqObj.Status.Name, inf.Tx.Tx)
+   } else if reqObj.Status.ID != nil {
+   status, statusExists, err = 
dbhelpers.GetStatusByID(*reqObj.Status.ID, inf.Tx.Tx)
+   } else {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusBadRequest, 
errors.New("status is required"), nil)
+   return
+   }
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.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)
+   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)
+   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)
+   return
+   }
+   offlineReason := ""
+   if reqObj.OfflineReason != nil {
+   offlineReason = *reqObj.OfflineReason
+   }
+   msg := "Updated status [ " + *status.Name + " ] for " + 
serverInfo.HostName + "." + serverInfo.DomainName + " [ " + offlineReason + " ]"
+
+   // queue updates on child servers if server is ^EDGE or ^MID
+   if strings.HasPrefix(serverInfo.Type, tc.CacheTypeEdge.String()) || 
strings.HasPrefix(serverInfo.Type, tc.CacheTypeMid.String()) {
 
 Review comment:
   Is this how Perl checked, with a strict prefix? I thought it was more like 
`/.*(MID|EDGE).*/` but I'll take your word for it.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to 

[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #4030: Rewrite PUT /api/1.1/servers/:id/status to Go

2019-10-29 Thread GitBox
ocket commented on a change in pull request #4030: Rewrite PUT 
/api/1.1/servers/:id/status to Go
URL: https://github.com/apache/trafficcontrol/pull/4030#discussion_r340265485
 
 

 ##
 File path: traffic_ops/testing/api/v14/serverupdatestatus_test.go
 ##
 @@ -0,0 +1,149 @@
+package v14
+
+/*
+
+   Licensed 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 (
+   "testing"
+
+   "github.com/apache/trafficcontrol/lib/go-tc"
+   "github.com/apache/trafficcontrol/lib/go-util"
+)
+
+func TestServerUpdateStatus(t *testing.T) {
+   WithObjs(t, []TCObj{CDNs, Types, Parameters, Profiles, Statuses, 
Divisions, Regions, PhysLocations, CacheGroups, Servers}, func() {
+   UpdateTestServerStatus(t)
+   })
+}
+
+func UpdateTestServerStatus(t *testing.T) {
+
+   edge1cdn1 := tc.Server{}
+   edge2cdn1 := tc.Server{}
+   mid1cdn1 := tc.Server{}
+   edge1cdn2 := tc.Server{}
+
+   getServers := func() {
+   for _, s := range []struct {
+   name   string
+   server *tc.Server
+   }{
+   {
+   "atlanta-edge-01",
+   ,
+   },
+   {
+   "atlanta-edge-03",
+   ,
+   },
+   {
+   "atlanta-mid-16",
+   ,
+   },
+   {
+   "edge1-cdn2",
+   ,
+   },
+   } {
+   resp, _, err := TOSession.GetServerByHostName(s.name)
+   if err != nil {
+   t.Errorf("cannot GET Server by hostname: %v - 
%v\n", s.name, err)
+   }
+   *s.server = resp[0]
+   }
+   }
+   getServers()
+
+   // assert that servers don't have updates pending
+   for _, s := range []tc.Server{
+   edge1cdn1,
+   edge2cdn1,
+   mid1cdn1,
+   edge1cdn2,
+   } {
+   if s.UpdPending {
+   t.Errorf("expected UpdPending: false, actual: true")
+   }
+   }
+
+   // update status of MID server to OFFLINE
+   _, _, err := TOSession.UpdateServerStatus(mid1cdn1.ID, 
tc.ServerPutStatus{
+   Status:util.JSONNameOrIDStr{Name: 
util.StrPtr("OFFLINE")},
+   OfflineReason: util.StrPtr("testing")})
+   if err != nil {
+   t.Errorf("cannot update server status: %v", err)
+   }
+
+   // assert that updates were queued for the proper EDGE servers
+   getServers()
+   if !edge1cdn1.UpdPending {
+   t.Errorf("expected: child %s to have updates pending, actual: 
no updates pending", edge1cdn1.HostName)
+   }
+   if !edge2cdn1.UpdPending {
+   t.Errorf("expected: child %s to have updates pending, actual: 
no updates pending", edge2cdn1.HostName)
+   }
+   if mid1cdn1.UpdPending {
+   t.Errorf("expected: server %s with updated status to have no 
updates pending, actual: updates pending", mid1cdn1.HostName)
+   }
+   if edge1cdn2.UpdPending {
+   t.Errorf("expected: server %s in different CDN than server with 
updated status to have no updates pending, actual: updates pending", 
edge2cdn1.HostName)
+   }
+
+   // update status of MID server to OFFLINE via ID
+   status, _, err := TOSession.GetStatusByName("OFFLINE")
+   if err != nil {
+   t.Errorf("cannot GET status by name: %v", err)
 
 Review comment:
   error should be fatal, as when an error occurs `status` could be `nil` (in 
fact the way the method is written I think it has to be)


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:
us...@infra.apache.org


With regards,
Apache Git Services