99 "io/ioutil"
1010 "net/http"
1111 "net/url"
12- "regexp"
1312 "time"
1413
14+ "github.com/m-lab/go/host"
1515 "github.com/m-lab/go/rtx"
1616 "github.com/m-lab/locate/static"
1717)
@@ -30,16 +30,18 @@ type target struct {
3030
3131// LegacyLocator manages requests to the legacy mlab-ns service.
3232type LegacyLocator struct {
33- Server url.URL
33+ Server url.URL
34+ project string
3435}
3536
3637// MustNewLegacyLocator creates a new LegacyLocator instance. If the given url
3738// fails to parse, the function will exit.
38- func MustNewLegacyLocator (u string ) * LegacyLocator {
39+ func MustNewLegacyLocator (u , project string ) * LegacyLocator {
3940 server , err := url .Parse (u )
4041 rtx .Must (err , "Failed to parse given url: %q" , u )
4142 return & LegacyLocator {
42- Server : * server ,
43+ Server : * server ,
44+ project : project ,
4345 }
4446}
4547
@@ -74,7 +76,7 @@ func (ll *LegacyLocator) Nearest(ctx context.Context, service, lat, lon string)
7476 if err != nil {
7577 return nil , err
7678 }
77- return collect (opts ), nil
79+ return ll . collect (opts ), nil
7880}
7981
8082// UnmarshalResponse reads the response from the given request and unmarshals
@@ -96,18 +98,23 @@ func UnmarshalResponse(req *http.Request, result interface{}) error {
9698 return json .Unmarshal (b , result )
9799}
98100
99- // NOTE: this pattern assumes mlab-ns is returning flat host names, and should
100- // be future compatible with project-decorated DNS names.
101- var machinePattern = regexp .MustCompile ("([a-z-]+)-(mlab[1-4][.-][a-z]{3}[0-9ct]{2}(\\ .mlab-[a-z]+)?.measurement-lab.org)" )
102-
103- func collect (opts * geoOptions ) []string {
101+ // collect reads all FQDN results from the given options and guarantees to
102+ // return v2 formatted hostnames.
103+ func (ll * LegacyLocator ) collect (opts * geoOptions ) []string {
104104 targets := []string {}
105105 for _ , opt := range * opts {
106- fields := machinePattern . FindStringSubmatch (opt .FQDN )
107- if len ( fields ) < 3 {
106+ name , err := host . Parse (opt .FQDN )
107+ if err != nil {
108108 continue
109109 }
110- targets = append (targets , fields [2 ])
110+ // TODO: after the v2 migration, eliminate v1 logic.
111+ if name .Version == "v1" {
112+ // Convert name to v2.
113+ name .Project = ll .project
114+ name .Version = "v2"
115+ }
116+ // Convert the service name into a canonical machine name.
117+ targets = append (targets , name .String ())
111118 }
112119 return targets
113120}
0 commit comments