rawlinp commented on a change in pull request #5091:
URL: https://github.com/apache/trafficcontrol/pull/5091#discussion_r498450705
##########
File path: traffic_ops/traffic_ops_golang/server/servers_server_capability.go
##########
@@ -137,46 +138,145 @@ JOIN server s ON sc.server = s.id ` + where + orderBy +
pagination +
}
func (ssc *TOServerServerCapability) Delete() (error, error, int) {
+ tenantIDs, err := tenant.GetUserTenantIDListTx(ssc.APIInfo().Tx.Tx,
ssc.APIInfo().User.TenantID)
+ if err != nil {
+ return nil, fmt.Errorf("deleting servers_server_capability:
%v", err), http.StatusInternalServerError
+ }
+ accessibleTenants := make(map[int]struct{}, len(tenantIDs))
+ for _, id := range tenantIDs {
+ accessibleTenants[id] = struct{}{}
+ }
+ userErr, sysErr, status :=
checkTopologyBasedDSRequiredCapabilities(ssc, accessibleTenants)
+ if userErr != nil || sysErr != nil {
+ return userErr, sysErr, status
+ }
+
+ userErr, sysErr, status = checkDSRequiredCapabilities(ssc,
accessibleTenants)
+ if userErr != nil || sysErr != nil {
+ return userErr, sysErr, status
+ }
+
+ return api.GenericDelete(ssc)
+}
+
+func checkTopologyBasedDSRequiredCapabilities(ssc *TOServerServerCapability,
accessibleTenants map[int]struct{}) (error, error, int) {
+ dsRows, err :=
ssc.APIInfo().Tx.Tx.Query(getTopologyBasedDSesReqCapQuery(), ssc.ServerID,
ssc.ServerCapability)
+ if err != nil {
+ return nil, fmt.Errorf("querying topology-based DSes with the
required capability %s: %v", *ssc.ServerCapability, err),
http.StatusInternalServerError
+ }
+ defer log.Close(dsRows, "closing dsRows in
checkTopologyBasedDSRequiredCapabilities")
+
+ xmlidToTopology := make(map[string]string)
+ xmlidToTenantID := make(map[string]int)
+ xmlidToReqCaps := make(map[string][]string)
+ for dsRows.Next() {
+ xmlID := ""
+ topology := ""
+ tenantID := 0
+ reqCaps := []string{}
+ if err := dsRows.Scan(&xmlID, &topology, &tenantID,
pq.Array(&reqCaps)); err != nil {
+ return nil, fmt.Errorf("scanning dsRows in
checkTopologyBasedDSRequiredCapabilities: %v", err),
http.StatusInternalServerError
+ }
+ xmlidToTenantID[xmlID] = tenantID
+ xmlidToTopology[xmlID] = topology
+ xmlidToReqCaps[xmlID] = reqCaps
+ }
+ if len(xmlidToTopology) == 0 {
+ return nil, nil, http.StatusOK
+ }
+
+ serverRows, err :=
ssc.APIInfo().Tx.Tx.Query(getServerCapabilitiesOfCachegoupQuery(),
ssc.ServerID, ssc.ServerCapability)
+ if err != nil {
+ return nil, fmt.Errorf("querying server capabilitites of server
%d's cachegroup: %v", *ssc.ServerID, err), http.StatusInternalServerError
+ }
+ defer log.Close(serverRows, "closing serverRows in
checkTopologyBasedDSRequiredCapabilities")
+
+ serverIDToCapabilities := make(map[int]map[string]struct{})
+ for serverRows.Next() {
+ serverID := 0
+ capabilities := []string{}
+ if err := serverRows.Scan(&serverID, pq.Array(&capabilities));
err != nil {
+ return nil, fmt.Errorf("scanning serverRows in
checkTopologyBasedDSRequiredCapabilities: %v", err),
http.StatusInternalServerError
+ }
+ serverIDToCapabilities[serverID] = make(map[string]struct{})
+ for _, c := range capabilities {
+ serverIDToCapabilities[serverID][c] = struct{}{}
+ }
+ }
+ var dsStrings []string
+ dsStrings = make([]string, 0, len(xmlidToTopology))
+ if len(serverIDToCapabilities) == 0 {
+ for ds, top := range xmlidToTopology {
+ if _, ok := accessibleTenants[xmlidToTenantID[ds]]; ok {
+ dsStrings = append(dsStrings, "(xml_id =
"+ds+", topology = "+top+")")
+ }
+ }
+ return fmt.Errorf("this capability is required by delivery
services, but there are no other servers in this server's cachegroup to satisfy
them %s", strings.Join(dsStrings, ", ")), nil, http.StatusBadRequest
+ }
Review comment:
I think this was necessary in an earlier version, but re-reading my code
here I believe I agree with you. I'll remove it in the next revision.
----------------------------------------------------------------
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]