ocket8888 commented on a change in pull request #4774:
URL: https://github.com/apache/trafficcontrol/pull/4774#discussion_r438992026



##########
File path: lib/go-tc/traffic_monitor.go
##########
@@ -41,6 +41,34 @@ type TrafficMonitorConfig struct {
        Profiles         []TMProfile            `json:"profiles,omitempty"`
 }
 
+// ToLegacyConfig converts TrafficMonitorConfig to LegacyTrafficMonitorConfig
+func (tmc *TrafficMonitorConfig) ToLegacyConfig() LegacyTrafficMonitorConfig {
+       var servers []LegacyTrafficServer
+       for _, s := range tmc.TrafficServers {
+               servers = append(servers, s.ToLegacyServer())
+       }
+
+       legacy := LegacyTrafficMonitorConfig{
+               CacheGroups:      tmc.CacheGroups,
+               Config:           tmc.Config,
+               TrafficMonitors:  tmc.TrafficMonitors,
+               Profiles:         tmc.Profiles,
+               DeliveryServices: tmc.DeliveryServices,
+               TrafficServers:   servers,
+       }
+       return legacy
+}
+
+// LegacyTrafficMonitorConfig represents TrafficMonitorConfig for ATC versions 
before 5.0

Review comment:
       GoDoc missing period.

##########
File path: lib/go-tc/servers.go
##########
@@ -97,6 +97,12 @@ type ServerInterfaceInfo struct {
        Name         string            `json:"name" db:"name"`
 }
 
+// ServerIPAddressCompoundKey represents the relationship between ipAddresses 
and servers/interfaces

Review comment:
       GoDoc needs a period.

##########
File path: lib/go-tc/traffic_monitor.go
##########
@@ -41,6 +41,34 @@ type TrafficMonitorConfig struct {
        Profiles         []TMProfile            `json:"profiles,omitempty"`
 }
 
+// ToLegacyConfig converts TrafficMonitorConfig to LegacyTrafficMonitorConfig

Review comment:
       GoDoc missing a period.

##########
File path: traffic_ops/traffic_ops_golang/monitoring/monitoring.go
##########
@@ -88,6 +108,11 @@ type Monitoring struct {
        Config           map[string]interface{} `json:"config"`
 }
 
+// LegacyMonitoringResponse represents MontiroingResponse for ATC versions 
before 5.0

Review comment:
       GoDoc missing period.

