Skip to content

Commit 9cbe603

Browse files
Add support for org filters (#200)
* Add support for org filters * Fix tests
1 parent c0fdb3a commit 9cbe603

File tree

3 files changed

+91
-6
lines changed

3 files changed

+91
-6
lines changed

handler/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func (c *Client) Nearest(rw http.ResponseWriter, req *http.Request) {
181181
t := q.Get("machine-type")
182182
country := req.Header.Get("X-AppEngine-Country")
183183
sites := q["site"]
184+
org := q.Get("org")
184185
strict := false
185186
if qsStrict, err := strconv.ParseBool(q.Get("strict")); err == nil {
186187
strict = qsStrict
@@ -190,7 +191,7 @@ func (c *Client) Nearest(rw http.ResponseWriter, req *http.Request) {
190191
if strict {
191192
country = q.Get("country")
192193
}
193-
opts := &heartbeat.NearestOptions{Type: t, Country: country, Sites: sites, Strict: strict}
194+
opts := &heartbeat.NearestOptions{Type: t, Country: country, Sites: sites, Org: org, Strict: strict}
194195
targetInfo, err := c.LocatorV2.Nearest(service, lat, lon, opts)
195196
if err != nil {
196197
result.Error = v2.NewError("nearest", "Failed to lookup nearest machines", http.StatusInternalServerError)

heartbeat/location.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type NearestOptions struct {
3030
Type string // Limit results to only machines of this type.
3131
Sites []string // Limit results to only machines at these sites.
3232
Country string // Bias results to prefer machines in this country.
33+
Org string // Limit results to only machines from this organization.
3334
Strict bool // When used with Country, limit results to only machines in this country.
3435
}
3536

@@ -161,6 +162,18 @@ func isValidInstance(service string, lat, lon float64, v v2.HeartbeatMessage, op
161162
return false, host.Name{}, 0
162163
}
163164

165+
if opts.Org != "" {
166+
// We are filtering on user-specified organization.
167+
if opts.Org != "mlab" && machineName.Version == "v2" {
168+
// All v2 names are "mlab" managed.
169+
return false, host.Name{}, 0
170+
}
171+
if machineName.Version == "v3" && opts.Org != machineName.Org {
172+
return false, host.Name{}, 0
173+
}
174+
// NOTE: Org == "mlab" will allow all v2 names.
175+
}
176+
164177
if _, ok := r.Services[service]; !ok {
165178
return false, host.Name{}, 0
166179
}

heartbeat/location_test.go

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ var (
109109
},
110110
Health: &v2.Health{Score: 1},
111111
}
112+
autonodeInstance = v2.HeartbeatMessage{
113+
Registration: &v2.Registration{
114+
City: "Council Bluffs",
115+
CountryCode: "US",
116+
ContinentCode: "NA",
117+
Experiment: "ndt",
118+
Hostname: "ndt-oma396982-2248791f.foo.sandbox.measurement-lab.org",
119+
Latitude: 41.3032,
120+
Longitude: -95.8941,
121+
Machine: "2248791f",
122+
Metro: "oma",
123+
Project: "mlab-sandbox",
124+
Probability: 1.0,
125+
Site: "oma396982",
126+
Type: "virtual",
127+
Uplink: "10g",
128+
Services: validNDT7Services,
129+
},
130+
Health: &v2.Health{Score: 1},
131+
}
112132
weheInstance = v2.HeartbeatMessage{
113133
Registration: &v2.Registration{
114134
City: "Portland",
@@ -186,6 +206,31 @@ var (
186206
},
187207
},
188208
}
209+
autonodeSite = site{
210+
distance: 1701.749354381346,
211+
registration: v2.Registration{
212+
City: "Council Bluffs",
213+
CountryCode: "US",
214+
ContinentCode: "NA",
215+
Experiment: "ndt",
216+
Latitude: 41.3032,
217+
Longitude: -95.8941,
218+
Metro: "oma",
219+
Project: "mlab-sandbox",
220+
Probability: 1.0,
221+
Site: "oma396982",
222+
Type: "virtual",
223+
Uplink: "10g",
224+
Services: validNDT7Services,
225+
},
226+
machines: []machine{
227+
{
228+
name: "ndt-oma396982-2248791f.foo.sandbox.measurement-lab.org",
229+
host: "ndt-oma396982-2248791f.foo.sandbox.measurement-lab.org",
230+
health: v2.Health{Score: 1},
231+
},
232+
},
233+
}
189234
weheSite = site{
190235
distance: 3710.7679340078703,
191236
registration: v2.Registration{
@@ -395,6 +440,7 @@ func TestFilterSites(t *testing.T) {
395440
"virtual1": virtualInstance1,
396441
"virtual2": virtualInstance2,
397442
"physical": physicalInstance,
443+
"autonode": autonodeInstance,
398444
"wehe": weheInstance,
399445
}
400446

@@ -404,6 +450,7 @@ func TestFilterSites(t *testing.T) {
404450
typ string
405451
country string
406452
strict bool
453+
org string
407454
lat float64
408455
lon float64
409456
expected []site
@@ -415,7 +462,7 @@ func TestFilterSites(t *testing.T) {
415462
country: "US",
416463
lat: 43.1988,
417464
lon: -75.3242,
418-
expected: []site{virtualSite, physicalSite},
465+
expected: []site{virtualSite, autonodeSite, physicalSite},
419466
},
420467
{
421468
name: "NDT7-physical",
@@ -433,7 +480,7 @@ func TestFilterSites(t *testing.T) {
433480
country: "US",
434481
lat: 43.1988,
435482
lon: -75.3242,
436-
expected: []site{virtualSite},
483+
expected: []site{virtualSite, autonodeSite},
437484
},
438485
{
439486
name: "wehe",
@@ -461,7 +508,7 @@ func TestFilterSites(t *testing.T) {
461508
strict: true,
462509
lat: 43.1988,
463510
lon: -75.3242,
464-
expected: []site{virtualSite, physicalSite},
511+
expected: []site{virtualSite, autonodeSite, physicalSite},
465512
},
466513
{
467514
name: "country-with-strict-no-results",
@@ -473,11 +520,35 @@ func TestFilterSites(t *testing.T) {
473520
lon: -75.3242,
474521
expected: []site{},
475522
},
523+
{
524+
name: "org-skip-v2-names",
525+
service: "ndt/ndt7",
526+
org: "foo",
527+
lat: 43.1988,
528+
lon: -75.3242,
529+
expected: []site{autonodeSite},
530+
},
531+
{
532+
name: "org-skip-v3-names-different-org",
533+
service: "ndt/ndt7",
534+
org: "zoom",
535+
lat: 43.1988,
536+
lon: -75.3242,
537+
expected: []site{},
538+
},
539+
{
540+
name: "org-allow-v2-names-for-mlab-org",
541+
service: "ndt/ndt7",
542+
org: "mlab",
543+
lat: 43.1988,
544+
lon: -75.3242,
545+
expected: []site{virtualSite, physicalSite},
546+
},
476547
}
477548

478549
for _, tt := range tests {
479550
t.Run(tt.name, func(t *testing.T) {
480-
opts := &NearestOptions{Type: tt.typ, Country: tt.country, Strict: tt.strict}
551+
opts := &NearestOptions{Type: tt.typ, Country: tt.country, Strict: tt.strict, Org: tt.org}
481552
got := filterSites(tt.service, tt.lat, tt.lon, instances, opts)
482553

483554
sortSites(got)
@@ -488,7 +559,7 @@ func TestFilterSites(t *testing.T) {
488559
}
489560

490561
if !reflect.DeepEqual(got, tt.expected) {
491-
t.Errorf("filterSites() got: %+v, want: %+v", got, tt.expected)
562+
t.Errorf("filterSites()\n got: %+v\nwant: %+v", got, tt.expected)
492563
}
493564
})
494565
}

0 commit comments

Comments
 (0)