diff --git a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go index 4d9d6a4fc2..ae5ae494fd 100644 --- a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go +++ b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities.go @@ -254,7 +254,12 @@ func (rc *RequiredCapability) Create() (error, error, int) { return errors.New("Invalid DS type. Only DNS and HTTP delivery services can have required capabilities"), nil, http.StatusBadRequest } - usrErr, sysErr, rCode := rc.ensureDSServerCap() + usrErr, sysErr, rCode := rc.checkServerCap() + if usrErr != nil || sysErr != nil { + return usrErr, sysErr, rCode + } + + usrErr, sysErr, rCode = rc.ensureDSServerCap() if usrErr != nil || sysErr != nil { return usrErr, sysErr, rCode } @@ -281,6 +286,25 @@ func (rc *RequiredCapability) Create() (error, error, int) { return nil, nil, http.StatusOK } +func (rc *RequiredCapability) checkServerCap() (error, error, int) { + tx := rc.APIInfo().Tx + + // Get server capability name + name := "" + if err := tx.QueryRow(` + SELECT name + FROM server_capability + WHERE name = $1`, rc.RequiredCapability).Scan(&name); err != nil && err != sql.ErrNoRows{ + return nil, fmt.Errorf("querying server capability for name '%v': %v", rc.RequiredCapability, err), http.StatusInternalServerError + } + + if len(name) == 0 { + return fmt.Errorf("server_capability not found"), nil, http.StatusNotFound + } + + return nil, nil, http.StatusOK +} + func (rc *RequiredCapability) ensureDSServerCap() (error, error, int) { tx := rc.APIInfo().Tx diff --git a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities_test.go b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities_test.go index c3b83b300d..85a5fad024 100644 --- a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities_test.go +++ b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices_required_capabilities_test.go @@ -82,6 +82,11 @@ func TestCreateDeliveryServicesRequiredCapability(t *testing.T) { ) mock.ExpectQuery("SELECT t.name FROM deliveryservice as ds").WillReturnRows(typeRows) + scRows := sqlmock.NewRows([]string{"name"}).AddRow( + "mem", + ) + mock.ExpectQuery("SELECT name FROM server_capability").WillReturnRows(scRows) + arrayRows := sqlmock.NewRows([]string{"array"}) mock.ExpectQuery("SELECT ds.server FROM deliveryservice_server").WillReturnRows(arrayRows)