rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes URL: https://github.com/apache/incubator-trafficcontrol/pull/2124#discussion_r186584997
########## 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 Review comment: I think this needs to be a pointer to an int so that `Scan(&tenantID)` doesn't error out when tenantID is null in the DB. I just ran into this while working on the Origin API and initially had it the same as you have it here. ---------------------------------------------------------------- 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
