jagan-parthiban commented on code in PR #7738:
URL: https://github.com/apache/trafficcontrol/pull/7738#discussion_r1302333734
##########
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:
This part of the code doesn't do anything of its own but used
dbhelpers.GetCDNNameFromProfileID & dbhelpers.CheckIfCurrentUserCanModifyCDN.
So It it not seem resourceful to use a intermediately function that uses
another function. Hence directly used it in two places.
--
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]