You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/12/06 21:43:15 UTC

[GitHub] rawlinp closed pull request #3081: Fixes missing deliveryservice data fields from the servers/deliveryservices endpoint.

rawlinp closed pull request #3081: Fixes missing deliveryservice data fields from the servers/deliveryservices endpoint.
URL: https://github.com/apache/trafficcontrol/pull/3081
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/lib/go-tc/deliveryservice_servers.go b/lib/go-tc/deliveryservice_servers.go
index 4bf023ee3..65652a1bb 100644
--- a/lib/go-tc/deliveryservice_servers.go
+++ b/lib/go-tc/deliveryservice_servers.go
@@ -55,6 +55,7 @@ type DeliveryServiceServer struct {
 }
 
 type Filter int
+
 const (
 	Assigned Filter = iota
 	Unassigned
@@ -62,7 +63,7 @@ const (
 )
 
 type DSServersAttrResponse struct {
-	Response []DSServer 	`json:"response"`
+	Response []DSServer `json:"response"`
 }
 
 type DSServer struct {
@@ -110,63 +111,3 @@ type DSServer struct {
 	TypeID           *int                 `json:"typeId" db:"server_type_id"`
 	UpdPending       *bool                `json:"updPending" db:"upd_pending"`
 }
-
-// DSSDeliveryService is a representation of a deliveryservice that allows for all fields to be null
-type DSSDeliveryService struct {
-	// NOTE: the db: struct tags are used for testing to map to their equivalent database column (if there is one)
-	//
-	Active               *bool            `json:"active" db:"active"`
-	CacheURL             *string          `json:"cacheurl" db:"cacheurl"`
-	CCRDNSTTL            *int             `json:"ccrDnsTtl" db:"ccr_dns_ttl"`
-	CDNID                *int             `json:"cdnId" db:"cdn_id"`
-	CheckPath            *string          `json:"checkPath" db:"check_path"`
-	DeepCachingType      *DeepCachingType `json:"deepCachingType" db:"deep_caching_type"`
-	DisplayName          *string          `json:"displayName" db:"display_name"`
-	DNSBypassCNAME       *string          `json:"dnsBypassCname" db:"dns_bypass_cname"`
-	DNSBypassIP          *string          `json:"dnsBypassIp" db:"dns_bypass_ip"`
-	DNSBypassIP6         *string          `json:"dnsBypassIp6" db:"dns_bypass_ip6"`
-	DNSBypassTTL         *int             `json:"dnsBypassTtl" db:"dns_bypass_ttl"`
-	DSCP                 *int             `json:"dscp" db:"dscp"`
-	EdgeHeaderRewrite    *string          `json:"edgeHeaderRewrite" db:"edge_header_rewrite"`
-	FQPacingRate         *int             `json:"fqPacingRate" db:"fq_pacing_rate"`
-	GeoLimit             *int             `json:"geoLimit" db:"geo_limit"`
-	GeoLimitCountries    *string          `json:"geoLimitCountries" db:"geo_limit_countries"`
-	GeoLimitRedirectURL  *string          `json:"geoLimitRedirectURL" db:"geolimit_redirect_url"`
-	GeoProvider          *int             `json:"geoProvider" db:"geo_provider"`
-	GlobalMaxMBPS        *int             `json:"globalMaxMbps" db:"global_max_mbps"`
-	GlobalMaxTPS         *int             `json:"globalMaxTps" db:"global_max_tps"`
-	HTTPBypassFQDN       *string          `json:"httpBypassFqdn" db:"http_bypass_fqdn"`
-	ID                   *int             `json:"id" db:"id"`
-	InfoURL              *string          `json:"infoUrl" db:"info_url"`
-	InitialDispersion    *int             `json:"initialDispersion" db:"initial_dispersion"`
-	IPV6RoutingEnabled   *bool            `json:"ipv6RoutingEnabled" db:"ipv6_routing_enabled"`
-	LastUpdated          *TimeNoMod       `json:"lastUpdated" db:"last_updated"`
-	LogsEnabled          *bool            `json:"logsEnabled" db:"logs_enabled"`
-	LongDesc             *string          `json:"longDesc" db:"long_desc"`
-	LongDesc1            *string          `json:"longDesc1" db:"long_desc_1"`
-	LongDesc2            *string          `json:"longDesc2" db:"long_desc_2"`
-	MaxDNSAnswers        *int             `json:"maxDnsAnswers" db:"max_dns_answers"`
-	MidHeaderRewrite     *string          `json:"midHeaderRewrite" db:"mid_header_rewrite"`
-	MissLat              *float64         `json:"missLat" db:"miss_lat"`
-	MissLong             *float64         `json:"missLong" db:"miss_long"`
-	MultiSiteOrigin      *bool            `json:"multiSiteOrigin" db:"multi_site_origin"`
-	MultiSiteOriginAlgo  *int             `json:"multiSiteOriginAlgo" db:"multi_site_origin_algorithm"`
-	OriginShield         *string          `json:"originShield" db:"origin_shield"`
-	OrgServerFQDN        *string          `json:"orgServerFqdn" db:"org_server_fqdn"`
-	ProfileDesc          *string          `json:"profileDescription"`
-	ProfileID            *int             `json:"profileId" db:"profile"`
-	Protocol             *int             `json:"protocol" db:"protocol"`
-	QStringIgnore        *int             `json:"qstringIgnore" db:"qstring_ignore"`
-	RangeRequestHandling *int             `json:"rangeRequestHandling" db:"range_request_handling"`
-	RegexRemap           *string          `json:"regexRemap" db:"regex_remap"`
-	RegionalGeoBlocking  *bool            `json:"regionalGeoBlocking" db:"regional_geo_blocking"`
-	RemapText            *string          `json:"remapText" db:"remap_text"`
-	RoutingName          *string          `json:"routingName" db:"routing_name"`
-	SigningAlgorithm     *string          `json:"signingAlgorithm" db:"signing_algorithm"`
-	SSLKeyVersion        *int             `json:"sslKeyVersion" db:"ssl_key_version"`
-	TRRequestHeaders     *string          `json:"trRequestHeaders" db:"tr_request_headers"`
-	TRResponseHeaders    *string          `json:"trResponseHeaders" db:"tr_response_headers"`
-	TenantID             *int             `json:"tenantId" db:"tenant_id"`
-	TypeID               *int             `json:"typeId" db:"type"`
-	XMLID                *string          `json:"xmlId" db:"xml_id"`
-}
diff --git a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
index 1ef12b705..1bfdc160f 100644
--- a/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
+++ b/traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
@@ -543,6 +543,10 @@ func readGetDeliveryServices(params map[string]string, tx *sqlx.Tx, user *auth.C
 	if strings.HasSuffix(params["id"], ".json") {
 		params["id"] = params["id"][:len(params["id"])-len(".json")]
 	}
+	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{
@@ -557,10 +561,6 @@ func readGetDeliveryServices(params map[string]string, tx *sqlx.Tx, user *auth.C
 		"signingAlgorithm": dbhelpers.WhereColumnInfo{"ds.signing_algorithm", nil},
 	}
 
-	if _, ok := params["orderby"]; !ok {
-		params["orderby"] = "xml_id"
-	}
-
 	where, orderBy, queryValues, errs := dbhelpers.BuildWhereAndOrderBy(params, queryParamsToSQLCols)
 	if len(errs) > 0 {
 		return nil, errs, tc.DataConflictError
@@ -586,6 +586,10 @@ func readGetDeliveryServices(params map[string]string, tx *sqlx.Tx, user *auth.C
 	log.Debugln("generated deliveryServices query: " + query)
 	log.Debugf("executing with values: %++v\n", queryValues)
 
+	return GetDeliveryServices(query, queryValues, tx)
+}
+
+func GetDeliveryServices(query string, queryValues map[string]interface{}, tx *sqlx.Tx) ([]tc.DeliveryServiceNullable, []error, tc.ApiErrorType) {
 	rows, err := tx.NamedQuery(query, queryValues)
 	if err != nil {
 		return nil, []error{fmt.Errorf("querying: %v", err)}, tc.SystemError
@@ -940,6 +944,11 @@ func deleteLocationParam(tx *sql.Tx, configFile string) error {
 	return nil
 }
 
+// export the selectQuery for the 'servers' package.
+func GetDSSelectQuery() string {
+	return selectQuery()
+}
+
 func selectQuery() string {
 	return `
 SELECT
diff --git a/traffic_ops/traffic_ops_golang/deliveryservice/servers/servers.go b/traffic_ops/traffic_ops_golang/deliveryservice/servers/servers.go
index 3f2b88a9c..1a0e88e81 100644
--- a/traffic_ops/traffic_ops_golang/deliveryservice/servers/servers.go
+++ b/traffic_ops/traffic_ops_golang/deliveryservice/servers/servers.go
@@ -33,6 +33,7 @@ import (
 	"github.com/apache/trafficcontrol/lib/go-util"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice"
 	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/tenant"
 
@@ -472,7 +473,7 @@ func dssSelectQuery() string {
 
 type TODSSDeliveryService struct {
 	ReqInfo *api.APIInfo `json:"-"`
-	tc.DSSDeliveryService
+	tc.DeliveryServiceNullable
 }
 
 func (dss *TODSSDeliveryService) APIInfo() *api.APIInfo {
@@ -480,98 +481,63 @@ func (dss *TODSSDeliveryService) APIInfo() *api.APIInfo {
 }
 
 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 := dss.APIInfo().Params
+	tx := dss.APIInfo().Tx.Tx
+	user := dss.APIInfo().User
+
+	if err := api.IsInt(params["id"]); err != nil {
+		return nil, errors.New("Resource not found."), nil, http.StatusNotFound //matches perl response
 	}
 
-	query := SDSSelectQuery()
-	log.Debugln("Query is ", query)
+	if _, ok := params["orderby"]; !ok {
+		params["orderby"] = "xml_id"
+	}
 
-	rows, err := dss.APIInfo().Tx.Queryx(query, dss.APIInfo().Params["id"])
-	if err != nil {
-		log.Errorf("Error querying DeliveryserviceServers: %v", err)
-		return nil, nil, errors.New("dss querying: " + err.Error()), http.StatusInternalServerError
+	// 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 {
+		return nil, nil, errors.New("reading server dses: " + util.JoinErrsStr(errs)), http.StatusInternalServerError
 	}
-	defer rows.Close()
 
-	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
-		}
-		services = append(services, s)
+	if where != "" {
+		where = where + " AND "
+	} else {
+		where = "WHERE "
 	}
+	where += "ds.id in (SELECT deliveryService FROM deliveryservice_server where server = :server)"
 
-	return services, nil, nil, http.StatusOK
-}
+	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
+	}
+	where, queryValues = dbhelpers.AddTenancyCheck(where, queryValues, "ds.tenant_id", tenantIDs)
 
-func SDSSelectQuery() string {
+	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)
 
-	selectStmt := `SELECT
- 		active,
-		ccr_dns_ttl,
-		cdn_id,
-		cacheurl,
-		check_path,
-		dns_bypass_cname,
-		dns_bypass_ip,
-		dns_bypass_ip6,
-		dns_bypass_ttl,
-		dscp,
-		display_name,
-		edge_header_rewrite,
-		geo_limit,
-		geo_limit_countries,
-		geolimit_redirect_url,
-		geo_provider,
-		global_max_mbps,
-		global_max_tps,
-		http_bypass_fqdn,
-		id,
-		ipv6_routing_enabled,
-		info_url,
-		initial_dispersion,
-		last_updated,
-		logs_enabled,
-		long_desc,
-		long_desc_1,
-		long_desc_2,
-		max_dns_answers,
-		mid_header_rewrite,
-		miss_lat,
-		miss_long,
-		multi_site_origin,
-		multi_site_origin_algorithm,
-		(SELECT o.protocol::text || '://' || o.fqdn || rtrim(concat(':', o.port::text), ':')
-		FROM origin o
-		WHERE o.deliveryservice = d.id
-		AND o.is_primary) as org_server_fqdn,
-		origin_shield,
-		profile,
-		protocol,
-		qstring_ignore,
-		range_request_handling,
-		regex_remap,
-		regional_geo_blocking,
-		remap_text,
-		routing_name,
-		ssl_key_version,
-		signing_algorithm,
-		tr_request_headers,
-		tr_response_headers,
-		tenant_id,
-		type,
-		xml_id
-	FROM deliveryservice d
-		WHERE id in (SELECT deliveryService FROM deliveryservice_server where server = $1)`
-	return selectStmt
+	dses, errs, _ := deliveryservice.GetDeliveryServices(query, queryValues, dss.APIInfo().Tx)
+	if len(errs) > 0 {
+		return nil, nil, errors.New("reading server dses: " + util.JoinErrsStr(errs)), http.StatusInternalServerError
+	}
+
+	for _, ds := range dses {
+		returnable = append(returnable, ds)
+	}
+	return returnable, nil, nil, http.StatusOK
 }
 
 func updateQuery() string {


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services