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/03/07 16:07:31 UTC

[incubator-trafficcontrol] branch master updated: use ASNNullable struct for asns endpoint

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/incubator-trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 7380c8f  use ASNNullable struct for asns endpoint
7380c8f is described below

commit 7380c8f48b05cc81eeae16c52f3cf9f769fbbb8d
Author: Dan Kirkwood <da...@apache.org>
AuthorDate: Mon Mar 5 13:44:05 2018 -0700

    use ASNNullable struct for asns endpoint
---
 lib/go-tc/asns.go                               |  4 ++--
 traffic_ops/traffic_ops_golang/asn/asns.go      | 22 +++++++++---------
 traffic_ops/traffic_ops_golang/asn/asns_test.go | 30 +++++++++++++------------
 3 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/lib/go-tc/asns.go b/lib/go-tc/asns.go
index ac73a57..8bc967a 100644
--- a/lib/go-tc/asns.go
+++ b/lib/go-tc/asns.go
@@ -25,7 +25,7 @@ type ASNsResponse struct {
 
 type ASN struct {
 	ASN          int       `json:"asn" db:"asn"`
-	Cachegroup   string    `json:"cachegroup" db:"cachegroup"`
+	Cachegroup   string    `json:"cachegroup"`
 	CachegroupID int       `json:"cachegroupId" db:"cachegroup_id"`
 	ID           int       `json:"id" db:"id"`
 	LastUpdated  TimeNoMod `json:"lastUpdated" db:"last_updated"`
@@ -33,7 +33,7 @@ type ASN struct {
 
 type ASNNullable struct {
 	ASN          *int       `json:"asn" db:"asn"`
-	Cachegroup   *string    `json:"cachegroup" db:"cachegroup"`
+	Cachegroup   *string    `json:"cachegroup"`
 	CachegroupID *int       `json:"cachegroupId" db:"cachegroup_id"`
 	ID           *int       `json:"id" db:"id"`
 	LastUpdated  *TimeNoMod `json:"lastUpdated" db:"last_updated"`
diff --git a/traffic_ops/traffic_ops_golang/asn/asns.go b/traffic_ops/traffic_ops_golang/asn/asns.go
index ecc2c56..4833aad 100644
--- a/traffic_ops/traffic_ops_golang/asn/asns.go
+++ b/traffic_ops/traffic_ops_golang/asn/asns.go
@@ -155,9 +155,10 @@ func (asn *TOASN) Read(db *sqlx.DB, parameters map[string]string, user auth.Curr
 	// Query Parameters to Database Query column mappings
 	// see the fields mapped in the SQL query
 	queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
-		"asn":        dbhelpers.WhereColumnInfo{"a.asn", nil},
-		"cachegroup": dbhelpers.WhereColumnInfo{"c.name", nil},
-		"id":         dbhelpers.WhereColumnInfo{"a.id", api.IsInt},
+		"asn":            dbhelpers.WhereColumnInfo{"a.asn", nil},
+		"cachegroup":     dbhelpers.WhereColumnInfo{"c.id", nil},
+		"id":             dbhelpers.WhereColumnInfo{"a.id", api.IsInt},
+		"cachegroupName": dbhelpers.WhereColumnInfo{"c.name", nil},
 	}
 	where, orderBy, queryValues, errs := dbhelpers.BuildWhereAndOrderBy(parameters, queryParamsToQueryCols)
 	if len(errs) > 0 {
@@ -170,16 +171,16 @@ func (asn *TOASN) Read(db *sqlx.DB, parameters map[string]string, user auth.Curr
 	rows, err := db.NamedQuery(query, queryValues)
 	if err != nil {
 		log.Errorf("Error querying ASNs: %v", err)
-		return nil, []error{tc.DBError}, tc.SystemError
+		return nil, []error{err}, tc.SystemError
 	}
 	defer rows.Close()
 
 	ASNs := []interface{}{}
 	for rows.Next() {
-		var s tc.ASN
+		var s TOASN
 		if err = rows.StructScan(&s); err != nil {
 			log.Errorf("error parsing ASN rows: %v", err)
-			return nil, []error{tc.DBError}, tc.SystemError
+			return nil, []error{err}, tc.SystemError
 		}
 		ASNs = append(ASNs, s)
 	}
@@ -315,9 +316,8 @@ asn,
 cachegroup) 
 VALUES (
 :asn,
-(SELECT id from cachegroup
-WHERE name=:cachegroup
-))
+:cachegroup_id
+)
 RETURNING id,last_updated`
 	return query
 }
@@ -326,9 +326,7 @@ func updateQuery() string {
 	query := `UPDATE
 asn SET
 asn=:asn,
-cachegroup=(SELECT id from cachegroup
-WHERE name=:cachegroup
-)
+cachegroup=:cachegroup_id
 WHERE id=:id RETURNING last_updated`
 	return query
 }
diff --git a/traffic_ops/traffic_ops_golang/asn/asns_test.go b/traffic_ops/traffic_ops_golang/asn/asns_test.go
index dcb0a3b..6c500ff 100644
--- a/traffic_ops/traffic_ops_golang/asn/asns_test.go
+++ b/traffic_ops/traffic_ops_golang/asn/asns_test.go
@@ -34,18 +34,21 @@ import (
 	sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
 )
 
-func getTestASNs() []tc.ASN {
-	ASNs := []tc.ASN{}
-	testCase := tc.ASN{
-		ASN:         1,
-		Cachegroup:  "Yukon",
-		ID:          1,
-		LastUpdated: tc.TimeNoMod{Time: time.Now()},
+func getTestASNs() []TOASN {
+	ASNs := []TOASN{}
+	i := 1
+	c := "Yukon"
+	testCase := TOASN{
+		ASN:          &i,
+		Cachegroup:   &c,
+		CachegroupID: &i,
+		ID:           &i,
+		LastUpdated:  &tc.TimeNoMod{Time: time.Now()},
 	}
 	ASNs = append(ASNs, testCase)
 
 	testCase2 := testCase
-	testCase2.ASN = 2
+	*testCase2.ASN = 2
 	ASNs = append(ASNs, testCase2)
 
 	return ASNs
@@ -62,16 +65,15 @@ func TestGetASNs(t *testing.T) {
 	defer db.Close()
 
 	testCase := getTestASNs()
-	cols := test.ColsFromStructByTag("db", tc.ASN{})
+	cols := test.ColsFromStructByTag("db", TOASN{})
 	rows := sqlmock.NewRows(cols)
 
 	for _, ts := range testCase {
 		rows = rows.AddRow(
-			ts.ASN,
-			ts.Cachegroup,
-			ts.CachegroupID,
-			ts.ID,
-			ts.LastUpdated,
+			*ts.ASN,
+			*ts.CachegroupID,
+			*ts.ID,
+			*ts.LastUpdated,
 		)
 	}
 	mock.ExpectQuery("SELECT").WillReturnRows(rows)

-- 
To stop receiving notification emails like this one, please contact
dewrich@apache.org.