ocket8888 commented on code in PR #6996: URL: https://github.com/apache/trafficcontrol/pull/6996#discussion_r940520327
########## docs/source/api/v4/multiple_server_capabilities.rst: ########## @@ -0,0 +1,83 @@ +.. +.. +.. 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. +.. + +.. _to-api-multiple_server_capabilities: + +******************************** +``multiple_server_capabilities`` +******************************** + +.. versionadded:: 4.1 + +``PUT`` +======== +Associates a list of :term:`Server Capability` to a server. Review Comment: To be clear, what this actually does is replace all of the Server Capabilities assigned to a server with the ones specified. It doesn't just associate the ones you send with the server, it also gets rid of all other such associations. Which is appropriate for a PUT request, but the docs could be a bit more clear on that point. ########## traffic_ops/traffic_ops_golang/server/servers_server_capability.go: ########## @@ -441,3 +442,88 @@ func getDSTenantIDsByIDs(tx *sqlx.Tx, dsIDs []int64) ([]DSTenant, error) { return dsTenantIDs, nil } + +// AssignMultipleServerCapabilities helps assign multiple server capabilities to a given server. +func AssignMultipleServerCapabilities(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil) + tx := inf.Tx.Tx + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + + var msc tc.MultipleServerCapabilities + if err := json.NewDecoder(r.Body).Decode(&msc); err != nil { + api.HandleErr(w, r, tx, http.StatusBadRequest, err, nil) + return + } + + // Check existence prior to checking type + _, exists, err := dbhelpers.GetServerNameFromID(tx, int64(msc.ServerID)) + if err != nil { + api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, err) + } + if !exists { + userErr := fmt.Errorf("server %d does not exist", msc.ServerID) + api.HandleErr(w, r, tx, http.StatusNotFound, userErr, nil) + return + } + + // Ensure type is correct + correctType := true + if err := tx.QueryRow(scCheckServerTypeQuery(), msc.ServerID).Scan(&correctType); err != nil { + api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, fmt.Errorf("checking server type: %w", err)) + return + } + if !correctType { + userErr := fmt.Errorf("server %d has an incorrect server type. Server capabilities can only be assigned to EDGE or MID servers", msc.ServerID) + api.HandleErr(w, r, tx, http.StatusBadRequest, userErr, nil) + return + } + + cdnName, err := dbhelpers.GetCDNNameFromServerID(tx, int64(msc.ServerID)) + if err != nil { + api.HandleErr(w, r, tx, http.StatusInternalServerError, nil, err) + return + } + + userErr, sysErr, errCode = dbhelpers.CheckIfCurrentUserCanModifyCDN(tx, string(cdnName), inf.User.UserName) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, tx, errCode, userErr, sysErr) + return + } + + //Delete existing rows from server_server_capability for a given server + _, err = tx.Exec("DELETE FROM server_server_capability ssc WHERE ssc.server=$1", msc.ServerID) + if err != nil { + useErr, sysErr, statusCode := api.ParseDBError(err) + api.HandleErr(w, r, tx, statusCode, useErr, sysErr) + return + } + + multipleServerCapabilities := make([]string, 0, len(msc.ServerCapabilities)) + + mscQuery := `WITH inserted AS ( + INSERT INTO server_server_capability + SELECT "server_capability", $2 + FROM UNNEST($1::text[]) AS tmp("server_capability") + RETURNING server_capability + ) + SELECT ARRAY_AGG(server_capability) + FROM ( + SELECT server_capability + FROM inserted + ) AS returned(server_capability)` + + err = tx.QueryRow(mscQuery, pq.Array(msc.ServerCapabilities), msc.ServerID).Scan(pq.Array(&multipleServerCapabilities)) + if err != nil { + useErr, sysErr, statusCode := api.ParseDBError(err) + api.HandleErr(w, r, tx, statusCode, useErr, sysErr) + return + } + msc.ServerCapabilities = multipleServerCapabilities + alerts := tc.CreateAlerts(tc.SuccessLevel, fmt.Sprint("Multiple Server Capabilities assigned to a server")) Review Comment: You don't need `fmt.Sprint` to turn a string into a string; it's already a string -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
