From 70ec30abe4832407fd60cdb75bc9f277ef854371 Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Fri, 12 Feb 2021 12:33:32 -0700 Subject: [PATCH 01/13] Adding query param to servercheck --- traffic_ops/traffic_ops_golang/routing/routes.go | 1 + traffic_ops/traffic_ops_golang/servercheck/servercheck.go | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/traffic_ops/traffic_ops_golang/routing/routes.go b/traffic_ops/traffic_ops_golang/routing/routes.go index ef762279f6..bc7a39709b 100644 --- a/traffic_ops/traffic_ops_golang/routing/routes.go +++ b/traffic_ops/traffic_ops_golang/routing/routes.go @@ -312,6 +312,7 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) { {api.Version{4, 0}, http.MethodGet, `deliveryservices/{id}/capacity/?$`, deliveryservice.GetCapacity, auth.PrivLevelReadOnly, Authenticated, nil, 42314091103, noPerlBypass}, //Serverchecks {api.Version{4, 0}, http.MethodGet, `servercheck/?$`, servercheck.ReadServerCheck, auth.PrivLevelReadOnly, Authenticated, nil, 47961129223, noPerlBypass}, + {api.Version{4, 0}, http.MethodGet, `servercheck/{id}?$`, servercheck.ReadServerCheck, auth.PrivLevelReadOnly, Authenticated, nil, 47562928103, noPerlBypass}, {api.Version{4, 0}, http.MethodPost, `servercheck/?$`, servercheck.CreateUpdateServercheck, auth.PrivLevelInvalid, Authenticated, nil, 47642815683, noPerlBypass}, // Servercheck Extensions diff --git a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go index 84449b965e..ddb21d9c42 100644 --- a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go +++ b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go @@ -239,6 +239,10 @@ func DeprecatedReadServersChecks(w http.ResponseWriter, r *http.Request) { func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerCheck, error, error, int) { extensions := make(map[string]string) + api.DefaultSort(inf, "id") + serverID := inf.IntParams["id"] + fmt.Println(serverID) + extRows, err := tx.Query(extensionsQuery) if err != nil { sysErr := fmt.Errorf("querying for extensions: %v", err) @@ -254,7 +258,7 @@ func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerChec extensions[shortName] = checkName } - colRows, err := inf.Tx.Queryx(serverChecksQuery) + colRows, err := inf.Tx.Queryx(serverChecksQuery, serverID) if err != nil { sysErr := fmt.Errorf("Querying server checks columns: %v", err) return nil, nil, sysErr, http.StatusInternalServerError From 296a5ceb7d51ba7a089704c9ff8612bba7579355 Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Wed, 17 Feb 2021 17:28:50 -0700 Subject: [PATCH 02/13] Added name as queryparam to servercheck endpoint --- .../traffic_ops_golang/routing/routes.go | 1 - .../servercheck/servercheck.go | 33 ++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/routing/routes.go b/traffic_ops/traffic_ops_golang/routing/routes.go index b259a548c0..a3c813aebf 100644 --- a/traffic_ops/traffic_ops_golang/routing/routes.go +++ b/traffic_ops/traffic_ops_golang/routing/routes.go @@ -313,7 +313,6 @@ func Routes(d ServerData) ([]Route, []RawRoute, http.Handler, error) { {api.Version{4, 0}, http.MethodGet, `deliveryservices/{id}/capacity/?$`, deliveryservice.GetCapacity, auth.PrivLevelReadOnly, Authenticated, nil, 42314091103, noPerlBypass}, //Serverchecks {api.Version{4, 0}, http.MethodGet, `servercheck/?$`, servercheck.ReadServerCheck, auth.PrivLevelReadOnly, Authenticated, nil, 47961129223, noPerlBypass}, - {api.Version{4, 0}, http.MethodGet, `servercheck/{id}?$`, servercheck.ReadServerCheck, auth.PrivLevelReadOnly, Authenticated, nil, 47562928103, noPerlBypass}, {api.Version{4, 0}, http.MethodPost, `servercheck/?$`, servercheck.CreateUpdateServercheck, auth.PrivLevelInvalid, Authenticated, nil, 47642815683, noPerlBypass}, // Servercheck Extensions diff --git a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go index ddb21d9c42..67b16cad39 100644 --- a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go +++ b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go @@ -239,9 +239,20 @@ func DeprecatedReadServersChecks(w http.ResponseWriter, r *http.Request) { func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerCheck, error, error, int) { extensions := make(map[string]string) - api.DefaultSort(inf, "id") - serverID := inf.IntParams["id"] - fmt.Println(serverID) + + queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{ + "id": dbhelpers.WhereColumnInfo{"servercheck.id", api.IsInt}, + "name": dbhelpers.WhereColumnInfo{"servercheck.server", nil}, + } + + where, orderBy, pagination, queryValues, errs := dbhelpers.BuildWhereAndOrderByAndPagination(inf.Params, queryParamsToQueryCols) + fmt.Println("where", where, "order", orderBy, "page", pagination, "qval", queryValues, "err", errs) + if len(errs) > 0 { + return nil, util.JoinErrs(errs), nil, http.StatusBadRequest + } + + query := serverChecksQuery + where + orderBy + pagination + fmt.Println(query) extRows, err := tx.Query(extensionsQuery) if err != nil { @@ -258,22 +269,25 @@ func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerChec extensions[shortName] = checkName } - colRows, err := inf.Tx.Queryx(serverChecksQuery, serverID) + colRows, err := inf.Tx.NamedQuery(query, queryValues) if err != nil { - sysErr := fmt.Errorf("Querying server checks columns: %v", err) + sysErr := fmt.Errorf("querying serverchecks columns: %v", err) return nil, nil, sysErr, http.StatusInternalServerError } + defer colRows.Close() columns := make(map[int]tc.ServerCheckColumns) + fmt.Println("len", *colRows.Rows) for colRows.Next() { var cols tc.ServerCheckColumns if err = colRows.StructScan(&cols); err != nil { sysErr := fmt.Errorf("scanning server checks columns: %v", err) return nil, nil, sysErr, http.StatusInternalServerError } - + fmt.Println(cols.Server, cols.ID) columns[cols.Server] = cols } + fmt.Println("col", columns) serverRows, err := tx.Query(serverInfoQuery) if err != nil { @@ -288,16 +302,19 @@ func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerChec sysErr := fmt.Errorf("scanning server info for checks: %v", err) return nil, nil, sysErr, http.StatusInternalServerError } - + fmt.Println("Here?") serverCheckCols, ok := columns[serverInfo.ID] if ok { + fmt.Println("in ok?") serverInfo.Checks = make(map[string]*int) } else { + fmt.Println("in else") data = append(data, serverInfo) continue } - + fmt.Println(extensions) for colName, checkName := range extensions { + fmt.Println(colName, checkName) switch colName { case "aa": serverInfo.Checks[checkName] = serverCheckCols.AA From c82d0eaceb4a32b1ee6fe4bfb5335ae77257d429 Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Thu, 18 Feb 2021 13:15:05 -0700 Subject: [PATCH 03/13] Differentiate name and id param wrt where clause for both server and servercheck table --- .../servercheck/servercheck.go | 46 +++++++++++-------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go index 67b16cad39..9cfb037133 100644 --- a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go +++ b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go @@ -50,8 +50,6 @@ LEFT JOIN profile ON server.profile = profile.id LEFT JOIN status ON server.status = status.id LEFT JOIN cachegroup ON server.cachegroup = cachegroup.id LEFT JOIN type ON server.type = type.id -WHERE type.name LIKE 'MID%' OR type.name LIKE 'EDGE%' -ORDER BY hostName ASC ` const serverChecksQuery = ` @@ -240,19 +238,33 @@ func DeprecatedReadServersChecks(w http.ResponseWriter, r *http.Request) { func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerCheck, error, error, int) { extensions := make(map[string]string) + // Query Parameters to Database Query column mappings queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{ - "id": dbhelpers.WhereColumnInfo{"servercheck.id", api.IsInt}, - "name": dbhelpers.WhereColumnInfo{"servercheck.server", nil}, + "id": dbhelpers.WhereColumnInfo{"servercheck.server", api.IsInt}, + "name": dbhelpers.WhereColumnInfo{"server.host_name", nil}, } where, orderBy, pagination, queryValues, errs := dbhelpers.BuildWhereAndOrderByAndPagination(inf.Params, queryParamsToQueryCols) - fmt.Println("where", where, "order", orderBy, "page", pagination, "qval", queryValues, "err", errs) if len(errs) > 0 { return nil, util.JoinErrs(errs), nil, http.StatusBadRequest } - query := serverChecksQuery + where + orderBy + pagination - fmt.Println(query) + // where clause is different for servercheck and server table. Also, it differs for the query param. + var whereSC, whereSI string + if where != "" { + for k, _ := range inf.Params { + if k == "id" { + whereSC = where + whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.id=:id " + } else { + whereSC = "" + whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.host_name=:name " + } + } + } else { + whereSC = "" + whereSI = "WHERE type.name LIKE 'MID%' OR type.name LIKE 'EDGE%'" + } extRows, err := tx.Query(extensionsQuery) if err != nil { @@ -269,29 +281,28 @@ func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerChec extensions[shortName] = checkName } - colRows, err := inf.Tx.NamedQuery(query, queryValues) + querySC := serverChecksQuery + whereSC + orderBy + pagination + colRows, err := inf.Tx.NamedQuery(querySC, queryValues) if err != nil { sysErr := fmt.Errorf("querying serverchecks columns: %v", err) return nil, nil, sysErr, http.StatusInternalServerError } - defer colRows.Close() columns := make(map[int]tc.ServerCheckColumns) - fmt.Println("len", *colRows.Rows) for colRows.Next() { var cols tc.ServerCheckColumns if err = colRows.StructScan(&cols); err != nil { sysErr := fmt.Errorf("scanning server checks columns: %v", err) return nil, nil, sysErr, http.StatusInternalServerError } - fmt.Println(cols.Server, cols.ID) columns[cols.Server] = cols } - fmt.Println("col", columns) - serverRows, err := tx.Query(serverInfoQuery) + orderBySI := orderBy + "ORDER BY hostName ASC" + querySI := serverInfoQuery + whereSI + orderBySI + pagination + serverRows, err := inf.Tx.NamedQuery(querySI, queryValues) if err != nil { - sysErr := fmt.Errorf("Querying server info for checks: %v", err) + sysErr := fmt.Errorf("querying server info for checks: %v", err) return nil, nil, sysErr, http.StatusInternalServerError } @@ -302,19 +313,16 @@ func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerChec sysErr := fmt.Errorf("scanning server info for checks: %v", err) return nil, nil, sysErr, http.StatusInternalServerError } - fmt.Println("Here?") + serverCheckCols, ok := columns[serverInfo.ID] if ok { - fmt.Println("in ok?") serverInfo.Checks = make(map[string]*int) } else { - fmt.Println("in else") data = append(data, serverInfo) continue } - fmt.Println(extensions) + for colName, checkName := range extensions { - fmt.Println(colName, checkName) switch colName { case "aa": serverInfo.Checks[checkName] = serverCheckCols.AA From 1ce8f1f435cd0c217125aeb859957328f3ad8489 Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Thu, 18 Feb 2021 16:02:57 -0700 Subject: [PATCH 04/13] Added tests --- .../testing/api/v4/serverchecks_test.go | 34 +++++++++++++++++++ traffic_ops/v4-client/servercheck.go | 22 ++++++++++++ 2 files changed, 56 insertions(+) diff --git a/traffic_ops/testing/api/v4/serverchecks_test.go b/traffic_ops/testing/api/v4/serverchecks_test.go index de9783560a..71c8c5ff95 100644 --- a/traffic_ops/testing/api/v4/serverchecks_test.go +++ b/traffic_ops/testing/api/v4/serverchecks_test.go @@ -16,6 +16,8 @@ package v4 */ import ( + "net/url" + "strconv" "testing" "time" @@ -28,6 +30,8 @@ func TestServerChecks(t *testing.T) { CreateTestInvalidServerChecks(t) UpdateTestServerChecks(t) GetTestServerChecks(t) + GetTestServerChecksWithName(t) + GetTestServerChecksWithID(t) }) } @@ -138,6 +142,36 @@ func GetTestServerChecks(t *testing.T) { } } +func GetTestServerChecksWithName(t *testing.T) { + params := url.Values{} + params.Set("name", "atlanta-edge-01") + + // Get server checks + scResp, alerts, _, err := TOSession.GetServerCheckWithParamHdr(params, nil) + if err != nil { + t.Fatalf("could not GET serverchecks by name (%v): %v (alerts: %+v)", scResp.HostName, err, alerts) + } +} + +func GetTestServerChecksWithID(t *testing.T) { + params := url.Values{} + serverChecksResp, _, _, err := TOSession.GetServersChecks() + if len(serverChecksResp) == 0 { + t.Fatal("no server checks in response, quitting") + } + if serverChecksResp[0].ID == 0 { + t.Fatal("ID of the response server is nil, quitting") + } + id := serverChecksResp[0].ID + params.Set("id", strconv.Itoa(id)) + + // Get server checks + scResp, alerts, _, err := TOSession.GetServerCheckWithParamHdr(params, nil) + if err != nil { + t.Fatalf("could not GET serverchecks by id (%v): %v (alerts: %+v)", scResp.ID, err, alerts) + } +} + // Need to define no-op function as TCObj interface expects a delete function // There is no delete path for serverchecks func DeleteTestServerChecks(t *testing.T) { diff --git a/traffic_ops/v4-client/servercheck.go b/traffic_ops/v4-client/servercheck.go index b08f3a75e5..d6ddb5c4a1 100644 --- a/traffic_ops/v4-client/servercheck.go +++ b/traffic_ops/v4-client/servercheck.go @@ -17,6 +17,8 @@ package client import ( "github.com/apache/trafficcontrol/lib/go-tc" + "net/http" + "net/url" ) const API_SERVERCHECK = apiBase + "/servercheck" @@ -41,3 +43,23 @@ func (to *Session) GetServersChecks() ([]tc.GenericServerCheck, tc.Alerts, ReqIn reqInf, err := to.get(API_SERVERCHECK, nil, &response) return response.Response, response.Alerts, reqInf, err } + +// GetServerCheckNameIDWithHdr fetches the Delivery Service with the given ID. +func (to *Session) GetServerCheckWithParamHdr(params url.Values, header http.Header) (*tc.GenericServerCheck, tc.Alerts, ReqInf, error) { + data := struct { + tc.Alerts + Response []tc.GenericServerCheck `json:"response"` + }{} + route := API_SERVERCHECK + if params != nil { + route += "?" + params.Encode() + } + reqInf, err := to.get(route, header, &data) + if err != nil { + return nil, data.Alerts, reqInf, err + } + if len(data.Response) == 0 { + return nil, data.Alerts, reqInf, nil + } + return &data.Response[0], data.Alerts, reqInf, nil +} From d899f952553021b3c75801310e3b52a08d05fc2c Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Fri, 19 Feb 2021 08:50:29 -0700 Subject: [PATCH 05/13] Updated documentation and changelog --- CHANGELOG.md | 1 + docs/source/api/v4/servercheck.rst | 41 ++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76aca2230e..82954a5cf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ## [unreleased] ### Added +- Traffic Ops: [#3577](https://github.com/apache/trafficcontrol/issues/3577) - Added a query param (server host_name or ID) for servercheck API - Traffic Portal: [#5318](https://github.com/apache/trafficcontrol/issues/5318) - Rename server columns for IPv4 address fields. - Traffic Portal: [#5361](https://github.com/apache/trafficcontrol/issues/5361) - Added the ability to change the name of a topology. - Traffic Portal: [#5340](https://github.com/apache/trafficcontrol/issues/5340) - Added the ability to resend a user registration from user screen. diff --git a/docs/source/api/v4/servercheck.rst b/docs/source/api/v4/servercheck.rst index 77c8a813c4..bebd735da5 100644 --- a/docs/source/api/v4/servercheck.rst +++ b/docs/source/api/v4/servercheck.rst @@ -31,7 +31,32 @@ Fetches identifying and meta information as well as "check" values regarding all Request Structure ----------------- -No parameters available. +.. table:: Request Query Parameters + + +-----------+------------------------------------------------------------------------------------+ + | Name | Description | + +===========+====================================================================================+ + | id | Return only :term:`Servers` with this integral, unique identifier (id) | + +-----------+------------------------------------------------------------------------------------+ + | name | Return only :term:`Servers` with this host_name | + +-----------+------------------------------------------------------------------------------------+ + +.. code-block:: http + :caption: Request Example + + GET /api/4.0/servercheck?name=edge HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... + + OR + + GET /api/4.0/servercheck?id=12 HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... Response Structure ------------------ @@ -55,9 +80,9 @@ Response Structure Access-Control-Allow-Origin: * Content-Encoding: gzip Content-Type: application/json - Set-Cookie: mojolicious=...; Path=/; Expires=Thu, 23 Jan 2020 20:00:19 GMT; Max-Age=3600; HttpOnly + Set-Cookie: mojolicious=...; Path=/; Expires=Thu, 18 Feb 2021 20:00:19 GMT; Max-Age=3600; HttpOnly X-Server-Name: traffic_ops_golang/ - Date: Thu, 23 Jan 2020 19:00:19 GMT + Date: Thu, 18 Feb 2021 19:00:19 GMT Content-Length: 352 { "response": [ @@ -70,16 +95,6 @@ Response Structure "profile": "ATS_EDGE_TIER_CACHE", "type": "EDGE", "updPending": false - }, - { - "adminState": "REPORTED", - "cacheGroup": "CDN_in_a_Box_Mid", - "id": 11, - "hostName": "mid", - "revalPending": false, - "profile": "ATS_MID_TIER_CACHE", - "type": "MID", - "updPending": false } ]} From f6eab75cadbfa364bcf5edf7a2bcbcda14222e3d Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Fri, 19 Feb 2021 09:04:26 -0700 Subject: [PATCH 06/13] Updated servercheck.rst for v3 --- docs/source/api/v3/servercheck.rst | 41 ++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/docs/source/api/v3/servercheck.rst b/docs/source/api/v3/servercheck.rst index dc4b7ef97f..79f3857c05 100644 --- a/docs/source/api/v3/servercheck.rst +++ b/docs/source/api/v3/servercheck.rst @@ -31,7 +31,32 @@ Fetches identifying and meta information as well as "check" values regarding all Request Structure ----------------- -No parameters available. +.. table:: Request Query Parameters + + +-----------+------------------------------------------------------------------------------------+ + | Name | Description | + +===========+====================================================================================+ + | id | Return only :term:`Servers` with this integral, unique identifier (id) | + +-----------+------------------------------------------------------------------------------------+ + | name | Return only :term:`Servers` with this host_name | + +-----------+------------------------------------------------------------------------------------+ + +.. code-block:: http + :caption: Request Example + + GET /api/4.0/servercheck?name=edge HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... + + OR + + GET /api/4.0/servercheck?id=12 HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... Response Structure ------------------ @@ -55,9 +80,9 @@ Response Structure Access-Control-Allow-Origin: * Content-Encoding: gzip Content-Type: application/json - Set-Cookie: mojolicious=...; Path=/; Expires=Thu, 23 Jan 2020 20:00:19 GMT; Max-Age=3600; HttpOnly + Set-Cookie: mojolicious=...; Path=/; Expires=Thu, 18 Feb 2021 20:00:19 GMT; Max-Age=3600; HttpOnly X-Server-Name: traffic_ops_golang/ - Date: Thu, 23 Jan 2020 19:00:19 GMT + Date: Thu, 18 Feb 2021 19:00:19 GMT Content-Length: 352 { "response": [ @@ -70,16 +95,6 @@ Response Structure "profile": "ATS_EDGE_TIER_CACHE", "type": "EDGE", "updPending": false - }, - { - "adminState": "REPORTED", - "cacheGroup": "CDN_in_a_Box_Mid", - "id": 11, - "hostName": "mid", - "revalPending": false, - "profile": "ATS_MID_TIER_CACHE", - "type": "MID", - "updPending": false } ]} From c4f520c74feb10ddc4002491712684fbfee92881 Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Fri, 19 Feb 2021 09:07:38 -0700 Subject: [PATCH 07/13] Updated function name in comments. --- traffic_ops/v4-client/servercheck.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/traffic_ops/v4-client/servercheck.go b/traffic_ops/v4-client/servercheck.go index d6ddb5c4a1..d0ae735f2e 100644 --- a/traffic_ops/v4-client/servercheck.go +++ b/traffic_ops/v4-client/servercheck.go @@ -44,7 +44,7 @@ func (to *Session) GetServersChecks() ([]tc.GenericServerCheck, tc.Alerts, ReqIn return response.Response, response.Alerts, reqInf, err } -// GetServerCheckNameIDWithHdr fetches the Delivery Service with the given ID. +// GetServerCheckWithParamHdr fetches the Delivery Service with the given ID. func (to *Session) GetServerCheckWithParamHdr(params url.Values, header http.Header) (*tc.GenericServerCheck, tc.Alerts, ReqInf, error) { data := struct { tc.Alerts From 3d1f7452e3959c31026dc1d9bb28a40b61d42172 Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Mon, 22 Feb 2021 13:01:13 -0700 Subject: [PATCH 08/13] Update based on review commetns --- docs/source/api/v3/servercheck.rst | 4 +++- docs/source/api/v4/servercheck.rst | 4 +++- .../testing/api/v4/serverchecks_test.go | 18 ++++++++++++------ .../servercheck/servercheck.go | 15 +++++++-------- traffic_ops/v4-client/servercheck.go | 17 ++++------------- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/docs/source/api/v3/servercheck.rst b/docs/source/api/v3/servercheck.rst index 79f3857c05..7db9a07a1f 100644 --- a/docs/source/api/v3/servercheck.rst +++ b/docs/source/api/v3/servercheck.rst @@ -42,7 +42,7 @@ Request Structure +-----------+------------------------------------------------------------------------------------+ .. code-block:: http - :caption: Request Example + :caption: Request Example with ``name`` query param GET /api/4.0/servercheck?name=edge HTTP/1.1 Host: trafficops.infra.ciab.test @@ -51,6 +51,8 @@ Request Structure Cookie: mojolicious=... OR +.. code-block:: http + :caption: Request Example with ``id`` query param GET /api/4.0/servercheck?id=12 HTTP/1.1 Host: trafficops.infra.ciab.test diff --git a/docs/source/api/v4/servercheck.rst b/docs/source/api/v4/servercheck.rst index bebd735da5..775761f02e 100644 --- a/docs/source/api/v4/servercheck.rst +++ b/docs/source/api/v4/servercheck.rst @@ -42,7 +42,7 @@ Request Structure +-----------+------------------------------------------------------------------------------------+ .. code-block:: http - :caption: Request Example + :caption: Request Example with ``name`` query param GET /api/4.0/servercheck?name=edge HTTP/1.1 Host: trafficops.infra.ciab.test @@ -51,6 +51,8 @@ Request Structure Cookie: mojolicious=... OR +.. code-block:: http + :caption: Request Example with ``id`` query param GET /api/4.0/servercheck?id=12 HTTP/1.1 Host: trafficops.infra.ciab.test diff --git a/traffic_ops/testing/api/v4/serverchecks_test.go b/traffic_ops/testing/api/v4/serverchecks_test.go index 71c8c5ff95..f782e4f2b3 100644 --- a/traffic_ops/testing/api/v4/serverchecks_test.go +++ b/traffic_ops/testing/api/v4/serverchecks_test.go @@ -105,7 +105,7 @@ func UpdateTestServerChecks(t *testing.T) { func GetTestServerChecks(t *testing.T) { hostname := testData.Serverchecks[0].HostName // Get server checks - serverChecksResp, alerts, _, err := TOSession.GetServersChecks() + serverChecksResp, alerts, _, err := TOSession.GetServersChecks(nil, nil) if err != nil { t.Fatalf("could not GET serverchecks: %v (alerts: %+v)", err, alerts) } @@ -147,15 +147,18 @@ func GetTestServerChecksWithName(t *testing.T) { params.Set("name", "atlanta-edge-01") // Get server checks - scResp, alerts, _, err := TOSession.GetServerCheckWithParamHdr(params, nil) + scResp, alerts, _, err := TOSession.GetServersChecks(params, nil) + if len(scResp) == 0 { + t.Fatal("no server checks in response, quitting") + } if err != nil { - t.Fatalf("could not GET serverchecks by name (%v): %v (alerts: %+v)", scResp.HostName, err, alerts) + t.Fatalf("could not GET serverchecks by name (%v): %v (alerts: %+v)", scResp[0].HostName, err, alerts) } } func GetTestServerChecksWithID(t *testing.T) { params := url.Values{} - serverChecksResp, _, _, err := TOSession.GetServersChecks() + serverChecksResp, _, _, err := TOSession.GetServersChecks(nil, nil) if len(serverChecksResp) == 0 { t.Fatal("no server checks in response, quitting") } @@ -166,9 +169,12 @@ func GetTestServerChecksWithID(t *testing.T) { params.Set("id", strconv.Itoa(id)) // Get server checks - scResp, alerts, _, err := TOSession.GetServerCheckWithParamHdr(params, nil) + scResp, alerts, _, err := TOSession.GetServersChecks(params, nil) + if len(scResp) == 0 { + t.Fatal("no server checks in response, quitting") + } if err != nil { - t.Fatalf("could not GET serverchecks by id (%v): %v (alerts: %+v)", scResp.ID, err, alerts) + t.Fatalf("could not GET serverchecks by id (%v): %v (alerts: %+v)", scResp[0].ID, err, alerts) } } diff --git a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go index 9cfb037133..a45fd9e968 100644 --- a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go +++ b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go @@ -248,22 +248,21 @@ func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerChec if len(errs) > 0 { return nil, util.JoinErrs(errs), nil, http.StatusBadRequest } - // where clause is different for servercheck and server table. Also, it differs for the query param. var whereSC, whereSI string - if where != "" { + if len(inf.Params) == 1 { for k, _ := range inf.Params { - if k == "id" { - whereSC = where - whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.id=:id " - } else { - whereSC = "" + if k == "name" { whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.host_name=:name " + whereSC = "" + } else if k == "id" { + whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.id=:id " + whereSC = where } } } else { + whereSI = "WHERE type.name LIKE 'MID%' OR type.name LIKE 'EDGE%' " whereSC = "" - whereSI = "WHERE type.name LIKE 'MID%' OR type.name LIKE 'EDGE%'" } extRows, err := tx.Query(extensionsQuery) diff --git a/traffic_ops/v4-client/servercheck.go b/traffic_ops/v4-client/servercheck.go index d0ae735f2e..4e2603cf37 100644 --- a/traffic_ops/v4-client/servercheck.go +++ b/traffic_ops/v4-client/servercheck.go @@ -16,9 +16,10 @@ package client import ( - "github.com/apache/trafficcontrol/lib/go-tc" "net/http" "net/url" + + "github.com/apache/trafficcontrol/lib/go-tc" ) const API_SERVERCHECK = apiBase + "/servercheck" @@ -34,18 +35,8 @@ func (to *Session) InsertServerCheckStatus(status tc.ServercheckRequestNullable) return &resp, reqInf, nil } -// GetServersChecks fetches check and meta information about servers from /servercheck. -func (to *Session) GetServersChecks() ([]tc.GenericServerCheck, tc.Alerts, ReqInf, error) { - var response struct { - tc.Alerts - Response []tc.GenericServerCheck `json:"response"` - } - reqInf, err := to.get(API_SERVERCHECK, nil, &response) - return response.Response, response.Alerts, reqInf, err -} - // GetServerCheckWithParamHdr fetches the Delivery Service with the given ID. -func (to *Session) GetServerCheckWithParamHdr(params url.Values, header http.Header) (*tc.GenericServerCheck, tc.Alerts, ReqInf, error) { +func (to *Session) GetServersChecks(params url.Values, header http.Header) ([]tc.GenericServerCheck, tc.Alerts, ReqInf, error) { data := struct { tc.Alerts Response []tc.GenericServerCheck `json:"response"` @@ -61,5 +52,5 @@ func (to *Session) GetServerCheckWithParamHdr(params url.Values, header http.Hea if len(data.Response) == 0 { return nil, data.Alerts, reqInf, nil } - return &data.Response[0], data.Alerts, reqInf, nil + return data.Response, data.Alerts, reqInf, nil } From 46bf52df93b1f33de207cb1593230dd86b182dda Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Mon, 22 Feb 2021 14:36:34 -0700 Subject: [PATCH 09/13] updated rst and comment --- docs/source/api/v3/servercheck.rst | 25 ++++++++++++------------- docs/source/api/v4/servercheck.rst | 25 ++++++++++++------------- traffic_ops/v4-client/servercheck.go | 2 +- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/docs/source/api/v3/servercheck.rst b/docs/source/api/v3/servercheck.rst index 7db9a07a1f..bd7acc2a73 100644 --- a/docs/source/api/v3/servercheck.rst +++ b/docs/source/api/v3/servercheck.rst @@ -36,29 +36,28 @@ Request Structure +-----------+------------------------------------------------------------------------------------+ | Name | Description | +===========+====================================================================================+ - | id | Return only :term:`Servers` with this integral, unique identifier (id) | + | id | Return only Servers with this integral, unique identifier (id) | +-----------+------------------------------------------------------------------------------------+ - | name | Return only :term:`Servers` with this host_name | + | name | Return only Servers with this host_name | +-----------+------------------------------------------------------------------------------------+ .. code-block:: http :caption: Request Example with ``name`` query param - GET /api/4.0/servercheck?name=edge HTTP/1.1 - Host: trafficops.infra.ciab.test - User-Agent: curl/7.47.0 - Accept: */* - Cookie: mojolicious=... + GET /api/4.0/servercheck?name=edge HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... - OR .. code-block:: http :caption: Request Example with ``id`` query param - GET /api/4.0/servercheck?id=12 HTTP/1.1 - Host: trafficops.infra.ciab.test - User-Agent: curl/7.47.0 - Accept: */* - Cookie: mojolicious=... + GET /api/4.0/servercheck?id=12 HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=.. Response Structure ------------------ diff --git a/docs/source/api/v4/servercheck.rst b/docs/source/api/v4/servercheck.rst index 775761f02e..3a4415fff0 100644 --- a/docs/source/api/v4/servercheck.rst +++ b/docs/source/api/v4/servercheck.rst @@ -36,29 +36,28 @@ Request Structure +-----------+------------------------------------------------------------------------------------+ | Name | Description | +===========+====================================================================================+ - | id | Return only :term:`Servers` with this integral, unique identifier (id) | + | id | Return only Servers with this integral, unique identifier (id) | +-----------+------------------------------------------------------------------------------------+ - | name | Return only :term:`Servers` with this host_name | + | name | Return only Servers with this host_name | +-----------+------------------------------------------------------------------------------------+ .. code-block:: http :caption: Request Example with ``name`` query param - GET /api/4.0/servercheck?name=edge HTTP/1.1 - Host: trafficops.infra.ciab.test - User-Agent: curl/7.47.0 - Accept: */* - Cookie: mojolicious=... + GET /api/4.0/servercheck?name=edge HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... - OR .. code-block:: http :caption: Request Example with ``id`` query param - GET /api/4.0/servercheck?id=12 HTTP/1.1 - Host: trafficops.infra.ciab.test - User-Agent: curl/7.47.0 - Accept: */* - Cookie: mojolicious=... + GET /api/4.0/servercheck?id=12 HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=.. Response Structure ------------------ diff --git a/traffic_ops/v4-client/servercheck.go b/traffic_ops/v4-client/servercheck.go index 4e2603cf37..be7067ab24 100644 --- a/traffic_ops/v4-client/servercheck.go +++ b/traffic_ops/v4-client/servercheck.go @@ -35,7 +35,7 @@ func (to *Session) InsertServerCheckStatus(status tc.ServercheckRequestNullable) return &resp, reqInf, nil } -// GetServerCheckWithParamHdr fetches the Delivery Service with the given ID. +// GetServersChecks fetches the Delivery Service with the given ID. func (to *Session) GetServersChecks(params url.Values, header http.Header) ([]tc.GenericServerCheck, tc.Alerts, ReqInf, error) { data := struct { tc.Alerts From 312214341293d74651edf558ef01f5f08ba53578 Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Tue, 23 Feb 2021 13:19:15 -0700 Subject: [PATCH 10/13] Updated rst with tabs --- docs/source/api/v3/servercheck.rst | 24 ++++++++++++------------ docs/source/api/v4/servercheck.rst | 24 ++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/source/api/v3/servercheck.rst b/docs/source/api/v3/servercheck.rst index bd7acc2a73..5aa18573d2 100644 --- a/docs/source/api/v3/servercheck.rst +++ b/docs/source/api/v3/servercheck.rst @@ -36,28 +36,28 @@ Request Structure +-----------+------------------------------------------------------------------------------------+ | Name | Description | +===========+====================================================================================+ - | id | Return only Servers with this integral, unique identifier (id) | + | id | Return only :term:`cache servers` with this integral, unique identifier (id) | +-----------+------------------------------------------------------------------------------------+ - | name | Return only Servers with this host_name | + | name | Return only :term:`cache servers` with this host_name | +-----------+------------------------------------------------------------------------------------+ .. code-block:: http :caption: Request Example with ``name`` query param - GET /api/4.0/servercheck?name=edge HTTP/1.1 - Host: trafficops.infra.ciab.test - User-Agent: curl/7.47.0 - Accept: */* - Cookie: mojolicious=... + GET /api/4.0/servercheck?name=edge HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... .. code-block:: http :caption: Request Example with ``id`` query param - GET /api/4.0/servercheck?id=12 HTTP/1.1 - Host: trafficops.infra.ciab.test - User-Agent: curl/7.47.0 - Accept: */* - Cookie: mojolicious=.. + GET /api/4.0/servercheck?id=12 HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... Response Structure ------------------ diff --git a/docs/source/api/v4/servercheck.rst b/docs/source/api/v4/servercheck.rst index 3a4415fff0..8fdab46c37 100644 --- a/docs/source/api/v4/servercheck.rst +++ b/docs/source/api/v4/servercheck.rst @@ -36,28 +36,28 @@ Request Structure +-----------+------------------------------------------------------------------------------------+ | Name | Description | +===========+====================================================================================+ - | id | Return only Servers with this integral, unique identifier (id) | + | id | Return only :term:`cache servers` with this integral, unique identifier (id) | +-----------+------------------------------------------------------------------------------------+ - | name | Return only Servers with this host_name | + | name | Return only :term:`cache servers` with this host_name | +-----------+------------------------------------------------------------------------------------+ .. code-block:: http :caption: Request Example with ``name`` query param - GET /api/4.0/servercheck?name=edge HTTP/1.1 - Host: trafficops.infra.ciab.test - User-Agent: curl/7.47.0 - Accept: */* - Cookie: mojolicious=... + GET /api/4.0/servercheck?name=edge HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... .. code-block:: http :caption: Request Example with ``id`` query param - GET /api/4.0/servercheck?id=12 HTTP/1.1 - Host: trafficops.infra.ciab.test - User-Agent: curl/7.47.0 - Accept: */* - Cookie: mojolicious=.. + GET /api/4.0/servercheck?id=12 HTTP/1.1 + Host: trafficops.infra.ciab.test + User-Agent: curl/7.47.0 + Accept: */* + Cookie: mojolicious=... Response Structure ------------------ From 7641b827287824ad9faf4481a1e59315741691ae Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Thu, 25 Feb 2021 13:31:51 -0700 Subject: [PATCH 11/13] Updated where clause logic to work against all query cases --- .../servercheck/servercheck.go | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go index a45fd9e968..385c9c38fd 100644 --- a/traffic_ops/traffic_ops_golang/servercheck/servercheck.go +++ b/traffic_ops/traffic_ops_golang/servercheck/servercheck.go @@ -250,19 +250,30 @@ func handleReadServerCheck(inf *api.APIInfo, tx *sql.Tx) ([]tc.GenericServerChec } // where clause is different for servercheck and server table. Also, it differs for the query param. var whereSC, whereSI string - if len(inf.Params) == 1 { - for k, _ := range inf.Params { - if k == "name" { - whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.host_name=:name " - whereSC = "" - } else if k == "id" { - whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.id=:id " - whereSC = where - } - } - } else { + if len(inf.Params) < 1 { whereSI = "WHERE type.name LIKE 'MID%' OR type.name LIKE 'EDGE%' " whereSC = "" + } else if len(inf.Params) == 1 { + if _, ok := inf.Params["name"]; ok { + whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.host_name=:name " + whereSC = "" + } else if _, ok = inf.Params["id"]; ok { + whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND server.id=:id " + whereSC = where + } else { + whereSI = "WHERE type.name LIKE 'MID%' OR type.name LIKE 'EDGE%' " + whereSC = "" + } + } else if len(inf.Params) > 1 { + _, ok := inf.Params["id"] + _, ok1 := inf.Params["name"] + if ok && ok1 { + whereSI = "WHERE (type.name LIKE 'MID%' OR type.name LIKE 'EDGE%') AND (server.host_name=:name AND server.id=:id)" + whereSC = "WHERE servercheck.server=:id" + } else { + whereSI = "WHERE type.name LIKE 'MID%' OR type.name LIKE 'EDGE%' " + whereSC = "" + } } extRows, err := tx.Query(extensionsQuery) From f7190e49168f9e798af08b9c8ca6cacf3bc4475a Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Thu, 25 Feb 2021 14:13:16 -0700 Subject: [PATCH 12/13] Updated comment --- traffic_ops/v4-client/servercheck.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/traffic_ops/v4-client/servercheck.go b/traffic_ops/v4-client/servercheck.go index be7067ab24..be6750d969 100644 --- a/traffic_ops/v4-client/servercheck.go +++ b/traffic_ops/v4-client/servercheck.go @@ -35,7 +35,7 @@ func (to *Session) InsertServerCheckStatus(status tc.ServercheckRequestNullable) return &resp, reqInf, nil } -// GetServersChecks fetches the Delivery Service with the given ID. +// GetServersChecks fetches Server Checks from Traffic Ops. func (to *Session) GetServersChecks(params url.Values, header http.Header) ([]tc.GenericServerCheck, tc.Alerts, ReqInf, error) { data := struct { tc.Alerts From 832174bd25fbb11c65e32ba07df2f791937e953b Mon Sep 17 00:00:00 2001 From: Rima Shah Date: Fri, 26 Feb 2021 10:01:37 -0700 Subject: [PATCH 13/13] Fixed merge conflict --- traffic_ops/v4-client/servercheck.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/traffic_ops/v4-client/servercheck.go b/traffic_ops/v4-client/servercheck.go index 3877ea0cd8..ac48acfe57 100644 --- a/traffic_ops/v4-client/servercheck.go +++ b/traffic_ops/v4-client/servercheck.go @@ -36,8 +36,8 @@ func (to *Session) InsertServerCheckStatus(status tc.ServercheckRequestNullable) return &resp, reqInf, nil } -// GetServersChecks fetches Server Checks from Traffic Ops. -func (to *Session) GetServersChecks(params url.Values, header http.Header) ([]tc.GenericServerCheck, tc.Alerts, ReqInf, error) { +// GetServersChecks fetches check and meta information about servers from /servercheck. +func (to *Session) GetServersChecks(params url.Values, header http.Header) ([]tc.GenericServerCheck, tc.Alerts, toclientlib.ReqInf, error) { data := struct { tc.Alerts Response []tc.GenericServerCheck `json:"response"`