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:15 UTC

[trafficcontrol] branch master updated (e916ca9 -> 68a9a2f)

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

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


    from e916ca9  made changes to cleanup the PMD errors and fixed a Unit test bug
     new dd85566  Add TO Go regions/name
     new 68a9a2f  Add TO Go regions/name client func, test

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 lib/go-tc/regions.go                               | 15 ++++++++++
 traffic_ops/client/v13/region.go                   | 17 +++++++++++
 traffic_ops/testing/api/v13/regions_test.go        | 11 +++++++
 .../parameterprofilebyname.go => region/name.go}   | 34 +++++++---------------
 traffic_ops/traffic_ops_golang/routes.go           |  2 ++
 5 files changed, 56 insertions(+), 23 deletions(-)
 copy traffic_ops/traffic_ops_golang/{profileparameter/parameterprofilebyname.go => region/name.go} (55%)


[trafficcontrol] 01/02: Add TO Go regions/name

Posted by de...@apache.org.
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 dd85566eb08b555dfcb70967d72768767311e97b
Author: Robert Butts <ro...@apache.org>
AuthorDate: Wed May 30 15:28:51 2018 -0600

    Add TO Go regions/name
---
 lib/go-tc/regions.go                          | 11 ++++++
 traffic_ops/traffic_ops_golang/region/name.go | 53 +++++++++++++++++++++++++++
 traffic_ops/traffic_ops_golang/routes.go      |  2 +
 3 files changed, 66 insertions(+)

diff --git a/lib/go-tc/regions.go b/lib/go-tc/regions.go
index 76a6816..2b1b77a 100644
--- a/lib/go-tc/regions.go
+++ b/lib/go-tc/regions.go
@@ -32,3 +32,14 @@ type Region struct {
 	LastUpdated  TimeNoMod `json:"lastUpdated" db:"last_updated"`
 	Name         string    `json:"name" db:"name"`
 }
+
+type RegionName struct {
+	ID           int       `json:"id"`
+	Name         string    `json:"name"`
+	Division RegionNameDivision `json:"division"`
+}
+
+type RegionNameDivision struct {
+	ID     int       `json:"id"`
+	Name string    `json:"name"`
+}
diff --git a/traffic_ops/traffic_ops_golang/region/name.go b/traffic_ops/traffic_ops_golang/region/name.go
new file mode 100644
index 0000000..3a7f440
--- /dev/null
+++ b/traffic_ops/traffic_ops_golang/region/name.go
@@ -0,0 +1,53 @@
+package region
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"database/sql"
+	"errors"
+	"net/http"
+
+	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetName(db *sql.DB) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		params, _, userErr, sysErr, errCode := api.AllParams(r, []string{"name"}, nil)
+		if userErr != nil || sysErr != nil {
+			api.HandleErr(w, r, errCode, userErr, sysErr)
+			return
+		}
+		api.RespWriter(w, r)(getName(db, params["name"]))
+	}
+}
+
+// getName returns a slice, even though only 1 region will ever be returned, because that's what the 1.x API responds with.
+func getName(db *sql.DB, name string) ([]tc.RegionName, error) {
+	r := tc.RegionName{Name: name}
+	err := db.QueryRow(`SELECT r.id, d.id, d.name FROM region as r JOIN division as d ON r.division = d.id WHERE r.name = $1`, name).Scan(&r.ID, &r.Division.ID, &r.Division.Name)
+	if err != nil {
+		if err == sql.ErrNoRows {
+			return []tc.RegionName{}, nil
+		}
+		return nil, errors.New("querying region by name: " + err.Error())
+	}
+	return []tc.RegionName{r}, nil
+}
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index 9b1d696..dbff573 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -159,6 +159,7 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		//Profile: CRUD
 		{1.1, http.MethodGet, `profiles/?(\.json)?$`, api.ReadHandler(profile.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.1, http.MethodGet, `profiles/trimmed/?(\.json)?$`, profile.Trimmed(d.DB.DB), auth.PrivLevelReadOnly, Authenticated, nil},
+
 		{1.1, http.MethodGet, `profiles/{id}$`, api.ReadHandler(profile.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.1, http.MethodPut, `profiles/{id}$`, api.UpdateHandler(profile.GetTypeSingleton()), auth.PrivLevelOperations, Authenticated, nil},
 		{1.1, http.MethodPost, `profiles/?$`, api.CreateHandler(profile.GetTypeSingleton()), auth.PrivLevelOperations, Authenticated, nil},
@@ -167,6 +168,7 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		//Region: CRUDs
 		{1.1, http.MethodGet, `regions/?(\.json)?$`, api.ReadHandler(region.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.1, http.MethodGet, `regions/{id}$`, api.ReadHandler(region.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
+		{1.1, http.MethodGet, `regions/name/{name}/?(\.json)?$`, region.GetName(d.DB.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.1, http.MethodPut, `regions/{id}$`, api.UpdateHandler(region.GetTypeSingleton()), auth.PrivLevelOperations, Authenticated, nil},
 		{1.1, http.MethodPost, `regions/?$`, api.CreateHandler(region.GetTypeSingleton()), auth.PrivLevelOperations, Authenticated, nil},
 		{1.1, http.MethodDelete, `regions/{id}$`, api.DeleteHandler(region.GetTypeSingleton()), auth.PrivLevelOperations, Authenticated, nil},


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

Posted by de...@apache.org.
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 {