You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/07/05 20:04:17 UTC

[trafficcontrol] 02/02: Add TO Go regions/name client func, test

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

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

commit 68a9a2f9d20cba22ee354567273bf372d88bd5b7
Author: Robert Butts <ro...@apache.org>
AuthorDate: Fri Jun 29 15:07:25 2018 -0600

    Add TO Go regions/name client func, test
---
 lib/go-tc/regions.go                        | 12 ++++++++----
 traffic_ops/client/v13/region.go            | 17 +++++++++++++++++
 traffic_ops/testing/api/v13/regions_test.go | 11 +++++++++++
 3 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/lib/go-tc/regions.go b/lib/go-tc/regions.go
index 2b1b77a..83ae751 100644
--- a/lib/go-tc/regions.go
+++ b/lib/go-tc/regions.go
@@ -34,12 +34,16 @@ type Region struct {
 }
 
 type RegionName struct {
-	ID           int       `json:"id"`
-	Name         string    `json:"name"`
+	ID       int                `json:"id"`
+	Name     string             `json:"name"`
 	Division RegionNameDivision `json:"division"`
 }
 
 type RegionNameDivision struct {
-	ID     int       `json:"id"`
-	Name string    `json:"name"`
+	ID   int    `json:"id"`
+	Name string `json:"name"`
+}
+
+type RegionNameResponse struct {
+	Response []RegionName `json:"response"`
 }
diff --git a/traffic_ops/client/v13/region.go b/traffic_ops/client/v13/region.go
index d018b90..fb71e78 100644
--- a/traffic_ops/client/v13/region.go
+++ b/traffic_ops/client/v13/region.go
@@ -130,3 +130,20 @@ func (to *Session) DeleteRegionByID(id int) (tc.Alerts, ReqInf, error) {
 	err = json.NewDecoder(resp.Body).Decode(&alerts)
 	return alerts, reqInf, nil
 }
+
+// GetRegionByNamePath gets a region by name, using the /api/version/region/name path. This gets the same data as GetRegionByName, but uses a different API path to get the same data, and returns a slightly different format.
+func (to *Session) GetRegionByNamePath(name string) ([]tc.RegionName, ReqInf, error) {
+	url := apiBase + `/regions/name/` + name
+	reqResp, remoteAddr, err := to.request(http.MethodGet, url, nil)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return nil, reqInf, err
+	}
+	defer reqResp.Body.Close()
+
+	resp := tc.RegionNameResponse{}
+	if err := json.NewDecoder(reqResp.Body).Decode(&resp); err != nil {
+		return nil, reqInf, err
+	}
+	return resp.Response, reqInf, nil
+}
diff --git a/traffic_ops/testing/api/v13/regions_test.go b/traffic_ops/testing/api/v13/regions_test.go
index 12176fb..4285c78 100644
--- a/traffic_ops/testing/api/v13/regions_test.go
+++ b/traffic_ops/testing/api/v13/regions_test.go
@@ -28,6 +28,7 @@ func TestRegions(t *testing.T) {
 	CreateTestRegions(t)
 	UpdateTestRegions(t)
 	GetTestRegions(t)
+	GetTestRegionsByNamePath(t)
 	DeleteTestRegions(t)
 	DeleteTestDivisions(t)
 
@@ -98,6 +99,16 @@ func GetTestRegions(t *testing.T) {
 	}
 }
 
+func GetTestRegionsByNamePath(t *testing.T) {
+	log.Debugln("GetTestRegionsByNamePath")
+	for _, region := range testData.Regions {
+		_, _, err := TOSession.GetRegionByNamePath(region.Name)
+		if err != nil {
+			t.Fatalf("cannot GET Region by name: %v - %v\n", region.Name, err)
+		}
+	}
+}
+
 func DeleteTestRegions(t *testing.T) {
 
 	for _, region := range testData.Regions {