[GitHub] [trafficcontrol] rob05c commented on a change in pull request #3875: Add TO Go ATS CDN configs

2019-08-29 Thread GitBox
rob05c commented on a change in pull request #3875: Add TO Go ATS CDN configs
URL: https://github.com/apache/trafficcontrol/pull/3875#discussion_r319182815
 
 

 ##
 File path: traffic_ops/traffic_ops_golang/ats/atscdn/atscdn.go
 ##
 @@ -0,0 +1,152 @@
+package atscdn
+
+/*
+ * 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"
+   "net/http"
+   "strconv"
+   "strings"
+
+   "github.com/apache/trafficcontrol/lib/go-tc"
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/ats"
+   "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config"
+   
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+)
+
+// GenericProfileConfig generates a generic profile config text, from the 
profile's parameters with the given config file name.
+// This does not include a header comment, because a generic config may not 
use a number sign as a comment.
+// If you need a header comment, it can be added manually via 
ats.HeaderComment, or automatically with WithProfileDataHdr.
+func GenericProfileConfig(tx *sql.Tx, profile ats.ProfileData, fileName 
string, separator string) (string, error) {
+   profileParamData, err := ats.GetProfileParamData(tx, profile.ID, 
fileName)
+   if err != nil {
+   return "", errors.New("getting profile param data: " + 
err.Error())
+   }
+   text := ""
+   for name, val := range profileParamData {
+   name = trimParamUnderscoreNumSuffix(name)
+   text += name + separator + val + "\n"
+   }
+   return text, nil
+}
+
+// trimParamUnderscoreNumSuffix removes any trailing "__[0-9]+" and returns 
the trimmed string.
+func trimParamUnderscoreNumSuffix(paramName string) string {
+   underscorePos := strings.LastIndex(paramName, `__`)
+   if underscorePos == -1 {
+   return paramName
+   }
+   if _, err := strconv.ParseFloat(paramName[underscorePos+2:], 64); err 
!= nil {
 
 Review comment:
   It is moved in https://github.com/apache/trafficcontrol/pull/3762
   Moving it here as well will make painful merge conflicts.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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] [trafficcontrol] rob05c commented on a change in pull request #3875: Add TO Go ATS CDN configs

2019-08-29 Thread GitBox
rob05c commented on a change in pull request #3875: Add TO Go ATS CDN configs
URL: https://github.com/apache/trafficcontrol/pull/3875#discussion_r319181237
 
 

 ##
 File path: lib/go-atscfg/regexremapdotconfig.go
 ##
 @@ -0,0 +1,58 @@
+package atscfg
+
+/*
+ * 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 (
+   "strings"
+
+   "github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+type CDNDS struct {
+   OrgServerFQDN string
+   QStringIgnore int
+   CacheURL  string
+   RegexRemapstring
+}
+
+func MakeRegexRemapDotConfig(
+   cdnName tc.CDNName,
+   toToolName string, // tm.toolname global parameter (TODO: cache itself?)
+   toURL string, // tm.url global parameter (TODO: cache itself?)
+   fileName string,
+   dses map[tc.DeliveryServiceName]CDNDS,
+) string {
+   text := GenericHeaderComment(string(cdnName), toToolName, toURL)
+
+   if len(fileName) < len(`regex_remap_.config`) {
+   return text // TODO warn? Perl doesn't
+   }
+
+   dsName := fileName[len("regex_remap_") : len(fileName)-len(".config")]
 
 Review comment:
   Ok, I changed it to use `strings.TrimPrefix` and `TrimSuffix`


This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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] [trafficcontrol] rob05c commented on a change in pull request #3875: Add TO Go ATS CDN configs

2019-08-29 Thread GitBox
rob05c commented on a change in pull request #3875: Add TO Go ATS CDN configs
URL: https://github.com/apache/trafficcontrol/pull/3875#discussion_r319177522
 
 

 ##
 File path: lib/go-atscfg/sslmulticertdotconfig.go
 ##
 @@ -0,0 +1,74 @@
+package atscfg
+
+/*
+ * 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 (
+   "strings"
+
+   "github.com/apache/trafficcontrol/lib/go-log"
+   "github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+type SSLMultiCertDS struct {
+   Typetc.DSType
+   Protocolint
+   ExampleURLs []string
+}
+
+func MakeSSLMultiCertDotConfig(
+   cdnName tc.CDNName,
+   toToolName string, // tm.toolname global parameter (TODO: cache itself?)
+   toURL string, // tm.url global parameter (TODO: cache itself?)
+   dses map[tc.DeliveryServiceName]SSLMultiCertDS,
+) string {
+   text := GenericHeaderComment(string(cdnName), toToolName, toURL)
+
+   for dsName, ds := range dses {
+   if ds.Type.IsSteering() {
+   continue // Steering delivery service SSLs should not 
be on the edges.
+   }
+   if ds.Protocol == 0 {
+   continue
+   }
+   if len(ds.ExampleURLs) == 0 {
+   continue // TODO warn? error? Perl doesn't
+   }
+
+   hostName := ds.ExampleURLs[0] // first one is the one we want
+
+   scheme := "https://";
+   if !strings.HasPrefix(hostName, scheme) {
+   scheme = "http://";
+   }
 
 Review comment:
   Yes, that's exactly how Perl behaves. It only supports https? schemes. I'm 
+1 on adding general scheme support, but IMO it's not worth the risk in this PR


This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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] [trafficcontrol] rob05c commented on a change in pull request #3875: Add TO Go ATS CDN configs

2019-08-29 Thread GitBox
rob05c commented on a change in pull request #3875: Add TO Go ATS CDN configs
URL: https://github.com/apache/trafficcontrol/pull/3875#discussion_r319175785
 
 

 ##
 File path: lib/go-atscfg/atscfg.go
 ##
 @@ -0,0 +1,114 @@
+package atscfg
+
+/*
+ * 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 (
+   "errors"
+   "strconv"
+   "strings"
+   "time"
+
+   "github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+// TODO move in config gen PR
+const InvalidID = -1
+
+const HeaderCommentDateFormat = "Mon Jan 2 15:04:05 MST 2006"
+
+type ServerInfo struct {
+   CacheGroupID  int
+   CDN   tc.CDNName
+   CDNID int
+   DomainNamestring
+   HostName  string
+   IDint
+   IPstring
+   ParentCacheGroupIDint
+   ParentCacheGroupType  string
+   ProfileID ProfileID
+   ProfileName   string
+   Port  int
+   SecondaryParentCacheGroupID   int
+   SecondaryParentCacheGroupType string
+   Type  string
+}
+
+func (s *ServerInfo) IsTopLevelCache() bool {
+   return (s.ParentCacheGroupType == tc.CacheGroupOriginTypeName || 
s.ParentCacheGroupID == InvalidID) &&
+   (s.SecondaryParentCacheGroupType == tc.CacheGroupOriginTypeName 
|| s.SecondaryParentCacheGroupID == InvalidID)
+}
+
+func HeaderCommentWithTOVersionStr(name string, nameVersionStr string) string {
+   return "# DO NOT EDIT - Generated for " + name + " by " + 
nameVersionStr + " on " + time.Now().Format(HeaderCommentDateFormat) + "\n"
+}
+
+func GetNameVersionStringFromToolNameAndURL(toolName string, url string) 
string {
+   return toolName + " (" + url + ")"
+}
+
+func GenericHeaderComment(name string, toolName string, url string) string {
+   return HeaderCommentWithTOVersionStr(name, 
GetNameVersionStringFromToolNameAndURL(toolName, url))
+}
+
+// GetATSMajorVersionFromATSVersion returns the major version of the given 
profile's package trafficserver parameter.
+// The atsVersion is typically a Parameter on the Server's Profile, with the 
configFile "package" name "trafficserver".
+// Returns an error if atsVersion is empty or not a number.
+func GetATSMajorVersionFromATSVersion(atsVersion string) (int, error) {
+   if len(atsVersion) == 0 {
+   return 0, errors.New("ats version missing")
+   }
+   atsMajorVer, err := strconv.Atoi(atsVersion[:1])
+   if err != nil {
+   return 0, errors.New("ats version parameter '" + atsVersion + 
"' is not a number")
+   }
+   return atsMajorVer, nil
+}
+
+type DeliveryServiceID int
+type ProfileID int
+type ServerID int
+
+// GenericProfileConfig generates a generic profile config text, from the 
profile's parameters with the given config file name.
+// This does not include a header comment, because a generic config may not 
use a number sign as a comment.
+// If you need a header comment, it can be added manually via 
ats.HeaderComment, or automatically with WithProfileDataHdr.
+func GenericProfileConfig(
+   paramData map[string]string, // GetProfileParamData(tx, profileID, 
fileName)
+   separator string,
+) string {
+   text := ""
+   for name, val := range paramData {
+   name = trimParamUnderscoreNumSuffix(name)
+   text += name + separator + val + "\n"
+   }
+   return text
+}
+
+// trimParamUnderscoreNumSuffix removes any trailing "__[0-9]+" and returns 
the trimmed string.
+func trimParamUnderscoreNumSuffix(paramName string) string {
+   underscorePos := strings.LastIndex(paramName, `__`)
+   if underscorePos == -1 {
+   return paramName
+   }
+   if _, err := strconv.ParseFloat(paramName[underscorePos+2:], 64); err 
!= nil {
 
 Review comment:
   This is part of "take and bake," the feature that lets you define any 
arbitrary config file, via Params. As far as I can tell, this exists because 
Parameters have a unique key of name-configfile-value, so a