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:16 UTC
[trafficcontrol] 01/02: Add TO Go regions/name
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},