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 589df81e82b1c7305e63cf3d43fe79c2bcbd3313 Author: Dewayne Richardson <dewr...@apache.org> AuthorDate: Wed Feb 14 11:07:28 2018 -0700 renamed the Inserter interface to Creator to match the CRUD acronym --- traffic_ops/client/division.go | 132 +++++++++++++++++++++ traffic_ops/traffic_ops_golang/api/change_log.go | 2 +- .../traffic_ops_golang/api/shared_handlers.go | 14 +-- .../traffic_ops_golang/api/shared_interfaces.go | 4 +- traffic_ops/traffic_ops_golang/cdn/cdns.go | 2 +- traffic_ops/traffic_ops_golang/routes.go | 5 + 6 files changed, 148 insertions(+), 11 deletions(-) diff --git a/traffic_ops/client/division.go b/traffic_ops/client/division.go new file mode 100644 index 0000000..cda8701 --- /dev/null +++ b/traffic_ops/client/division.go @@ -0,0 +1,132 @@ +/* + + Licensed 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. +*/ + +package client + +import ( + "encoding/json" + "fmt" + "net" + "net/http" + + "github.com/apache/incubator-trafficcontrol/lib/go-tc" +) + +const ( + API_v2_Divisions = "/api/1.3/divisions" +) + +// Create a Division +func (to *Session) CreateDivision(division tc.Division) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(division) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + resp, remoteAddr, err := to.request(http.MethodPost, API_v2_Divisions, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Update a Division by ID +func (to *Session) UpdateDivisionByID(id int, division tc.Division) (tc.Alerts, ReqInf, error) { + + var remoteAddr net.Addr + reqBody, err := json.Marshal(division) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + route := fmt.Sprintf("%s/%d", API_v2_Divisions, id) + resp, remoteAddr, err := to.request(http.MethodPut, route, reqBody) + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} + +// Returns a list of Divisions +func (to *Session) GetDivisions() ([]tc.Division, ReqInf, error) { + resp, remoteAddr, err := to.request(http.MethodGet, API_v2_Divisions, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.DivisionsResponse + err = json.NewDecoder(resp.Body).Decode(&data) + return data.Response, reqInf, nil +} + +// GET a Division by the Division id +func (to *Session) GetDivisionByID(id int) ([]tc.Division, ReqInf, error) { + route := fmt.Sprintf("%s/%d", API_v2_Divisions, id) + resp, remoteAddr, err := to.request(http.MethodGet, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.DivisionsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// GET a Division by the Division name +func (to *Session) GetDivisionByName(name string) ([]tc.Division, ReqInf, error) { + url := fmt.Sprintf("%s/name/%s", API_v2_Divisions, name) + resp, remoteAddr, err := to.request(http.MethodGet, url, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return nil, reqInf, err + } + defer resp.Body.Close() + + var data tc.DivisionsResponse + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, reqInf, err + } + + return data.Response, reqInf, nil +} + +// DELETE a Division by Division name +func (to *Session) DeleteDivisionByName(name string) (tc.Alerts, ReqInf, error) { + route := fmt.Sprintf("%s/name/%s", API_v2_Divisions, name) + resp, remoteAddr, err := to.request(http.MethodDelete, route, nil) + reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr} + if err != nil { + return tc.Alerts{}, reqInf, err + } + defer resp.Body.Close() + var alerts tc.Alerts + err = json.NewDecoder(resp.Body).Decode(&alerts) + return alerts, reqInf, nil +} diff --git a/traffic_ops/traffic_ops_golang/api/change_log.go b/traffic_ops/traffic_ops_golang/api/change_log.go index 53d4128..73c06fc 100644 --- a/traffic_ops/traffic_ops_golang/api/change_log.go +++ b/traffic_ops/traffic_ops_golang/api/change_log.go @@ -44,7 +44,7 @@ const ( Deleted = "Deleted" ) -func InsertChangeLog(level string, action string, i Identifier, user auth.CurrentUser, db *sqlx.DB) error { +func CreateChangeLog(level string, action string, i Identifier, user auth.CurrentUser, db *sqlx.DB) error { query := `INSERT INTO log (level, message, tm_user) VALUES ($1, $2, $3)` id, _ := i.GetID() message := action + " " + i.GetType() + ": " + i.GetAuditName() + " id: " + strconv.Itoa(id) diff --git a/traffic_ops/traffic_ops_golang/api/shared_handlers.go b/traffic_ops/traffic_ops_golang/api/shared_handlers.go index 89208fa..903fa9f 100644 --- a/traffic_ops/traffic_ops_golang/api/shared_handlers.go +++ b/traffic_ops/traffic_ops_golang/api/shared_handlers.go @@ -220,7 +220,7 @@ func UpdateHandler(typeRef Updater, db *sqlx.DB) http.HandlerFunc { return } //auditing here - InsertChangeLog(ApiChange, Updated, u, *user, db) + CreateChangeLog(ApiChange, Updated, u, *user, db) //form response to send across the wire resp := struct { Response interface{} `json:"response"` @@ -291,7 +291,7 @@ func DeleteHandler(typeRef Deleter, db *sqlx.DB) http.HandlerFunc { return } //audit here - InsertChangeLog(ApiChange, Deleted, d, *user, db) + CreateChangeLog(ApiChange, Deleted, d, *user, db) // resp := struct { tc.Alerts @@ -308,7 +308,7 @@ func DeleteHandler(typeRef Deleter, db *sqlx.DB) http.HandlerFunc { } } -//this creates a handler function from the pointer to a struct implementing the Inserter interface +//this creates a handler function from the pointer to a struct implementing the Creator interface //it must be immediately assigned to a local variable // this generic handler encapsulates the logic for handling: // *fetching the id from the path parameter @@ -316,7 +316,7 @@ func DeleteHandler(typeRef Deleter, db *sqlx.DB) http.HandlerFunc { // *decoding and validating the struct // *change log entry // *forming and writing the body over the wire -func CreateHandler(typeRef Inserter, db *sqlx.DB) http.HandlerFunc { +func CreateHandler(typeRef Creator, db *sqlx.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { handleErrs := tc.GetHandleErrorsFunc(w, r) @@ -326,7 +326,7 @@ func CreateHandler(typeRef Inserter, db *sqlx.DB) http.HandlerFunc { handleErrs(http.StatusBadRequest, errs...) return } - i := decoded.(Inserter) + i := decoded.(Creator) log.Debugf("%++v", i) //now we have a validated local object to insert @@ -351,13 +351,13 @@ func CreateHandler(typeRef Inserter, db *sqlx.DB) http.HandlerFunc { } } - err, errType := i.Insert(db, *user) + err, errType := i.Create(db, *user) if err != nil { tc.HandleErrorsWithType([]error{err}, errType, handleErrs) return } - InsertChangeLog(ApiChange, Created, i, *user, db) + CreateChangeLog(ApiChange, Created, i, *user, db) resp := struct { Response interface{} `json:"response"` diff --git a/traffic_ops/traffic_ops_golang/api/shared_interfaces.go b/traffic_ops/traffic_ops_golang/api/shared_interfaces.go index 0cd04ba..da23de5 100644 --- a/traffic_ops/traffic_ops_golang/api/shared_interfaces.go +++ b/traffic_ops/traffic_ops_golang/api/shared_interfaces.go @@ -37,8 +37,8 @@ type Identifier interface { GetAuditName() string } -type Inserter interface { - Insert(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) +type Creator interface { + Create(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) SetID(int) Identifier Validator diff --git a/traffic_ops/traffic_ops_golang/cdn/cdns.go b/traffic_ops/traffic_ops_golang/cdn/cdns.go index 9766c28..00b99a8 100644 --- a/traffic_ops/traffic_ops_golang/cdn/cdns.go +++ b/traffic_ops/traffic_ops_golang/cdn/cdns.go @@ -194,7 +194,7 @@ func (cdn *TOCDN) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiError //generic error message returned //The insert sql returns the id and lastUpdated values of the newly inserted cdn and have //to be added to the struct -func (cdn *TOCDN) Insert(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) { +func (cdn *TOCDN) Create(db *sqlx.DB, user auth.CurrentUser) (error, tc.ApiErrorType) { rollbackTransaction := true tx, err := db.Beginx() defer func() { diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go index 4bdd5a7..4be4d4d 100644 --- a/traffic_ops/traffic_ops_golang/routes.go +++ b/traffic_ops/traffic_ops_golang/routes.go @@ -66,6 +66,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { {1.2, http.MethodGet, `cdns/routing$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, {1.2, http.MethodGet, `cdns/{name}/configs/monitoring(\.json)?$`, monitoringHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + //CDN generic handlers: {1.3, http.MethodGet, `cdns/?(\.json)?$`, api.ReadHandler(cdn.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, {1.3, http.MethodGet, `cdns/{id}$`, api.ReadHandler(cdn.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, @@ -89,14 +90,18 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { //Divisions {1.2, http.MethodGet, `divisions/?(\.json)?$`, api.ReadHandler(division.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + {1.3, http.MethodGet, `divisions/{id}$`, api.ReadHandler(division.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, //HwInfo {1.2, http.MethodGet, `hwinfo-wip/?(\.json)?$`, hwInfoHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + //Parameters {1.3, http.MethodGet, `parameters/?(\.json)?$`, parametersHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + //Regions {1.2, http.MethodGet, `regions/?(\.json)?$`, regionsHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, {1.2, http.MethodGet, `regions/{id}$`, regionsHandler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + //Servers // explicitly passed to legacy system until fully implemented. Auth handled by legacy system. {1.2, http.MethodGet, `servers/checks$`, handlerToFunc(proxyHandler), 0, NoAuth, []Middleware{}}, -- To stop receiving notification emails like this one, please contact dang...@apache.org.