This is an automated email from the ASF dual-hosted git repository.

rawlin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 382cb74  Fixed filtering query parameters in the /servers API (#4797)
382cb74 is described below

commit 382cb74790ae0f988a5c74285b85746d6e2e316d
Author: ocket8888 <[email protected]>
AuthorDate: Tue Jun 16 18:44:53 2020 -0600

    Fixed filtering query parameters in the /servers API (#4797)
    
    * Fixed filtering query parameters in the /servers API
    
    * Added tests
    
    * Removed extraneous parts of the test
    
    * Made /servers JOIN statements a shared const
    
    * Added test for parentCacheGroup
    
    * Moved FROM clause to shared constant
    
    * Go fmt
---
 traffic_ops/testing/api/v3/servers_test.go         | 69 ++++++++++++++++++++--
 traffic_ops/traffic_ops_golang/crconfig/servers.go |  2 +-
 traffic_ops/traffic_ops_golang/server/servers.go   | 25 ++++----
 .../traffic_ops_golang/server/servers_test.go      | 12 ++--
 4 files changed, 85 insertions(+), 23 deletions(-)

diff --git a/traffic_ops/testing/api/v3/servers_test.go 
b/traffic_ops/testing/api/v3/servers_test.go
index 03b24ae..c77f282 100644
--- a/traffic_ops/testing/api/v3/servers_test.go
+++ b/traffic_ops/testing/api/v3/servers_test.go
@@ -77,10 +77,6 @@ func GetTestServersDetails(t *testing.T) {
 }
 
 func GetTestServersQueryParameters(t *testing.T) {
-       if len(testData.DeliveryServices) < 1 {
-               t.Fatal("Need at least one server to test getting servers with 
query parameters")
-       }
-
        dses, _, err := TOSession.GetDeliveryServicesNullable()
        if err != nil {
                t.Fatalf("Failed to get Delivery Services: %v", err)
@@ -101,6 +97,70 @@ func GetTestServersQueryParameters(t *testing.T) {
                t.Fatalf("Failed to get server by Delivery Service ID: %v", err)
        }
        params.Del("dsId")
+
+       resp, _, err := TOSession.GetServers(nil)
+       if err != nil {
+               t.Fatalf("Failed to get servers: %v", err)
+       }
+
+       if len(resp.Response) < 1 {
+               t.Fatalf("Failed to get at least one server")
+       }
+
+       s := resp.Response[0]
+
+       params.Add("type", s.Type)
+       if _, _, err := TOSession.GetServers(&params); err != nil {
+               t.Errorf("Error getting servers by type: %v", err)
+       }
+       params.Del("type")
+
+       if s.CachegroupID == nil {
+               t.Error("Found server with no Cache Group ID")
+       } else {
+               params.Add("cachegroup", strconv.Itoa(*s.CachegroupID))
+               if _, _, err := TOSession.GetServers(&params); err != nil {
+                       t.Errorf("Error getting servers by Cache Group ID: %v", 
err)
+               }
+               params.Del("cachegroup")
+       }
+
+       if s.Status == nil {
+               t.Error("Found server with no status")
+       } else {
+               params.Add("status", *s.Status)
+               if _, _, err := TOSession.GetServers(&params); err != nil {
+                       t.Errorf("Error getting servers by status: %v", err)
+               }
+               params.Del("status")
+       }
+
+       if s.ProfileID == nil {
+               t.Error("Found server with no Profile ID")
+       } else {
+               params.Add("profileId", strconv.Itoa(*s.ProfileID))
+               if _, _, err := TOSession.GetServers(&params); err != nil {
+                       t.Errorf("Error getting servers by Profile ID: %v", err)
+               }
+               params.Del("profileId")
+       }
+
+       cgs, _, err := TOSession.GetCacheGroupsNullable()
+       if err != nil {
+               t.Fatalf("Failed to get Cache Groups: %v", err)
+       }
+       if len(cgs) < 1 {
+               t.Fatal("Failed to get at least one Cache Group")
+       }
+       if cgs[0].ID == nil {
+               t.Fatal("Cache Group found with no ID")
+       }
+
+       params.Add("parentCacheGroup", strconv.Itoa(*cgs[0].ID))
+       if _, _, err = TOSession.GetServers(&params); err != nil {
+               t.Errorf("Error getting servers by parentCacheGroup: %v", err)
+       }
+       params.Del("parentCacheGroup")
 }
 
 func UpdateTestServers(t *testing.T) {
@@ -228,7 +288,6 @@ func UpdateTestServers(t *testing.T) {
        } else {
                t.Logf("type change update alerts: %+v", alerts)
        }
-
 }
 
 func DeleteTestServers(t *testing.T) {
diff --git a/traffic_ops/traffic_ops_golang/crconfig/servers.go 
b/traffic_ops/traffic_ops_golang/crconfig/servers.go
index 9c7c644..998cdec 100644
--- a/traffic_ops/traffic_ops_golang/crconfig/servers.go
+++ b/traffic_ops/traffic_ops_golang/crconfig/servers.go
@@ -102,7 +102,7 @@ type ServerUnion struct {
 
 type ServerAndHost struct {
        Server ServerUnion
-       Host string
+       Host   string
 }
 
 const DefaultWeightMultiplier = 1000.0
diff --git a/traffic_ops/traffic_ops_golang/server/servers.go 
b/traffic_ops/traffic_ops_golang/server/servers.go
index fd68bab..d977b13 100644
--- a/traffic_ops/traffic_ops_golang/server/servers.go
+++ b/traffic_ops/traffic_ops_golang/server/servers.go
@@ -48,11 +48,20 @@ import (
        "github.com/lib/pq"
 )
 
-const serverCountQuery = `
-SELECT COUNT(s.id)
+const serversFromAndJoin = `
 FROM server AS s
+JOIN cachegroup cg ON s.cachegroup = cg.id
+JOIN cdn cdn ON s.cdn_id = cdn.id
+JOIN phys_location pl ON s.phys_location = pl.id
+JOIN profile p ON s.profile = p.id
+JOIN status st ON s.status = st.id
+JOIN type t ON s.type = t.id
 `
 
+const serverCountQuery = `
+SELECT COUNT(s.id)
+` + serversFromAndJoin
+
 const selectQuery = `
 SELECT
        cg.name AS cachegroup,
@@ -91,14 +100,8 @@ SELECT
        s.upd_pending AS upd_pending,
        s.xmpp_id,
        s.xmpp_passwd
-FROM server AS s
-JOIN cachegroup cg ON s.cachegroup = cg.id
-JOIN cdn cdn ON s.cdn_id = cdn.id
-JOIN phys_location pl ON s.phys_location = pl.id
-JOIN profile p ON s.profile = p.id
-JOIN status st ON s.status = st.id
-JOIN type t ON s.type = t.id
-`
+` + serversFromAndJoin
+
 const InterfacesArray = `
 ARRAY ( SELECT (
                json_build_object (
@@ -710,7 +713,7 @@ func getServers(params map[string]string, tx *sqlx.Tx, user 
*auth.CurrentUser) (
                        return nil, serverCount, nil, fmt.Errorf("getting 
server interfaces: %v", err), http.StatusInternalServerError
                }
 
-               if _, ok := servers[server]; !ok  {
+               if _, ok := servers[server]; !ok {
                        continue
                }
 
diff --git a/traffic_ops/traffic_ops_golang/server/servers_test.go 
b/traffic_ops/traffic_ops_golang/server/servers_test.go
index 2f80a8a..78b4593 100644
--- a/traffic_ops/traffic_ops_golang/server/servers_test.go
+++ b/traffic_ops/traffic_ops_golang/server/servers_test.go
@@ -94,15 +94,15 @@ func getTestServers() []ServerAndInterfaces {
        iface := tc.ServerInterfaceInfo{
                IPAddresses: []tc.ServerIPAddress{
                        tc.ServerIPAddress{
-                               Address: testServer.IPAddress,
-                               Gateway: nil,
+                               Address:        testServer.IPAddress,
+                               Gateway:        nil,
                                ServiceAddress: true,
                        },
                },
                MaxBandwidth: nil,
-               Monitor: true,
-               MTU: &mtu,
-               Name: testServer.InterfaceName,
+               Monitor:      true,
+               MTU:          &mtu,
+               Name:         testServer.InterfaceName,
        }
 
        servers = append(servers, ServerAndInterfaces{Server: testServer, 
Interface: iface})
@@ -486,7 +486,7 @@ func TestGetMidServers(t *testing.T) {
 
        if serverMap[mid[0]].CachegroupID == nil {
                t.Error("getMidServers expected: CachegroupID == 2, actual: 
nil")
-       } else if *(serverMap[mid[0]].CachegroupID) != 2  {
+       } else if *(serverMap[mid[0]].CachegroupID) != 2 {
                t.Errorf("getMidServers expected: CachegroupID == 2, actual: 
%v", *(serverMap[mid[0]].CachegroupID))
        }
 }

Reply via email to