rob05c commented on a change in pull request #3688: Add TO Go remap.config URL: https://github.com/apache/trafficcontrol/pull/3688#discussion_r330281193
########## File path: traffic_ops/ort/atstccfg/remapdotconfig.go ########## @@ -0,0 +1,372 @@ +package main + +/* + * 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" + "sort" + "strconv" + "strings" + + "github.com/apache/trafficcontrol/lib/go-atscfg" + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/lib/go-util" +) + +func GetConfigFileServerRemapDotConfig(cfg TCCfg, serverNameOrID string) (string, error) { + // TODO TOAPI add /servers?cdn=1 query param + servers, err := GetServers(cfg) + if err != nil { + return "", errors.New("getting servers: " + err.Error()) + } + + server := tc.Server{ID: atscfg.InvalidID} + if serverID, err := strconv.Atoi(serverNameOrID); err == nil { + for _, toServer := range servers { + if toServer.ID == serverID { + server = toServer + break + } + } + } else { + serverName := serverNameOrID + for _, toServer := range servers { + if toServer.HostName == serverName { + server = toServer + break + } + } + } + if server.ID == atscfg.InvalidID { + return "", errors.New("server '" + serverNameOrID + " not found in servers") + } + + serverName := server.HostName + + cdn, err := GetCDN(cfg, tc.CDNName(server.CDNName)) + if err != nil { + return "", errors.New("getting cdn '" + string(server.CDNName) + "': " + err.Error()) + } + + serverCDNDomain := cdn.DomainName + + toToolName, toURL, err := GetTOToolNameAndURLFromTO(cfg) + if err != nil { + return "", errors.New("getting global parameters: " + err.Error()) + } + + serverProfileParameters, err := GetServerProfileParameters(cfg, server.Profile) + if err != nil { + return "", errors.New("getting server profile '" + server.Profile + "' parameters: " + err.Error()) + } + + atsVersionParam := "" + for _, param := range serverProfileParameters { + if param.ConfigFile != "package" || param.Name != "trafficserver" { + continue + } + atsVersionParam = param.Value + break + } + if atsVersionParam == "" { + atsVersionParam = atscfg.DefaultATSVersion + } + + atsMajorVer, err := atscfg.GetATSMajorVersionFromATSVersion(atsVersionParam) + if err != nil { + return "", errors.New("getting ATS major version from version parameter (profile '" + server.Profile + "' configFile 'package' name 'trafficserver'): " + err.Error()) + } + + deliveryServices, err := GetCDNDeliveryServices(cfg, server.CDNID) + if err != nil { + return "", errors.New("getting delivery services: " + err.Error()) + } + + dsIDs := []int{} + for _, ds := range deliveryServices { + if ds.ID == nil { + // TODO log error? + continue + } + dsIDs = append(dsIDs, *ds.ID) + } + + isMid := strings.HasPrefix(server.Type, string(tc.CacheTypeMid)) + + serverIDs := ([]int)(nil) + if !isMid { + // mids use all servers, so pass nil=all. Edges only use this current server + serverIDs = append(serverIDs, server.ID) + } + + dsServers, err := GetDeliveryServiceServers(cfg, dsIDs, serverIDs) + if err != nil { + return "", errors.New("getting parent.config cachegroup parent server delivery service servers: " + err.Error()) + } + + dssMap := map[int]map[int]struct{}{} // set of map[dsID][serverID] + for _, dss := range dsServers { + if dss.Server == nil || dss.DeliveryService == nil { + continue // TODO log? + } + if dssMap[*dss.DeliveryService] == nil { + dssMap[*dss.DeliveryService] = map[int]struct{}{} + } + dssMap[*dss.DeliveryService][*dss.Server] = struct{}{} + } + + useInactive := false + if !isMid { + // mids get inactive DSes, edges don't. This is how it's always behaved, not necessarily how it should. + useInactive = true + } Review comment: It could. But I'd prefer to leave it within an if-statement for now, because as the preceding comment says, I'm not convinced it should behave this way, and an if-block is easier to modify to something more complex in the future, or to remove outright. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
