You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/03/02 17:37:28 UTC

[GitHub] dangogh closed pull request #1953: TO golang -- adds validation to regions

dangogh closed pull request #1953: TO golang -- adds validation to regions
URL: https://github.com/apache/incubator-trafficcontrol/pull/1953
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/lib/go-tc/regions.go b/lib/go-tc/regions.go
index 379d56e3f9..bf4e72a85f 100644
--- a/lib/go-tc/regions.go
+++ b/lib/go-tc/regions.go
@@ -30,3 +30,11 @@ type Region struct {
 	LastUpdated  TimeNoMod `json:"lastUpdated" db:"last_updated"`
 	Name         string    `json:"name" db:"name"`
 }
+
+type RegionNullable struct {
+	DivisionName *string    `json:"divisionName"`
+	Division     *int       `json:"division" db:"division"`
+	ID           *int       `json:"id" db:"id"`
+	LastUpdated  *TimeNoMod `json:"lastUpdated" db:"last_updated"`
+	Name         *string    `json:"name" db:"name"`
+}
diff --git a/traffic_ops/traffic_ops_golang/region/regions.go b/traffic_ops/traffic_ops_golang/region/regions.go
index 6719f3e2e1..c77065d4df 100644
--- a/traffic_ops/traffic_ops_golang/region/regions.go
+++ b/traffic_ops/traffic_ops_golang/region/regions.go
@@ -22,49 +22,59 @@ package region
 import (
 	"errors"
 	"fmt"
+	"strconv"
 
 	"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/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tovalidate"
+	validation "github.com/go-ozzo/ozzo-validation"
 	"github.com/jmoiron/sqlx"
 	"github.com/lib/pq"
 )
 
 //we need a type alias to define functions on
-type TORegion tc.Region
+type TORegion tc.RegionNullable
 
 //the refType is passed into the handlers where a copy of its type is used to decode the json.
-var refType = TORegion(tc.Region{})
+var refType = TORegion(tc.RegionNullable{})
 
 func GetRefType() *TORegion {
 	return &refType
 }
 
 //Implementation of the Identifier, Validator interface functions
-func (region *TORegion) GetID() (int, bool) {
-	return region.ID, true
+func (region TORegion) GetID() (int, bool) {
+	if region.ID == nil {
+		return 0, false
+	}
+	return *region.ID, true
 }
 
-func (region *TORegion) GetAuditName() string {
-	return region.Name
+func (region TORegion) GetAuditName() string {
+	if region.Name == nil {
+		id, _ := region.GetID()
+		return strconv.Itoa(id)
+	}
+	return *region.Name
 }
 
-func (region *TORegion) GetType() string {
+func (region TORegion) GetType() string {
 	return "region"
 }
 
 func (region *TORegion) SetID(i int) {
-	region.ID = i
+	region.ID = &i
 }
 
-func (region *TORegion) Validate(db *sqlx.DB) []error {
-	errs := []error{}
-	if len(region.Name) < 1 {
-		errs = append(errs, errors.New(`Region 'name' is required.`))
+func (region TORegion) Validate(db *sqlx.DB) []error {
+	errs := validation.Errors{
+		"name":       validation.Validate(region.Name, validation.Required),
+		"divisionId": validation.Validate(region.Division, validation.NotNil, validation.Min(0)),
 	}
-	return errs
+	return tovalidate.ToErrors(errs)
 }
 
 func (region *TORegion) Read(db *sqlx.DB, parameters map[string]string, user auth.CurrentUser) ([]interface{}, []error, tc.ApiErrorType) {
@@ -94,7 +104,7 @@ func (region *TORegion) Read(db *sqlx.DB, parameters map[string]string, user aut
 
 	regions := []interface{}{}
 	for rows.Next() {
-		var s tc.Region
+		var s TORegion
 		if err = rows.StructScan(&s); err != nil {
 			log.Errorf("error parsing Region rows: %v", err)
 			return nil, []error{tc.DBError}, tc.SystemError
@@ -165,7 +175,7 @@ func (region *TORegion) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc.Ap
 		}
 	}
 	log.Debugf("lastUpdated: %++v", lastUpdated)
-	region.LastUpdated = lastUpdated
+	region.LastUpdated = &lastUpdated
 	if rowsAffected != 1 {
 		if rowsAffected < 1 {
 			return errors.New("no region found with this id"), tc.DataMissingError
@@ -240,7 +250,7 @@ func (region *TORegion) Create(db *sqlx.DB, user auth.CurrentUser) (error, tc.Ap
 		return tc.DBError, tc.SystemError
 	}
 	region.SetID(id)
-	region.LastUpdated = lastUpdated
+	region.LastUpdated = &lastUpdated
 	err = tx.Commit()
 	if err != nil {
 		log.Errorln("Could not commit transaction: ", err)
diff --git a/traffic_ops/traffic_ops_golang/region/regions_test.go b/traffic_ops/traffic_ops_golang/region/regions_test.go
index 3f08cc1f7b..bee63d3de7 100644
--- a/traffic_ops/traffic_ops_golang/region/regions_test.go
+++ b/traffic_ops/traffic_ops_golang/region/regions_test.go
@@ -20,6 +20,8 @@ package region
  */
 
 import (
+	"errors"
+	"reflect"
 	"testing"
 	"time"
 
@@ -106,3 +108,25 @@ func TestInterfaces(t *testing.T) {
 		t.Errorf("Region must be Identifier")
 	}
 }
+
+func TestValidate(t *testing.T) {
+	region := TORegion{}
+
+	errs := test.SortErrors(region.Validate(nil))
+	expected := []error{
+		errors.New(`'divisionId' is required`),
+		errors.New(`'name' cannot be blank`),
+	}
+	if !reflect.DeepEqual(expected, errs) {
+		t.Errorf(`expected %v,  got %v`, expected, errs)
+	}
+
+	i := 99
+	name := "just another cdn region"
+	region = TORegion{Name: &name, Division: &i}
+	errs = test.SortErrors(region.Validate(nil))
+	expected = []error{}
+	if !reflect.DeepEqual(expected, errs) {
+		t.Errorf(`expected %v,  got %v`, expected, errs)
+	}
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services