rimashah25 commented on code in PR #7738:
URL: https://github.com/apache/trafficcontrol/pull/7738#discussion_r1302158300
##########
lib/go-tc/profile_parameters.go:
##########
@@ -59,3 +61,50 @@ type ProfileParameterNullable struct {
Parameter *string `json:"parameter" db:"parameter"`
ParameterID *int `json:"parameterId" db:"parameter_id"`
}
+
+// ProfileParametersResponseV5 is the type of the response from Traffic Ops to
+// GET requests made to its /profileparameters API endpoint.
+type ProfileParametersResponseV5 struct {
+ Response []ProfileParameterV5 `json:"response"`
+ Alerts
+}
+
+// ProfileParameterResponseV5 is a single ProfileParameter response for Create
to
+// depict what changed.
+// swagger:response ProfileParameterResponse
+// in: body
+type ProfileParameterResponseV5 struct {
+ // in: body
+ Response ProfileParameterV5 `json:"response"`
+ Alerts
+}
+
+// ProfileParameterV5 is the latest minor version of the major version 5
+type ProfileParameterV5 ProfileParameterV50
+
+// ProfileParameterV50 is a representation of a relationship between a
Parameter
+// and a Profile to which it is assigned.
+//
+// Note that not all unique identifiers for each represented object in this
+// relationship structure are guaranteed to be populated by the Traffic Ops
+// API.
+type ProfileParameterV50 struct {
+ LastUpdated time.Time `json:"lastUpdated"`
+ Profile string `json:"profile"`
+ ProfileID int `json:"profileId"`
+ Parameter string `json:"parameter"`
+ ParameterID int `json:"parameterId"`
+}
+
+// ProfileParameterNullableV5 is the latest minor version of the major version
5
Review Comment:
You don't seem to using this struct anywhere in the codebase, then why
define it?
##########
traffic_ops/traffic_ops_golang/profileparameter/profile_parameters.go:
##########
@@ -212,3 +219,301 @@ func deleteQuery() string {
WHERE profile=:profile_id and parameter=:parameter_id`
return query
}
+
+func GetProfileParameter(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{
+ "profileId": {Column: "pp.profile"},
+ "parameterId": {Column: "pp.parameter"},
+ "lastUpdated": {Column: "pp.last_updated"},
+ }
+ if _, ok := inf.Params["orderby"]; !ok {
+ inf.Params["orderby"] = "parameter"
+ }
+ 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)
+ }
+
+ 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 := selectQuery() + where + orderBy + pagination
+ rows, err := tx.NamedQuery(query, queryValues)
+ if err != nil {
+ api.HandleErr(w, r, tx.Tx, http.StatusInternalServerError, nil,
fmt.Errorf("Profile Parameter read: error getting Profile Parameter(s): %w",
err))
+ return
+ }
+ defer log.Close(rows, "unable to close DB connection")
+
+ profileParams := tc.ProfileParametersNullableV5{}
+ profileParamsList := []tc.ProfileParametersNullableV5{}
+ for rows.Next() {
+ if err = rows.Scan(&profileParams.LastUpdated,
&profileParams.Parameter, &profileParams.Profile); err != nil {
+ api.HandleErr(w, r, tx.Tx,
http.StatusInternalServerError, nil, fmt.Errorf("error getting profile
parameter(s): %w", err))
+ return
+ }
+
+ profileParamsList = append(profileParamsList, profileParams)
+ }
+
+ api.WriteResp(w, r, profileParamsList)
+ return
+}
+
+func CreateProfileParameter(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
+
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
errors.New("error reading request body"), nil)
+ return
+ }
+ defer r.Body.Close()
+
+ // Initial Unmarshal to validate request body
+ var data interface{}
+ err = json.Unmarshal(body, &data)
+ if err != nil {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
errors.New("invalid request format"), nil)
+ return
+ }
+
+ // This code block decides if the request body is a slice of parameters
or a single object.
+ var profileParams []tc.ProfileParameterCreationRequest
+ switch reflect.TypeOf(data).Kind() {
+ case reflect.Slice:
+ if err := json.Unmarshal(body, &profileParams); err != nil {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
errors.New("error unmarshalling slice"), nil)
+ return
+ }
+ case reflect.Map:
+ // If it is a single object it is still converted to a slice
for code simplicity.
+ var profileParam tc.ProfileParameterCreationRequest
+ if err := json.Unmarshal(body, &profileParam); err != nil {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
errors.New("error unmarshalling single object"), nil)
+ return
+ }
+ profileParams = append(profileParams, profileParam)
+ default:
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
errors.New("invalid request format"), nil)
+ return
+ }
+
+ // Validate all objects of the every profile parameter from the request
slice
+ for _, profileParameter := range profileParams {
+ readValErr := validateRequestProfileParameter(profileParameter)
+ if readValErr != nil {
+ api.HandleErr(w, r, tx, http.StatusBadRequest,
readValErr, nil)
+ return
+ }
+ }
+
+ // Check user Permissions on all Profiles requested
+ for _, profileParameter := range profileParams {
+ cdnName, err := dbhelpers.GetCDNNameFromProfileID(tx,
profileParameter.ProfileID)
Review Comment:
Same code base (L340-349) repeated in two functions (create and delete).
Wanna combine into one function?
##########
traffic_ops/traffic_ops_golang/profileparameter/profile_parameters.go:
##########
@@ -212,3 +219,301 @@ func deleteQuery() string {
WHERE profile=:profile_id and parameter=:parameter_id`
return query
}
+
+func GetProfileParameter(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{
+ "profileId": {Column: "pp.profile"},
+ "parameterId": {Column: "pp.parameter"},
+ "lastUpdated": {Column: "pp.last_updated"},
+ }
+ if _, ok := inf.Params["orderby"]; !ok {
+ inf.Params["orderby"] = "parameter"
+ }
+ 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)
+ }
Review Comment:
Shouldn't we have a return statement after finding errors?
--
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]