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



##########
File path: traffic_ops/traffic_ops_golang/server/servers.go
##########
@@ -359,267 +737,482 @@ WHERE s.id IN (` + edgeIDs + `)))
        return mids, nil, nil, http.StatusOK
 }
 
-func (s *TOServer) Update() (error, error, int) {
-       if s.IP6Address != nil && len(strings.TrimSpace(*s.IP6Address)) == 0 {
-               s.IP6Address = nil
-       }
-       // see if type changed
-       typeID := -1
-       // see if cdn changed
-       cdnId := -1
-
-       if err := s.APIInfo().Tx.QueryRow("SELECT type, cdn_id FROM server 
WHERE id = $1", s.ID).Scan(&typeID, &cdnId); err != nil {
+func checkTypeChangeSafety(server tc.CommonServerProperties, tx *sqlx.Tx) 
(error, error, int) {
+       // see if cdn or type changed
+       var cdnID int
+       var typeID int
+       if err := tx.QueryRow("SELECT type, cdn_id FROM server WHERE id = $1", 
*server.ID).Scan(&typeID, &cdnID); err != nil {
                if err == sql.ErrNoRows {
-                       return errors.New("no server found with this id"), nil, 
http.StatusNotFound
+                       return errors.New("no server found with this ID"), nil, 
http.StatusNotFound
                }
                return nil, fmt.Errorf("getting current server type: %v", err), 
http.StatusInternalServerError
        }
 
-       dsIDs := []int64{}
-       if err := s.APIInfo().Tx.QueryRowx("SELECT ARRAY(SELECT deliveryservice 
FROM deliveryservice_server WHERE server = $1)", s.ID).Scan(pq.Array(&dsIDs)); 
err != nil && err != sql.ErrNoRows {
+       var dsIDs []int64
+       if err := tx.QueryRowx("SELECT ARRAY(SELECT deliveryservice FROM 
deliveryservice_server WHERE server = $1)", server.ID).Scan(pq.Array(&dsIDs)); 
err != nil && err != sql.ErrNoRows {
                return nil, fmt.Errorf("getting server assigned delivery 
services: %v", err), http.StatusInternalServerError
        }
+       // If type is changing ensure it isn't assigned to any DSes.
+       if typeID != *server.TypeID {
+               if len(dsIDs) != 0 {
+                       return errors.New("server type can not be updated when 
it is currently assigned to Delivery Services"), nil, http.StatusConflict
+               }
+       }
        // Check to see if the user is trying to change the CDN of a server, 
which is already linked with a DS
-       if cdnId != *s.CDNID && len(dsIDs) != 0 {
+       if cdnID != *server.CDNID && len(dsIDs) != 0 {
                return errors.New("server cdn can not be updated when it is 
currently assigned to delivery services"), nil, http.StatusConflict
        }
-       // If type is changing ensure it isn't assigned to any DSes.
-       if typeID != *s.TypeID {
-               if len(dsIDs) != 0 {
-                       return errors.New("server type can not be updated when 
it is currently assigned to delivery services"), nil, http.StatusConflict
+
+       return nil, nil, http.StatusOK
+}
+
+func createInterfaces(id int, interfaces []tc.ServerInterfaceInfo, tx *sql.Tx) 
(error, error, int) {
+       ifaceQry := `
+       INSERT INTO interface (
+               max_bandwidth,
+               monitor,
+               mtu,
+               name,
+               server
+       ) VALUES
+       `
+       ipQry := `
+       INSERT INTO ip_address (
+               address,
+               gateway,
+               interface,
+               server,
+               service_address
+       ) VALUES
+       `
+
+       ifaceQueryParts := make([]string, 0, len(interfaces))
+       ipQueryParts := make([]string, 0, len(interfaces))
+       ifaceArgs := make([]interface{}, 0, len(interfaces))
+       ipArgs := make([]interface{}, 0, len(interfaces))
+       for i, iface := range interfaces {
+               argStart := i * 5
+               ifaceQueryParts = append(ifaceQueryParts, fmt.Sprintf("($%d, 
$%d, $%d, $%d, $%d)", argStart+1, argStart+2, argStart+3, argStart+4, 
argStart+5))
+               ifaceArgs = append(ifaceArgs, iface.MaxBandwidth, 
iface.Monitor, iface.MTU, iface.Name, id)
+               for _, ip := range iface.IPAddresses {
+                       argStart = len(ipArgs)
+                       ipQueryParts = append(ipQueryParts, fmt.Sprintf("($%d, 
$%d, $%d, $%d, $%d)", argStart+1, argStart+2, argStart+3, argStart+4, 
argStart+5))
+                       ipArgs = append(ipArgs, ip.Address, ip.Gateway, 
iface.Name, id, ip.ServiceAddress)
                }
        }
 
-       current := TOServer{}
-       err := s.ReqInfo.Tx.QueryRowx(selectV20UpdatesQuery()+` WHERE 
sv.id=$1`, strconv.Itoa(*s.ID)).StructScan(&current)
+       ifaceQry += strings.Join(ifaceQueryParts, ",")
+       log.Debugf("Inserting interfaces for new server, query is: %s", 
ifaceQry)
+
+       ifaceRows, err := tx.Query(ifaceQry, ifaceArgs...)

Review comment:
       TBH I don't even really care about the rows affected, I just ran into 
problems with not exhausting the rows so I counted them.




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