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/03/08 22:31:23 UTC

[incubator-trafficcontrol] 09/28: successfully implemented the Update functionality

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 10e7801fbf882a7f099f5d57a1408189a23e0b42
Author: Dewayne Richardson <de...@apache.org>
AuthorDate: Fri Mar 2 09:54:07 2018 -0700

    successfully implemented the Update functionality
---
 traffic_ops/testing/api/v13/parameters_test.go     |  5 +-
 .../traffic_ops_golang/parameter/parameters.go     | 76 ++++++++++++++++++++++
 traffic_ops/traffic_ops_golang/routes.go           |  1 +
 3 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/traffic_ops/testing/api/v13/parameters_test.go b/traffic_ops/testing/api/v13/parameters_test.go
index 916e546..7c59e1d 100644
--- a/traffic_ops/testing/api/v13/parameters_test.go
+++ b/traffic_ops/testing/api/v13/parameters_test.go
@@ -16,7 +16,6 @@
 package v13
 
 import (
-	"fmt"
 	"testing"
 
 	"github.com/apache/incubator-trafficcontrol/lib/go-log"
@@ -26,7 +25,7 @@ import (
 func TestParameters(t *testing.T) {
 
 	CreateTestParameters(t)
-	//UpdateTestParameters(t)
+	UpdateTestParameters(t)
 	GetTestParameters(t)
 	DeleteTestParameters(t)
 
@@ -108,8 +107,6 @@ func DeleteTestParameters(t *testing.T) {
 			if len(pls) > 0 {
 				t.Errorf("expected Parameter Name: %s and ConfigFile: %s to be deleted\n", pl.Name, pl.ConfigFile)
 			}
-		} else {
-			fmt.Printf("no resp ---> %v\n", resp)
 		}
 	}
 }
diff --git a/traffic_ops/traffic_ops_golang/parameter/parameters.go b/traffic_ops/traffic_ops_golang/parameter/parameters.go
index a3f0e90..f2c8845 100644
--- a/traffic_ops/traffic_ops_golang/parameter/parameters.go
+++ b/traffic_ops/traffic_ops_golang/parameter/parameters.go
@@ -204,6 +204,70 @@ func (parameter *TOParameter) Read(db *sqlx.DB, parameters map[string]string, us
 
 }
 
+//The TOParameter implementation of the Updater interface
+//all implementations of Updater should use transactions and return the proper errorType
+//ParsePQUniqueConstraintError is used to determine if a parameter 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 (pl *TOParameter) Update(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
+	}
+	log.Debugf("about to run exec query: %s with parameter: %++v", updateQuery(), pl)
+	resultRows, err := tx.NamedQuery(updateQuery(), pl)
+	if err != nil {
+		if pqErr, ok := err.(*pq.Error); ok {
+			err, eType := dbhelpers.ParsePQUniqueConstraintError(pqErr)
+			if eType == tc.DataConflictError {
+				return errors.New("a parameter with " + err.Error()), eType
+			}
+			return err, eType
+		}
+		log.Errorf("received error: %++v from update execution", err)
+		return tc.DBError, tc.SystemError
+	}
+	defer resultRows.Close()
+
+	// get LastUpdated field -- updated by trigger in the db
+	var lastUpdated tc.TimeNoMod
+	rowsAffected := 0
+	for resultRows.Next() {
+		rowsAffected++
+		if err := resultRows.Scan(&lastUpdated); err != nil {
+			log.Error.Printf("could not scan lastUpdated from insert: %s\n", err)
+			return tc.DBError, tc.SystemError
+		}
+	}
+	log.Debugf("lastUpdated: %++v", lastUpdated)
+	pl.LastUpdated = &lastUpdated
+	if rowsAffected != 1 {
+		if rowsAffected < 1 {
+			return errors.New("no parameter found with this id"), tc.DataMissingError
+		}
+		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
+}
+
 //The Parameter implementation of the Deleter interface
 //all implementations of Deleter should use transactions and return the proper errorType
 func (pl *TOParameter) Delete(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) {
@@ -265,6 +329,18 @@ LEFT JOIN profile pr ON pp.profile = pr.id`
 	return query
 }
 
+func updateQuery() string {
+	query := `UPDATE
+parameter SET
+config_file=:config_file,
+id=:id,
+name=:name,
+value=:value,
+secure=:secure
+WHERE id=:id RETURNING last_updated`
+	return query
+}
+
 // ParametersGroupBy ...
 func ParametersGroupBy() string {
 	groupBy := ` GROUP BY p.config_file, p.id, p.last_updated, p.name, p.value, p.secure`
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index f6783a9..b5ccaf0 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -151,6 +151,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
 		//Parameters
 		{1.3, http.MethodGet, `parameters/?(\.json)?$`, api.ReadHandler(parameter.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
 		{1.3, http.MethodGet, `parameters/{id}$`, api.ReadHandler(parameter.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},
+		{1.3, http.MethodPut, `parameters/{id}$`, api.UpdateHandler(parameter.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
 		{1.3, http.MethodPost, `parameters/?$`, api.CreateHandler(parameter.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
 		{1.3, http.MethodDelete, `parameters/{id}$`, api.DeleteHandler(parameter.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
 

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