rob05c commented on a change in pull request #2269: Deliveryservice_Server API
conversion to Go
URL:
https://github.com/apache/incubator-trafficcontrol/pull/2269#discussion_r188113319
##########
File path: traffic_ops/traffic_ops_golang/deliveryservice/servers/servers.go
##########
@@ -263,7 +243,409 @@ func (dss *TODeliveryServiceServer) Delete(db *sqlx.DB,
user auth.CurrentUser) (
rollbackTransaction = false
return nil, tc.NoError
}
-func selectQuery() string {
+
+func selectQuery( orderby string, limit string, offset string) string {
+
+ selectStmt := `SELECT
+ s.deliveryService,
+ s.server,
+ s.last_updated
+ FROM deliveryservice_server s
+ ORDER BY `+ orderby +` LIMIT `+limit+` OFFSET `+offset+` ROWS`
+
+ return selectStmt
+}
+
+func deleteQuery() string {
+ query := `DELETE FROM deliveryservice_server
+ WHERE deliveryservice=:deliveryservice and server=:server`
+ return query
+}
+
+
+type DSServers struct {
+ DsId *int `json:"dsId"
db:"deliveryservice"`
+ Servers []int `json:"servers"`
+ Replace *bool `json:"replace"`
+}
+
+type TODSServers DSServers
+var dsserversRef = TODSServers(DSServers{})
+
+func GetServersForDsIdRef() *TODSServers {
+ return &dsserversRef
+}
+
+func GetReplaceHandler( db *sqlx.DB ) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ handleErrs := tc.GetHandleErrorsFunc(w, r)
+ ctx := r.Context()
+ user, err := auth.GetCurrentUser(ctx)
+ if err != nil {
+ log.Errorf("unable to retrieve current user from
context: %s", err)
+ handleErrs(http.StatusInternalServerError, err)
+ return
+ }
+
+ // get list of server Ids to insert
+ defer r.Body.Close()
+ payload := GetServersForDsIdRef()
+ servers := payload.Servers
+ dsId := payload.DsId
+
+ if err := json.NewDecoder(r.Body).Decode(payload); err != nil {
+ log.Errorf("Error trying to decode the request body:
%s", err)
+ handleErrs(http.StatusInternalServerError, err)
+ return
+ }
+
+ // if the object has tenancy enabled, check that user is able
to access the tenant
+ // check user tenancy access to this resource.
+ row := db.QueryRow("SELECT xml_id FROM deliveryservice WHERE id
= $1", *dsId)
+ var xmlId string
+ row.Scan(&xmlId)
+ hasAccess, err, apiStatus := tenant.HasTenant(*user, xmlId, db)
+ if !hasAccess {
+ switch apiStatus {
+ case tc.SystemError:
+ handleErrs(http.StatusInternalServerError, err)
+ return
+ case tc.DataMissingError:
+ handleErrs(http.StatusBadRequest, err)
+ return
+ case tc.ForbiddenError:
+ handleErrs(http.StatusForbidden, err)
+ return
+ }
+ }
+
+ // perform the insert transaction
+ 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)
+ handleErrs(http.StatusInternalServerError, err)
+ return
+ }
+
+ if *payload.Replace {
+ // delete existing
+ rows, err := db.Queryx( "DELETE FROM
deliveryservice_server WHERE deliveryservice = $1", *dsId)
+ if err != nil {
+ log.Errorf("unable to remove the existing
servers assigned to the delivery service: %s", err)
+ handleErrs(http.StatusInternalServerError, err)
+ return
+ }
+
+ defer rows.Close()
+ }
+
+ i := 0
+ respServers := []int{}
+
+ for i < len(servers) {
Review comment:
This will be clearer and safer with a range loop: `for _, server := range
servers {`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services