ocket8888 commented on code in PR #7734:
URL: https://github.com/apache/trafficcontrol/pull/7734#discussion_r1305005669
##########
traffic_ops/traffic_ops_golang/profile/profiles.go:
##########
@@ -360,3 +363,290 @@ type) VALUES (
func deleteQuery() string {
return `DELETE FROM profile WHERE id = :id`
}
+
+// Read gets list of Profiles for APIv5
+func Read(w http.ResponseWriter, r *http.Request) {
+ var runSecond bool
+ var maxTime time.Time
+ inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+ tx := inf.Tx
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, tx.Tx, errCode, userErr, sysErr)
+ return
+ }
+ defer inf.Close()
+
+ // Query Parameters to Database Query column mappings
+ queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
+ "cdn": {Column: "c.id", Checker: api.IsInt},
+ "name": {Column: "prof.name", Checker: nil},
+ "id": {Column: "prof.id", Checker: api.IsInt},
+ "param": {Column: "pp.parameter", Checker: api.IsInt},
+ }
+
+ query := selectProfilesQuery()
+ if paramValue, ok := inf.Params["param"]; ok {
+ if len(paramValue) > 0 {
+ query += " LEFT JOIN profile_parameter pp ON prof.id =
pp.profile"
+ }
+ }
+
+ where, orderBy, pagination, queryValues, errs :=
dbhelpers.BuildWhereAndOrderByAndPagination(inf.Params, queryParamsToQueryCols)
+ if len(errs) > 0 {
+ api.HandleErr(w, r, tx.Tx, http.StatusBadRequest,
util.JoinErrs(errs), nil)
+ return
+ }
+
+ if inf.Config.UseIMS {
+ runSecond, maxTime = ims.TryIfModifiedSinceQuery(tx, r.Header,
queryValues, selectMaxLastUpdatedQuery(where))
+ if !runSecond {
+ log.Debugln("IMS HIT")
+ api.AddLastModifiedHdr(w, maxTime)
+ w.WriteHeader(http.StatusNotModified)
+ return
+ }
+ log.Debugln("IMS MISS")
+ } else {
+ log.Debugln("Non IMS request")
+ }
+
+ query += where + orderBy + pagination
+ rows, err := tx.NamedQuery(query, queryValues)
+ if err != nil {
+ api.HandleErr(w, r, tx.Tx, http.StatusInternalServerError, nil,
fmt.Errorf("profile read: error getting profile(s): %w", err))
+ return
+ }
+ defer log.Close(rows, "unable to close DB connection")
+
+ profile := tc.ProfileV5{}
+ var profileList []tc.ProfileV5
+ for rows.Next() {
+ if err = rows.Scan(&profile.Description, &profile.ID,
&profile.LastUpdated, &profile.Name, &profile.RoutingDisabled, &profile.Type,
&profile.CDNID, &profile.CDNName); err != nil {
+ api.HandleErr(w, r, tx.Tx,
http.StatusInternalServerError, nil, fmt.Errorf("error getting profile(s): %w",
err))
+ return
+ }
+ profileList = append(profileList, profile)
+ }
+ rows.Close()
+ profileInterfaces := []interface{}{}
+ for _, p := range profileList {
+ // Attach Parameters if the 'param' parameter is sent
+ if _, ok := inf.Params["param"]; ok {
+ p.Parameters, err = ReadParameters(inf.Tx, inf.User,
&p.ID)
+ if err != nil {
+ api.HandleErr(w, r, tx.Tx,
http.StatusInternalServerError, nil, fmt.Errorf("profile read: error reading
parameters for a profile: %w", err))
+ return
+ }
+ }
+ profileInterfaces = append(profileInterfaces, p)
+ }
+
+ api.WriteResp(w, r, profileInterfaces)
+ return
+}
+
+// Create a Profile for APIv5
Review Comment:
GoDoc comments should end with punctuation.
##########
traffic_ops/traffic_ops_golang/profile/profiles.go:
##########
@@ -360,3 +363,290 @@ type) VALUES (
func deleteQuery() string {
return `DELETE FROM profile WHERE id = :id`
}
+
+// Read gets list of Profiles for APIv5
Review Comment:
GoDoc comments should end with punctuation.
##########
traffic_ops/traffic_ops_golang/profile/profiles.go:
##########
@@ -360,3 +363,290 @@ type) VALUES (
func deleteQuery() string {
return `DELETE FROM profile WHERE id = :id`
}
+
+// Read gets list of Profiles for APIv5
+func Read(w http.ResponseWriter, r *http.Request) {
+ var runSecond bool
+ var maxTime time.Time
+ inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+ tx := inf.Tx
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, tx.Tx, errCode, userErr, sysErr)
+ return
+ }
+ defer inf.Close()
+
+ // Query Parameters to Database Query column mappings
+ queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
+ "cdn": {Column: "c.id", Checker: api.IsInt},
+ "name": {Column: "prof.name", Checker: nil},
+ "id": {Column: "prof.id", Checker: api.IsInt},
+ "param": {Column: "pp.parameter", Checker: api.IsInt},
+ }
+
+ query := selectProfilesQuery()
+ if paramValue, ok := inf.Params["param"]; ok {
+ if len(paramValue) > 0 {
+ query += " LEFT JOIN profile_parameter pp ON prof.id =
pp.profile"
+ }
+ }
+
+ where, orderBy, pagination, queryValues, errs :=
dbhelpers.BuildWhereAndOrderByAndPagination(inf.Params, queryParamsToQueryCols)
+ if len(errs) > 0 {
+ api.HandleErr(w, r, tx.Tx, http.StatusBadRequest,
util.JoinErrs(errs), nil)
+ return
+ }
+
+ if inf.Config.UseIMS {
+ runSecond, maxTime = ims.TryIfModifiedSinceQuery(tx, r.Header,
queryValues, selectMaxLastUpdatedQuery(where))
+ if !runSecond {
+ log.Debugln("IMS HIT")
+ api.AddLastModifiedHdr(w, maxTime)
+ w.WriteHeader(http.StatusNotModified)
+ return
+ }
+ log.Debugln("IMS MISS")
+ } else {
+ log.Debugln("Non IMS request")
+ }
+
+ query += where + orderBy + pagination
+ rows, err := tx.NamedQuery(query, queryValues)
+ if err != nil {
+ api.HandleErr(w, r, tx.Tx, http.StatusInternalServerError, nil,
fmt.Errorf("profile read: error getting profile(s): %w", err))
+ return
+ }
+ defer log.Close(rows, "unable to close DB connection")
+
+ profile := tc.ProfileV5{}
+ var profileList []tc.ProfileV5
+ for rows.Next() {
+ if err = rows.Scan(&profile.Description, &profile.ID,
&profile.LastUpdated, &profile.Name, &profile.RoutingDisabled, &profile.Type,
&profile.CDNID, &profile.CDNName); err != nil {
+ api.HandleErr(w, r, tx.Tx,
http.StatusInternalServerError, nil, fmt.Errorf("error getting profile(s): %w",
err))
+ return
+ }
+ profileList = append(profileList, profile)
+ }
+ rows.Close()
+ profileInterfaces := []interface{}{}
+ for _, p := range profileList {
+ // Attach Parameters if the 'param' parameter is sent
+ if _, ok := inf.Params["param"]; ok {
+ p.Parameters, err = ReadParameters(inf.Tx, inf.User,
&p.ID)
+ if err != nil {
+ api.HandleErr(w, r, tx.Tx,
http.StatusInternalServerError, nil, fmt.Errorf("profile read: error reading
parameters for a profile: %w", err))
+ return
+ }
+ }
+ profileInterfaces = append(profileInterfaces, p)
+ }
+
+ api.WriteResp(w, r, profileInterfaces)
+ return
+}
+
+// Create a Profile for APIv5
+func Create(w http.ResponseWriter, r *http.Request) {
+ inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+ return
+ }
+ defer inf.Close()
+ tx := inf.Tx.Tx
+
+ profile, readValErr := readAndValidateJsonStruct(r)
+ if readValErr != nil {
+ api.HandleErr(w, r, tx, http.StatusBadRequest, readValErr, nil)
+ return
+ }
+
+ //check if user can modify.
+ if len(strings.TrimSpace(profile.CDNName)) != 0 || profile.CDNID != 0 {
+ userErr, sysErr, statusCode :=
canProfileBeAlteredByCurrentUser(inf.User.UserName, inf.Tx.Tx,
&profile.CDNName, &profile.CDNID)
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, tx, statusCode, userErr, sysErr)
+ return
+ }
+ }
+
+ // check if profile already exists
+ var count int
+ err := tx.QueryRow("SELECT count(*) from profile where name=$1",
profile.Name).Scan(&count)
+ if err != nil {
+ api.HandleErr(w, r, tx, http.StatusInternalServerError, nil,
fmt.Errorf("error: %w, when checking if profile '%s' exists", err,
profile.Name))
+ return
+ }
+ if count == 1 {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
fmt.Errorf("profile:'%s' already exists", profile.Name), nil)
+ return
+ }
+
+ // create profile
+ query := `INSERT INTO profile (name, cdn, type, routing_disabled,
description)
+ VALUES ($1, $2, $3, $4, $5)
+ RETURNING id, last_updated, name, description, (select name FROM cdn
where id = $2), cdn, routing_disabled, type`
+
+ err = tx.QueryRow(query, profile.Name, profile.CDNID, profile.Type,
profile.RoutingDisabled, profile.Description).
+ Scan(&profile.ID, &profile.LastUpdated, &profile.Name,
&profile.Description, &profile.CDNName, &profile.CDNID,
&profile.RoutingDisabled, &profile.Type)
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ api.HandleErr(w, r, tx, http.StatusInternalServerError,
fmt.Errorf("error: %w in creating profile:%s", err, profile.Name), nil)
+ return
+ }
+ usrErr, sysErr, code := api.ParseDBError(err)
+ api.HandleErr(w, r, tx, code, usrErr, sysErr)
+ return
+ }
+
+ alerts := tc.CreateAlerts(tc.SuccessLevel, "profile was created.")
+ w.Header().Set(rfc.Location, fmt.Sprintf("/api/%s/profiles?id=%d",
inf.Version, profile.ID))
+ api.WriteAlertsObj(w, r, http.StatusCreated, alerts, profile)
+ return
+}
+
+// Update a profile for APIv5
Review Comment:
GoDoc comments should end with punctuation.
##########
traffic_ops/traffic_ops_golang/profile/profiles.go:
##########
@@ -360,3 +363,290 @@ type) VALUES (
func deleteQuery() string {
return `DELETE FROM profile WHERE id = :id`
}
+
+// Read gets list of Profiles for APIv5
+func Read(w http.ResponseWriter, r *http.Request) {
+ var runSecond bool
+ var maxTime time.Time
+ inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+ tx := inf.Tx
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, tx.Tx, errCode, userErr, sysErr)
+ return
+ }
+ defer inf.Close()
+
+ // Query Parameters to Database Query column mappings
+ queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
+ "cdn": {Column: "c.id", Checker: api.IsInt},
+ "name": {Column: "prof.name", Checker: nil},
+ "id": {Column: "prof.id", Checker: api.IsInt},
+ "param": {Column: "pp.parameter", Checker: api.IsInt},
+ }
+
+ query := selectProfilesQuery()
+ if paramValue, ok := inf.Params["param"]; ok {
+ if len(paramValue) > 0 {
+ query += " LEFT JOIN profile_parameter pp ON prof.id =
pp.profile"
+ }
+ }
+
+ where, orderBy, pagination, queryValues, errs :=
dbhelpers.BuildWhereAndOrderByAndPagination(inf.Params, queryParamsToQueryCols)
+ if len(errs) > 0 {
+ api.HandleErr(w, r, tx.Tx, http.StatusBadRequest,
util.JoinErrs(errs), nil)
+ return
+ }
+
+ if inf.Config.UseIMS {
+ runSecond, maxTime = ims.TryIfModifiedSinceQuery(tx, r.Header,
queryValues, selectMaxLastUpdatedQuery(where))
+ if !runSecond {
+ log.Debugln("IMS HIT")
+ api.AddLastModifiedHdr(w, maxTime)
+ w.WriteHeader(http.StatusNotModified)
+ return
+ }
+ log.Debugln("IMS MISS")
+ } else {
+ log.Debugln("Non IMS request")
+ }
+
+ query += where + orderBy + pagination
+ rows, err := tx.NamedQuery(query, queryValues)
+ if err != nil {
+ api.HandleErr(w, r, tx.Tx, http.StatusInternalServerError, nil,
fmt.Errorf("profile read: error getting profile(s): %w", err))
+ return
+ }
+ defer log.Close(rows, "unable to close DB connection")
+
+ profile := tc.ProfileV5{}
+ var profileList []tc.ProfileV5
+ for rows.Next() {
+ if err = rows.Scan(&profile.Description, &profile.ID,
&profile.LastUpdated, &profile.Name, &profile.RoutingDisabled, &profile.Type,
&profile.CDNID, &profile.CDNName); err != nil {
+ api.HandleErr(w, r, tx.Tx,
http.StatusInternalServerError, nil, fmt.Errorf("error getting profile(s): %w",
err))
+ return
+ }
+ profileList = append(profileList, profile)
+ }
+ rows.Close()
+ profileInterfaces := []interface{}{}
+ for _, p := range profileList {
+ // Attach Parameters if the 'param' parameter is sent
+ if _, ok := inf.Params["param"]; ok {
+ p.Parameters, err = ReadParameters(inf.Tx, inf.User,
&p.ID)
+ if err != nil {
+ api.HandleErr(w, r, tx.Tx,
http.StatusInternalServerError, nil, fmt.Errorf("profile read: error reading
parameters for a profile: %w", err))
+ return
+ }
+ }
+ profileInterfaces = append(profileInterfaces, p)
+ }
+
+ api.WriteResp(w, r, profileInterfaces)
+ return
+}
+
+// Create a Profile for APIv5
+func Create(w http.ResponseWriter, r *http.Request) {
+ inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+ return
+ }
+ defer inf.Close()
+ tx := inf.Tx.Tx
+
+ profile, readValErr := readAndValidateJsonStruct(r)
+ if readValErr != nil {
+ api.HandleErr(w, r, tx, http.StatusBadRequest, readValErr, nil)
+ return
+ }
+
+ //check if user can modify.
+ if len(strings.TrimSpace(profile.CDNName)) != 0 || profile.CDNID != 0 {
+ userErr, sysErr, statusCode :=
canProfileBeAlteredByCurrentUser(inf.User.UserName, inf.Tx.Tx,
&profile.CDNName, &profile.CDNID)
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, tx, statusCode, userErr, sysErr)
+ return
+ }
+ }
+
+ // check if profile already exists
+ var count int
+ err := tx.QueryRow("SELECT count(*) from profile where name=$1",
profile.Name).Scan(&count)
+ if err != nil {
+ api.HandleErr(w, r, tx, http.StatusInternalServerError, nil,
fmt.Errorf("error: %w, when checking if profile '%s' exists", err,
profile.Name))
+ return
+ }
+ if count == 1 {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
fmt.Errorf("profile:'%s' already exists", profile.Name), nil)
+ return
+ }
+
+ // create profile
+ query := `INSERT INTO profile (name, cdn, type, routing_disabled,
description)
+ VALUES ($1, $2, $3, $4, $5)
+ RETURNING id, last_updated, name, description, (select name FROM cdn
where id = $2), cdn, routing_disabled, type`
+
+ err = tx.QueryRow(query, profile.Name, profile.CDNID, profile.Type,
profile.RoutingDisabled, profile.Description).
+ Scan(&profile.ID, &profile.LastUpdated, &profile.Name,
&profile.Description, &profile.CDNName, &profile.CDNID,
&profile.RoutingDisabled, &profile.Type)
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ api.HandleErr(w, r, tx, http.StatusInternalServerError,
fmt.Errorf("error: %w in creating profile:%s", err, profile.Name), nil)
+ return
+ }
+ usrErr, sysErr, code := api.ParseDBError(err)
+ api.HandleErr(w, r, tx, code, usrErr, sysErr)
+ return
+ }
+
+ alerts := tc.CreateAlerts(tc.SuccessLevel, "profile was created.")
+ w.Header().Set(rfc.Location, fmt.Sprintf("/api/%s/profiles?id=%d",
inf.Version, profile.ID))
+ api.WriteAlertsObj(w, r, http.StatusCreated, alerts, profile)
+ return
+}
+
+// Update a profile for APIv5
+func Update(w http.ResponseWriter, r *http.Request) {
+ inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"id"},
[]string{"id"})
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+ return
+ }
+ defer inf.Close()
+
+ tx := inf.Tx.Tx
+ profile, readValErr := readAndValidateJsonStruct(r)
+ if readValErr != nil {
+ api.HandleErr(w, r, tx, http.StatusBadRequest, readValErr, nil)
+ return
+ }
+
+ //check if user can modify.
+ if len(strings.TrimSpace(profile.CDNName)) != 0 || profile.CDNID != 0 {
+ userErr, sysErr, statusCode :=
canProfileBeAlteredByCurrentUser(inf.User.UserName, inf.Tx.Tx,
&profile.CDNName, &profile.CDNID)
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, tx, statusCode, userErr, sysErr)
+ return
+ }
+ }
+
+ requestedProfileId := inf.IntParams["id"]
+ // check if the entity was already updated
+ userErr, sysErr, errCode = api.CheckIfUnModified(r.Header, inf.Tx,
requestedProfileId, "profile")
+ if userErr != nil || sysErr != nil {
+ api.HandleErr(w, r, tx, errCode, userErr, sysErr)
+ return
+ }
+
+ //update profile
+ query := `UPDATE profile SET
+ name = $2,
+ cdn = $3,
+ type = $4,
+ routing_disabled = $5,
+ description = $6
+ WHERE id = $1
+ RETURNING id, last_updated, name, description, (select name FROM cdn
where id = $3), cdn, routing_disabled, type`
+
+ err := tx.QueryRow(query, requestedProfileId, profile.Name,
profile.CDNID, profile.Type, profile.RoutingDisabled, profile.Description).
+ Scan(&profile.ID, &profile.LastUpdated, &profile.Name,
&profile.Description, &profile.CDNName, &profile.CDNID,
&profile.RoutingDisabled, &profile.Type)
+
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
fmt.Errorf("profile: %s not found", profile.Name), nil)
+ return
+ }
+ usrErr, sysErr, code := api.ParseDBError(err)
+ api.HandleErr(w, r, tx, code, usrErr, sysErr)
+ return
+ }
+
+ alerts := tc.CreateAlerts(tc.SuccessLevel, "profile was updated")
+ api.WriteAlertsObj(w, r, http.StatusOK, alerts, profile)
+ return
+}
+
+// Delete an profile for APIv5
Review Comment:
GoDoc comments should end with punctuation.
Grammar:
> Delete a<del>n</del> Profile <del>for</del><ins>in</ins> APIv5<ins>.</ins>
(for -> in is a suggestion, just sounds better imo)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]