Skip to content

Commit

Permalink
bus: deprecate /hosts endpoint infavour of /search/hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Mar 19, 2024
1 parent dabe983 commit 36021ab
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 103 deletions.
15 changes: 6 additions & 9 deletions api/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ type (

// Option types.
type (
HostsOptions struct {
Offset int
Limit int
FilterMode string
GetHostsOptions struct {
Offset int
Limit int
}
HostsForScanningOptions struct {
MaxLastScan TimeRFC3339
Limit int
Offset int
}

SearchHostOptions struct {
AddressContains string
FilterMode string
Expand All @@ -92,20 +92,17 @@ type (
func DefaultSearchHostOptions() SearchHostOptions {
return SearchHostOptions{
Limit: -1,
FilterMode: HostFilterModeAll,
FilterMode: HostFilterModeAllowed,
}
}

func (opts HostsOptions) Apply(values url.Values) {
func (opts GetHostsOptions) Apply(values url.Values) {
if opts.Offset != 0 {
values.Set("offset", fmt.Sprint(opts.Offset))
}
if opts.Limit != 0 {
values.Set("limit", fmt.Sprint(opts.Limit))
}
if opts.FilterMode != "" {
values.Set("filterMode", opts.FilterMode)
}
}

func (opts HostsForScanningOptions) Apply(values url.Values) {
Expand Down
3 changes: 1 addition & 2 deletions autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type Bus interface {

// hostdb
Host(ctx context.Context, hostKey types.PublicKey) (hostdb.HostInfo, error)
Hosts(ctx context.Context, opts api.HostsOptions) ([]hostdb.HostInfo, error)
HostsForScanning(ctx context.Context, opts api.HostsForScanningOptions) ([]hostdb.HostAddress, error)
RemoveOfflineHosts(ctx context.Context, minRecentScanFailures uint64, maxDowntime time.Duration) (uint64, error)
SearchHosts(ctx context.Context, opts api.SearchHostOptions) ([]hostdb.HostInfo, error)
Expand Down Expand Up @@ -196,7 +195,7 @@ func (ap *Autopilot) configHandlerPOST(jc jape.Context) {
state := ap.State()

// fetch hosts
hosts, err := ap.bus.Hosts(ctx, api.HostsOptions{})
hosts, err := ap.bus.SearchHosts(ctx, api.DefaultSearchHostOptions())
if jc.Check("failed to get hosts", err) != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion autopilot/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (c *contractor) performContractMaintenance(ctx context.Context, w Worker) (
}

// fetch all hosts
hosts, err := c.ap.bus.Hosts(ctx, api.HostsOptions{})
hosts, err := c.ap.bus.SearchHosts(ctx, api.DefaultSearchHostOptions())
if err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion autopilot/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type (
// a bit, we currently use inline interfaces to avoid having to update the
// scanner tests with every interface change
bus interface {
Hosts(ctx context.Context, opts api.HostsOptions) ([]hostdb.HostInfo, error)
SearchHosts(ctx context.Context, opts api.SearchHostOptions) ([]hostdb.HostInfo, error)
HostsForScanning(ctx context.Context, opts api.HostsForScanningOptions) ([]hostdb.HostAddress, error)
RemoveOfflineHosts(ctx context.Context, minRecentScanFailures uint64, maxDowntime time.Duration) (uint64, error)
}
Expand Down
4 changes: 2 additions & 2 deletions autopilot/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type mockBus struct {
reqs []string
}

func (b *mockBus) Hosts(ctx context.Context, opts api.HostsOptions) ([]hostdb.HostInfo, error) {
func (b *mockBus) SearchHosts(ctx context.Context, opts api.SearchHostOptions) ([]hostdb.HostInfo, error) {
b.reqs = append(b.reqs, fmt.Sprintf("%d-%d", opts.Offset, opts.Offset+opts.Limit))

start := opts.Offset
Expand All @@ -40,7 +40,7 @@ func (b *mockBus) Hosts(ctx context.Context, opts api.HostsOptions) ([]hostdb.Ho
}

func (b *mockBus) HostsForScanning(ctx context.Context, opts api.HostsForScanningOptions) ([]hostdb.HostAddress, error) {
hosts, err := b.Hosts(ctx, api.HostsOptions{
hosts, err := b.SearchHosts(ctx, api.SearchHostOptions{
Offset: opts.Offset,
Limit: opts.Limit,
})
Expand Down
24 changes: 8 additions & 16 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ type (
// A HostDB stores information about hosts.
HostDB interface {
Host(ctx context.Context, hostKey types.PublicKey) (hostdb.HostInfo, error)
Hosts(ctx context.Context, filterMode string, offset, limit int) ([]hostdb.HostInfo, error)
HostsForScanning(ctx context.Context, maxLastScan time.Time, offset, limit int) ([]hostdb.HostAddress, error)
RecordHostScans(ctx context.Context, scans []hostdb.HostScan) error
RecordPriceTables(ctx context.Context, priceTableUpdate []hostdb.PriceTableUpdate) error
Expand Down Expand Up @@ -285,7 +284,7 @@ func (b *bus) Handler() http.Handler {
"GET /contract/:id/roots": b.contractIDRootsHandlerGET,
"GET /contract/:id/size": b.contractSizeHandlerGET,

"GET /hosts": b.hostsHandlerGET,
"GET /hosts": b.hostsHandlerGETDeprecated,
"GET /hosts/allowlist": b.hostsAllowlistHandlerGET,
"PUT /hosts/allowlist": b.hostsAllowlistHandlerPUT,
"GET /hosts/blocklist": b.hostsBlocklistHandlerGET,
Expand Down Expand Up @@ -755,26 +754,15 @@ func (b *bus) walletPendingHandler(jc jape.Context) {
jc.Encode(relevant)
}

func (b *bus) hostsHandlerGET(jc jape.Context) {
func (b *bus) hostsHandlerGETDeprecated(jc jape.Context) {
offset := 0
limit := -1
filterMode := api.HostFilterModeAllowed
if jc.DecodeForm("offset", &offset) != nil || jc.DecodeForm("limit", &limit) != nil || jc.DecodeForm("filterMode", &filterMode) != nil {
return
}

// validate filterMode
switch filterMode {
case api.HostFilterModeAllowed:
case api.HostFilterModeBlocked:
case api.HostFilterModeAll:
default:
jc.Error(errors.New("invalid filter mode"), http.StatusBadRequest)
if jc.DecodeForm("offset", &offset) != nil || jc.DecodeForm("limit", &limit) != nil {
return
}

// fetch hosts
hosts, err := b.hdb.Hosts(jc.Request.Context(), filterMode, offset, limit)
hosts, err := b.hdb.SearchHosts(jc.Request.Context(), api.HostFilterModeAllowed, "", nil, offset, limit)
if jc.Check(fmt.Sprintf("couldn't fetch hosts %d-%d", offset, offset+limit), err) != nil {
return
}
Expand All @@ -786,6 +774,10 @@ func (b *bus) searchHostsHandlerPOST(jc jape.Context) {
if jc.Decode(&req) != nil {
return
}

// TODO: on the next major release we should:
// - remove api.DefaultSearchHostOptions and set defaults in the handler
// - validate the filter mode here and return a 400
hosts, err := b.hdb.SearchHosts(jc.Request.Context(), req.FilterMode, req.AddressContains, req.KeyIn, req.Offset, req.Limit)
if jc.Check(fmt.Sprintf("couldn't fetch hosts %d-%d", req.Offset, req.Offset+req.Limit), err) != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion bus/client/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *Client) HostBlocklist(ctx context.Context) (blocklist []string, err err
}

// Hosts returns 'limit' hosts at given 'offset'.
func (c *Client) Hosts(ctx context.Context, opts api.HostsOptions) (hosts []hostdb.HostInfo, err error) {
func (c *Client) Hosts(ctx context.Context, opts api.GetHostsOptions) (hosts []hostdb.HostInfo, err error) {
values := url.Values{}
opts.Apply(values)
err = c.c.WithContext(ctx).GET("/hosts?"+values.Encode(), &hosts)
Expand Down
8 changes: 5 additions & 3 deletions internal/test/e2e/blocklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func TestBlocklist(t *testing.T) {
hosts: 3,
})
defer cluster.Shutdown()

// convenience variables
b := cluster.Bus
tt := cluster.tt

Expand Down Expand Up @@ -117,7 +119,7 @@ func TestBlocklist(t *testing.T) {
}

// assert we have 4 hosts
hosts, err := b.Hosts(context.Background(), api.HostsOptions{})
hosts, err := b.SearchHosts(context.Background(), api.DefaultSearchHostOptions())
tt.OK(err)
if len(hosts) != 4 {
t.Fatal("unexpected number of hosts", len(hosts))
Expand All @@ -142,7 +144,7 @@ func TestBlocklist(t *testing.T) {
}

// assert all others are blocked
hosts, err = b.Hosts(context.Background(), api.HostsOptions{})
hosts, err = b.SearchHosts(context.Background(), api.DefaultSearchHostOptions())
tt.OK(err)
if len(hosts) != 1 {
t.Fatal("unexpected number of hosts", len(hosts))
Expand All @@ -152,7 +154,7 @@ func TestBlocklist(t *testing.T) {
tt.OK(b.UpdateHostAllowlist(context.Background(), nil, nil, true))

// assert no hosts are blocked
hosts, err = b.Hosts(context.Background(), api.HostsOptions{})
hosts, err = b.SearchHosts(context.Background(), api.DefaultSearchHostOptions())
tt.OK(err)
if len(hosts) != 5 {
t.Fatal("unexpected number of hosts", len(hosts))
Expand Down
2 changes: 1 addition & 1 deletion internal/test/e2e/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestNewTestCluster(t *testing.T) {
})

// Get host info for every host.
hosts, err := cluster.Bus.Hosts(context.Background(), api.HostsOptions{})
hosts, err := cluster.Bus.SearchHosts(context.Background(), api.DefaultSearchHostOptions())
tt.OK(err)
for _, host := range hosts {
hi, err := cluster.Autopilot.HostInfo(host.PublicKey)
Expand Down
4 changes: 2 additions & 2 deletions internal/test/e2e/pruning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestHostPruning(t *testing.T) {
}

// assert the host was not pruned
hostss, err := b.Hosts(context.Background(), api.HostsOptions{})
hostss, err := b.SearchHosts(context.Background(), api.DefaultSearchHostOptions())
tt.OK(err)
if len(hostss) != 1 {
t.Fatal("host was pruned")
Expand All @@ -96,7 +96,7 @@ func TestHostPruning(t *testing.T) {

// assert the host was pruned
tt.Retry(10, time.Second, func() error {
hostss, err = b.Hosts(context.Background(), api.HostsOptions{})
hostss, err = b.SearchHosts(context.Background(), api.DefaultSearchHostOptions())
tt.OK(err)
if len(hostss) != 0 {
return fmt.Errorf("host was not pruned, %+v", hostss[0].Interactions)
Expand Down
5 changes: 0 additions & 5 deletions stores/hostdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,6 @@ func (ss *SQLStore) SearchHosts(ctx context.Context, filterMode, addressContains
return hosts, err
}

// Hosts returns non-blocked hosts at given offset and limit.
func (ss *SQLStore) Hosts(ctx context.Context, filterMode string, offset, limit int) ([]hostdb.HostInfo, error) {
return ss.SearchHosts(ctx, filterMode, "", nil, offset, limit)
}

func (ss *SQLStore) RemoveOfflineHosts(ctx context.Context, minRecentFailures uint64, maxDowntime time.Duration) (removed uint64, err error) {
// sanity check 'maxDowntime'
if maxDowntime < 0 {
Expand Down

0 comments on commit 36021ab

Please sign in to comment.