rimashah25 commented on code in PR #6996:
URL: https://github.com/apache/trafficcontrol/pull/6996#discussion_r938003370
##########
traffic_ops/traffic_ops_golang/server/servers_server_capability.go:
##########
@@ -441,3 +442,87 @@ 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, []string{"id"},
[]string{"id"})
+ 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.ServerCapability))
Review Comment:
aha yes.. I had a different train of thought but originally that is want to
return but then I changed on how I am writing the object.. i'll scan the
results from the query into `msc.ServerCapability`
--
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]