rob05c commented on a change in pull request #3688: Add TO Go remap.config URL: https://github.com/apache/trafficcontrol/pull/3688#discussion_r330278062
########## 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) Review comment: I don't agree. I prefer to always use `:=` for two reasons. For one, `var` doesn't need to exist, you can do everything with `:=`. IMO it's better to have one way to do things. Any time there are multiple ways to do the same thing, it's that much more to learn, to deal with, to understand the exact behavior of, and easy for especially a new programmer to misunderstand the exact behavior of one of the many ways, and write a bug. Second, it's explicit. Explicit is better than Implicit. The `:=` syntax makes it abundantly clear that `serverIDs` is `nil` until it's set to something else. Where the implicit zero-initialization isn't always obvious at first glance. It's easy to skim over `var serverIDs []int` and not mentally register that it's `nil`. It's much harder to see the word `nil` and not immediately realize that it's nil, and hopefully catch bugs like "oh, I see `myPtr := (*int)(nil)` is dereferenced on the next line, that's going to panic! ---------------------------------------------------------------- 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
