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 99f7c240c848a3ee47d35214e904573627411a8b Author: Dewayne Richardson <dewr...@apache.org> AuthorDate: Tue Feb 27 11:36:28 2018 -0700 updated to use the Read() interface --- lib/go-tc/parameters.go | 14 +++ .../{ => parameter}/parameters.go | 101 +++++++++------------ .../{ => parameter}/parameters_test.go | 2 +- .../physlocation/phys_locations.go | 5 +- traffic_ops/traffic_ops_golang/routes.go | 4 +- 5 files changed, 64 insertions(+), 62 deletions(-) diff --git a/lib/go-tc/parameters.go b/lib/go-tc/parameters.go index cbf7afb..5949e89 100644 --- a/lib/go-tc/parameters.go +++ b/lib/go-tc/parameters.go @@ -36,3 +36,17 @@ type Parameter struct { Secure bool `json:"secure" db:"secure"` Value string `json:"value" db:"value"` } + +// ParameterNullable - a struct version that allows for all fields to be null, mostly used by the API side +type ParameterNullable struct { + // + // NOTE: the db: struct tags are used for testing to map to their equivalent database column (if there is one) + // + ConfigFile *string `json:"configFile" db:"config_file"` + ID *int `json:"id" db:"id"` + LastUpdated *TimeNoMod `json:"lastUpdated" db:"last_updated"` + Name *string `json:"name" db:"name"` + Profiles json.RawMessage `json:"profiles" db:"profiles"` + Secure *bool `json:"secure" db:"secure"` + Value *string `json:"value" db:"value"` +} diff --git a/traffic_ops/traffic_ops_golang/parameters.go b/traffic_ops/traffic_ops_golang/parameter/parameters.go similarity index 54% rename from traffic_ops/traffic_ops_golang/parameters.go rename to traffic_ops/traffic_ops_golang/parameter/parameters.go index bbba922..41fbeaa 100644 --- a/traffic_ops/traffic_ops_golang/parameters.go +++ b/traffic_ops/traffic_ops_golang/parameter/parameters.go @@ -1,4 +1,4 @@ -package main +package parameter /* * Licensed to the Apache Software Foundation (ASF) under one @@ -20,9 +20,7 @@ package main */ import ( - "encoding/json" - "fmt" - "net/http" + "strconv" "github.com/apache/incubator-trafficcontrol/lib/go-log" "github.com/apache/incubator-trafficcontrol/lib/go-tc" @@ -33,61 +31,48 @@ import ( "github.com/jmoiron/sqlx" ) -func parametersHandler(db *sqlx.DB) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - handleErrs := tc.GetHandleErrorsFunc(w, r) +//we need a type alias to define functions on +type TOParameter tc.ParameterNullable - ctx := r.Context() - user, err := auth.GetCurrentUser(ctx) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } - privLevel := user.PrivLevel - - params, err := api.GetCombinedParams(r) - if err != nil { - log.Errorf("unable to get parameters from request: %s", err) - handleErrs(http.StatusInternalServerError, err) - } - - resp, errs, errType := getParametersResponse(params, db, privLevel) - if len(errs) > 0 { - tc.HandleErrorsWithType(errs, errType, handleErrs) - return - } +//the refType is passed into the handlers where a copy of its type is used to decode the json. +var refType = TOParameter(tc.ParameterNullable{}) - respBts, err := json.Marshal(resp) - if err != nil { - handleErrs(http.StatusInternalServerError, err) - return - } +func GetRefType() *TOParameter { + return &refType +} - w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, "%s", respBts) +//Implementation of the Identifier, Validator interface functions +func (parameter *TOParameter) GetID() (int, bool) { + if parameter.ID == nil { + return 0, false } + return *parameter.ID, true } -func getParametersResponse(params map[string]string, db *sqlx.DB, privLevel int) (*tc.ParametersResponse, []error, tc.ApiErrorType) { - parameters, errs, errType := getParameters(params, db, privLevel) - if len(errs) > 0 { - return nil, errs, errType +func (parameter *TOParameter) GetAuditName() string { + if parameter.Name != nil { + return *parameter.Name } - - resp := tc.ParametersResponse{ - Response: parameters, + if parameter.ID != nil { + return strconv.Itoa(*parameter.ID) } - return &resp, nil, tc.NoError + return "unknown" +} + +func (parameter *TOParameter) GetType() string { + return "parameter" } -func getParameters(params map[string]string, db *sqlx.DB, privLevel int) ([]tc.Parameter, []error, tc.ApiErrorType) { +func (parameter *TOParameter) SetID(i int) { + parameter.ID = &i +} +func (parameter *TOParameter) Read(db *sqlx.DB, parameters map[string]string, user auth.CurrentUser) ([]interface{}, []error, tc.ApiErrorType) { var rows *sqlx.Rows - var err error // Query Parameters to Database Query column mappings // see the fields mapped in the SQL query - queryParamsToSQLCols := map[string]dbhelpers.WhereColumnInfo{ + queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{ "config_file": dbhelpers.WhereColumnInfo{"p.config_file", nil}, "id": dbhelpers.WhereColumnInfo{"p.id", api.IsInt}, "last_updated": dbhelpers.WhereColumnInfo{"p.last_updated", nil}, @@ -95,36 +80,36 @@ func getParameters(params map[string]string, db *sqlx.DB, privLevel int) ([]tc.P "secure": dbhelpers.WhereColumnInfo{"p.secure", api.IsBool}, } - where, orderBy, queryValues, errs := dbhelpers.BuildWhereAndOrderBy(params, queryParamsToSQLCols) + where, orderBy, queryValues, errs := dbhelpers.BuildWhereAndOrderBy(parameters, queryParamsToQueryCols) if len(errs) > 0 { return nil, errs, tc.DataConflictError } - query := selectParametersQuery() + where + ParametersGroupBy() + orderBy + query := selectQuery() + where + ParametersGroupBy() + orderBy log.Debugln("Query is ", query) - rows, err = db.NamedQuery(query, queryValues) + rows, err := db.NamedQuery(query, queryValues) if err != nil { - return nil, []error{fmt.Errorf("querying: %v", err)}, tc.SystemError + log.Errorf("Error querying Parameters: %v", err) + return nil, []error{tc.DBError}, tc.SystemError } defer rows.Close() - parameters := []tc.Parameter{} + params := []interface{}{} for rows.Next() { - var s tc.Parameter + var s tc.ParameterNullable if err = rows.StructScan(&s); err != nil { - return nil, []error{fmt.Errorf("getting parameters: %v", err)}, tc.SystemError + log.Errorf("error parsing Parameter rows: %v", err) + return nil, []error{tc.DBError}, tc.SystemError } - if s.Secure && privLevel < auth.PrivLevelAdmin { - // Secure params only visible to admin - continue - } - parameters = append(parameters, s) + params = append(params, s) } - return parameters, nil, tc.NoError + + return params, []error{}, tc.NoError + } -func selectParametersQuery() string { +func selectQuery() string { query := `SELECT p.config_file, diff --git a/traffic_ops/traffic_ops_golang/parameters_test.go b/traffic_ops/traffic_ops_golang/parameter/parameters_test.go similarity index 99% rename from traffic_ops/traffic_ops_golang/parameters_test.go rename to traffic_ops/traffic_ops_golang/parameter/parameters_test.go index 1b9049c..a5e20b0 100644 --- a/traffic_ops/traffic_ops_golang/parameters_test.go +++ b/traffic_ops/traffic_ops_golang/parameter/parameters_test.go @@ -1,4 +1,4 @@ -package main +package parameter /* * Licensed to the Apache Software Foundation (ASF) under one diff --git a/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go b/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go index 451fd92..be45249 100644 --- a/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go +++ b/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go @@ -186,6 +186,7 @@ func (pl *TOPhysLocation) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc. } defer resultRows.Close() + // get LastUpdated field -- updated by trigger in the db var lastUpdated tc.TimeNoMod rowsAffected := 0 for resultRows.Next() { @@ -196,7 +197,7 @@ func (pl *TOPhysLocation) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc. } } log.Debugf("lastUpdated: %++v", lastUpdated) - pl.LastUpdated = lastUpdated + pl.LastUpdated = &lastUpdated if rowsAffected != 1 { if rowsAffected < 1 { return errors.New("no phys_location found with this id"), tc.DataMissingError @@ -272,7 +273,7 @@ func (pl *TOPhysLocation) Create(db *sqlx.DB, user auth.CurrentUser) (error, tc. } pl.SetID(id) - pl.LastUpdated = lastUpdated + pl.LastUpdated = &lastUpdated err = tx.Commit() if err != nil { log.Errorln("Could not commit transaction: ", err) diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go index 7c7df77..4bbe650 100644 --- a/traffic_ops/traffic_ops_golang/routes.go +++ b/traffic_ops/traffic_ops_golang/routes.go @@ -35,6 +35,7 @@ import ( "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/cdn" dsrequest "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice/request" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/division" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/parameter" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/physlocation" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/ping" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/region" @@ -148,7 +149,8 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { {1.3, http.MethodDelete, `regions/{id}$`, api.DeleteHandler(region.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil}, //Parameters - {1.3, http.MethodGet, `parameters/?(\.json)?$`, parametersHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + {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}, //Ping {1.2, http.MethodGet, `ping$`, ping.PingHandler(), auth.PrivLevelReadOnly, Authenticated, nil}, -- To stop receiving notification emails like this one, please contact dang...@apache.org.