Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow listing sites and filtering on org #52

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/v0/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ListResponse struct {
Error *v2.Error `json:",omitempty"`
StaticConfig []discovery.StaticConfig `json:",omitempty"`
Servers []string `json:",omitempty"`
Sites []string `json:",omitempty"`
}

// Network contains IPv4 and IPv6 addresses.
Expand Down
17 changes: 17 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ func (s *Server) Delete(rw http.ResponseWriter, req *http.Request) {
// List handler is used by monitoring to generate a list of known, active
// hostnames previously registered with the Autojoin API.
func (s *Server) List(rw http.ResponseWriter, req *http.Request) {
// Set CORS policy to allow third-party websites to use returned resources.
rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.Header().Set("Cache-Control", "no-store") // Prevent caching of result.

configs := []discovery.StaticConfig{}
resp := v0.ListResponse{}
hosts, ports, err := s.dnsTracker.List()
Expand All @@ -347,14 +352,21 @@ func (s *Server) List(rw http.ResponseWriter, req *http.Request) {
return
}

org := req.URL.Query().Get("org")
format := req.URL.Query().Get("format")
sites := map[string]bool{}

// Create a prometheus StaticConfig for each known host.
for i := range hosts {
h, err := host.Parse(hosts[i])
if err != nil {
continue
}
if org != "" && org != h.Org {
// Skip hosts that are not part of the given org.
continue
}
sites[h.Site] = true
if format == "script-exporter" {
// NOTE: do not assign any ports for script exporter.
ports[i] = []string{""}
Expand Down Expand Up @@ -396,6 +408,11 @@ func (s *Server) List(rw http.ResponseWriter, req *http.Request) {
case "servers":
resp.Servers = hosts
results = resp
case "sites":
for k := range sites {
resp.Sites = append(resp.Sites, k)
}
results = resp
default:
// NOTE: default format is not valid for prometheus StaticConfig format.
resp.StaticConfig = configs
Expand Down
26 changes: 26 additions & 0 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,28 @@ func TestServer_List(t *testing.T) {
wantCode: http.StatusOK,
wantLength: 1,
},
{
name: "success-sites",
params: "?format=sites&org=foo",
lister: &fakeStatusTracker{
nodes: []string{"ndt-lga3356-040e9f4b.mlab.autojoin.measurement-lab.org"},
ports: [][]string{{"9990"}},
},
wantCode: http.StatusOK,
wantLength: 0,
},
{
name: "success-one-site-two-nodes",
params: "?format=sites&org=mlab",
lister: &fakeStatusTracker{
nodes: []string{
"ndt-lga3356-040e9f4b.mlab.autojoin.measurement-lab.org",
"ndt-lga3356-abcdef12.mlab.autojoin.measurement-lab.org"},
ports: [][]string{{"9990"}, {"9990"}},
},
wantCode: http.StatusOK,
wantLength: 1,
},
{
name: "success-script-exporter",
params: "?format=script-exporter&service=ndt7_client_byos",
Expand Down Expand Up @@ -634,6 +656,10 @@ func TestServer_List(t *testing.T) {
resp := v0.ListResponse{}
err = json.Unmarshal(raw, &resp)
length = len(resp.Servers)
} else if strings.Contains(tt.params, "sites") {
resp := v0.ListResponse{}
err = json.Unmarshal(raw, &resp)
length = len(resp.Sites)
} else {
resp := v0.ListResponse{}
err = json.Unmarshal(raw, &resp)
Expand Down