##########
File path: traffic_ops/traffic_ops_golang/monitoring/monitoring.go
##########
@@ -151,27 +176,100 @@ func GetMonitoringJSON(tx *sql.Tx, cdnName string) 
(*Monitoring, error) {
 }
 
 func getMonitoringServers(tx *sql.Tx, cdn string) ([]Monitor, []Cache, 
[]Router, error) {
-       query := `SELECT
-me.host_name as hostName,
-CONCAT(me.host_name, '.', me.domain_name) as fqdn,
-status.name as status,
-cachegroup.name as cachegroup,
-me.tcp_port as port,
-me.ip_address as ip,
-me.ip6_address as ip6,
-profile.name as profile,
-me.interface_name as interfaceName,
-type.name as type,
-me.xmpp_id as hashID
+       serversQuery := `
+SELECT
+       me.host_name as hostName,
+       CONCAT(me.host_name, '.', me.domain_name) as fqdn,
+       status.name as status,
+       cachegroup.name as cachegroup,
+       me.tcp_port as port,
+       me.ip_address as ip,
+       me.ip6_address as ip6,
+       profile.name as profile,
+       type.name as type,
+       me.xmpp_id as hashID
 FROM server me
 JOIN type type ON type.id = me.type
 JOIN status status ON status.id = me.status
 JOIN cachegroup cachegroup ON cachegroup.id = me.cachegroup
 JOIN profile profile ON profile.id = me.profile
 JOIN cdn cdn ON cdn.id = me.cdn_id
-WHERE cdn.name = $1`
+WHERE cdn.name = $1
+`
 
-       rows, err := tx.Query(query, cdn)
+       interfacesQuery := `
+SELECT 
+   i.name, i.max_bandwidth, i.mtu, i.monitor, i.server
+FROM interface i
+WHERE i.server in (
+       SELECT 
+               s.id 
+       FROM "server" s 
+       JOIN cdn c 
+               on c.id = s.cdn_id 
+       WHERE c.name = $1
+)`
+
+       ipAddressQuery := `
+SELECT 
+       ip.address, ip.gateway, ip.service_address, ip.server, ip.interface
+FROM ip_address ip
+JOIN server s 
+       ON s.id = ip.server
+JOIN cdn cdn 
+       ON cdn.id = s.cdn_id
+WHERE ip.server = ANY($1)
+AND ip.interface = ANY($2)
+AND cdn.name = $3
+`
+
+       interfaceRows, err := tx.Query(interfacesQuery, cdn)
+       if err != nil {
+               return nil, nil, nil, err
+       }
+       defer interfaceRows.Close()
+
+       //For constant time lookup of which interface/server belongs to the 
ipAddress
+       var interfaceIndexByCompoundKey = 
make(map[tc.ServerIPAddressCompoundKey]int)
+       var interfaces []tc.ServerInterfaceInfo
+       var serverIDs []int
+       var interfaceNames []string
+       for interfaceRows.Next() {
+               interf := tc.ServerInterfaceInfo{}
+               ipAddressCompoundKey := tc.ServerIPAddressCompoundKey{}
+               if err := interfaceRows.Scan(&interf.Name, 
&interf.MaxBandwidth, &interf.MTU, &interf.Monitor, 
&ipAddressCompoundKey.ServerID); err != nil {
+                       return nil, nil, nil, err
+               }
+               interfaces = append(interfaces, interf)
+               ipAddressCompoundKey.InterfaceName = interf.Name
+               interfaceIndexByCompoundKey[ipAddressCompoundKey] = 
len(interfaces) - 1
+               serverIDs = append(serverIDs, ipAddressCompoundKey.ServerID)
+               interfaceNames = append(interfaceNames, interf.Name)
+       }
+
+       ipAddressRows, err := tx.Query(ipAddressQuery, pq.Array(serverIDs), 
pq.Array(interfaceNames), cdn)

Review comment:
       don't forget to `defer ipAddressRows.Close()`

##########
File path: lib/go-tc/crconfig.go
##########
@@ -90,7 +90,7 @@ type CRConfigTrafficOpsServer struct {
        HashCount        *int                  `json:"hashCount,omitempty"`
        HashId           *string               `json:"hashId,omitempty"`
        HttpsPort        *int                  `json:"httpsPort"`
-       InterfaceName    *string               `json:"interfaceName,omitempty"`
+       Interfaces       []InterfaceInfo       `json:"interfaces"`

Review comment:
       Why was this necessary?

##########
File path: traffic_ops/traffic_ops_golang/monitoring/monitoring.go
##########
@@ -56,13 +58,21 @@ type Monitor struct {
        BasicServer
 }
 
-type Cache struct {
+// LegacyCache represents a Cache for ATC versions before 5.0

Review comment:
       GoDoc missing period.

##########
File path: lib/go-tc/traffic_router.go
##########
@@ -147,6 +150,96 @@ type TrafficServer struct {
        DeliveryServices []tsdeliveryService 
`json:"deliveryServices,omitempty"` // the deliveryServices key does not exist 
on mids
 }
 
+// GetDefaultAddress returns the ipv4 and ipv6 service addresses of the 
interface

Review comment:
       GoDoc missing period.

##########
File path: traffic_ops/traffic_ops_golang/monitoring/monitoring.go
##########
@@ -79,6 +89,16 @@ type Profile struct {
        Parameters map[string]interface{} `json:"parameters"`
 }
 
+// LegacyMonitoring represents Monitoring for ATC versions before 5.0

Review comment:
       GoDoc missing period

##########
File path: traffic_ops/traffic_ops_golang/crconfig/handler.go
##########
@@ -78,6 +79,43 @@ func SnapshotGetHandler(w http.ResponseWriter, r 
*http.Request) {
        w.Write([]byte(`{"response":` + snapshot + `}`))
 }
 
+func SnapshotGetMonitoringLegacyHandler(w http.ResponseWriter, r 
*http.Request) {
+       inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn"}, nil)
+       if userErr != nil || sysErr != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+               return
+       }
+       defer inf.Close()
+
+       snapshot, cdnExists, err := GetSnapshotMonitoring(inf.Tx.Tx, 
inf.Params["cdn"])
+
+       if err != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting snapshot: "+err.Error()))
+               return
+       }
+       if !cdnExists {
+               api.HandleErr(w, r, inf.Tx.Tx, http.StatusNotFound, 
errors.New("CDN not found"), nil)
+               return
+       }
+       var data tc.TrafficMonitorConfig
+       if err := json.Unmarshal([]byte(snapshot), &data); err != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting snapshot: "+err.Error()))
+               return
+       }
+       convertedData := data.ToLegacyConfig()
+
+       type snapshotResponse struct {
+               Response tc.LegacyTrafficMonitorConfig
+       }
+       bytes, err := json.Marshal(snapshotResponse{Response: convertedData})
+       if err != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("getting snapshot: "+err.Error()))
+               return
+       }
+       w.Header().Set(rfc.ContentType, rfc.ApplicationJSON)
+       w.Write(bytes)

Review comment:
       
`github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api.WriteResp` 
handles all of this for you, and it makes the output legal contents for a text 
file (terminating newline).

##########
File path: lib/go-tc/traffic_router.go
##########
@@ -147,6 +150,96 @@ type TrafficServer struct {
        DeliveryServices []tsdeliveryService 
`json:"deliveryServices,omitempty"` // the deliveryServices key does not exist 
on mids
 }
 
+// GetDefaultAddress returns the ipv4 and ipv6 service addresses of the 
interface
+func (i *InterfaceInfo) GetDefaultAddress() (string, string) {
+       var ipv4 string
+       var ipv6 string
+       for _, ip := range i.IPAddresses {
+               if ip.ServiceAddress {
+                       address := net.ParseIP(ip.Address)
+                       if address == nil {
+                               log.Warnf("Unable to parse ipaddress %v on 
interface %v", ip.Address, i.Name)
+                               continue
+                       }
+                       if address.To4() != nil {
+                               ipv4 = ip.Address
+                       } else if address.To16() != nil {
+                               ipv6 = ip.Address
+                       } else {
+                               log.Warnf("Invalid address %v on interface %v", 
address, i.Name)
+                       }
+
+                       if ipv4 != "" && ipv6 != "" {
+                               break
+                       }
+               }
+       }
+       return ipv4, ipv6
+}
+
+// GetVIPInterface returns the primary interface specified by the `Monitor` 
property of an Interface. First interface marked as `Monitor` is returned.
+func GetVIPInterface(ts TrafficServer) InterfaceInfo {
+       for _, interf := range ts.Interfaces {
+               if interf.Monitor {
+                       return interf
+               }
+       }
+       return InterfaceInfo{}
+}
+
+// ToLegacyServer converts a TrafficServer to LegacyTrafficServer

Review comment:
       GoDoc missing period.




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


Reply via email to