Skip to content

Commit

Permalink
https://github.com/apache/trafficcontrol/issues/7413
Browse files Browse the repository at this point in the history
Removing dependency of service catergory on generic cruder for V5 version
  • Loading branch information
jagan-parthiban committed Mar 24, 2023
1 parent c1f5e13 commit 07f716f
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 144 deletions.
51 changes: 0 additions & 51 deletions traffic_ops/traffic_ops_golang/api/generic_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ type GenericCreator interface {
InsertQuery() string
}

// GenericCreatorV5 uses time.Time as Time format type for SetLastUpdated
type GenericCreatorV5 interface {
GetType() string
APIInfo() *APIInfo
SetKeys(map[string]interface{})
SetLastUpdated(time.Time)
InsertQuery() string
}

type GenericReader interface {
GetType() string
APIInfo() *APIInfo
Expand Down Expand Up @@ -162,48 +153,6 @@ func GenericCreateNameBasedID(val GenericCreator) (error, error, int) {
return nil, nil, http.StatusOK
}

// GenericCreateNameBasedIDV5 does a Create (POST) for the given GenericCreatorV5 object and type. This exists as a generic function, for the use case of a single "name" key (not a numerical "id" key) and a lastUpdated field.
func GenericCreateNameBasedIDV5(val GenericCreatorV5) (error, error, int) {
resultRows, err := val.APIInfo().Tx.NamedQuery(val.InsertQuery(), val)
if err != nil {
return ParseDBError(err)
}
defer resultRows.Close()

var name string
lastUpdated := time.Time{}
rowsAffected := 0

for resultRows.Next() {
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() == "serviceCategoryV5" {
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() == "serviceCategoryV5" {
val.SetKeys(map[string]interface{}{"name": name})
}

val.SetLastUpdated(lastUpdated)
return nil, nil, http.StatusOK
}

// TryIfModifiedSinceQuery checks to see the max time that an entity was changed, and then returns a boolean (which tells us whether or not to run the main query for the endpoint)
// along with the max time
// If the returned boolean is false, there is no need to run the main query for the GET API endpoint, and we return a 304 status
Expand Down
15 changes: 15 additions & 0 deletions traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2187,3 +2187,18 @@ func GetSCInfo(tx *sql.Tx, name string) (bool, error) {
}
return true, nil
}

// GetServiceCategoryInfo confirms whether the service category exists, and an error (if one occurs).
func GetServiceCategoryInfo(tx *sql.Tx, name string) (bool, error) {
var count int
if err := tx.QueryRow("SELECT count(name) FROM service_category AS sc WHERE sc.name=$1", name).Scan(&count); err != nil {
return false, fmt.Errorf("error getting service category info: %w", err)
}
if count == 0 {
return false, nil
}
if count != 1 {
return false, fmt.Errorf("getting service category info - expected row count: 1, actual: %d", count)
}
return true, nil
}
8 changes: 4 additions & 4 deletions traffic_ops/traffic_ops_golang/routing/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodDelete, Path: `deliveryservices/{dsid}/regexes/{regexid}?$`, Handler: deliveryservicesregexes.Delete, RequiredPrivLevel: auth.PrivLevelOperations, RequiredPermissions: []string{"DELIVERY-SERVICE:UPDATE", "DELIVERY-SERVICE:READ", "TYPE:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 424673166331},

//ServiceCategories
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodGet, Path: `service_categories/?$`, Handler: api.ReadHandler(&servicecategory.TOServiceCategoryV5{}), RequiredPrivLevel: auth.PrivLevelReadOnly, RequiredPermissions: []string{"SERVICE-CATEGORY:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 40851815431},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodPut, Path: `service_categories/{name}/?$`, Handler: servicecategory.UpdateV5, RequiredPrivLevel: auth.PrivLevelOperations, RequiredPermissions: []string{"SERVICE-CATEGORY:UPDATE", "SERVICE-CATEGORY:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 4063691411},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodPost, Path: `service_categories/?$`, Handler: api.CreateHandler(&servicecategory.TOServiceCategoryV5{}), RequiredPrivLevel: auth.PrivLevelOperations, RequiredPermissions: []string{"SERVICE-CATEGORY:CREATE", "SERVICE-CATEGORY:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 4537138011},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodDelete, Path: `service_categories/{name}$`, Handler: api.DeleteHandler(&servicecategory.TOServiceCategoryV5{}), RequiredPrivLevel: auth.PrivLevelOperations, RequiredPermissions: []string{"SERVICE-CATEGORY:DELETE", "SERVICE-CATEGORY:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 43253822381},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodGet, Path: `service_categories/?$`, Handler: servicecategory.GetServiceCategory, RequiredPrivLevel: auth.PrivLevelReadOnly, RequiredPermissions: []string{"SERVICE-CATEGORY:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 40851815431},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodPut, Path: `service_categories/{name}/?$`, Handler: servicecategory.UpdateServiceCategory, RequiredPrivLevel: auth.PrivLevelOperations, RequiredPermissions: []string{"SERVICE-CATEGORY:UPDATE", "SERVICE-CATEGORY:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 4063691411},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodPost, Path: `service_categories/?$`, Handler: servicecategory.CreateServiceCategory, RequiredPrivLevel: auth.PrivLevelOperations, RequiredPermissions: []string{"SERVICE-CATEGORY:CREATE", "SERVICE-CATEGORY:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 4537138011},
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodDelete, Path: `service_categories/{name}$`, Handler: servicecategory.DeleteServiceCategory, RequiredPrivLevel: auth.PrivLevelOperations, RequiredPermissions: []string{"SERVICE-CATEGORY:DELETE", "SERVICE-CATEGORY:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 43253822381},

//StaticDNSEntries
{Version: api.Version{Major: 5, Minor: 0}, Method: http.MethodGet, Path: `staticdnsentries/?$`, Handler: api.ReadHandler(&staticdnsentry.TOStaticDNSEntry{}), RequiredPrivLevel: auth.PrivLevelReadOnly, RequiredPermissions: []string{"STATIC-DN:READ", "CACHE-GROUP:READ", "DELIVERY-SERVICE:READ"}, Authenticated: Authenticated, Middlewares: nil, ID: 42893947731},
Expand Down
Loading

0 comments on commit 07f716f

Please sign in to comment.