From ab5790c65ec2595dfd99d0200c0e67ed2b95f4af Mon Sep 17 00:00:00 2001 From: Jagan Parthiban Date: Tue, 7 Mar 2023 20:16:35 +0530 Subject: [PATCH 1/5] 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 --- traffic_ops/traffic_ops_golang/api/generic_crud.go | 8 +++++--- .../servicecategory/servicecategories.go | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/api/generic_crud.go b/traffic_ops/traffic_ops_golang/api/generic_crud.go index 2ad292d6d7..b6fa4dfd09 100644 --- a/traffic_ops/traffic_ops_golang/api/generic_crud.go +++ b/traffic_ops/traffic_ops_golang/api/generic_crud.go @@ -119,19 +119,21 @@ func GenericCreateNameBasedID(val GenericCreator) (error, error, int) { } defer resultRows.Close() + var name interface{} lastUpdated := tc.TimeNoMod{} rowsAffected := 0 for resultRows.Next() { rowsAffected++ - if err := resultRows.Scan(&lastUpdated); err != nil { + if err := resultRows.Scan(&name, &lastUpdated); 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 + return nil, errors.New(val.GetType() + " create: no " + val.GetType() + " was inserted, no name was returned"), http.StatusInternalServerError } else if rowsAffected > 1 { - return nil, errors.New("too many rows returned from " + val.GetType() + " insert"), http.StatusInternalServerError + return nil, errors.New("too many names returned from " + val.GetType() + " insert"), http.StatusInternalServerError } + 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) { From 85adb99ad26924a2028c4590ee20249baba32a53 Mon Sep 17 00:00:00 2001 From: Jagan Parthiban Date: Wed, 8 Mar 2023 11:42:17 +0530 Subject: [PATCH 2/5] #7130 - Updated GenericCreateNameBasedID to server serviceCategories while not breaking other functionalities that existed earlier. --- .../traffic_ops_golang/api/generic_crud.go | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/api/generic_crud.go b/traffic_ops/traffic_ops_golang/api/generic_crud.go index b6fa4dfd09..5517070791 100644 --- a/traffic_ops/traffic_ops_golang/api/generic_crud.go +++ b/traffic_ops/traffic_ops_golang/api/generic_crud.go @@ -119,21 +119,39 @@ func GenericCreateNameBasedID(val GenericCreator) (error, error, int) { } defer resultRows.Close() - var name interface{} + var name string lastUpdated := tc.TimeNoMod{} rowsAffected := 0 - for resultRows.Next() { - rowsAffected++ - if err := resultRows.Scan(&name, &lastUpdated); err != nil { - return nil, errors.New(val.GetType() + " create scanning: " + err.Error()), http.StatusInternalServerError + + // Only when the type is of serviceCategory, &name is scanned and returned from the DB. + // Else return only &lastUpdated. + if val.GetType() == "serviceCategory" { + for resultRows.Next() { + rowsAffected++ + if err := resultRows.Scan(&name, &lastUpdated); err != nil { + return nil, errors.New(val.GetType() + " create scanning: " + err.Error()), http.StatusInternalServerError + } + } + } else { + for resultRows.Next() { + rowsAffected++ + if err := resultRows.Scan(&lastUpdated); 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 name was returned"), http.StatusInternalServerError + 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 names returned from " + val.GetType() + " insert"), http.StatusInternalServerError + return nil, errors.New("too many rows returned from " + val.GetType() + " insert"), http.StatusInternalServerError } - val.SetKeys(map[string]interface{}{"name": name}) + + // 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 } From 8bd5644a2409fee907d5084871dc488522ec3346 Mon Sep 17 00:00:00 2001 From: Jagan Parthiban Date: Thu, 9 Mar 2023 00:53:23 +0530 Subject: [PATCH 3/5] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 503305553c..f6d1d6eed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - [#7366](https://github.com/apache/trafficcontrol/pull/7366) *t3c* Removed timestamp from metadata file since it's changing every minute and causing excessive commits to git repo. ### 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. From 51d59c5905f35ad94d6d7b3b0bd831f9b2053372 Mon Sep 17 00:00:00 2001 From: Jagan Parthiban Date: Fri, 10 Mar 2023 23:52:22 +0530 Subject: [PATCH 4/5] Updated generic_crud.go's GenericCreateNameBasedID --- .../traffic_ops_golang/api/generic_crud.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/api/generic_crud.go b/traffic_ops/traffic_ops_golang/api/generic_crud.go index 5517070791..4c6cec6891 100644 --- a/traffic_ops/traffic_ops_golang/api/generic_crud.go +++ b/traffic_ops/traffic_ops_golang/api/generic_crud.go @@ -123,21 +123,19 @@ func GenericCreateNameBasedID(val GenericCreator) (error, error, int) { lastUpdated := tc.TimeNoMod{} rowsAffected := 0 - // Only when the type is of serviceCategory, &name is scanned and returned from the DB. - // Else return only &lastUpdated. - if val.GetType() == "serviceCategory" { - for resultRows.Next() { - rowsAffected++ + for resultRows.Next() { + rowsAffected++ + // Only when the type is of serviceCategory, &name is scanned and returned from the DB. + // Else return only &lastUpdated. + if val.GetType() == "serviceCategory" { if err := resultRows.Scan(&name, &lastUpdated); err != nil { return nil, errors.New(val.GetType() + " create scanning: " + err.Error()), http.StatusInternalServerError } - } - } else { - for resultRows.Next() { - rowsAffected++ + } else { if err := resultRows.Scan(&lastUpdated); err != nil { return nil, errors.New(val.GetType() + " create scanning: " + err.Error()), http.StatusInternalServerError } + } } From 7ebfa19be3611c9ada8dde670d6e02ec1772cc49 Mon Sep 17 00:00:00 2001 From: Jagan Parthiban Date: Sat, 11 Mar 2023 00:27:34 +0530 Subject: [PATCH 5/5] Removed code repetition in generic_crud.go's GenericCreateNameBasedID --- traffic_ops/traffic_ops_golang/api/generic_crud.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/traffic_ops/traffic_ops_golang/api/generic_crud.go b/traffic_ops/traffic_ops_golang/api/generic_crud.go index 4c6cec6891..f1cf640e78 100644 --- a/traffic_ops/traffic_ops_golang/api/generic_crud.go +++ b/traffic_ops/traffic_ops_golang/api/generic_crud.go @@ -127,15 +127,14 @@ func GenericCreateNameBasedID(val GenericCreator) (error, error, int) { rowsAffected++ // 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" { - if err := resultRows.Scan(&name, &lastUpdated); err != nil { - return nil, errors.New(val.GetType() + " create scanning: " + err.Error()), http.StatusInternalServerError - } + err = resultRows.Scan(&name, &lastUpdated) } else { - if err := resultRows.Scan(&lastUpdated); err != nil { - return nil, errors.New(val.GetType() + " create scanning: " + err.Error()), http.StatusInternalServerError - } - + err = resultRows.Scan(&lastUpdated) + } + if err != nil { + return nil, errors.New(val.GetType() + " create scanning: " + err.Error()), http.StatusInternalServerError } }