rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes URL: https://github.com/apache/incubator-trafficcontrol/pull/2124#discussion_r186202885
########## File path: traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv12.go ########## @@ -0,0 +1,326 @@ +package deliveryservice + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "database/sql" + "errors" + "fmt" + "regexp" + "strings" + + "github.com/apache/incubator-trafficcontrol/lib/go-tc" + "github.com/apache/incubator-trafficcontrol/lib/go-log" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/config" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tenant" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tovalidate" + + "github.com/asaskevich/govalidator" + "github.com/go-ozzo/ozzo-validation" + "github.com/jmoiron/sqlx" +) + +type TODeliveryServiceV12 struct { + DS *tc.DeliveryServiceNullableV12 + Cfg config.Config + DB *sqlx.DB +} + +func GetRefTypeV12(cfg config.Config, db *sqlx.DB) *TODeliveryServiceV12 { + return &TODeliveryServiceV12{Cfg: cfg, DB: db, DS: &tc.DeliveryServiceNullableV12{}} +} + +func (ds TODeliveryServiceV12) GetKeyFieldsInfo() []api.KeyFieldInfo { + return []api.KeyFieldInfo{{"id", api.GetIntKey}} +} + +func (tods TODeliveryServiceV12) GetKeys() (map[string]interface{}, bool) { + if tods.DS.ID == nil { + return map[string]interface{}{"id": 0}, false + } + return map[string]interface{}{"id": *tods.DS.ID}, true +} + +func (tods *TODeliveryServiceV12) SetKeys(keys map[string]interface{}) { + i, _ := keys["id"].(int) //this utilizes the non panicking type assertion, if the thrown away ok variable is false i will be the zero of the type, 0 here. + tods.DS.ID = &i +} + +func (tods *TODeliveryServiceV12) GetAuditName() string { + if tods.DS != nil && tods.DS.XMLID != nil { + return *tods.DS.XMLID + } + return "" +} + +func (tods *TODeliveryServiceV12) GetType() string { + return "ds" +} + +func ValidateV12(db *sqlx.DB, ds *tc.DeliveryServiceNullableV12) []error { + if ds == nil { + return []error{} + } + tods := TODeliveryServiceV12{DS: ds, DB: db} // TODO pass config? + return tods.Validate(db) +} + +func (tods *TODeliveryServiceV12) Sanitize(db *sqlx.DB) { + ds := tods.DS + if ds.GeoLimitCountries != nil { + *ds.GeoLimitCountries = strings.ToUpper(strings.Replace(*ds.GeoLimitCountries, " ", "", -1)) + } + if ds.ProfileID != nil && *ds.ProfileID == -1 { + ds.ProfileID = nil + } + if ds.EdgeHeaderRewrite != nil && strings.TrimSpace(*ds.EdgeHeaderRewrite) == "" { + ds.EdgeHeaderRewrite = nil + } + if ds.MidHeaderRewrite != nil && strings.TrimSpace(*ds.MidHeaderRewrite) == "" { + ds.MidHeaderRewrite = nil + } +} + +// LoadTenantID loads the DeliveryService's tenant ID from the database, using the DS ID or XMLID if either exists. Sets tods.DS.TenantID on success, and returns whether the delivery service was found, and any error. +func (tods *TODeliveryServiceV12) LoadTenantID(db *sqlx.DB) (bool, error) { + if tods.DS.ID != nil { + tenantID := 0 + if err := db.QueryRow(`SELECT tenant_id FROM deliveryservice where id = $1`, tods.DS.ID).Scan(&tenantID); err != nil { + if err == sql.ErrNoRows { + return false, nil + } + return false, fmt.Errorf("querying tenant ID for delivery service ID '%v': %v", *tods.DS.ID, err) + } + tods.DS.TenantID = &tenantID + return true, nil + } + if tods.DS.XMLID != nil { + tenantID := 0 + if err := db.QueryRow(`SELECT tenant_id FROM deliveryservice where xml_id = $1`, *tods.DS.XMLID).Scan(&tenantID); err != nil { + if err == sql.ErrNoRows { + return false, nil + } + return false, fmt.Errorf("querying tenant ID for delivery service xml_id '%v': %v", *tods.DS.XMLID, err) + } + tods.DS.TenantID = &tenantID + return true, nil + } + return false, errors.New("no id or xml_id") +} + +// LoadXMLID loads the DeliveryService's xml_id from the database, from the ID. Returns whether the delivery service was found, and any error. +func (tods *TODeliveryServiceV12) LoadXMLID(db *sqlx.DB) (bool, error) { + if tods.DS.ID == nil { + return false, errors.New("missing ID") + } + + xmlID := "" + if err := db.QueryRow(`SELECT xml_id FROM deliveryservice where id = $1`, tods.DS.ID).Scan(&xmlID); err != nil { + if err == sql.ErrNoRows { + return false, nil + } + return false, fmt.Errorf("querying xml_id for delivery service ID '%v': %v", *tods.DS.ID, err) + } + tods.DS.XMLID = &xmlID + return true, nil +} + +func (tods *TODeliveryServiceV12) IsTenantAuthorized(user auth.CurrentUser, db *sqlx.DB) (bool, error) { + tods.LoadTenantID(db) // try to load, but ignore errors and keep the user-set tenant ID on failure (which will happen with a Create) Review comment: Here I think we need to consider how we're going to handle an update request that attempts to change the tenantID. If we're always loading the existing tenantID from the db before doing the actual update, then the tenantID essentially becomes immutable, and I don't think that's what we want. Perhaps we need to check the tenantID from the DB, make sure the user has access to the DS, then make sure the tenantID in the request is accessible to the user as well. That's basically what the Perl API does today. If the user is setting the tenantID to nil (clearing the tenant), then we should check if tenancy is enabled before allowing it, in addition to making sure the user has access to the existing tenant. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services