rimashah25 commented on a change in pull request #5307:
URL: https://github.com/apache/trafficcontrol/pull/5307#discussion_r532859260



##########
File path: traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
##########
@@ -1090,3 +1090,53 @@ GROUP BY t.name, ds.topology
        }
        return dsType, reqCap, topology, true, nil
 }
+
+// CheckOriginServerInCacheGroupTopology checks if a DS has ORG server and if 
it does, to make sure the cachegroup is part of DS
+func CheckOriginServerInCacheGroupTopology(tx *sql.Tx, dsID int, dsTopology 
string) (error, error, int) {
+       // get servers and respective cachegroup name that have ORG type in a 
delivery service
+       q := `
+               SELECT s.host_name, c.name 
+               FROM server s
+                       INNER JOIN deliveryservice_server ds ON ds.server = s.id
+                       INNER JOIN type t ON t.id = s.type
+                       INNER JOIN cachegroup c ON c.id = s.cachegroup
+               WHERE ds.deliveryservice=$1 AND t.name=$2
+       `
+
+       serverName := ""
+       cacheGroupName := ""
+       servers := make(map[string]string)
+       var offendingSCG []string
+       rows, err := tx.Query(q, dsID, tc.OriginTypeName)
+       if err != nil {
+               return nil, fmt.Errorf("querying deliveryservice origin server: 
%s", err), http.StatusInternalServerError
+       }
+       for rows.Next() {
+               if err := rows.Scan(&serverName, &cacheGroupName); err != nil {
+                       return nil, fmt.Errorf("querying deliveryservice origin 
server: %s", err), http.StatusInternalServerError
+               }
+               servers[cacheGroupName] = serverName
+       }
+
+       if len(servers) > 0 {
+               _, cachegroups, sysErr := GetTopologyCachegroups(tx, dsTopology)
+               if sysErr != nil {
+                       return nil, fmt.Errorf("validating %s servers in 
topology: %v", tc.OriginTypeName, sysErr), http.StatusInternalServerError
+               }
+               // put slice values into map
+               topoCachegroups := make(map[string]string)
+               for _, cg := range cachegroups {
+                       topoCachegroups[cg] = ""
+               }
+               for cg, s := range servers {
+                       _, ok := topoCachegroups[cg]
+                       if !ok {
+                               offendingSCG = append(offendingSCG, 
fmt.Sprintf("%s-%s", s, cg))
+                       }
+               }
+       }
+       if len(offendingSCG) > 0 {
+               return errors.New("list of server-cachegroup: " + 
strings.Join(offendingSCG, ", ") + " not present in this DS"), nil, 
http.StatusBadRequest

Review comment:
       Sure, changed.




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