You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2021/07/28 03:46:46 UTC

[GitHub] [trafficcontrol] dmohan001c opened a new pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

dmohan001c opened a new pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064


   ## What does this PR (Pull Request) do?
   
   - [x] This PR fixes #4442  <!-- You can check for an issue here: https://github.com/apache/trafficcontrol/issues -->
   
   This fixes the internal server error causing in update cache group with the already existing cache group name
   
   ## Which Traffic Control components are affected by this PR?
   
   - CDN in a Box
   - Traffic Control Client <!-- Please specify which; e.g. 'Python', 'Go', 'Java' -->
   - Traffic Ops
   - CI tests
   
   ## What is the best way to verify this PR?
   Execute all the Integration tests and make sure the tests are passed.
   
   ## If this is a bug fix, what versions of Traffic Control are affected?
   * Master
   
   ## The following criteria are ALL met by this PR
   <!-- Check the boxes to signify that the associated statement is true. To
   "check a box", replace the space inside of the square brackets with an 'x'.
   e.g.
   
   - [ x] <- Wrong
   - [x ] <- Wrong
   - [] <- Wrong
   - [*] <- Wrong
   - [x] <- Correct!
   
   -->
   
   - [x] This PR includes tests OR I have explained why tests are unnecessary
   - [x] This PR includes documentation OR I have explained why documentation is unnecessary
   - [x] This PR includes an update to CHANGELOG.md OR such an update is not necessary
   - [x] This PR includes any and all required license headers
   - [x] This PR **DOES NOT FIX A SERIOUS SECURITY VULNERABILITY** (see [the Apache Software Foundation's security guidelines](https://www.apache.org/security/) for details)
   


-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r678501631



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -688,7 +688,7 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 	}
 
 	if err = cg.updateCoordinate(); err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+		return nil, err, tc.DBError, http.StatusInternalServerError

Review comment:
       Internal Server Errors must not be reported to the user. The bug as documented in Issue #4442 is that the Internal Server Error occurs when attempting to set a Cache Group's Name to the Name of a different, existing Cache Group. That should be a client error in the range [400,500) (400 seems the best fit) because the client can change something about their request to make it succeed. However, that's not the _only_ error that can be returned by `updateCoordinate`, so you can't just return whatever error that gives back.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r700721104



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -663,7 +681,7 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, fmt.Errorf("no cachegroup with id %d found", *cg.ID), nil, http.StatusNotFound
 	}
 	if err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+		return nil, nil, nil, http.StatusInternalServerError

