trafficcontrol-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dang...@apache.org
Subject [incubator-trafficcontrol] 07/28: implemented the Delete for parameters
Date Thu, 08 Mar 2018 22:31:21 GMT
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 8a8c80176f3ed602c7f93311532a86539c777c71
Author: Dewayne Richardson <dewrich@apache.org>
AuthorDate: Thu Mar 1 11:10:30 2018 -0700

    implemented the Delete for parameters
---
 .../traffic_ops_golang/parameter/parameters.go     | 52 ++++++++++++++++++++++
 .../parameter/parameters_test.go                   | 14 +++++-
 traffic_ops/traffic_ops_golang/routes.go           |  1 +
 3 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/traffic_ops/traffic_ops_golang/parameter/parameters.go b/traffic_ops/traffic_ops_golang/parameter/parameters.go
index 1d07f53..a3f0e90 100644
--- a/traffic_ops/traffic_ops_golang/parameter/parameters.go
+++ b/traffic_ops/traffic_ops_golang/parameter/parameters.go
@@ -21,6 +21,7 @@ package parameter
 
 import (
 	"errors"
+	"fmt"
 	"strconv"
 
 	"github.com/apache/incubator-trafficcontrol/lib/go-log"
@@ -203,6 +204,51 @@ func (parameter *TOParameter) Read(db *sqlx.DB, parameters map[string]string,
us
 
 }
 
+//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)
{
+	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", deleteQuery(), pl)
+	result, err := tx.NamedExec(deleteQuery(), pl)
+	if err != nil {
+		log.Errorf("received error: %++v from delete execution", err)
+		return tc.DBError, tc.SystemError
+	}
+	rowsAffected, err := result.RowsAffected()
+	if err != nil {
+		return tc.DBError, tc.SystemError
+	}
+	if rowsAffected < 1 {
+		return errors.New("no parameter with that id found"), tc.DataMissingError
+	}
+	if rowsAffected > 1 {
+		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 {
 
 	query := `SELECT
@@ -224,3 +270,9 @@ func ParametersGroupBy() string {
 	groupBy := ` GROUP BY p.config_file, p.id, p.last_updated, p.name, p.value, p.secure`
 	return groupBy
 }
+
+func deleteQuery() string {
+	query := `DELETE FROM parameter
+WHERE id=:id`
+	return query
+}
diff --git a/traffic_ops/traffic_ops_golang/parameter/parameters_test.go b/traffic_ops/traffic_ops_golang/parameter/parameters_test.go
index 114cf41..779d53c 100644
--- a/traffic_ops/traffic_ops_golang/parameter/parameters_test.go
+++ b/traffic_ops/traffic_ops_golang/parameter/parameters_test.go
@@ -108,7 +108,19 @@ func TestInterfaces(t *testing.T) {
 	var i interface{}
 	i = &TOParameter{}
 
+	if _, ok := i.(api.Creator); !ok {
+		t.Errorf("Parameter must be Creator")
+	}
 	if _, ok := i.(api.Reader); !ok {
-		t.Errorf("PhysLocation must be Reader")
+		t.Errorf("Parameter must be Reader")
+	}
+	//if _, ok := i.(api.Updater); !ok {
+	//t.Errorf("Parameter must be Updater")
+	//}
+	if _, ok := i.(api.Deleter); !ok {
+		t.Errorf("Parameter must be Deleter")
+	}
+	if _, ok := i.(api.Identifier); !ok {
+		t.Errorf("Parameter must be Identifier")
 	}
 }
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index 4b1feb3..f6783a9 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -152,6 +152,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
 		{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.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},
 
 		//Ping
 		{1.2, http.MethodGet, `ping$`, ping.PingHandler(), auth.PrivLevelReadOnly, Authenticated,
nil},

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

Mime
View raw message