rawlinp commented on a change in pull request #2862: Add Traffic Ops Golang federations endpoints URL: https://github.com/apache/trafficcontrol/pull/2862#discussion_r221757392
########## File path: traffic_ops/traffic_ops_golang/federations/federations.go ########## @@ -0,0 +1,169 @@ +package federations + +/* + * 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" + + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/lib/go-util" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth" + + "github.com/lib/pq" +) + +func Get(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + + feds, err := getUserFederations(inf.Tx.Tx, inf.User) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("federations.Get getting federations: "+err.Error())) + return + } + fedsResolvers, err := getFederationResolvers(inf.Tx.Tx, fedInfoIDs(feds)) + if err != nil { + api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("federations.Get getting federations resolvers: "+err.Error())) + return + } + allFederations := addResolvers([]tc.IAllFederation{}, feds, fedsResolvers) + api.WriteResp(w, r, allFederations) +} + +func addResolvers(allFederations []tc.IAllFederation, feds []FedInfo, fedsResolvers map[int][]FedResolverInfo) []tc.IAllFederation { + dsFeds := map[tc.DeliveryServiceName][]tc.AllFederationMapping{} + for _, fed := range feds { + mapping := tc.AllFederationMapping{} + mapping.TTL = util.IntPtr(fed.TTL) + mapping.CName = util.StrPtr(fed.CName) + for _, resolver := range fedsResolvers[fed.ID] { + switch resolver.Type { + case tc.FederationResolverType4: + mapping.Resolve4 = append(mapping.Resolve4, resolver.IP) + case tc.FederationResolverType6: + mapping.Resolve6 = append(mapping.Resolve6, resolver.IP) + default: + log.Warnf("federations addResolvers got invalid resolver type for federation '%v', skipping\n", fed.ID) + } + } + dsFeds[fed.DS] = append(dsFeds[fed.DS], mapping) + } + + for ds, mappings := range dsFeds { + allFederations = append(allFederations, tc.AllFederation{DeliveryService: ds, Mappings: mappings}) + } + return allFederations +} + +func fedInfoIDs(feds []FedInfo) []int { + ids := []int{} + for _, fed := range feds { + ids = append(ids, fed.ID) + } + return ids +} + +type FedInfo struct { + ID int + TTL int + CName string + DS tc.DeliveryServiceName +} + +type FedResolverInfo struct { + Type tc.FederationResolverType + IP string +} + +// getFederationResolvers takes a slice of federation IDs, and returns a map[federationID]info. +func getFederationResolvers(tx *sql.Tx, fedIDs []int) (map[int][]FedResolverInfo, error) { + qry := ` +SELECT + ffr.federation, + frt.name as resolver_type, + fr.ip_address +FROM + federation_federation_resolver ffr + JOIN federation_resolver fr ON ffr.federation_resolver = fr.id + JOIN type frt on fr.type = frt.id +WHERE + ffr.federation = ANY($1) +` + rows, err := tx.Query(qry, pq.Array(fedIDs)) + if err != nil { + return nil, errors.New("all federations resolvers querying: " + err.Error()) + } + defer rows.Close() + + feds := map[int][]FedResolverInfo{} + for rows.Next() { + fedID := 0 + f := FedResolverInfo{} + fType := "" + if err := rows.Scan(&fedID, &fType, &f.IP); err != nil { + return nil, errors.New("all federations resolvers scanning: " + err.Error()) + } + f.Type = tc.FederationResolverTypeFromString(fType) + feds[fedID] = append(feds[fedID], f) + } + return feds, nil +} + +func getUserFederations(tx *sql.Tx, user *auth.CurrentUser) ([]FedInfo, error) { Review comment: nitpick: this function doesn't need the full `*auth.CurrentUser`, just the username. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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
