You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by da...@apache.org on 2018/02/22 18:26:09 UTC
[incubator-trafficcontrol] 01/08: updated to use the Read()
interface
This is an automated email from the ASF dual-hosted git repository.
dangogh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git
commit 57d8fc4ba842ada0960784e70d439a53224946da
Author: Dewayne Richardson <de...@apache.org>
AuthorDate: Mon Feb 19 12:04:00 2018 -0700
updated to use the Read() interface
---
traffic_ops/traffic_ops_golang/routes.go | 12 +++-
.../traffic_ops_golang/{ => status}/statuses.go | 79 +++++++---------------
.../{ => status}/statuses_test.go | 32 +++------
3 files changed, 43 insertions(+), 80 deletions(-)
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index 58f2a54..b59d92b 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -35,6 +35,7 @@ import (
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/cdn"
dsrequest "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice/request"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/division"
+ "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/status"
"github.com/basho/riak-go-client"
)
@@ -94,6 +95,13 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
{1.3, http.MethodPut, `deliveryservices/{xmlID}/urisignkeys$`, saveDeliveryServiceURIKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
{1.3, http.MethodDelete, `deliveryservices/{xmlID}/urisignkeys$`, removeDeliveryServiceURIKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
+ //Statuses
+ {1.3, http.MethodGet, `statuses/?(\.json)?$`, api.ReadHandler(status.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
+ {1.3, http.MethodGet, `statuses/{id}$`, api.ReadHandler(status.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
+ {1.3, http.MethodPut, `statuses/{id}$`, api.UpdateHandler(status.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
+ {1.3, http.MethodPost, `statuses/?$`, api.CreateHandler(status.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
+ {1.3, http.MethodDelete, `statuses/{id}$`, api.DeleteHandler(status.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
+
//Divisions
{1.2, http.MethodGet, `divisions/?(\.json)?$`, api.ReadHandler(division.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
{1.3, http.MethodGet, `divisions/{id}$`, api.ReadHandler(division.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
@@ -125,9 +133,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
{1.2, http.MethodGet, `deliveryservices-wip/xmlId/{xmlID}/sslkeys$`, getDeliveryServiceSSLKeysByXMLIDHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
{1.2, http.MethodGet, `deliveryservices-wip/hostname/{hostName}/sslkeys$`, getDeliveryServiceSSLKeysByHostNameHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
{1.2, http.MethodPost, `deliveryservices-wip/hostname/{hostName}/sslkeys/add$`, addDeliveryServiceSSLKeysHandler(d.DB, d.Config), auth.PrivLevelAdmin, Authenticated, nil},
- //Statuses
- {1.2, http.MethodGet, `statuses/?(\.json)?$`, statusesHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
- {1.2, http.MethodGet, `statuses/{id}$`, statusesHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
+
//System
{1.2, http.MethodGet, `system/info/?(\.json)?$`, systemInfoHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
diff --git a/traffic_ops/traffic_ops_golang/statuses.go b/traffic_ops/traffic_ops_golang/status/statuses.go
similarity index 52%
rename from traffic_ops/traffic_ops_golang/statuses.go
rename to traffic_ops/traffic_ops_golang/status/statuses.go
index 03d38ff..a281a7d 100644
--- a/traffic_ops/traffic_ops_golang/statuses.go
+++ b/traffic_ops/traffic_ops_golang/status/statuses.go
@@ -1,4 +1,4 @@
-package main
+package status
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -20,94 +20,63 @@ package main
*/
import (
- "encoding/json"
- "fmt"
- "net/http"
-
"github.com/apache/incubator-trafficcontrol/lib/go-log"
"github.com/apache/incubator-trafficcontrol/lib/go-tc"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api"
+ "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
"github.com/jmoiron/sqlx"
)
-func statusesHandler(db *sqlx.DB) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- handleErrs := tc.GetHandleErrorsFunc(w, r)
+//we need a type alias to define functions on
+type TOStatus tc.Status
- params, err := api.GetCombinedParams(r)
- if err != nil {
- log.Errorf("unable to get parameters from request: %s", err)
- handleErrs(http.StatusInternalServerError, err)
- }
+//the refType is passed into the handlers where a copy of its type is used to decode the json.
+var refType = TOStatus(tc.Status{})
- resp, errs, errType := getStatusesResponse(params, db)
- if len(errs) > 0 {
- tc.HandleErrorsWithType(errs, errType, handleErrs)
- return
- }
-
- respBts, err := json.Marshal(resp)
- if err != nil {
- handleErrs(http.StatusInternalServerError, err)
- return
- }
-
- w.Header().Set("Content-Type", "application/json")
- fmt.Fprintf(w, "%s", respBts)
- }
+func GetRefType() *TOStatus {
+ return &refType
}
-func getStatusesResponse(params map[string]string, db *sqlx.DB) (*tc.StatusesResponse, []error, tc.ApiErrorType) {
- cdns, errs, errType := getStatuses(params, db)
- if len(errs) > 0 {
- return nil, errs, errType
- }
-
- resp := tc.StatusesResponse{
- Response: cdns,
- }
- return &resp, nil, tc.NoError
-}
-
-func getStatuses(params map[string]string, db *sqlx.DB) ([]tc.Status, []error, tc.ApiErrorType) {
+func (cdn *TOStatus) Read(db *sqlx.DB, parameters map[string]string, user auth.CurrentUser) ([]interface{}, []error, tc.ApiErrorType) {
var rows *sqlx.Rows
- var err error
// Query Parameters to Database Query column mappings
// see the fields mapped in the SQL query
- queryParamsToSQLCols := map[string]dbhelpers.WhereColumnInfo{
+ queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
"id": dbhelpers.WhereColumnInfo{"id", api.IsInt},
- "name": dbhelpers.WhereColumnInfo{"name", nil},
"description": dbhelpers.WhereColumnInfo{"description", nil},
+ "name": dbhelpers.WhereColumnInfo{"name", nil},
}
-
- where, orderBy, queryValues, errs := dbhelpers.BuildWhereAndOrderBy(params, queryParamsToSQLCols)
+ where, orderBy, queryValues, errs := dbhelpers.BuildWhereAndOrderBy(parameters, queryParamsToQueryCols)
if len(errs) > 0 {
return nil, errs, tc.DataConflictError
}
- query := selectStatusesQuery() + where + orderBy
+ query := selectQuery() + where + orderBy
log.Debugln("Query is ", query)
- rows, err = db.NamedQuery(query, queryValues)
+ rows, err := db.NamedQuery(query, queryValues)
if err != nil {
- return nil, []error{err}, tc.SystemError
+ log.Errorf("Error querying Status: %v", err)
+ return nil, []error{tc.DBError}, tc.SystemError
}
defer rows.Close()
- statuses := []tc.Status{}
+ status := []interface{}{}
for rows.Next() {
var s tc.Status
if err = rows.StructScan(&s); err != nil {
- return nil, []error{fmt.Errorf("getting statuses: %v", err)}, tc.SystemError
+ log.Errorf("error parsing Status rows: %v", err)
+ return nil, []error{tc.DBError}, tc.SystemError
}
- statuses = append(statuses, s)
+ status = append(status, s)
}
- return statuses, nil, tc.NoError
+
+ return status, []error{}, tc.NoError
}
-func selectStatusesQuery() string {
+func selectQuery() string {
query := `SELECT
description,
@@ -115,6 +84,6 @@ id,
last_updated,
name
-FROM status c`
+FROM status s`
return query
}
diff --git a/traffic_ops/traffic_ops_golang/statuses_test.go b/traffic_ops/traffic_ops_golang/status/statuses_test.go
similarity index 73%
rename from traffic_ops/traffic_ops_golang/statuses_test.go
rename to traffic_ops/traffic_ops_golang/status/statuses_test.go
index 6be62e6..9c12806 100644
--- a/traffic_ops/traffic_ops_golang/statuses_test.go
+++ b/traffic_ops/traffic_ops_golang/status/statuses_test.go
@@ -1,4 +1,4 @@
-package main
+package status
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -24,6 +24,7 @@ import (
"time"
"github.com/apache/incubator-trafficcontrol/lib/go-tc"
+ "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/test"
"github.com/jmoiron/sqlx"
@@ -48,7 +49,7 @@ func getTestStatuses() []tc.Status {
return cdns
}
-func TestGetStatus(t *testing.T) {
+func TestReadStatuses(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
@@ -58,13 +59,13 @@ func TestGetStatus(t *testing.T) {
db := sqlx.NewDb(mockDB, "sqlmock")
defer db.Close()
- testStatus := getTestStatuses()
+ refType := GetRefType()
+
+ testStatuses := getTestStatuses()
cols := test.ColsFromStructByTag("db", tc.Status{})
rows := sqlmock.NewRows(cols)
- //TODO: drichardson - build helper to add these Rows from the struct values
- // or by CSV if types get in the way
- for _, ts := range testStatus {
+ for _, ts := range testStatuses {
rows = rows.AddRow(
ts.Description,
ts.ID,
@@ -75,25 +76,12 @@ func TestGetStatus(t *testing.T) {
mock.ExpectQuery("SELECT").WillReturnRows(rows)
v := map[string]string{"dsId": "1"}
- servers, errs, errType := getStatuses(v, db)
+ servers, errs, _ := refType.Read(db, v, auth.CurrentUser{})
if len(errs) > 0 {
- t.Errorf("getStatus expected: no errors, actual: %v with error type: %s", errs, errType.String())
+ t.Errorf("cdn.Read expected: no errors, actual: %v", errs)
}
if len(servers) != 2 {
- t.Errorf("getStatus expected: len(servers) == 1, actual: %v", len(servers))
+ t.Errorf("cdn.Read expected: len(servers) == 2, actual: %v", len(servers))
}
-
-}
-
-type SortableStatuses []tc.Status
-
-func (s SortableStatuses) Len() int {
- return len(s)
-}
-func (s SortableStatuses) Swap(i, j int) {
- s[i], s[j] = s[j], s[i]
-}
-func (s SortableStatuses) Less(i, j int) bool {
- return s[i].Name < s[j].Name
}
--
To stop receiving notification emails like this one, please contact
dangogh@apache.org.