You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ma...@apache.org on 2020/03/30 15:50:08 UTC

[trafficcontrol] branch master updated: Add default sort to phys_locations (#4547)

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

mattjackson 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 6f0f926  Add default sort to phys_locations (#4547)
6f0f926 is described below

commit 6f0f9264adc3b10003c33840aec76666c0c7a5c1
Author: Steve Hamrick <sh...@users.noreply.github.com>
AuthorDate: Mon Mar 30 09:49:58 2020 -0600

    Add default sort to phys_locations (#4547)
    
    * Add default sort to phys_locations
    
    * add test to v1
---
 traffic_ops/client/phys_location.go                |  7 ++---
 traffic_ops/client/util.go                         | 13 ++++++++++
 traffic_ops/testing/api/v1/phys_locations_test.go  | 15 +++++++++++
 traffic_ops/testing/api/v2/phys_locations_test.go  | 30 ++++++++++++++++++++++
 .../physlocation/phys_locations.go                 |  7 ++++-
 5 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/traffic_ops/client/phys_location.go b/traffic_ops/client/phys_location.go
index 1a8ca1e..b82f1c9 100644
--- a/traffic_ops/client/phys_location.go
+++ b/traffic_ops/client/phys_location.go
@@ -78,9 +78,10 @@ func (to *Session) UpdatePhysLocationByID(id int, pl tc.PhysLocation) (tc.Alerts
 	return alerts, reqInf, nil
 }
 
-// Returns a list of PhysLocations
-func (to *Session) GetPhysLocations() ([]tc.PhysLocation, ReqInf, error) {
-	resp, remoteAddr, err := to.request(http.MethodGet, API_PHYS_LOCATIONS, nil)
+// Returns a list of PhysLocations with optional query parameters applied
+func (to *Session) GetPhysLocations(params map[string]string) ([]tc.PhysLocation, ReqInf, error) {
+	path := API_PHYS_LOCATIONS + mapToQueryParameters(params)
+	resp, remoteAddr, err := to.request(http.MethodGet, path, nil)
 	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
 	if err != nil {
 		return nil, reqInf, err
diff --git a/traffic_ops/client/util.go b/traffic_ops/client/util.go
index ed4443a..ec718c9 100644
--- a/traffic_ops/client/util.go
+++ b/traffic_ops/client/util.go
@@ -56,3 +56,16 @@ func makeReq(to *Session, method, endpoint string, body []byte, respStruct inter
 
 	return reqInf, nil
 }
+
+func mapToQueryParameters(params map[string]string) string {
+	path := ""
+	for key, value := range params {
+		if path == "" {
+			path += "?"
+		} else {
+			path += "&"
+		}
+		path += key + "=" + value
+	}
+	return path
+}
diff --git a/traffic_ops/testing/api/v1/phys_locations_test.go b/traffic_ops/testing/api/v1/phys_locations_test.go
index 82a8b27..9378c17 100644
--- a/traffic_ops/testing/api/v1/phys_locations_test.go
+++ b/traffic_ops/testing/api/v1/phys_locations_test.go
@@ -17,12 +17,14 @@ package v1
 
 import (
 	"testing"
+	"sort"
 
 	tc "github.com/apache/trafficcontrol/lib/go-tc"
 )
 
 func TestPhysLocations(t *testing.T) {
 	WithObjs(t, []TCObj{CDNs, Parameters, Divisions, Regions, PhysLocations}, func() {
+		GetDefaultSortPhysLocationsTest(t)
 		UpdateTestPhysLocations(t)
 		GetTestPhysLocations(t)
 	})
@@ -105,3 +107,16 @@ func DeleteTestPhysLocations(t *testing.T) {
 		}
 	}
 }
+
+func GetDefaultSortPhysLocationsTest(t *testing.T) {
+	resp, _, err := TOSession.GetPhysLocations()
+	if err != nil {
+		t.Error(err.Error())
+	}
+	sorted := sort.SliceIsSorted(resp, func(i, j int) bool {
+		return resp[i].Name < resp[j].Name
+	})
+	if !sorted {
+		t.Error("expected response to be sorted by name")
+	}
+}
diff --git a/traffic_ops/testing/api/v2/phys_locations_test.go b/traffic_ops/testing/api/v2/phys_locations_test.go
index c4b143c..ca34954 100644
--- a/traffic_ops/testing/api/v2/phys_locations_test.go
+++ b/traffic_ops/testing/api/v2/phys_locations_test.go
@@ -16,6 +16,7 @@ package v2
 */
 
 import (
+	"sort"
 	"testing"
 
 	tc "github.com/apache/trafficcontrol/lib/go-tc"
@@ -23,6 +24,8 @@ import (
 
 func TestPhysLocations(t *testing.T) {
 	WithObjs(t, []TCObj{CDNs, Parameters, Divisions, Regions, PhysLocations}, func() {
+		GetDefaultSortPhysLocationsTest(t)
+		GetSortPhysLocationsTest(t)
 		UpdateTestPhysLocations(t)
 		GetTestPhysLocations(t)
 	})
@@ -76,6 +79,33 @@ func GetTestPhysLocations(t *testing.T) {
 			t.Errorf("cannot GET PhysLocation by name: %v - %v", err, resp)
 		}
 	}
+
+}
+
+func GetSortPhysLocationsTest(t *testing.T) {
+	resp, _, err := TOSession.GetPhysLocations(map[string]string{"orderby": "id"})
+	if err != nil {
+		t.Error(err.Error())
+	}
+	sorted := sort.SliceIsSorted(resp, func(i, j int) bool {
+		return resp[i].ID < resp[j].ID
+	})
+	if !sorted {
+		t.Error("expected response to be sorted by id")
+	}
+}
+
+func GetDefaultSortPhysLocationsTest(t *testing.T) {
+	resp, _, err := TOSession.GetPhysLocations(nil)
+	if err != nil {
+		t.Error(err.Error())
+	}
+	sorted := sort.SliceIsSorted(resp, func(i, j int) bool {
+		return resp[i].Name < resp[j].Name
+	})
+	if !sorted {
+		t.Error("expected response to be sorted by name")
+	}
 }
 
 func DeleteTestPhysLocations(t *testing.T) {
diff --git a/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go b/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go
index 1c699d5..cdfbc5a 100644
--- a/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go
+++ b/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go
@@ -98,7 +98,12 @@ func (pl *TOPhysLocation) Validate() error {
 	return nil
 }
 
-func (pl *TOPhysLocation) Read() ([]interface{}, error, error, int) { return api.GenericRead(pl) }
+func (pl *TOPhysLocation) Read() ([]interface{}, error, error, int) {
+	if _, ok := pl.APIInfo().Params["orderby"]; !ok {
+		pl.APIInfo().Params["orderby"] = "name"
+	}
+	return api.GenericRead(pl)
+}
 func (pl *TOPhysLocation) Update() (error, error, int)              { return api.GenericUpdate(pl) }
 func (pl *TOPhysLocation) Create() (error, error, int)              { return api.GenericCreate(pl) }
 func (pl *TOPhysLocation) Delete() (error, error, int)              { return api.GenericDelete(pl) }