Review comment:
       returned the error.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683420173



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {

Review comment:
       It was returned as nil before, so i didn't change anything.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r699715744



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -37,11 +37,13 @@ import (
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 
-	"github.com/go-ozzo/ozzo-validation"
+	validation "github.com/go-ozzo/ozzo-validation"
 	"github.com/jmoiron/sqlx"
 	"github.com/lib/pq"
 )
 
+var ErrDuplicateExist = errors.New("cachegroup name already exist, give different name")

Review comment:
       This should not be exported

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`
+		result, err := cg.ReqInfo.Tx.Tx.Exec(q, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name)
+		if err != nil {
+			return fmt.Errorf("getting coordinate for cachegroup '%s': %s", *cg.Name, err.Error())

Review comment:
       `fmt.Errorf` formatting directive should be `%w` and consume the error, not be `%s` and consume the output of the error's `Error` method.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,18 +699,25 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
+	err = cg.updateCoordinate()
+	if err != nil {
+		if err == ErrDuplicateExist {

Review comment:
       To compare errors, [`errors.Is`](https://pkg.go.dev/errors#Is) should be used. 

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`

Review comment:
       Instead of checking the number of rows returned by this query, why not use a query that returns the number of rows? E.g.
   ```go
   const numberOfDuplicatesQuery = `
   SELECT COUNT(*)
   FROM public.coordinate
   WHERE id NOT IN (
       SELECT coordinate
       FROM public.cachegroup
       WHERE id = $1
   )
   AND name = $2
   `
   
   func (cg *TOCacheGroup) updateCoordinate() error {
       if cg.Latitude != nil && cg.Longitude != nil {
           var count uint
           if err := cg.ReqInfo.Tx.Tx.QueryRow(numberOfDuplicatesQuery, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name).Scan(&count); err != nil {
               return fmt.Errorf("getting coordinate for Cache Group '%s': %w", *cg.Name, err)
           }
           if count > 0 {
           // ... etc.
   ```

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -37,11 +37,13 @@ import (
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 
-	"github.com/go-ozzo/ozzo-validation"
+	validation "github.com/go-ozzo/ozzo-validation"
 	"github.com/jmoiron/sqlx"
 	"github.com/lib/pq"
 )
 
+var ErrDuplicateExist = errors.New("cachegroup name already exist, give different name")

Review comment:
       also, grammar nit:
   
   <del>exist</del> -> <ins>exists<ins>
   
   <del>give different name</del> -> e.g. <ins>please choose a different name</ins> or similar

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`
+		result, err := cg.ReqInfo.Tx.Tx.Exec(q, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name)
+		if err != nil {
+			return fmt.Errorf("getting coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+		}
+		rowsAffected, _ := result.RowsAffected()

Review comment:
       should not ignore the possible error here - in which case, `rowsAffected` is likely to be zero and will not be caught below.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,18 +699,25 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
+	err = cg.updateCoordinate()
+	if err != nil {
+		if err == ErrDuplicateExist {
+			return nil, err, tc.DBError, http.StatusBadRequest

Review comment:
       `tc.DBError` should never be used

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`

Review comment:
       also, query should specify `public` schema, as in my example




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r700761980



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -680,25 +698,32 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 	//
 	if cg.Latitude == nil || cg.Longitude == nil {
 		if err = cg.deleteCoordinate(*coordinateID); err != nil {
-			return nil, nil, tc.DBError, http.StatusInternalServerError
+			return nil, nil, nil, http.StatusInternalServerError

Review comment:
       Fixed..

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -680,25 +698,32 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 	//
 	if cg.Latitude == nil || cg.Longitude == nil {
 		if err = cg.deleteCoordinate(*coordinateID); err != nil {
-			return nil, nil, tc.DBError, http.StatusInternalServerError
+			return nil, nil, nil, http.StatusInternalServerError
 		}
 		cg.Latitude = nil
 		cg.Longitude = nil
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+	err = cg.updateCoordinate()
+	if err != nil {
+		if errors.Is(err, errors.New("cachegroup name already exists, please choose a different name")) {
+			return nil, err, err, http.StatusBadRequest
+		}
+		return nil, nil, nil, http.StatusInternalServerError

Review comment:
       Fixed.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -680,25 +698,32 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 	//
 	if cg.Latitude == nil || cg.Longitude == nil {
 		if err = cg.deleteCoordinate(*coordinateID); err != nil {
-			return nil, nil, tc.DBError, http.StatusInternalServerError
+			return nil, nil, nil, http.StatusInternalServerError
 		}
 		cg.Latitude = nil
 		cg.Longitude = nil
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+	err = cg.updateCoordinate()
+	if err != nil {
+		if errors.Is(err, errors.New("cachegroup name already exists, please choose a different name")) {

Review comment:
       updated the code as per your comments.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 merged pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 merged pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064


   


-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r700721235



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -680,25 +698,32 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 	//
 	if cg.Latitude == nil || cg.Longitude == nil {
 		if err = cg.deleteCoordinate(*coordinateID); err != nil {
-			return nil, nil, tc.DBError, http.StatusInternalServerError
+			return nil, nil, nil, http.StatusInternalServerError

Review comment:
       returned the error,




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r700586720



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -680,25 +698,32 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 	//
 	if cg.Latitude == nil || cg.Longitude == nil {
 		if err = cg.deleteCoordinate(*coordinateID); err != nil {
-			return nil, nil, tc.DBError, http.StatusInternalServerError
+			return nil, nil, nil, http.StatusInternalServerError

Review comment:
       this should return the error that occurred so it can be logged

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -680,25 +698,32 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 	//
 	if cg.Latitude == nil || cg.Longitude == nil {
 		if err = cg.deleteCoordinate(*coordinateID); err != nil {
-			return nil, nil, tc.DBError, http.StatusInternalServerError
+			return nil, nil, nil, http.StatusInternalServerError
 		}
 		cg.Latitude = nil
 		cg.Longitude = nil
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+	err = cg.updateCoordinate()
+	if err != nil {
+		if errors.Is(err, errors.New("cachegroup name already exists, please choose a different name")) {

Review comment:
       This will always evaluate to false, because no error will ever arise from an error literally produced in-line from errors.New: https://play.golang.org/p/L_jyTbcUxPO (incidentally `==` also couldn't do that: https://play.golang.org/p/Urfd6vGTyLE)
   
   You have to compare it to literally the same error for that to work, like this: https://play.golang.org/p/-sShrMsL5HS

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -663,7 +681,7 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, fmt.Errorf("no cachegroup with id %d found", *cg.ID), nil, http.StatusNotFound
 	}
 	if err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+		return nil, nil, nil, http.StatusInternalServerError

Review comment:
       this should return the error that occurred so that it can be logged

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -680,25 +698,32 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 	//
 	if cg.Latitude == nil || cg.Longitude == nil {
 		if err = cg.deleteCoordinate(*coordinateID); err != nil {
-			return nil, nil, tc.DBError, http.StatusInternalServerError
+			return nil, nil, nil, http.StatusInternalServerError
 		}
 		cg.Latitude = nil
 		cg.Longitude = nil
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+	err = cg.updateCoordinate()
+	if err != nil {
+		if errors.Is(err, errors.New("cachegroup name already exists, please choose a different name")) {
+			return nil, err, err, http.StatusBadRequest
+		}
+		return nil, nil, nil, http.StatusInternalServerError

Review comment:
       This should return the error that occurred so that it can be logged




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683610487



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest

Review comment:
       reverted.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', getting rows affected: %s", *cg.Name, err.Error())
+			return nil, errors.New("updating coordinate for cachegroup , getting rows affected:" + *cg.Name + err.Error()), tc.DBError, http.StatusInternalServerError
 		}
 		if rowsAffected == 0 {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', zero rows affected", *cg.Name)
+			return nil, errors.New("updating coordinate for cachegroup - Zero rows affected" + *cg.Name), tc.DBError, http.StatusInternalServerError
 		}
 	}
-	return nil
+	return nil, nil, nil, http.StatusBadRequest

Review comment:
       reverted,




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r700761935



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -663,7 +681,7 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, fmt.Errorf("no cachegroup with id %d found", *cg.ID), nil, http.StatusNotFound
 	}
 	if err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+		return nil, nil, nil, http.StatusInternalServerError

Review comment:
       Fixed.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683426148



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', getting rows affected: %s", *cg.Name, err.Error())
+			return nil, errors.New("updating coordinate for cachegroup , getting rows affected:" + *cg.Name + err.Error()), tc.DBError, http.StatusInternalServerError

Review comment:
       updated back to fmt.Errorf

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', getting rows affected: %s", *cg.Name, err.Error())
+			return nil, errors.New("updating coordinate for cachegroup , getting rows affected:" + *cg.Name + err.Error()), tc.DBError, http.StatusInternalServerError
 		}
 		if rowsAffected == 0 {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', zero rows affected", *cg.Name)
+			return nil, errors.New("updating coordinate for cachegroup - Zero rows affected" + *cg.Name), tc.DBError, http.StatusInternalServerError

Review comment:
       updated back to fmt.Errorf




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r681827478



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {

Review comment:
       It looks like first return value is always `nil` - what's that for?

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', getting rows affected: %s", *cg.Name, err.Error())
+			return nil, errors.New("updating coordinate for cachegroup , getting rows affected:" + *cg.Name + err.Error()), tc.DBError, http.StatusInternalServerError

Review comment:
       changing this to `errors.New` changes the formatting, so that it needs to be re-ordered or re-worded - I'd just leave it as `fmt.Errorf`, there's no need to change it.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', getting rows affected: %s", *cg.Name, err.Error())
+			return nil, errors.New("updating coordinate for cachegroup , getting rows affected:" + *cg.Name + err.Error()), tc.DBError, http.StatusInternalServerError
 		}
 		if rowsAffected == 0 {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', zero rows affected", *cg.Name)
+			return nil, errors.New("updating coordinate for cachegroup - Zero rows affected" + *cg.Name), tc.DBError, http.StatusInternalServerError

Review comment:
       same as above RE: changing `fmt.Errorf` to `errors.New`

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', getting rows affected: %s", *cg.Name, err.Error())
+			return nil, errors.New("updating coordinate for cachegroup , getting rows affected:" + *cg.Name + err.Error()), tc.DBError, http.StatusInternalServerError
 		}
 		if rowsAffected == 0 {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', zero rows affected", *cg.Name)
+			return nil, errors.New("updating coordinate for cachegroup - Zero rows affected" + *cg.Name), tc.DBError, http.StatusInternalServerError
 		}
 	}
-	return nil
+	return nil, nil, nil, http.StatusBadRequest

Review comment:
       If no error occurred the status code should, by convention, be `http.StatusOK`. I think zero would also be acceptable, since no actual code is prescribed when no errors occur, but people tend to use `http.StatusOK` just in case it bubbles out to the actual response.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,8 +687,8 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+	if err1, err2, DBError, statusCode := cg.updateCoordinate(); err != nil {
+		return err1, err2, DBError, statusCode

Review comment:
       This isn't checking any of the error values returned by `cg.updatedCoordinate()`, essentially ignoring them and only returning if `cg.getCoordinateID` failed earlier - and if that were true it wouldn't have gotten this far.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest

Review comment:
       This error does not always indicate that a CacheGroup by the given name already exists - and since it`s returning `errors.New("constant string")` and `tc.DBError`, the actual error that occurred is obscured so that looking into the logs won't tell you what went wrong.
   
   This error should be returned as a system error, and a check earlier in handling should look for a name collision before handling reaches this point.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c closed pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c closed pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064


   


-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r700006099



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`
+		result, err := cg.ReqInfo.Tx.Tx.Exec(q, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name)
+		if err != nil {
+			return fmt.Errorf("getting coordinate for cachegroup '%s': %s", *cg.Name, err.Error())

Review comment:
       Fixed.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`
+		result, err := cg.ReqInfo.Tx.Tx.Exec(q, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name)
+		if err != nil {
+			return fmt.Errorf("getting coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+		}
+		rowsAffected, _ := result.RowsAffected()

Review comment:
       Fixed.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683608428



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', getting rows affected: %s", *cg.Name, err.Error())
+			return nil, errors.New("updating coordinate for cachegroup , getting rows affected:" + *cg.Name + err.Error()), tc.DBError, http.StatusInternalServerError

Review comment:
       reverted




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683483088



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {

Review comment:
       I'm not sure what you mean. It used to have a single return value, which was an error; the `int*` is totally new




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r700319304



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,18 +699,25 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
+	err = cg.updateCoordinate()
+	if err != nil {
+		if err == ErrDuplicateExist {
+			return nil, err, tc.DBError, http.StatusBadRequest

Review comment:
       replaced the tc.DBError




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r699715744



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -37,11 +37,13 @@ import (
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 
-	"github.com/go-ozzo/ozzo-validation"
+	validation "github.com/go-ozzo/ozzo-validation"
 	"github.com/jmoiron/sqlx"
 	"github.com/lib/pq"
 )
 
+var ErrDuplicateExist = errors.New("cachegroup name already exist, give different name")

Review comment:
       This should not be exported

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`
+		result, err := cg.ReqInfo.Tx.Tx.Exec(q, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name)
+		if err != nil {
+			return fmt.Errorf("getting coordinate for cachegroup '%s': %s", *cg.Name, err.Error())

Review comment:
       `fmt.Errorf` formatting directive should be `%w` and consume the error, not be `%s` and consume the output of the error's `Error` method.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,18 +699,25 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
+	err = cg.updateCoordinate()
+	if err != nil {
+		if err == ErrDuplicateExist {

Review comment:
       To compare errors, [`errors.Is`](https://pkg.go.dev/errors#Is) should be used. 

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`

Review comment:
       Instead of checking the number of rows returned by this query, why not use a query that returns the number of rows? E.g.
   ```go
   const numberOfDuplicatesQuery = `
   SELECT COUNT(*)
   FROM public.coordinate
   WHERE id NOT IN (
       SELECT coordinate
       FROM public.cachegroup
       WHERE id = $1
   )
   AND name = $2
   `
   
   func (cg *TOCacheGroup) updateCoordinate() error {
       if cg.Latitude != nil && cg.Longitude != nil {
           var count uint
           if err := cg.ReqInfo.Tx.Tx.QueryRow(numberOfDuplicatesQuery, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name).Scan(&count); err != nil {
               return fmt.Errorf("getting coordinate for Cache Group '%s': %w", *cg.Name, err)
           }
           if count > 0 {
           // ... etc.
   ```

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -37,11 +37,13 @@ import (
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 
-	"github.com/go-ozzo/ozzo-validation"
+	validation "github.com/go-ozzo/ozzo-validation"
 	"github.com/jmoiron/sqlx"
 	"github.com/lib/pq"
 )
 
+var ErrDuplicateExist = errors.New("cachegroup name already exist, give different name")

Review comment:
       also, grammar nit:
   
   <del>exist</del> -> <ins>exists<ins>
   
   <del>give different name</del> -> e.g. <ins>please choose a different name</ins> or similar

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`
+		result, err := cg.ReqInfo.Tx.Tx.Exec(q, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name)
+		if err != nil {
+			return fmt.Errorf("getting coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+		}
+		rowsAffected, _ := result.RowsAffected()

Review comment:
       should not ignore the possible error here - in which case, `rowsAffected` is likely to be zero and will not be caught below.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,18 +699,25 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
+	err = cg.updateCoordinate()
+	if err != nil {
+		if err == ErrDuplicateExist {
+			return nil, err, tc.DBError, http.StatusBadRequest

Review comment:
       `tc.DBError` should never be used

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`

Review comment:
       also, query should specify `public` schema, as in my example




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r700006245



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`

Review comment:
       updated.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,18 +699,25 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
+	err = cg.updateCoordinate()
+	if err != nil {
+		if err == ErrDuplicateExist {

Review comment:
       updated.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683483088



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {

Review comment:
       I'm not sure what you mean. T used to have a single return value, which was an error; the `int*` is totally new




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683607962



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {

Review comment:
       I added the validation to check the existence of the cache group. So, I reverted back changes.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] ocket8888 commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
ocket8888 commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r681833500



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest

Review comment:
       This error does not always indicate that a CacheGroup by the given name already exists - and since it's returning `errors.New("constant string")` and `tc.DBError`, the actual error that occurred is obscured so that looking into the logs won't tell you what went wrong.
   
   This error should be returned as a system error, and a check earlier in handling should look for a name collision before handling reaches this point.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683608835



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,8 +687,8 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
-		return nil, nil, tc.DBError, http.StatusInternalServerError
+	if err1, err2, DBError, statusCode := cg.updateCoordinate(); err != nil {
+		return err1, err2, DBError, statusCode

Review comment:
       reverted.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r699868234



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -37,11 +37,13 @@ import (
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 
-	"github.com/go-ozzo/ozzo-validation"
+	validation "github.com/go-ozzo/ozzo-validation"
 	"github.com/jmoiron/sqlx"
 	"github.com/lib/pq"
 )
 
+var ErrDuplicateExist = errors.New("cachegroup name already exist, give different name")

Review comment:
       Fixed.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`
+		result, err := cg.ReqInfo.Tx.Tx.Exec(q, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name)
+		if err != nil {
+			return fmt.Errorf("getting coordinate for cachegroup '%s': %s", *cg.Name, err.Error())

Review comment:
       Fixed.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`
+		result, err := cg.ReqInfo.Tx.Tx.Exec(q, *cg.ID, tc.CachegroupCoordinateNamePrefix+*cg.Name)
+		if err != nil {
+			return fmt.Errorf("getting coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+		}
+		rowsAffected, _ := result.RowsAffected()

Review comment:
       Fixed.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -416,12 +418,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 
 func (cg *TOCacheGroup) updateCoordinate() error {
 	if cg.Latitude != nil && cg.Longitude != nil {
-		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
-		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
+		q := `SELECT * FROM coordinate WHERE id != (SELECT coordinate FROM cachegroup WHERE id = $1) AND name =$2`

Review comment:
       updated.

##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -687,18 +699,25 @@ func (cg *TOCacheGroup) handleCoordinateUpdate() (*int, error, error, int) {
 		return nil, nil, nil, http.StatusOK
 	}
 
-	if err = cg.updateCoordinate(); err != nil {
+	err = cg.updateCoordinate()
+	if err != nil {
+		if err == ErrDuplicateExist {

Review comment:
       updated.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r699868234



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -37,11 +37,13 @@ import (
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 
-	"github.com/go-ozzo/ozzo-validation"
+	validation "github.com/go-ozzo/ozzo-validation"
 	"github.com/jmoiron/sqlx"
 	"github.com/lib/pq"
 )
 
+var ErrDuplicateExist = errors.New("cachegroup name already exist, give different name")

Review comment:
       Fixed.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [trafficcontrol] dmohan001c commented on a change in pull request #6064: Fix Internal server error in update cachegroups by already exist cg name

Posted by GitBox <gi...@apache.org>.
dmohan001c commented on a change in pull request #6064:
URL: https://github.com/apache/trafficcontrol/pull/6064#discussion_r683608684



##########
File path: traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
##########
@@ -414,22 +414,22 @@ func (cg *TOCacheGroup) createCoordinate() (*int, error) {
 	return coordinateID, nil
 }
 
-func (cg *TOCacheGroup) updateCoordinate() error {
+func (cg *TOCacheGroup) updateCoordinate() (*int, error, error, int) {
 	if cg.Latitude != nil && cg.Longitude != nil {
 		q := `UPDATE coordinate SET name = $1, latitude = $2, longitude = $3 WHERE id = (SELECT coordinate FROM cachegroup WHERE id = $4)`
 		result, err := cg.ReqInfo.Tx.Tx.Exec(q, tc.CachegroupCoordinateNamePrefix+*cg.Name, *cg.Latitude, *cg.Longitude, *cg.ID)
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s': %s", *cg.Name, err.Error())
+			return nil, errors.New("cachegroup name already exist, give different name"), tc.DBError, http.StatusBadRequest
 		}
 		rowsAffected, err := result.RowsAffected()
 		if err != nil {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', getting rows affected: %s", *cg.Name, err.Error())
+			return nil, errors.New("updating coordinate for cachegroup , getting rows affected:" + *cg.Name + err.Error()), tc.DBError, http.StatusInternalServerError
 		}
 		if rowsAffected == 0 {
-			return fmt.Errorf("updating coordinate for cachegroup '%s', zero rows affected", *cg.Name)
+			return nil, errors.New("updating coordinate for cachegroup - Zero rows affected" + *cg.Name), tc.DBError, http.StatusInternalServerError

Review comment:
       reverted.




-- 
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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org