limited commented on a change in pull request #3516: Add Traffic Ops Golang ats meta config route URL: https://github.com/apache/trafficcontrol/pull/3516#discussion_r290315358
########## File path: traffic_ops/traffic_ops_golang/ats/meta.go ########## @@ -0,0 +1,394 @@ +package ats + +/* + * 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" + "strings" + + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" +) + +func GetConfigMetaData(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"id"}, []string{"id"}) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + + server, ok, err := getServerInfo(inf.Tx.Tx, inf.IntParams["id"]) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("GetConfigMetaData getting server info: "+err.Error())) + return + } else if !ok { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusNotFound, nil, errors.New("server not found")) + return + } + + tmParams, err := GetTMParams(inf.Tx.Tx) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("GetConfigMetaData getting tm.url parameter: "+err.Error())) + return + } + if tmParams.URL == "" { + log.Warnln("ats.GetConfigMetadata: global tm.url parameter missing or empty!") + } + + atsData := tc.ATSConfigMetaData{ + Info: tc.ATSConfigMetaDataInfo{ + ProfileID: server.ProfileID, + TOReverseProxyURL: tmParams.ReverseProxyURL, + TOURL: tmParams.URL, + 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{}, + } + + locationParams, err := GetLocationParams(inf.Tx.Tx, server.ProfileID) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("GetConfigMetaData getting location parameters: "+err.Error())) + return + } + + if locationParams["remap.config"].Location != "" { + configLocation := locationParams["remap.config"].Location + uriSignedDSes, err := GetServerURISignedDSes(inf.Tx.Tx, server.HostName, server.Port) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("GetConfigMetaData getting server uri-signed dses: "+err.Error())) + return + } + 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 + } + } + } + + for cfgFile, cfgParams := range locationParams { + atsCfg := tc.ATSConfigMetaDataConfigFile{ + FileNameOnDisk: cfgParams.FileNameOnDisk, + Location: cfgParams.Location, + } + + scope, err := getServerScope(inf.Tx.Tx, cfgFile, server.Type) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("GetConfigMetaData getting scope: "+err.Error())) + return + } + if cfgParams.URL != "" { + scope = tc.ATSConfigMetaDataConfigFileScopeCDNs + } + atsCfg.Scope = string(scope) + + if cfgParams.URL != "" { + 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 + } + + atsData.ConfigFiles = append(atsData.ConfigFiles, atsCfg) + } + + api.WriteRespRaw(w, r, atsData) +} + +// getServerInfo returns the necessary info about the server, whether the server exists, and any error. +func getServerInfo(tx *sql.Tx, id int) (*ServerInfo, bool, error) { + // TODO separate this into only what's requried for each config file, and create interfaces for funcs? + qry := ` +SELECT + c.name as cdn, + s.cdn_id, + s.host_name, + c.domain_name, + s.ip_address, + s.profile AS profile_id, + p.name AS profile_name, + s.tcp_port, + t.name as type, + s.cachegroup, + COALESCE(cg.parent_cachegroup_id, -1), + COALESCE(cg.secondary_parent_cachegroup_id, -1), + COALESCE(parentt.name, '') as parent_cachegroup_type, + COALESCE(sparentt.name, '') as secondary_parent_cachegroup_type +FROM + server s + JOIN cdn c ON s.cdn_id = c.id + JOIN type t ON s.type = t.id + JOIN profile p ON p.id = s.profile + JOIN cachegroup cg on s.cachegroup = cg.id + LEFT JOIN type parentt on parentt.id = (select type from cachegroup where id = cg.parent_cachegroup_id) + LEFT JOIN type sparentt on sparentt.id = (select type from cachegroup where id = cg.secondary_parent_cachegroup_id) +WHERE + s.id = $1 +` + s := ServerInfo{ID: id} + if err := tx.QueryRow(qry, id).Scan(&s.CDN, &s.CDNID, &s.HostName, &s.DomainName, &s.IP, &s.ProfileID, &s.ProfileName, &s.Port, &s.Type, &s.CacheGroupID, &s.ParentCacheGroupID, &s.SecondaryParentCacheGroupID, &s.ParentCacheGroupType, &s.SecondaryParentCacheGroupType); err != nil { + if err == sql.ErrNoRows { + return nil, false, nil + } + return nil, false, errors.New("querying server info: " + err.Error()) + } + return &s, true, nil +} + +// GetTMParams returns the global "tm.url" and "tm.rev_proxy.url" parameters, and any error. If either param doesn't exist, an empty string is returned without error. +func GetTMParams(tx *sql.Tx) (TMParams, error) { + rows, err := tx.Query(`SELECT name, value from parameter where config_file = 'global' AND (name = 'tm.url' OR name = 'tm.rev_proxy.url')`) + if err != nil { + return TMParams{}, errors.New("querying: " + err.Error()) + } + defer rows.Close() + + p := TMParams{} + for rows.Next() { + name := "" + val := "" + if err := rows.Scan(&name, &val); err != nil { + return TMParams{}, errors.New("scanning: " + err.Error()) + } + if name == "tm.url" { + p.URL = val + } else if name == "tm.rev_proxy.url" { + p.ReverseProxyURL = val + } else { + return TMParams{}, errors.New("querying got unexpected parameter: " + name + " (value: '" + val + "')") // should never happen + } + } + return p, nil +} + +// GetLocationParams returns a map[configFile]locationParams, and any error. If either param doesn't exist, an empty string is returned without error. +func GetLocationParams(tx *sql.Tx, profileID int) (map[string]ConfigProfileParams, error) { + qry := ` +SELECT + p.name, + p.config_file, + p.value +FROM + parameter p + JOIN profile_parameter pp ON pp.parameter = p.id +WHERE + pp.profile = $1 +` + rows, err := tx.Query(qry, profileID) + if err != nil { + return nil, errors.New("querying: " + err.Error()) + } + defer rows.Close() + + params := map[string]ConfigProfileParams{} + for rows.Next() { + name := "" + file := "" + val := "" + if err := rows.Scan(&name, &file, &val); err != nil { + return nil, errors.New("scanning: " + err.Error()) + } + if name == "location" { + p := params[file] + p.FileNameOnDisk = file + p.Location = val + params[file] = p + } else if name == "URL" { + p := params[file] + p.URL = val + params[file] = p + } + } + return params, nil +} + +// GetServerURISignedDSes returns a list of delivery service names which have the given server assigned and have URI signing enabled, and any error. +func GetServerURISignedDSes(tx *sql.Tx, serverHostName string, serverPort int) ([]tc.DeliveryServiceName, error) { + qry := ` +SELECT + ds.xml_id +FROM + deliveryservice ds + JOIN deliveryservice_server dss ON ds.id = dss.deliveryservice + JOIN server s ON s.id = dss.server +WHERE + s.host_name = $1 + AND s.tcp_port = $2 Review comment: Are you running multiple servers on the same host? Otherwise I'm not sure why you need to filter by tcp_port ---------------------------------------------------------------- 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
