You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2019/11/01 18:33:31 UTC
[GitHub] [trafficcontrol] alficles commented on a change in pull request
#4063: Add atstccfg meta config
alficles commented on a change in pull request #4063: Add atstccfg meta config
URL: https://github.com/apache/trafficcontrol/pull/4063#discussion_r341695918
##########
File path: lib/go-atscfg/meta.go
##########
@@ -0,0 +1,195 @@
+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 (
+ "encoding/json"
+ "strings"
+
+ "github.com/apache/trafficcontrol/lib/go-log"
+ "github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+type ConfigProfileParams struct {
+ FileNameOnDisk string
+ Location string
+ URL string
+ APIURI string
+}
+
+func MakeMetaConfig(
+ serverHostName tc.CacheName,
+ server *ServerInfo,
+ tmURL string, // global tm.url Parameter
+ tmReverseProxyURL string, // global tm.rev_proxy.url Parameter
+ locationParams map[string]ConfigProfileParams, // map[configFile]params; 'location' and 'URL' Parameters on serverHostName's Profile
+ uriSignedDSes []tc.DeliveryServiceName,
+ scopeParams map[string]string, // map[configFileName]scopeParam
+) string {
+ if tmURL == "" {
+ log.Errorln("ats.GetConfigMetadata: global tm.url parameter missing or empty! Setting empty in meta config!")
+ }
+
+ atsData := tc.ATSConfigMetaData{
+ Info: tc.ATSConfigMetaDataInfo{
+ ProfileID: int(server.ProfileID),
+ TOReverseProxyURL: tmReverseProxyURL,
+ TOURL: tmURL,
+ ServerIPv4: server.IP,
+ ServerPort: server.Port,
+ ServerName: server.HostName,
+ CDNID: server.CDNID,
+ CDNName: string(server.CDN),
+ ServerID: server.ID,
+ ProfileName: server.ProfileName,
+ },
+ ConfigFiles: []tc.ATSConfigMetaDataConfigFile{},
+ }
+
+ if locationParams["remap.config"].Location != "" {
+ configLocation := locationParams["remap.config"].Location
+ for _, ds := range uriSignedDSes {
+ cfgName := "uri_signing_" + string(ds) + ".config"
+ // If there's already a parameter for it, don't clobber it. The user may wish to override the location.
+ if _, ok := locationParams[cfgName]; !ok {
+ p := locationParams[cfgName]
+ p.FileNameOnDisk = cfgName
+ p.Location = configLocation
+ locationParams[cfgName] = p
+ }
+ }
+ }
+
+ for cfgFile, cfgParams := range locationParams {
+ atsCfg := tc.ATSConfigMetaDataConfigFile{
+ FileNameOnDisk: cfgParams.FileNameOnDisk,
+ Location: cfgParams.Location,
+ }
+
+ scope := getServerScope(cfgFile, server.Type, scopeParams)
+
+ if cfgParams.URL != "" {
+ scope = tc.ATSConfigMetaDataConfigFileScopeCDNs
+ atsCfg.URL = cfgParams.URL
+ } else {
+ scopeID := ""
+ if scope == tc.ATSConfigMetaDataConfigFileScopeCDNs {
+ scopeID = string(server.CDN)
+ } else if scope == tc.ATSConfigMetaDataConfigFileScopeProfiles {
+ scopeID = server.ProfileName
+ } else { // ATSConfigMetaDataConfigFileScopeServers
+ scopeID = server.HostName
+ }
+ atsCfg.APIURI = "/api/1.2/" + string(scope) + "/" + scopeID + "/configfiles/ats/" + cfgFile
+ }
+
+ atsCfg.Scope = string(scope)
+
+ atsData.ConfigFiles = append(atsData.ConfigFiles, atsCfg)
+ }
+
+ bts, err := json.Marshal(atsData)
+ if err != nil {
+ // should never happen
+ log.Errorln("marshalling chkconfig NameVersions: " + err.Error())
+ bts = []byte("error encoding to json, see log for details")
+ }
+ return string(bts)
+}
+
+func getServerScope(cfgFile string, serverType string, scopeParams map[string]string) tc.ATSConfigMetaDataConfigFileScope {
+ switch {
+ case cfgFile == "cache.config" && tc.CacheTypeFromString(serverType) == tc.CacheTypeMid:
+ return tc.ATSConfigMetaDataConfigFileScopeServers
+ default:
+ return getScope(cfgFile, scopeParams)
+ }
+}
+
+const DefaultScope = tc.ATSConfigMetaDataConfigFileScopeServers
+
+// getScope returns the ATSConfigMetaDataConfigFileScope for the given config file, and potentially the given server. If the config is not a Server scope, i.e. was part of an endpoint which does not include a server name or id, the server may be nil.
+func getScope(cfgFile string, scopeParams map[string]string) tc.ATSConfigMetaDataConfigFileScope {
+ switch {
Review comment:
Is the fallthrough here better than multiple-case? Like: `cfgFile == "ip_allow.config", cfgFile == "parent.config", ...`
----------------------------------------------------------------
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:
users@infra.apache.org
With regards,
Apache Git Services