You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by oc...@apache.org on 2021/09/20 23:58:09 UTC

[trafficcontrol] branch master updated: Fix Internal server error in Get Profiles by invalid Params (#6003)

This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new bf0df00  Fix Internal server error in Get Profiles by invalid Params (#6003)
bf0df00 is described below

commit bf0df00b4408b5c8d229e91ae31bf3e282fbaeb4
Author: dmohan001c <de...@comcast.com>
AuthorDate: Tue Sep 21 05:28:00 2021 +0530

    Fix Internal server error in Get Profiles by invalid Params (#6003)
    
    * merged changes
    
    * updated error message in get profiles
    
    * updated error message to print the exact errors
    
    * changed the error status code for get profiles by invalid cdn name
    
    * Added validation to check integer value
    
    * updated DB query to support params value in getprofiles by param
    
    * added tests for get profile by param in all versions
    
    * Fixed merge commits
    
    * updated read profiles tests to support integer param values
    
    * reverted back the method signature of GetProfileByParameterWithHdr
    
    * updated the get profiles validation
    
    * added new client methods for getprofilebyparameterId
    
    * added back the already available comment
    
    * added error checks
    
    * formatted the code
    
    * removed unnecessary review comments
---
 traffic_ops/testing/api/v2/profiles_test.go        | 27 ++++++++--
 traffic_ops/testing/api/v3/profiles_test.go        | 57 ++++++++++++++++++----
 traffic_ops/testing/api/v4/profiles_test.go        | 34 ++++++++++---
 traffic_ops/traffic_ops_golang/profile/profiles.go | 14 +++---
 traffic_ops/v2-client/profile.go                   | 16 ++++++
 traffic_ops/v3-client/profile.go                   | 14 ++++++
 6 files changed, 134 insertions(+), 28 deletions(-)

diff --git a/traffic_ops/testing/api/v2/profiles_test.go b/traffic_ops/testing/api/v2/profiles_test.go
index 0c7be6d..92af1e1 100644
--- a/traffic_ops/testing/api/v2/profiles_test.go
+++ b/traffic_ops/testing/api/v2/profiles_test.go
@@ -210,12 +210,29 @@ func GetTestProfiles(t *testing.T) {
 			t.Errorf("cannot GET Profile by name: %v - %v", err, resp)
 		}
 		profileID := resp[0].ID
-
-		resp, _, err = TOSession.GetProfileByParameter(pr.Parameter)
-		if err != nil {
-			t.Errorf("cannot GET Profile by param: %v - %v", err, resp)
+		if len(pr.Parameters) > 0 {
+			parameter := pr.Parameters[0]
+			respParameter, _, err := TOSession.GetParameterByName(*parameter.Name)
+			if err != nil {
+				t.Errorf("Cannot GET Parameter by name: %v", err)
+			}
+			if len(respParameter) > 0 {
+				parameterID := respParameter[0].ID
+				if parameterID > 0 {
+					resp, _, err = TOSession.GetProfileByParameterId(parameterID)
+					if err != nil {
+						t.Errorf("cannot GET Profile by param: %v", err)
+					}
+					if len(resp) < 1 {
+						t.Errorf("Expected atleast one response for Get Profile by Parameters, but found %d", len(resp))
+					}
+				} else {
+					t.Errorf("Invalid parameter ID %d", parameterID)
+				}
+			} else {
+				t.Errorf("No response found for GET Parameters by name")
+			}
 		}
-
 		resp, _, err = TOSession.GetProfileByCDNID(pr.CDNID)
 		if err != nil {
 			t.Errorf("cannot GET Profile by cdn: %v - %v", err, resp)
diff --git a/traffic_ops/testing/api/v3/profiles_test.go b/traffic_ops/testing/api/v3/profiles_test.go
index 7c8f7d1..1cd84ac 100644
--- a/traffic_ops/testing/api/v3/profiles_test.go
+++ b/traffic_ops/testing/api/v3/profiles_test.go
@@ -83,12 +83,31 @@ func GetTestProfilesIMS(t *testing.T) {
 		if reqInf.StatusCode != http.StatusNotModified {
 			t.Fatalf("Expected 304 status code, got %v", reqInf.StatusCode)
 		}
-		_, reqInf, err = TOSession.GetProfileByParameterWithHdr(pr.Parameter, header)
-		if err != nil {
-			t.Fatalf("Expected no error, but got %v", err.Error())
-		}
-		if reqInf.StatusCode != http.StatusNotModified {
-			t.Fatalf("Expected 304 status code, got %v", reqInf.StatusCode)
+		if len(pr.Parameters) > 0 {
+			parameter := pr.Parameters[0]
+			respParameter, _, err := TOSession.GetParameterByName(*parameter.Name)
+			if err != nil {
+				t.Errorf("Cannot GET Parameter by name: %v", err)
+			}
+			if len(respParameter) > 0 {
+				parameterID := respParameter[0].ID
+				if parameterID > 0 {
+					resp, _, err := TOSession.GetProfileByParameterIdWithHdr(parameterID, nil)
+					if err != nil {
+						t.Fatalf("Expected no error, but got %v", err.Error())
+					}
+					if reqInf.StatusCode != http.StatusNotModified {
+						t.Fatalf("Expected 304 status code, got %v", reqInf.StatusCode)
+					}
+					if len(resp) < 1 {
+						t.Errorf("Expected atleast one response for Get Profile by Parameters, but found %d", len(resp))
+					}
+				} else {
+					t.Errorf("Invalid parameter ID %d", parameterID)
+				}
+			} else {
+				t.Errorf("No response found for GET Parameters by name")
+			}
 		}
 	}
 }
@@ -269,10 +288,28 @@ func GetTestProfiles(t *testing.T) {
 			t.Errorf("cannot GET Profile by name: %v - %v", err, resp)
 		}
 		profileID := resp[0].ID
-
-		resp, _, err = TOSession.GetProfileByParameter(pr.Parameter)
-		if err != nil {
-			t.Errorf("cannot GET Profile by param: %v - %v", err, resp)
+		if len(pr.Parameters) > 0 {
+			parameter := pr.Parameters[0]
+			respParameter, _, err := TOSession.GetParameterByName(*parameter.Name)
+			if err != nil {
+				t.Errorf("Cannot GET Parameter by name: %v", err)
+			}
+			if len(respParameter) > 0 {
+				parameterID := respParameter[0].ID
+				if parameterID > 0 {
+					resp, _, err = TOSession.GetProfileByParameterId(parameterID)
+					if err != nil {
+						t.Errorf("cannot GET Profile by param: %v - %v", err, resp)
+					}
+					if len(resp) < 1 {
+						t.Errorf("Expected atleast one response for Get Profile by Parameters, but found %d", len(resp))
+					}
+				} else {
+					t.Errorf("Invalid parameter ID %d", parameterID)
+				}
+			} else {
+				t.Errorf("No response found for GET Parameters by name")
+			}
 		}
 
 		resp, _, err = TOSession.GetProfileByCDNID(pr.CDNID)
diff --git a/traffic_ops/testing/api/v4/profiles_test.go b/traffic_ops/testing/api/v4/profiles_test.go
index 0d84916..b9ef892 100644
--- a/traffic_ops/testing/api/v4/profiles_test.go
+++ b/traffic_ops/testing/api/v4/profiles_test.go
@@ -517,12 +517,34 @@ func GetTestProfiles(t *testing.T) {
 		}
 		profileID := resp.Response[0].ID
 
-		// TODO: figure out what the 'Parameter' field of a Profile is and how
-		// passing it to this is supposed to work.
-		// resp, _, err = TOSession.GetProfileByParameter(pr.Parameter)
-		// if err != nil {
-		// 	t.Errorf("cannot GET Profile by param: %v - %v", err, resp)
-		// }
+		if len(pr.Parameters) > 0 {
+			parameter := pr.Parameters[0]
+			opts.QueryParameters.Set("name", *parameter.Name)
+			respParameter, _, err := TOSession.GetParameters(opts)
+			if err != nil {
+				t.Errorf("cannot get parameter '%s' by name: %v - alerts: %+v", *parameter.Name, err, resp.Alerts)
+			}
+			opts.QueryParameters.Del("name")
+			if len(respParameter.Response) > 0 {
+				parameterID := respParameter.Response[0].ID
+				if parameterID > 0 {
+					opts.QueryParameters.Set("params", strconv.Itoa(parameterID))
+					resp, _, err = TOSession.GetProfiles(opts)
+					opts.QueryParameters.Del("params")
+					if err != nil {
+						t.Errorf("cannot GET Profile by param: %v - %v", err, resp)
+					}
+					if len(resp.Response) < 1 {
+						t.Errorf("Expected atleast one response for Get Profile by Parameters, but found %d", len(resp.Response))
+					}
+				} else {
+					t.Errorf("Invalid parameter ID %d", parameterID)
+				}
+			} else {
+				t.Errorf("No response found for GET Parameters by name")
+			}
+
+		}
 
 		opts.QueryParameters.Set("cdn", strconv.Itoa(pr.CDNID))
 		resp, _, err = TOSession.GetProfiles(opts)
diff --git a/traffic_ops/traffic_ops_golang/profile/profiles.go b/traffic_ops/traffic_ops_golang/profile/profiles.go
index 6e12387..4ae0e8a 100644
--- a/traffic_ops/traffic_ops_golang/profile/profiles.go
+++ b/traffic_ops/traffic_ops_golang/profile/profiles.go
@@ -135,19 +135,19 @@ func (prof *TOProfile) Read(h http.Header, useIMS bool) ([]interface{}, error, e
 	// Query Parameters to Database Query column mappings
 	// see the fields mapped in the SQL query
 	queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
-		CDNQueryParam:  dbhelpers.WhereColumnInfo{Column: "c.id"},
-		NameQueryParam: dbhelpers.WhereColumnInfo{Column: "prof.name"},
-		IDQueryParam:   dbhelpers.WhereColumnInfo{Column: "prof.id", Checker: api.IsInt},
+		CDNQueryParam:   dbhelpers.WhereColumnInfo{Column: "c.id", Checker: api.IsInt},
+		NameQueryParam:  dbhelpers.WhereColumnInfo{Column: "prof.name"},
+		IDQueryParam:    dbhelpers.WhereColumnInfo{Column: "prof.id", Checker: api.IsInt},
+		ParamQueryParam: dbhelpers.WhereColumnInfo{Column: "pp.parameter", Checker: api.IsInt},
 	}
 	where, orderBy, pagination, queryValues, errs := dbhelpers.BuildWhereAndOrderByAndPagination(prof.APIInfo().Params, queryParamsToQueryCols)
 
+	query := selectProfilesQuery()
 	// Narrow down if the query parameter is 'param'
-
 	// TODO add generic where clause to api.GenericRead
 	if paramValue, ok := prof.APIInfo().Params[ParamQueryParam]; ok {
-		queryValues["parameter_id"] = paramValue
 		if len(paramValue) > 0 {
-			where += " LEFT JOIN profile_parameter pp ON prof.id  = pp.profile where pp.parameter=:parameter_id"
+			query += " LEFT JOIN profile_parameter pp ON prof.id = pp.profile"
 		}
 	}
 
@@ -166,7 +166,7 @@ func (prof *TOProfile) Read(h http.Header, useIMS bool) ([]interface{}, error, e
 		log.Debugln("Non IMS request")
 	}
 
-	query := selectProfilesQuery() + where + orderBy + pagination
+	query += where + orderBy + pagination
 	log.Debugln("Query is ", query)
 
 	rows, err := prof.ReqInfo.Tx.NamedQuery(query, queryValues)
diff --git a/traffic_ops/v2-client/profile.go b/traffic_ops/v2-client/profile.go
index 66af27a..e74d87d 100644
--- a/traffic_ops/v2-client/profile.go
+++ b/traffic_ops/v2-client/profile.go
@@ -178,6 +178,22 @@ func (to *Session) GetProfileByParameter(param string) ([]tc.Profile, ReqInf, er
 	return data.Response, reqInf, err
 }
 
+// GetProfileByParameterId GETs a Profile by Parameter ID
+func (to *Session) GetProfileByParameterId(param int) ([]tc.Profile, ReqInf, error) {
+	URI := fmt.Sprintf("%s?param=%d", API_PROFILES, param)
+	resp, remoteAddr, err := to.request(http.MethodGet, URI, nil)
+	reqInf := ReqInf{CacheHitStatus: CacheHitStatusMiss, RemoteAddr: remoteAddr}
+	if err != nil {
+		return nil, reqInf, err
+	}
+	defer resp.Body.Close()
+
+	var data tc.ProfilesResponse
+	err = json.NewDecoder(resp.Body).Decode(&data)
+
+	return data.Response, reqInf, err
+}
+
 // GetProfileByCDNID GETs a Profile by the Profile CDN ID.
 func (to *Session) GetProfileByCDNID(cdnID int) ([]tc.Profile, ReqInf, error) {
 	URI := fmt.Sprintf("%s?cdn=%s", API_PROFILES, strconv.Itoa(cdnID))
diff --git a/traffic_ops/v3-client/profile.go b/traffic_ops/v3-client/profile.go
index 342b220..88fdb2c 100644
--- a/traffic_ops/v3-client/profile.go
+++ b/traffic_ops/v3-client/profile.go
@@ -139,6 +139,20 @@ func (to *Session) GetProfileByParameter(param string) ([]tc.Profile, toclientli
 	return to.GetProfileByParameterWithHdr(param, nil)
 }
 
+// GetProfileByParameterIdWithHdr GETs a Profile by the ParameterID and Header.
+func (to *Session) GetProfileByParameterIdWithHdr(param int, header http.Header) ([]tc.Profile, toclientlib.ReqInf, error) {
+	URI := fmt.Sprintf("%s?param=%d", APIProfiles, param)
+	var data tc.ProfilesResponse
+	reqInf, err := to.get(URI, header, &data)
+	return data.Response, reqInf, err
+}
+
+// GetProfileByParameterId GETs a Profile by the Profile "param".
+// Deprecated: GetProfileByParameterId will be removed in 6.0. Use GetProfileByParameterIdWithHdr.
+func (to *Session) GetProfileByParameterId(param int) ([]tc.Profile, toclientlib.ReqInf, error) {
+	return to.GetProfileByParameterIdWithHdr(param, nil)
+}
+
 func (to *Session) GetProfileByCDNIDWithHdr(cdnID int, header http.Header) ([]tc.Profile, toclientlib.ReqInf, error) {
 	uri := fmt.Sprintf("%s?cdn=%d", APIProfiles, cdnID)
 	var data tc.ProfilesResponse