zrhoffman commented on code in PR #7708:
URL: https://github.com/apache/trafficcontrol/pull/7708#discussion_r1304943384


##########
traffic_ops/traffic_ops_golang/parameter/parameters.go:
##########
@@ -259,3 +264,351 @@ func deleteQuery() string {
 WHERE id=:id`
        return query
 }
+
+func GetParameters(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{
+               ConfigFileQueryParam: {Column: "p.config_file"},
+               IDQueryParam:         {Column: "p.id", Checker: api.IsInt},
+               NameQueryParam:       {Column: "p.name"},
+               SecureQueryParam:     {Column: "p.secure", Checker: api.IsBool},
+               ValueQueryParam:      {Column: "p.value"},
+       }
+       if _, ok := inf.Params["orderby"]; !ok {
+               inf.Params["orderby"] = "name"
+       }
+       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 := selectQuery() + where + ParametersGroupBy() + orderBy + 
pagination
+       rows, err := tx.NamedQuery(query, queryValues)
+       if err != nil {
+               api.HandleErr(w, r, tx.Tx, http.StatusInternalServerError, nil, 
fmt.Errorf("Parameter read: error getting Parameter(s): %w", err))
+               return
+       }
+       defer log.Close(rows, "unable to close DB connection")
+
+       params := tc.ParameterNullableV5{}
+       paramsList := []tc.ParameterNullableV5{}
+       for rows.Next() {
+               if err = rows.Scan(&params.ConfigFile, &params.ID, 
&params.LastUpdated, &params.Name, &params.Value, &params.Secure, 
&params.Profiles); err != nil {
+                       api.HandleErr(w, r, tx.Tx, 
http.StatusInternalServerError, nil, fmt.Errorf("error getting parameter(s): 
%w", err))
+                       return
+               }
+               if params.Secure != nil && *params.Secure {
+                       if inf.Version.Major >= 4 &&
+                               inf.Config.RoleBasedPermissions &&
+                               !inf.User.Can("PARAMETER-SECURE:READ") {
+                               params.Value = &HiddenField
+                       } else if inf.User.PrivLevel < auth.PrivLevelAdmin {
+                               params.Value = &HiddenField
+                       }
+               }
+
+               paramsList = append(paramsList, params)
+       }
+
+       api.WriteResp(w, r, paramsList)
+       return
+}
+
+func CreateParameter(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 params []tc.ParameterV5
+       switch reflect.TypeOf(data).Kind() {
+       case reflect.Slice:
+               if err := json.Unmarshal(body, &params); err != nil {
+                       api.HandleErr(w, r, tx, http.StatusBadRequest, 
errors.New("error unmarshalling slice"), nil)
+                       return
+               }
+       case reflect.Map:

Review Comment:
   Reflection is slow, using it is a no-go. Instead of reflecting, try 
typecasting:
   ```go
   if my_array, ok := data.([]tc.ParameterV5); ok {
        /* ... */
   } else if my_parameter, ok := data.(tc.ParameterV5); ok {
        /* ... */
   } else {
        /* ... */
   }
   ```



-- 
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]

Reply via email to