alficles commented on a change in pull request #3875: Add TO Go ATS CDN configs
URL: https://github.com/apache/trafficcontrol/pull/3875#discussion_r318796102
 
 

 ##########
 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
+       DomainName                    string
+       HostName                      string
+       ID                            int
+       IP                            string
+       ParentCacheGroupID            int
+       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:
   Float? So `foo_2e9` is ok?

----------------------------------------------------------------
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

Reply via email to