[GitHub] rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes

2018-05-07 Thread GitBox
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 {Cfg: cfg, DB: db, DS: 
{}}
+}
+
+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 = 
+}
+
+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()` 
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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes

2018-05-04 Thread GitBox
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 {Cfg: cfg, DB: db, DS: 
{}}
+}
+
+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 = 
+}
+
+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(); 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 = 
+   return true, nil
+   }
+   if tods.DS.XMLID != nil {
+   tenantID := 0
+   if err := db.QueryRow(`SELECT tenant_id FROM 

[GitHub] rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes

2018-04-27 Thread GitBox
rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices 
routes
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/2124#discussion_r184779072
 
 

 ##
 File path: 
traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
 ##
 @@ -0,0 +1,1109 @@
+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"
+   "encoding/json"
+   "errors"
+   "fmt"
+   "strings"
+
+   "github.com/apache/incubator-trafficcontrol/lib/go-log"
+   "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+   
"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/dbhelpers"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/riaksvc"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tenant"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tovalidate"
+
+   "github.com/go-ozzo/ozzo-validation"
+   "github.com/jmoiron/sqlx"
+   "github.com/lib/pq"
+)
+
+//we need a type alias to define functions on
+type TODeliveryServiceV13 struct {
+   DS  *tc.DeliveryServiceNullableV13
+   Cfg config.Config
+   DB  *sqlx.DB
+}
+
+func (tods *TODeliveryServiceV13) V12() *TODeliveryServiceV12 {
+   return {DS: , 
DB: tods.DB, Cfg: tods.Cfg}
+}
+
+func (tods TODeliveryServiceV13) MarshalJSON() ([]byte, error) { return 
json.Marshal(tods.DS) }
+func (tods *TODeliveryServiceV13) UnmarshalJSON(data []byte) error {
+   return json.Unmarshal(data, tods.DS)
+}
+
+func GetRefTypeV13(cfg config.Config, db *sqlx.DB) *TODeliveryServiceV13 {
+   return {Cfg: cfg, DB: db, DS: 
{}}
+}
+
+func (ds TODeliveryServiceV13) GetKeyFieldsInfo() []api.KeyFieldInfo {
+   return ds.V12().GetKeyFieldsInfo()
+}
+
+//Implementation of the Identifier, Validator interface functions
+func (ds TODeliveryServiceV13) GetKeys() (map[string]interface{}, bool) {
+   return ds.V12().GetKeys()
+}
+
+func (tods *TODeliveryServiceV13) SetKeys(keys map[string]interface{}) {
+   tods.V12().SetKeys(keys)
+}
+
+func (ds *TODeliveryServiceV13) GetAuditName() string {
+   return ds.V12().GetAuditName()
+}
+
+func (ds *TODeliveryServiceV13) GetType() string {
+   return ds.V12().GetType()
+}
+
+func ValidateV13(db *sqlx.DB, ds *tc.DeliveryServiceNullableV13) []error {
+   if ds == nil {
+   return []error{}
+   }
+   tods := TODeliveryServiceV13{DS: ds, DB: db} // TODO set Cfg?
+   return tods.Validate(db)
+}
+
+func (tods *TODeliveryServiceV13) Sanitize(db *sqlx.DB) {
+   v12 := tods.V12()
+   v12.Sanitize(db)
+   tods.DS.DeliveryServiceNullableV12 = 
tc.DeliveryServiceNullableV12(*v12.DS) // TODO avoid copy
+   signedAlgorithm := "url_sig"
+   if tods.DS.Signed && (tods.DS.SigningAlgorithm == nil || 
*tods.DS.SigningAlgorithm == "") {
+   tods.DS.SigningAlgorithm = 
+   }
+   if !tods.DS.Signed && tods.DS.SigningAlgorithm != nil && 
*tods.DS.SigningAlgorithm == signedAlgorithm {
+   tods.DS.Signed = true
+   }
+   if tods.DS.DeepCachingType == nil {
+   s := tc.DeepCachingType("")
+   tods.DS.DeepCachingType = 
+   }
+   *tods.DS.DeepCachingType = 
tc.DeepCachingTypeFromString(string(*tods.DS.DeepCachingType))
+}
+
+func (tods *TODeliveryServiceV13) Validate(db *sqlx.DB) []error {
+   tods.Sanitize(db)
+   ds := tods.DS
+   neverOrAlways := 
validation.NewStringRule(tovalidate.IsOneOfStringICase("NEVER", "ALWAYS"),
+   "must be one of 'NEVER' or 'ALWAYS'")
+   errs := tovalidate.ToErrors(validation.Errors{
+   "deepCachingType": validation.Validate(ds.DeepCachingType, 
neverOrAlways),
+   })
+
+   oldErrs := tods.V12().Validate(db)
+   return append(errs, oldErrs...)
+}
+
+// Create 

[GitHub] rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes

2018-04-27 Thread GitBox
rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices 
routes
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/2124#discussion_r184749963
 
 

 ##
 File path: 
traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
 ##
 @@ -0,0 +1,1109 @@
+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"
+   "encoding/json"
+   "errors"
+   "fmt"
+   "strings"
+
+   "github.com/apache/incubator-trafficcontrol/lib/go-log"
+   "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+   
"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/dbhelpers"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/riaksvc"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tenant"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tovalidate"
+
+   "github.com/go-ozzo/ozzo-validation"
+   "github.com/jmoiron/sqlx"
+   "github.com/lib/pq"
+)
+
+//we need a type alias to define functions on
+type TODeliveryServiceV13 struct {
+   DS  *tc.DeliveryServiceNullableV13
+   Cfg config.Config
+   DB  *sqlx.DB
+}
+
+func (tods *TODeliveryServiceV13) V12() *TODeliveryServiceV12 {
+   return {DS: , 
DB: tods.DB, Cfg: tods.Cfg}
+}
+
+func (tods TODeliveryServiceV13) MarshalJSON() ([]byte, error) { return 
json.Marshal(tods.DS) }
+func (tods *TODeliveryServiceV13) UnmarshalJSON(data []byte) error {
+   return json.Unmarshal(data, tods.DS)
+}
+
+func GetRefTypeV13(cfg config.Config, db *sqlx.DB) *TODeliveryServiceV13 {
+   return {Cfg: cfg, DB: db, DS: 
{}}
+}
+
+func (ds TODeliveryServiceV13) GetKeyFieldsInfo() []api.KeyFieldInfo {
+   return ds.V12().GetKeyFieldsInfo()
+}
+
+//Implementation of the Identifier, Validator interface functions
+func (ds TODeliveryServiceV13) GetKeys() (map[string]interface{}, bool) {
+   return ds.V12().GetKeys()
+}
+
+func (tods *TODeliveryServiceV13) SetKeys(keys map[string]interface{}) {
+   tods.V12().SetKeys(keys)
+}
+
+func (ds *TODeliveryServiceV13) GetAuditName() string {
+   return ds.V12().GetAuditName()
+}
+
+func (ds *TODeliveryServiceV13) GetType() string {
+   return ds.V12().GetType()
+}
+
+func ValidateV13(db *sqlx.DB, ds *tc.DeliveryServiceNullableV13) []error {
+   if ds == nil {
+   return []error{}
+   }
+   tods := TODeliveryServiceV13{DS: ds, DB: db} // TODO set Cfg?
+   return tods.Validate(db)
+}
+
+func (tods *TODeliveryServiceV13) Sanitize(db *sqlx.DB) {
+   v12 := tods.V12()
+   v12.Sanitize(db)
+   tods.DS.DeliveryServiceNullableV12 = 
tc.DeliveryServiceNullableV12(*v12.DS) // TODO avoid copy
+   signedAlgorithm := "url_sig"
+   if tods.DS.Signed && (tods.DS.SigningAlgorithm == nil || 
*tods.DS.SigningAlgorithm == "") {
+   tods.DS.SigningAlgorithm = 
+   }
+   if !tods.DS.Signed && tods.DS.SigningAlgorithm != nil && 
*tods.DS.SigningAlgorithm == signedAlgorithm {
+   tods.DS.Signed = true
+   }
+   if tods.DS.DeepCachingType == nil {
+   s := tc.DeepCachingType("")
+   tods.DS.DeepCachingType = 
+   }
+   *tods.DS.DeepCachingType = 
tc.DeepCachingTypeFromString(string(*tods.DS.DeepCachingType))
+}
+
+func (tods *TODeliveryServiceV13) Validate(db *sqlx.DB) []error {
+   tods.Sanitize(db)
+   ds := tods.DS
+   neverOrAlways := 
validation.NewStringRule(tovalidate.IsOneOfStringICase("NEVER", "ALWAYS"),
+   "must be one of 'NEVER' or 'ALWAYS'")
+   errs := tovalidate.ToErrors(validation.Errors{
+   "deepCachingType": validation.Validate(ds.DeepCachingType, 
neverOrAlways),
+   })
+
+   oldErrs := tods.V12().Validate(db)
+   return append(errs, oldErrs...)
+}
+
+// Create 

[GitHub] rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes

2018-04-27 Thread GitBox
rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices 
routes
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/2124#discussion_r184749963
 
 

 ##
 File path: 
traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv13.go
 ##
 @@ -0,0 +1,1109 @@
