[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338779159
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   Hm, regexes can be constructed to both be extremely CPU and Memory 
intensive. This will be a security vulnerability, if it's exposed with Self 
Service.
   
   Since it's never been documented, what about changing it to `=`, or if you 
think people are using it for prefixes, maybe `LIKE '%' + $1` or `LIKE '%' + $1 
+ '%'`?


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


[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338779159
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   Hm, regexes can be constructed to both be extremely CPU and Memory 
intensive. This will be a security vulnerability, if it's exposed with Self 
Service.
   
   Since it's never been documented, what about changing it to `=`, or if you 
think people are using it for prefixes, maybe `LIKE '%' + $1` or `LIKE '%' + $1 
+ '%'`? (LIKE on user input will also need a `strings.Replace(i, "%", "\%")` 
and `strings.Replace(i, "_", "\_")`)


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


[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338779159
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   Hm, regexes can be constructed to both be extremely CPU and Memory 
intensive. This will be a security vulnerability, if it's exposed with Self 
Service.
   
   Since it's never been documented, what about changing it to `=`, or if you 
think people are using it for prefixes, maybe `LIKE '%' + $1` or `LIKE '%' + $1 
+ '%'`? (LIKE on user input will also need a `strings.Replace(i, "%", "\\%")` 
and `strings.Replace(i, "_", "\\_")`)


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


[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338788809
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   Escaping `%` and `_` was more to prevent accidents than malice. LIKE isn't 
overly expensive, it's not really a security risk for someone to do their own 
`%` if they really want to.
   
   Though we could prevent that with an additional `strings.Replace(i, "\", 
"\\")` if we wanted.


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


[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338797552
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   >what if I did \%? -> \\% -> %
   
   The \ replace needs to come before the % replace, so `\%` becomes `\\\%`, 
which will be parsed by Postgres as a literal \ and a literal %.
   
   > Also, if parsing user-input regexes can be considered a security risk then 
Content Invalidation Jobs are a huge hole.
   
   Indeed. :(


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


[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338779159
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   Hm, regexes can be constructed to both be extremely CPU and Memory 
intensive. This will be a security vulnerability, if it's exposed with Self 
Service.
   
   Since it's never been documented, what about changing it to `=`, or if you 
think people are using it for prefixes, maybe `LIKE '%' + $1` or `LIKE '%' + $1 
+ '%'`? (LIKE on user input will also need a strings.Replace(i, `%`, `\%`) and 
strings.Replace(i, `_`, `\_`) )


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


[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338788809
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   Escaping `%` and `_` was more to prevent accidents than malice. LIKE isn't 
overly expensive, it's not really a security risk for someone to do their own 
`%` if they really want to.
   
   Though we could prevent that with an additional strings.Replace(i, `\`, 
`\\`) if we wanted.


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


[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338797552
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   >what if I did \%? -> \\% -> %
   
   The \ replace needs to come before the % replace, so `\%` becomes `\\\%`, 
which will be parsed by Postgres as a literal \ and a literal %.
   
   > Also, if parsing user-input regexes can be considered a security risk then 
Content Invalidation Jobs are a huge hole.
   
   Indeed. ☚ī¸


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


[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4013: Add TO-Go /api/1.1/servers/status GET handler

2019-10-24 Thread GitBox
rob05c commented on a change in pull request #4013: Add TO-Go 
/api/1.1/servers/status GET handler
URL: https://github.com/apache/trafficcontrol/pull/4013#discussion_r338797552
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/server/status_count.go
 ##
 @@ -0,0 +1,70 @@
+package server
+
+/*
+ * 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 (
+   "database/sql"
+   "errors"
+   "net/http"
+
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+func GetServersStatusCountsHandler(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()
+
+   statusCounts, err := getServersStatusCounts(inf.Tx.Tx, 
inf.Params["type"])
+   if err != nil {
+   api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting servers status counts: "+err.Error()))
+   return
+   }
+   api.WriteResp(w, r, statusCounts)
+}
+
+func getServersStatusCounts(tx *sql.Tx, typeName string) (map[string]int, 
error) {
+   q := `
+SELECT status.name, count(server.id)
+FROM server
+JOIN status ON server.status = status.id
+JOIN type ON server.type = type.id
+WHERE type.name ~ $1
 
 Review comment:
   >what if I did \%? -> \\% -> %
   
   The \ replace needs to come before the % replace, so `\%` becomes `\\\%`, 
which will be parsed by Postgres as a literal \ and a literal %.
   
   > Also, if parsing user-input regexes can be considered a security risk then 
Content Invalidation Jobs are a huge hole.
   
   Indeed. 😞


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