rawlinp commented on a change in pull request #3081: Fixes missing
deliveryservice data fields from the servers/deliveryservices endpoint.
URL: https://github.com/apache/trafficcontrol/pull/3081#discussion_r239275738
##########
File path: traffic_ops/traffic_ops_golang/deliveryservice/servers/servers.go
##########
@@ -472,106 +473,85 @@ func dssSelectQuery() string {
type TODSSDeliveryService struct {
ReqInfo *api.APIInfo `json:"-"`
- tc.DSSDeliveryService
+ tc.DeliveryServiceNullable
}
func (dss *TODSSDeliveryService) APIInfo() *api.APIInfo {
return dss.ReqInfo
}
func TypeSingleton(reqInfo *api.APIInfo) api.Reader {
- return &TODSSDeliveryService{reqInfo, tc.DSSDeliveryService{}}
+ return &TODSSDeliveryService{reqInfo, tc.DeliveryServiceNullable{}}
}
// Read shows all of the delivery services associated with the specified
server.
func (dss *TODSSDeliveryService) Read() ([]interface{}, error, error, int) {
- orderby := dss.APIInfo().Params["orderby"]
- if orderby == "" {
- orderby = "deliveryService"
+ returnable := []interface{}{}
+ params := make(map[string]string)
+ tx := dss.APIInfo().Tx.Tx
+ user := dss.APIInfo().User
+
+ if _, ok := params["orderby"]; !ok {
+ params["orderby"] = "xml_id"
+ }
+
+ // Query Parameters to Database Query column mappings
+ // see the fields mapped in the SQL query
+ queryParamsToSQLCols := map[string]dbhelpers.WhereColumnInfo{
+ "xml_id": dbhelpers.WhereColumnInfo{"ds.xml_id", nil},
+ "xmlId": dbhelpers.WhereColumnInfo{"ds.xml_id", nil},
+ }
+ where, orderBy, queryValues, errs :=
dbhelpers.BuildWhereAndOrderBy(params, queryParamsToSQLCols)
+ if len(errs) > 0 {
+ for _, err := range errs {
+ if err.Error() == `id cannot parse to integer` { //
TODO create const for string
+ return nil, errors.New("Resource not found."),
nil, http.StatusNotFound //matches perl response
+ }
+ }
+ return nil, nil, errors.New("reading dses: " +
util.JoinErrsStr(errs)), http.StatusInternalServerError
}
- query := SDSSelectQuery()
- log.Debugln("Query is ", query)
-
- rows, err := dss.APIInfo().Tx.Queryx(query, dss.APIInfo().Params["id"])
+ tenancyEnabled, err := tenant.IsTenancyEnabledTx(tx)
if err != nil {
- log.Errorf("Error querying DeliveryserviceServers: %v", err)
- return nil, nil, errors.New("dss querying: " + err.Error()),
http.StatusInternalServerError
+ log.Errorln("checking if tenancy is enabled: " + err.Error())
+ return nil, nil, err, http.StatusInternalServerError
}
- defer rows.Close()
+ if where != "" {
+ where = where + " AND "
+ } else {
+ where = "WHERE "
+ }
+ where += "ds.id in (SELECT deliveryService FROM deliveryservice_server
where server = :server)"
- services := []interface{}{}
- for rows.Next() {
- var s tc.DSSDeliveryService
- if err = rows.StructScan(&s); err != nil {
- return nil, nil, errors.New("dss scanning: " +
err.Error()), http.StatusInternalServerError
+ if tenancyEnabled {
+ log.Debugln("Tenancy is enabled")
+ tenantIDs, err := tenant.GetUserTenantIDListTx(tx,
user.TenantID)
+ if err != nil {
+ log.Errorln("received error querying for user's
tenants: " + err.Error())
+ return nil, nil, err, http.StatusInternalServerError
}
- services = append(services, s)
+ where, queryValues = dbhelpers.AddTenancyCheck(where,
queryValues, "ds.tenant_id", tenantIDs)
}
- return services, nil, nil, http.StatusOK
-}
+ query := deliveryservice.GetDSSelectQuery() + where + orderBy
+ queryValues["server"] = dss.APIInfo().Params["id"]
+ log.Debugln("generated deliveryServices query: " + query)
+ log.Debugf("executing with values: %++v\n", queryValues)
-func SDSSelectQuery() string {
+ dses, errs, _ := deliveryservice.GetDeliveryServices(query,
queryValues, dss.APIInfo().Tx)
+ if len(errs) > 0 {
+ for _, err := range errs {
+ if err.Error() == `id cannot parse to integer` { //
TODO create const for string
Review comment:
Does this check really need to be made again here? Is it coming from the
`BuildWhereAndOrderBy` function?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services