You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by rs...@apache.org on 2023/03/10 19:57:32 UTC

[trafficcontrol] branch master updated: Fixes service_categories response to POST API (#7383)

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

rshah 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 5b1fa0b938 Fixes service_categories response to POST API (#7383)
5b1fa0b938 is described below

commit 5b1fa0b9381ee6a378001a8bad334d9d749c6679
Author: Jagan Parthiban <33...@users.noreply.github.com>
AuthorDate: Sat Mar 11 01:27:24 2023 +0530

    Fixes service_categories response to POST API (#7383)
    
    * Initial commit to fix bug https://github.com/apache/trafficcontrol/issues/7130
    
    Updated GenericCreateNameBasedID in api/generic_crud.go and used it in Create() function traffic_ops_golang/servicecategory.go
    
    * #7130 - Updated GenericCreateNameBasedID to server serviceCategories while not breaking other functionalities that existed earlier.
    
    * Updated CHANGELOG.md
    
    * Updated generic_crud.go's GenericCreateNameBasedID
    
    * Removed code repetition in generic_crud.go's GenericCreateNameBasedID
---
 CHANGELOG.md                                          |  1 +
 traffic_ops/traffic_ops_golang/api/generic_crud.go    | 19 ++++++++++++++++++-
 .../servicecategory/servicecategories.go              |  2 +-
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7648fb7f6e..d781932e2a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -49,6 +49,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#7386](https://github.com/apache/trafficcontrol/pull/7386) *Traffic Portal* Increased the number of events that are logged to the TP access log.
 
 ### Fixed
+- [#7130](https://github.com/apache/trafficcontrol/issues/7130) *Traffic Ops* Fixes service_categories response to POST API.
 - [#7340](https://github.com/apache/trafficcontrol/pull/7340) *Traffic Router* Fixed TR logging for the `cqhv` field when absent.
 - [#5557](https://github.com/apache/trafficcontrol/issues/5557) *Traffic Portal* Moved `Fair Queueing Pacing Rate Bps` DS field to `Cache Configuration Settings` section.
 - [#7252](https://github.com/apache/trafficcontrol/issues/7252) *Traffic Router* Fixed integer overflow for `czCount`, by resetting the count to max value when it overflows.
diff --git a/traffic_ops/traffic_ops_golang/api/generic_crud.go b/traffic_ops/traffic_ops_golang/api/generic_crud.go
index 2ad292d6d7..f1cf640e78 100644
--- a/traffic_ops/traffic_ops_golang/api/generic_crud.go
+++ b/traffic_ops/traffic_ops_golang/api/generic_crud.go
@@ -119,19 +119,36 @@ func GenericCreateNameBasedID(val GenericCreator) (error, error, int) {
 	}
 	defer resultRows.Close()
 
+	var name string
 	lastUpdated := tc.TimeNoMod{}
 	rowsAffected := 0
+
 	for resultRows.Next() {
 		rowsAffected++
-		if err := resultRows.Scan(&lastUpdated); err != nil {
+		// Only when the type is of serviceCategory, &name is scanned and returned from the DB.
+		// Else return only &lastUpdated.
+		var err error
+		if val.GetType() == "serviceCategory" {
+			err = resultRows.Scan(&name, &lastUpdated)
+		} else {
+			err = resultRows.Scan(&lastUpdated)
+		}
+		if err != nil {
 			return nil, errors.New(val.GetType() + " create scanning: " + err.Error()), http.StatusInternalServerError
 		}
 	}
+
 	if rowsAffected == 0 {
 		return nil, errors.New(val.GetType() + " create: no " + val.GetType() + " was inserted, no row was returned"), http.StatusInternalServerError
 	} else if rowsAffected > 1 {
 		return nil, errors.New("too many rows returned from " + val.GetType() + " insert"), http.StatusInternalServerError
 	}
+
+	// Only when the type is of serviceCategory, setKeys to return name parameter.
+	if val.GetType() == "serviceCategory" {
+		val.SetKeys(map[string]interface{}{"name": name})
+	}
+
 	val.SetLastUpdated(lastUpdated)
 	return nil, nil, http.StatusOK
 }
diff --git a/traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go b/traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go
index 79e85e1b7d..8201de1fd5 100644
--- a/traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go
+++ b/traffic_ops/traffic_ops_golang/servicecategory/servicecategories.go
@@ -100,7 +100,7 @@ func (serviceCategory TOServiceCategory) Validate() (error, error) {
 }
 
 func (serviceCategory *TOServiceCategory) Create() (error, error, int) {
-	return api.GenericCreate(serviceCategory)
+	return api.GenericCreateNameBasedID(serviceCategory)
 }
 
 func (serviceCategory *TOServiceCategory) Read(h http.Header, useIMS bool) ([]interface{}, error, error, int, *time.Time) {