You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2018/07/11 01:19:58 UTC

[trafficcontrol] 11/13: cleaned up after merge conflicts

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

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

commit 27a8394404cf7702677179c720c0b962415d3458
Author: Dewayne Richardson <de...@apache.org>
AuthorDate: Tue Jul 10 15:34:18 2018 -0600

    cleaned up after merge conflicts
---
 lib/go-tc/v13/staticdnsentries.go                  | 137 ---------------------
 traffic_ops/traffic_ops_golang/routes.go           |  11 +-
 .../staticdnsentry/staticdnsentry.go               | 136 +++++++++-----------
 3 files changed, 60 insertions(+), 224 deletions(-)

diff --git a/lib/go-tc/v13/staticdnsentries.go b/lib/go-tc/v13/staticdnsentries.go
deleted file mode 100644
index cfb1ef8..0000000
--- a/lib/go-tc/v13/staticdnsentries.go
+++ /dev/null
@@ -1,137 +0,0 @@
-package v13
-
-import tc "github.com/apache/trafficcontrol/lib/go-tc"
-
-/*
- * 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.
- */
-
-// StaticDNSEntries ...
-type StaticDNSEntriesResponse struct {
-	Response []StaticDNSEntry `json:"response"`
-}
-
-// StatisDNSEntry ...
-type StaticDNSEntry struct {
-
-	// The static IP Address or fqdn of the static dns entry
-	//
-	// required: true
-	Address string `json:"address" db:"address"`
-
-	// The Cachegroup Name associated
-	//
-	CacheGroupName string `json:"cachegroup"`
-
-	// The Cachegroup ID associated
-	//
-	CacheGroupID int `json:"cachegroupId" db:"cachegroup_id"`
-
-	// The DeliveryService associated
-	//
-	DeliveryService string `json:"deliveryservice" db:"dsname"`
-
-	// The DeliveryService associated
-	//
-	// required: true
-	DeliveryServiceID int `json:"deliveryserviceId" db:"deliveryservice_id"`
-
-	// The host of the static dns entry
-	//
-	// required: true
-	Host string `json:"host" db:"host"`
-
-	// ID of the StaticDNSEntry
-	//
-	// required: true
-	ID int `json:"id" db:"id"`
-
-	// LastUpdated
-	//
-	LastUpdated tc.TimeNoMod `json:"lastUpdated" db:"last_updated"`
-
-	// The Time To Live for the static dns entry
-	//
-	// required: true
-	TTL int64 `json:"ttl" db:"ttl"`
-
-	// The type of the static DNS entry
-	//
-	// enum: ["A_RECORD", "AAAA_RECORD", "CNAME_RECORD"]
-	Type string `json:"type"`
-
-	// The type id of the static DNS entry
-	//
-	// required: true
-	TypeID int `json:"typeId" db:"type_id"`
-}
-
-// StatisDNSEntryNullable ...
-type StaticDNSEntryNullable struct {
-
-	// The static IP Address or fqdn of the static dns entry
-	//
-	// required: true
-	Address *string `json:"address" db:"address"`
-
-	// The Cachegroup Name associated
-	//
-	CacheGroupName *string `json:"cachegroup" db:"cachegroup"`
-
-	// The Cachegroup ID associated
-	//
-	CacheGroupID *int `json:"cachegroupId" db:"cachegroup_id"`
-
-	// The DeliveryService Name associated
-	//
-	DeliveryService *string `json:"deliveryservice" db:"dsname"`
-
-	// DeliveryService ID of the StaticDNSEntry
-	//
-	// required: true
-	DeliveryServiceID *int `json:"deliveryserviceId" db:"deliveryservice_id"`
-
-	// The host of the static dns entry
-	//
-	// required: true
-	Host *string `json:"host" db:"host"`
-
-	// ID of the StaticDNSEntry
-	//
-	// required: true
-	ID *int `json:"id" db:"id"`
-
-	// LastUpdated
-	//
-	LastUpdated *tc.TimeNoMod `json:"lastUpdated" db:"last_updated"`
-
-	// The Time To Live for the static dns entry
-	//
-	// required: true
-	TTL *int64 `json:"ttl" db:"ttl"`
-
-	// The type of the static DNS entry
-	//
-	// enum: ["A_RECORD", "AAAA_RECORD", "CNAME_RECORD"]
-	Type *string `json:"type"`
-
-	// The type id of the static DNS entry
-	//
-	// required: true
-	TypeID int `json:"typeId" db:"type_id"`
-}
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index dc79fb6..ed48e2a 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -316,12 +316,11 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) {
 		{1.3, http.MethodGet, `servers/{host_name}/update_status$`, server.GetServerUpdateStatusHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 
 		//StaticDNSEntries
-		{1.1, http.MethodGet, `staticdnsentries/?(\.json)?$`, api.ReadOnlyHandler(staticdnsentry.GetReaderSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
-		{1.3, http.MethodGet, `staticdnsentries/?(\.json)?$`, api.ReadHandler(staticdnsentry.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
-		{1.3, http.MethodGet, `staticdnsentries/?$`, api.ReadHandler(staticdnsentry.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
-		{1.3, http.MethodPut, `staticdnsentries/?$`, api.UpdateHandler(staticdnsentry.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
-		{1.3, http.MethodPost, `staticdnsentries/?$`, api.CreateHandler(staticdnsentry.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
-		{1.3, http.MethodDelete, `staticdnsentries/?$`, api.DeleteHandler(staticdnsentry.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
+		{1.1, http.MethodGet, `staticdnsentries/?(\.json)?$`, api.ReadHandler(staticdnsentry.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
+		{1.3, http.MethodGet, `staticdnsentries/?$`, api.ReadHandler(staticdnsentry.GetTypeSingleton()), auth.PrivLevelReadOnly, Authenticated, nil},
+		{1.3, http.MethodPut, `staticdnsentries/?$`, api.UpdateHandler(staticdnsentry.GetTypeSingleton()), auth.PrivLevelOperations, Authenticated, nil},
+		{1.3, http.MethodPost, `staticdnsentries/?$`, api.CreateHandler(staticdnsentry.GetTypeSingleton()), auth.PrivLevelOperations, Authenticated, nil},
+		{1.3, http.MethodDelete, `staticdnsentries/?$`, api.DeleteHandler(staticdnsentry.GetTypeSingleton()), auth.PrivLevelOperations, Authenticated, nil},
 
 		//ProfileParameters
 		{1.1, http.MethodGet, `profiles/{id}/parameters/?(\.json)?$`, profileparameter.GetProfileID(d.DB.DB), auth.PrivLevelReadOnly, Authenticated, nil},
diff --git a/traffic_ops/traffic_ops_golang/staticdnsentry/staticdnsentry.go b/traffic_ops/traffic_ops_golang/staticdnsentry/staticdnsentry.go
index 292338c..6ba3944 100644
--- a/traffic_ops/traffic_ops_golang/staticdnsentry/staticdnsentry.go
+++ b/traffic_ops/traffic_ops_golang/staticdnsentry/staticdnsentry.go
@@ -22,33 +22,74 @@ package staticdnsentry
 import (
 	"errors"
 	"fmt"
+	"strconv"
 
 	"github.com/apache/trafficcontrol/lib/go-log"
 	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/lib/go-tc/tovalidate"
+	"github.com/apache/trafficcontrol/lib/go-tc/v13"
+	"github.com/apache/trafficcontrol/lib/go-util"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
-	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
-	"github.com/jmoiron/sqlx"
+	validation "github.com/go-ozzo/ozzo-validation"
 	"github.com/lib/pq"
 )
 
 type TOStaticDNSEntry struct {
 	ReqInfo *api.APIInfo `json:"-"`
-	tc.StaticDNSEntry
+	v13.StaticDNSEntryNullable
 }
 
-func GetReaderSingleton() func(reqInfo *api.APIInfo) api.Reader {
-	return func(reqInfo *api.APIInfo) api.Reader {
-		toReturn := TOStaticDNSEntry{reqInfo, tc.StaticDNSEntry{}}
+func GetTypeSingleton() api.CRUDFactory {
+	return func(reqInfo *api.APIInfo) api.CRUDer {
+		toReturn := TOStaticDNSEntry{reqInfo, v13.StaticDNSEntryNullable{}}
 		return &toReturn
 	}
 }
 
+func (staticDNSEntry TOStaticDNSEntry) GetKeyFieldsInfo() []api.KeyFieldInfo {
+	return []api.KeyFieldInfo{{"id", api.GetIntKey}}
+}
+
+//Implementation of the Identifier, Validator interface functions
+func (staticDNSEntry TOStaticDNSEntry) GetKeys() (map[string]interface{}, bool) {
+	if staticDNSEntry.ID == nil {
+		return map[string]interface{}{"id": 0}, false
+	}
+	return map[string]interface{}{"id": *staticDNSEntry.ID}, true
+}
+
+func (staticDNSEntry TOStaticDNSEntry) GetAuditName() string {
+	if staticDNSEntry.Host != nil {
+		return *staticDNSEntry.Host
+	}
+	if staticDNSEntry.ID != nil {
+		return strconv.Itoa(*staticDNSEntry.ID)
+	}
+	return "0"
+}
+
+func (staticDNSEntry TOStaticDNSEntry) GetType() string {
+	return "staticDNSEntry"
+}
+
 func (staticDNSEntry *TOStaticDNSEntry) SetKeys(keys map[string]interface{}) {
 	i, _ := keys["id"].(int) //this utilizes the non panicking type assertion, if the thrown away ok variable is false i will be the zero of the type, 0 here.
 	staticDNSEntry.ID = &i
 }
 
+// Validate fulfills the api.Validator interface
+func (staticDNSEntry TOStaticDNSEntry) Validate() error {
+	errs := validation.Errors{
+		"host":    validation.Validate(staticDNSEntry.Host, validation.Required),
+		"address": validation.Validate(staticDNSEntry.Address, validation.Required),
+		"dsname":  validation.Validate(staticDNSEntry.DeliveryService, validation.Required),
+		"ttl":     validation.Validate(staticDNSEntry.TTL, validation.Required),
+		"type":    validation.Validate(staticDNSEntry.Type, validation.Required),
+	}
+	return util.JoinErrs(tovalidate.ToErrors(errs))
+}
+
 func (staticDNSEntry *TOStaticDNSEntry) Read(parameters map[string]string) ([]interface{}, []error, tc.ApiErrorType) {
 	queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
 		"deliveryservice": dbhelpers.WhereColumnInfo{"deliveryservice", nil}, // order by
@@ -68,7 +109,7 @@ func (staticDNSEntry *TOStaticDNSEntry) Read(parameters map[string]string) ([]in
 	defer rows.Close()
 	staticDNSEntries := []interface{}{}
 	for rows.Next() {
-		s := tc.StaticDNSEntry{}
+		s := v13.StaticDNSEntry{}
 		if err = rows.StructScan(&s); err != nil {
 			log.Errorln("error parsing StaticDNSEntry rows: " + err.Error())
 			return nil, []error{tc.DBError}, tc.SystemError
@@ -85,25 +126,8 @@ func (staticDNSEntry *TOStaticDNSEntry) Read(parameters map[string]string) ([]in
 //generic error message returned
 //The insert sql returns the id and lastUpdated values of the newly inserted staticDNSEntry and have
 //to be added to the struct
-func (staticDNSEntry *TOStaticDNSEntry) Create(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
-	rollbackTransaction := true
-	tx, err := db.Beginx()
-	defer func() {
-		if tx == nil || !rollbackTransaction {
-			return
-		}
-		err := tx.Rollback()
-		if err != nil {
-			log.Errorln(errors.New("rolling back transaction: " + err.Error()))
-		}
-	}()
-
-	if err != nil {
-		log.Error.Printf("could not begin transaction: %v", err)
-		return tc.DBError, tc.SystemError
-	}
-	// make sure that staticDNSEntry.DomainName is lowercase
-	resultRows, err := tx.NamedQuery(insertQuery(), staticDNSEntry)
+func (staticDNSEntry *TOStaticDNSEntry) Create() (error, tc.ApiErrorType) {
+	resultRows, err := staticDNSEntry.ReqInfo.Tx.NamedQuery(insertQuery(), staticDNSEntry)
 	if err != nil {
 		if pqErr, ok := err.(*pq.Error); ok {
 			err, eType := dbhelpers.ParsePQUniqueConstraintError(pqErr)
@@ -139,12 +163,6 @@ func (staticDNSEntry *TOStaticDNSEntry) Create(db *sqlx.DB, user auth.CurrentUse
 	}
 	staticDNSEntry.SetKeys(map[string]interface{}{"id": id})
 	staticDNSEntry.LastUpdated = &lastUpdated
-	err = tx.Commit()
-	if err != nil {
-		log.Errorln("Could not commit transaction: ", err)
-		return tc.DBError, tc.SystemError
-	}
-	rollbackTransaction = false
 	return nil, tc.NoError
 }
 
@@ -170,26 +188,9 @@ ttl) VALUES (
 //ParsePQUniqueConstraintError is used to determine if a staticDNSEntry with conflicting values exists
 //if so, it will return an errorType of DataConflict and the type should be appended to the
 //generic error message returned
-func (staticDNSEntry *TOStaticDNSEntry) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
-	fmt.Printf("staticDNSEntry.DeliveryService ---> %v\n", *staticDNSEntry.DeliveryService)
-	rollbackTransaction := true
-	tx, err := db.Beginx()
-	defer func() {
-		if tx == nil || !rollbackTransaction {
-			return
-		}
-		err := tx.Rollback()
-		if err != nil {
-			log.Errorln(errors.New("rolling back transaction: " + err.Error()))
-		}
-	}()
-
-	if err != nil {
-		log.Error.Printf("could not begin transaction: %v", err)
-		return tc.DBError, tc.SystemError
-	}
+func (staticDNSEntry *TOStaticDNSEntry) Update() (error, tc.ApiErrorType) {
 	log.Debugf("about to run exec query: %s with staticDNSEntry: %++v", updateQuery(), staticDNSEntry)
-	resultRows, err := tx.NamedQuery(updateQuery(), staticDNSEntry)
+	resultRows, err := staticDNSEntry.ReqInfo.Tx.NamedQuery(updateQuery(), staticDNSEntry)
 	if err != nil {
 		if pqErr, ok := err.(*pq.Error); ok {
 			err, eType := dbhelpers.ParsePQUniqueConstraintError(pqErr)
@@ -222,12 +223,6 @@ func (staticDNSEntry *TOStaticDNSEntry) Update(db *sqlx.DB, user auth.CurrentUse
 			return fmt.Errorf("this update affected too many rows: %d", rowsAffected), tc.SystemError
 		}
 	}
-	err = tx.Commit()
-	if err != nil {
-		log.Errorln("Could not commit transaction: ", err)
-		return tc.DBError, tc.SystemError
-	}
-	rollbackTransaction = false
 	return nil, tc.NoError
 }
 
@@ -247,25 +242,9 @@ WHERE id=:id RETURNING last_updated`
 
 //The StaticDNSEntry implementation of the Deleter interface
 //all implementations of Deleter should use transactions and return the proper errorType
-func (staticDNSEntry *TOStaticDNSEntry) Delete(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
-	rollbackTransaction := true
-	tx, err := db.Beginx()
-	defer func() {
-		if tx == nil || !rollbackTransaction {
-			return
-		}
-		err := tx.Rollback()
-		if err != nil {
-			log.Errorln(errors.New("rolling back transaction: " + err.Error()))
-		}
-	}()
-
-	if err != nil {
-		log.Error.Printf("could not begin transaction: %v", err)
-		return tc.DBError, tc.SystemError
-	}
+func (staticDNSEntry *TOStaticDNSEntry) Delete() (error, tc.ApiErrorType) {
 	log.Debugf("about to run exec query: %s with staticDNSEntry: %++v", deleteQuery(), staticDNSEntry)
-	result, err := tx.NamedExec(deleteQuery(), staticDNSEntry)
+	result, err := staticDNSEntry.ReqInfo.Tx.NamedExec(deleteQuery(), staticDNSEntry)
 	if err != nil {
 		log.Errorf("received error: %++v from delete execution", err)
 		return tc.DBError, tc.SystemError
@@ -281,13 +260,8 @@ func (staticDNSEntry *TOStaticDNSEntry) Delete(db *sqlx.DB, user auth.CurrentUse
 			return fmt.Errorf("this create affected too many rows: %d", rowsAffected), tc.SystemError
 		}
 	}
-	err = tx.Commit()
-	if err != nil {
-		log.Errorln("Could not commit transaction: ", err)
-		return tc.DBError, tc.SystemError
-	}
-	rollbackTransaction = false
 	return nil, tc.NoError
+
 }
 
 func selectQuery() string {