+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"
+   "encoding/json"
+   "errors"
+   "fmt"
+   "strings"
+
+   "github.com/apache/incubator-trafficcontrol/lib/go-log"
+   "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+   
"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/dbhelpers"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/riaksvc"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tenant"
+   
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tovalidate"
+
+   "github.com/go-ozzo/ozzo-validation"
+   "github.com/jmoiron/sqlx"
+   "github.com/lib/pq"
+)
+
+//we need a type alias to define functions on
+type TODeliveryServiceV13 struct {
+   DS  *tc.DeliveryServiceNullableV13
+   Cfg config.Config
+   DB  *sqlx.DB
+}
+
+func (tods *TODeliveryServiceV13) V12() *TODeliveryServiceV12 {
+   return {DS: , 
DB: tods.DB, Cfg: tods.Cfg}
+}
+
+func (tods TODeliveryServiceV13) MarshalJSON() ([]byte, error) { return 
json.Marshal(tods.DS) }
+func (tods *TODeliveryServiceV13) UnmarshalJSON(data []byte) error {
+   return json.Unmarshal(data, tods.DS)
+}
+
+func GetRefTypeV13(cfg config.Config, db *sqlx.DB) *TODeliveryServiceV13 {
+   return {Cfg: cfg, DB: db, DS: 
{}}
+}
+
+func (ds TODeliveryServiceV13) GetKeyFieldsInfo() []api.KeyFieldInfo {
+   return ds.V12().GetKeyFieldsInfo()
+}
+
+//Implementation of the Identifier, Validator interface functions
+func (ds TODeliveryServiceV13) GetKeys() (map[string]interface{}, bool) {
+   return ds.V12().GetKeys()
+}
+
+func (tods *TODeliveryServiceV13) SetKeys(keys map[string]interface{}) {
+   tods.V12().SetKeys(keys)
+}
+
+func (ds *TODeliveryServiceV13) GetAuditName() string {
+   return ds.V12().GetAuditName()
+}
+
+func (ds *TODeliveryServiceV13) GetType() string {
+   return ds.V12().GetType()
+}
+
+func ValidateV13(db *sqlx.DB, ds *tc.DeliveryServiceNullableV13) []error {
+   if ds == nil {
+   return []error{}
+   }
+   tods := TODeliveryServiceV13{DS: ds, DB: db} // TODO set Cfg?
+   return tods.Validate(db)
+}
+
+func (tods *TODeliveryServiceV13) Sanitize(db *sqlx.DB) {
+   v12 := tods.V12()
+   v12.Sanitize(db)
+   tods.DS.DeliveryServiceNullableV12 = 
tc.DeliveryServiceNullableV12(*v12.DS) // TODO avoid copy
+   signedAlgorithm := "url_sig"
+   if tods.DS.Signed && (tods.DS.SigningAlgorithm == nil || 
*tods.DS.SigningAlgorithm == "") {
+   tods.DS.SigningAlgorithm = 
+   }
+   if !tods.DS.Signed && tods.DS.SigningAlgorithm != nil && 
*tods.DS.SigningAlgorithm == signedAlgorithm {
+   tods.DS.Signed = true
+   }
+   if tods.DS.DeepCachingType == nil {
+   s := tc.DeepCachingType("")
+   tods.DS.DeepCachingType = 
+   }
+   *tods.DS.DeepCachingType = 
tc.DeepCachingTypeFromString(string(*tods.DS.DeepCachingType))
+}
+
+func (tods *TODeliveryServiceV13) Validate(db *sqlx.DB) []error {
+   tods.Sanitize(db)
+   ds := tods.DS
+   neverOrAlways := 
validation.NewStringRule(tovalidate.IsOneOfStringICase("NEVER", "ALWAYS"),
+   "must be one of 'NEVER' or 'ALWAYS'")
+   errs := tovalidate.ToErrors(validation.Errors{
+   "deepCachingType": validation.Validate(ds.DeepCachingType, 
neverOrAlways),
+   })
+
+   oldErrs := tods.V12().Validate(db)
+   return append(errs, oldErrs...)
+}
+
+// Create 

[GitHub] rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes

2018-04-24 Thread GitBox
rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices 
routes
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/2124#discussion_r183907743
 
 

 ##
 File path: 
traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv12.go
 ##
 @@ -0,0 +1,300 @@
+package deliveryservice
+
+import (
+   "database/sql"
+   "errors"
+   "fmt"
+   "regexp"
+   "strings"
+
+   "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+   
"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 {Cfg: cfg, DB: db, DS: 
{}}
+}
+
+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 = 
+}
+
+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(); 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 = 
+   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(); 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 = 
+   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 = 

[GitHub] rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices routes

2018-04-24 Thread GitBox
rawlinp commented on a change in pull request #2124: Add TO Go deliveryservices 
routes
URL: 
https://github.com/apache/incubator-trafficcontrol/pull/2124#discussion_r183898437
 
 

 ##
 File path: 
traffic_ops/traffic_ops_golang/deliveryservice/deliveryservicesv12.go
 ##
 @@ -0,0 +1,300 @@
+package deliveryservice
+
+import (
+   "database/sql"
+   "errors"
+   "fmt"
+   "regexp"
+   "strings"
+
+   "github.com/apache/incubator-trafficcontrol/lib/go-tc"
+   
"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 {Cfg: cfg, DB: db, DS: 
{}}
+}
+
+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 = 
+}
+
+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(); 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 = 
+   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(); 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 = 
+   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 =