ocket8888 commented on a change in pull request #3900: Rewrite 
/cachegroups/{{id}}/parameters to Go
URL: https://github.com/apache/trafficcontrol/pull/3900#discussion_r320848632
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/cachegroup/parameters.go
 ##########
 @@ -0,0 +1,97 @@
+package cachegroup
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "errors"
+       "net/http"
+
+       "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/lib/go-util"
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/parameter"
+)
+
+const (
+       CacheGroupIDQueryParam = "id"
+       ParameterIDQueryParam  = "parameterId"
+)
+
+//we need a type alias to define functions on
+type TOCacheGroupParameter struct {
+       api.APIInfoImpl `json:"-"`
+       tc.ParameterNullable
+}
+
+func (cgparam *TOCacheGroupParameter) ParamColumns() 
map[string]dbhelpers.WhereColumnInfo {
+       return map[string]dbhelpers.WhereColumnInfo{
+               CacheGroupIDQueryParam: 
dbhelpers.WhereColumnInfo{"cgp.cachegroup", api.IsInt},
+               ParameterIDQueryParam:  dbhelpers.WhereColumnInfo{"p.id", 
api.IsInt},
+       }
+}
+
+func (cgparam *TOCacheGroupParameter) GetType() string {
+       return "cachegroup_params"
+}
+
+func (cgparam *TOCacheGroupParameter) Read() ([]interface{}, error, error, 
int) {
+       queryParamsToQueryCols := cgparam.ParamColumns()
+       where, orderBy, pagination, queryValues, errs := 
dbhelpers.BuildWhereAndOrderByAndPagination(cgparam.APIInfo().Params, 
queryParamsToQueryCols)
+       if len(errs) > 0 {
+               return nil, util.JoinErrs(errs), nil, http.StatusBadRequest
+       }
+
+       query := selectQuery() + where + orderBy + pagination
+       rows, err := cgparam.ReqInfo.Tx.NamedQuery(query, queryValues)
+       if err != nil {
+               return nil, nil, errors.New("querying " + cgparam.GetType() + 
": " + err.Error()), http.StatusInternalServerError
+       }
+       defer rows.Close()
+
+       params := []interface{}{}
+       for rows.Next() {
+               var p tc.ParameterNullable
+               if err = rows.StructScan(&p); err != nil {
+                       return nil, nil, errors.New("scanning " + 
cgparam.GetType() + ": " + err.Error()), http.StatusInternalServerError
+               }
+               if p.Secure != nil && *p.Secure && 
cgparam.ReqInfo.User.PrivLevel < auth.PrivLevelAdmin {
+                       p.Value = &parameter.HiddenField
+               }
+               params = append(params, p)
+       }
+
+       return params, nil, nil, http.StatusOK
 
 Review comment:
   This implementation doesn't check for existence of the requested Cache Group.
   
   In Perl:
   ```http
   GET /api/1.3/cachegroups/-1/parameters HTTP/1.1
   User-Agent: python-requests/2.20.1
   Accept-Encoding: gzip, deflate
   Accept: */*
   Connection: keep-alive
   Cookie: mojolicious=...
   
   HTTP/1.1 404 Not Found
   Access-Control-Allow-Credentials: true
   Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
   Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
   Access-Control-Allow-Origin: *
   Cache-Control: no-cache, no-store, max-age=0, must-revalidate
   Content-Encoding: gzip
   Content-Length: 77
   Content-Type: application/json
   Date: Wed, 04 Sep 2019 16:08:48 GMT
   Server: Mojolicious (Perl)
   Set-Cookie: mojolicious=...; expires=Wed, 04 Sep 2019 20:08:48 GMT; path=/; 
HttpOnly
   Vary: Accept-Encoding
   Whole-Content-Sha512: ...
   
   {
        "alerts": [
                {
                        "level": "error",
                        "text": "Resource not found."
                }
        ]
   }
   ```
   In your rewrite:
   ```http
   GET /api/1.3/cachegroups/-1/parameters HTTP/1.1
   User-Agent: python-requests/2.20.1
   Accept-Encoding: gzip, deflate
   Accept: */*
   Connection: keep-alive
   Cookie: mojolicious=...
   
   HTTP/1.1 200 OK
   Access-Control-Allow-Credentials: true
   Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, 
Accept, Set-Cookie, Cookie
   Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
   Access-Control-Allow-Origin: *
   Content-Encoding: gzip
   Content-Type: application/json
   Set-Cookie: mojolicious=...; Path=/; HttpOnly
   Whole-Content-Sha512: ...
   X-Server-Name: traffic_ops_golang/
   Date: Wed, 04 Sep 2019 16:09:01 GMT
   Content-Length: 39
   
   {
        "response": []
   }
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to