rawlinp commented on a change in pull request #5307:
URL: https://github.com/apache/trafficcontrol/pull/5307#discussion_r529986449
##########
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
+ }
Review comment:
After checking `err != nil`, we always need to `defer rows.Close()` or
even better `defer log.Close(rows, "some error message prefix")` in order to
prevent resource leaks.
##########
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:
How about `"the following ORG server cachegroups are not in the delivery
service's topology ("+dsTopology+"): " + strings.Join(offendingSCG, ", ")"`?
##########
File path: traffic_ops/testing/api/v3/deliveryservices_test.go
##########
@@ -855,6 +856,83 @@ func UpdateDeliveryServiceWithInvalidSliceRangeRequest(t
*testing.T) {
}
+// UpdateValidateORGServerCacheGroup validates ORG server's cachegroup are
part of topology's cachegroup
+func UpdateValidateORGServerCacheGroup(t *testing.T) {
+ msoDS := testData.DeliveryServices[7]
+
+ //Get the correct DS
+ dses, _, err := TOSession.GetDeliveryServicesV30WithHdr(nil, nil)
+ if err != nil {
+ t.Errorf("cannot GET Delivery Services: %v", err)
+ }
+ remoteDS := tc.DeliveryServiceNullableV30{}
+ found := false
+ for _, ds := range dses {
+ if *ds.XMLID == *msoDS.XMLID {
+ found = true
+ remoteDS = ds
+ break
+ }
+ }
+ if !found {
+ t.Errorf("GET Delivery Services missing: %v", *msoDS.XMLID)
+ }
+
+ //Assign ORG server to DS
+ assignServer := []string{"denver-mso-org-01"}
+ _, _, err = TOSession.AssignServersToDeliveryService(assignServer,
*msoDS.XMLID)
+ if err != nil {
+ t.Errorf("cannot assign server to Delivery Services: %v", err)
+ }
+
+ //Get Topology node to update and remove ORG server nodes
+ resp, _, err := TOSession.GetTopologyWithHdr(*remoteDS.Topology, nil)
+ if err != nil {
+ t.Fatalf("couldn't find any topologies: %v", err)
+ }
+ var p []int
+ var origNodes []tc.TopologyNode
+ newNodes := []tc.TopologyNode{{Id: 0, Cachegroup:
"topology-edge-cg-01", Parents: p, LastUpdated: nil}}
+ if *msoDS.Topology == resp.Name {
+ origNodes = resp.Nodes
+ resp.Nodes = newNodes
+ }
+ _, _, err = TOSession.UpdateTopology(*msoDS.Topology, *resp)
Review comment:
So with the part 3 of this validation, topology updates will fail if any
assigned DSes have ORG servers assigned that would no longer be in the
topology. So this test will fail once that validation is added.
It would probably be better to take a valid DS (with ORG servers assigned),
with or without a valid topology already assigned, and attempt to update the DS
to use an invalid topology per this new validation.
##########
File path: traffic_ops/testing/api/v3/deliveryservices_test.go
##########
@@ -855,6 +856,83 @@ func UpdateDeliveryServiceWithInvalidSliceRangeRequest(t
*testing.T) {
}
+// UpdateValidateORGServerCacheGroup validates ORG server's cachegroup are
part of topology's cachegroup
+func UpdateValidateORGServerCacheGroup(t *testing.T) {
+ msoDS := testData.DeliveryServices[7]
Review comment:
Rather than relying upon the exact index in the json, it is usually
safer to just use the xmlID from the json and get that exact DS from TO. Then
you also wouldn't have to iterate over all the DSes to find the one you're
looking for.
##########
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))
Review comment:
In addition to my review comment on L1139, I think we should format it
as `"%s (%s)", cg, s`, since cachegroup names might contain hyphens, so it
looks like `mycachegroup (myserver)`.
----------------------------------------------------------------
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]