Skip to content

Commit

Permalink
Refactor Bus Routes (#668)
Browse files Browse the repository at this point in the history
* bus: fix route consistency

* bus: fix bucket route

* debug: add logging

* Revert "debug: add logging"

This reverts commit b560a7c.

* accounts: fix bad merge

* bus: add old routes to ease UI switch
  • Loading branch information
peterjan authored Oct 30, 2023
1 parent 2be77bb commit 3ea0663
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 99 deletions.
6 changes: 3 additions & 3 deletions api/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,20 @@ type UpdateBlocklistRequest struct {
Clear bool `json:"clear"`
}

// AccountsUpdateBalanceRequest is the request type for /accounts/:id/update
// AccountsUpdateBalanceRequest is the request type for /account/:id/update
// endpoint.
type AccountsUpdateBalanceRequest struct {
HostKey types.PublicKey `json:"hostKey"`
Amount *big.Int `json:"amount"`
}

// AccountsRequiresSyncRequest is the request type for
// /accounts/:id/requiressync endpoint.
// /account/:id/requiressync endpoint.
type AccountsRequiresSyncRequest struct {
HostKey types.PublicKey `json:"hostKey"`
}

// AccountsAddBalanceRequest is the request type for /accounts/:id/add
// AccountsAddBalanceRequest is the request type for /account/:id/add
// endpoint.
type AccountsAddBalanceRequest struct {
HostKey types.PublicKey `json:"hostKey"`
Expand Down
161 changes: 88 additions & 73 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -1987,56 +1987,33 @@ func (b *bus) multipartHandlerListPartsPOST(jc jape.Context) {
// Handler returns an HTTP handler that serves the bus API.
func (b *bus) Handler() http.Handler {
return jape.Mux(tracing.TracedRoutes("bus", map[string]jape.Handler{
"GET /alerts": b.handleGETAlerts,
"POST /alerts/dismiss": b.handlePOSTAlertsDismiss,
"POST /alerts/register": b.handlePOSTAlertsRegister,
"GET /accounts": b.accountsHandlerGET,
"POST /accounts/:id": b.accountHandlerGET,
"POST /accounts/:id/lock": b.accountsLockHandlerPOST,
"POST /accounts/:id/unlock": b.accountsUnlockHandlerPOST,
"POST /accounts/:id/add": b.accountsAddHandlerPOST,
"POST /accounts/:id/update": b.accountsUpdateHandlerPOST,
"POST /accounts/:id/requiressync": b.accountsRequiresSyncHandlerPOST,
"POST /accounts/:id/resetdrift": b.accountsResetDriftHandlerPOST,

"GET /autopilots": b.autopilotsListHandlerGET,
"GET /autopilots/:id": b.autopilotsHandlerGET,
"PUT /autopilots/:id": b.autopilotsHandlerPUT,

"GET /syncer/address": b.syncerAddrHandler,
"GET /syncer/peers": b.syncerPeersHandler,
"POST /syncer/connect": b.syncerConnectHandler,
"GET /accounts": b.accountsHandlerGET,
"POST /account/:id": b.accountHandlerGET,
"POST /account/:id/add": b.accountsAddHandlerPOST,
"POST /account/:id/lock": b.accountsLockHandlerPOST,
"POST /account/:id/unlock": b.accountsUnlockHandlerPOST,
"POST /account/:id/update": b.accountsUpdateHandlerPOST,
"POST /account/:id/requiressync": b.accountsRequiresSyncHandlerPOST,
"POST /account/:id/resetdrift": b.accountsResetDriftHandlerPOST,

"GET /alerts": b.handleGETAlerts,
"POST /alerts/dismiss": b.handlePOSTAlertsDismiss,
"POST /alerts/register": b.handlePOSTAlertsRegister,

"GET /autopilots": b.autopilotsListHandlerGET,
"GET /autopilot/:id": b.autopilotsHandlerGET,
"PUT /autopilot/:id": b.autopilotsHandlerPUT,

"GET /buckets": b.bucketsHandlerGET,
"POST /buckets": b.bucketsHandlerPOST,
"PUT /bucket/:name/policy": b.bucketsHandlerPolicyPUT,
"DELETE /bucket/:name": b.bucketHandlerDELETE,
"GET /bucket/:name": b.bucketHandlerGET,

"POST /consensus/acceptblock": b.consensusAcceptBlock,
"GET /consensus/state": b.consensusStateHandler,
"GET /consensus/network": b.consensusNetworkHandler,
"GET /consensus/siafundfee/:payout": b.contractTaxHandlerGET,

"GET /txpool/recommendedfee": b.txpoolFeeHandler,
"GET /txpool/transactions": b.txpoolTransactionsHandler,
"POST /txpool/broadcast": b.txpoolBroadcastHandler,

"GET /wallet": b.walletHandler,
"GET /wallet/transactions": b.walletTransactionsHandler,
"GET /wallet/outputs": b.walletOutputsHandler,
"POST /wallet/fund": b.walletFundHandler,
"POST /wallet/sign": b.walletSignHandler,
"POST /wallet/redistribute": b.walletRedistributeHandler,
"POST /wallet/discard": b.walletDiscardHandler,
"POST /wallet/prepare/form": b.walletPrepareFormHandler,
"POST /wallet/prepare/renew": b.walletPrepareRenewHandler,
"GET /wallet/pending": b.walletPendingHandler,

"GET /hosts": b.hostsHandlerGET,
"GET /host/:hostkey": b.hostsPubkeyHandlerGET,
"POST /hosts/scans": b.hostsScanHandlerPOST,
"POST /hosts/pricetables": b.hostsPricetableHandlerPOST,
"POST /hosts/remove": b.hostsRemoveHandlerPOST,
"GET /hosts/allowlist": b.hostsAllowlistHandlerGET,
"PUT /hosts/allowlist": b.hostsAllowlistHandlerPUT,
"GET /hosts/blocklist": b.hostsBlocklistHandlerGET,
"PUT /hosts/blocklist": b.hostsBlocklistHandlerPUT,
"GET /hosts/scanning": b.hostsScanningHandlerGET,
"GET /consensus/state": b.consensusStateHandler,

"GET /contracts": b.contractsHandlerGET,
"DELETE /contracts/all": b.contractsAllHandlerDELETE,
Expand All @@ -2050,20 +2027,33 @@ func (b *bus) Handler() http.Handler {
"POST /contracts/spending": b.contractsSpendingHandlerPOST,
"GET /contract/:id": b.contractIDHandlerGET,
"POST /contract/:id": b.contractIDHandlerPOST,
"GET /contract/:id/ancestors": b.contractIDAncestorsHandler,
"POST /contract/:id/renewed": b.contractIDRenewedHandlerPOST,
"DELETE /contract/:id": b.contractIDHandlerDELETE,
"POST /contract/:id/acquire": b.contractAcquireHandlerPOST,
"GET /contract/:id/ancestors": b.contractIDAncestorsHandler,
"POST /contract/:id/keepalive": b.contractKeepaliveHandlerPOST,
"POST /contract/:id/renewed": b.contractIDRenewedHandlerPOST,
"POST /contract/:id/release": b.contractReleaseHandlerPOST,
"GET /contract/:id/roots": b.contractIDRootsHandlerGET,
"GET /contract/:id/size": b.contractSizeHandlerGET,
"DELETE /contract/:id": b.contractIDHandlerDELETE,

"GET /buckets": b.bucketsHandlerGET,
"POST /buckets": b.bucketsHandlerPOST,
"PUT /buckets/:name/policy": b.bucketsHandlerPolicyPUT,
"DELETE /buckets/:name": b.bucketHandlerDELETE,
"GET /buckets/:name": b.bucketHandlerGET,
"GET /hosts": b.hostsHandlerGET,
"GET /hosts/allowlist": b.hostsAllowlistHandlerGET,
"PUT /hosts/allowlist": b.hostsAllowlistHandlerPUT,
"GET /hosts/blocklist": b.hostsBlocklistHandlerGET,
"PUT /hosts/blocklist": b.hostsBlocklistHandlerPUT,
"POST /hosts/pricetables": b.hostsPricetableHandlerPOST,
"POST /hosts/remove": b.hostsRemoveHandlerPOST,
"POST /hosts/scans": b.hostsScanHandlerPOST,
"GET /hosts/scanning": b.hostsScanningHandlerGET,
"GET /host/:hostkey": b.hostsPubkeyHandlerGET,

"POST /multipart/create": b.multipartHandlerCreatePOST,
"POST /multipart/abort": b.multipartHandlerAbortPOST,
"POST /multipart/complete": b.multipartHandlerCompletePOST,
"PUT /multipart/part": b.multipartHandlerUploadPartPUT,
"GET /multipart/upload/:id": b.multipartHandlerUploadGET,
"POST /multipart/listuploads": b.multipartHandlerListUploadsPOST,
"POST /multipart/listparts": b.multipartHandlerListPartsPOST,

"GET /objects/*path": b.objectsHandlerGET,
"PUT /objects/*path": b.objectsHandlerPUT,
Expand All @@ -2072,15 +2062,42 @@ func (b *bus) Handler() http.Handler {
"POST /objects/rename": b.objectsRenameHandlerPOST,
"POST /objects/list": b.objectsListHandlerPOST,

"GET /params/upload": b.paramsHandlerUploadGET,
"GET /params/gouging": b.paramsHandlerGougingGET,
"GET /params/upload": b.paramsHandlerUploadGET,

"GET /syncer/address": b.syncerAddrHandler,
"POST /syncer/connect": b.syncerConnectHandler,
"GET /syncer/peers": b.syncerPeersHandler,

"GET /txpool/recommendedfee": b.txpoolFeeHandler,
"GET /txpool/transactions": b.txpoolTransactionsHandler,
"POST /txpool/broadcast": b.txpoolBroadcastHandler,

"GET /wallet": b.walletHandler,
"POST /wallet/discard": b.walletDiscardHandler,
"POST /wallet/fund": b.walletFundHandler,
"GET /wallet/outputs": b.walletOutputsHandler,
"GET /wallet/pending": b.walletPendingHandler,
"POST /wallet/prepare/form": b.walletPrepareFormHandler,
"POST /wallet/prepare/renew": b.walletPrepareRenewHandler,
"POST /wallet/redistribute": b.walletRedistributeHandler,
"POST /wallet/sign": b.walletSignHandler,
"GET /wallet/transactions": b.walletTransactionsHandler,

"GET /slabbuffers": b.slabbuffersHandlerGET,
"POST /slabbuffer/fetch": b.packedSlabsHandlerFetchPOST,
"POST /slabbuffer/done": b.packedSlabsHandlerDonePOST,
"POST /slabbuffer/fetch": b.packedSlabsHandlerFetchPOST,

"POST /search/hosts": b.searchHostsHandlerPOST,
"GET /search/objects": b.searchObjectsHandlerGET,

"DELETE /sectors/:hk/:root": b.sectorsHostRootHandlerDELETE,

"GET /settings": b.settingsHandlerGET,
"GET /setting/:key": b.settingKeyHandlerGET,
"PUT /setting/:key": b.settingKeyHandlerPUT,
"DELETE /setting/:key": b.settingKeyHandlerDELETE,

"POST /slabs/migration": b.slabsMigrationHandlerPOST,
"GET /slabs/partial/:key": b.slabsPartialHandlerGET,
"POST /slabs/partial": b.slabsPartialHandlerPOST,
Expand All @@ -2089,33 +2106,31 @@ func (b *bus) Handler() http.Handler {
"GET /slab/:key/objects": b.slabObjectsHandlerGET,
"PUT /slab": b.slabHandlerPUT,

"POST /search/hosts": b.searchHostsHandlerPOST,
"GET /search/objects": b.searchObjectsHandlerGET,

"GET /settings": b.settingsHandlerGET,
"GET /setting/:key": b.settingKeyHandlerGET,
"PUT /setting/:key": b.settingKeyHandlerPUT,
"DELETE /setting/:key": b.settingKeyHandlerDELETE,

"GET /state": b.stateHandlerGET,
"GET /stats/objects": b.objectsStatshandlerGET,

"POST /upload/:id": b.uploadTrackHandlerPOST,
"POST /upload/:id/sector": b.uploadAddSectorHandlerPOST,
"DELETE /upload/:id": b.uploadFinishedHandlerDELETE,

"POST /multipart/create": b.multipartHandlerCreatePOST,
"POST /multipart/abort": b.multipartHandlerAbortPOST,
"POST /multipart/complete": b.multipartHandlerCompletePOST,
"PUT /multipart/part": b.multipartHandlerUploadPartPUT,
"GET /multipart/upload/:id": b.multipartHandlerUploadGET,
"POST /multipart/listuploads": b.multipartHandlerListUploadsPOST,
"POST /multipart/listparts": b.multipartHandlerListPartsPOST,
"POST /upload/:id/sector": b.uploadAddSectorHandlerPOST,

"GET /webhooks": b.webhookHandlerGet,
"POST /webhooks": b.webhookHandlerPost,
"POST /webhooks/action": b.webhookActionHandlerPost,
"POST /webhook/delete": b.webhookHandlerDelete,

// TODO: remove these deprecated routes
"POST /accounts/:id": b.accountHandlerGET,
"POST /accounts/:id/add": b.accountsAddHandlerPOST,
"POST /accounts/:id/lock": b.accountsLockHandlerPOST,
"POST /accounts/:id/unlock": b.accountsUnlockHandlerPOST,
"POST /accounts/:id/update": b.accountsUpdateHandlerPOST,
"POST /accounts/:id/requiressync": b.accountsRequiresSyncHandlerPOST,
"POST /accounts/:id/resetdrift": b.accountsResetDriftHandlerPOST,
"GET /autopilots/:id": b.autopilotsHandlerGET,
"PUT /autopilots/:id": b.autopilotsHandlerPUT,
"PUT /buckets/:name/policy": b.bucketsHandlerPolicyPUT,
"DELETE /buckets/:name": b.bucketHandlerDELETE,
"GET /buckets/:name": b.bucketHandlerGET,
}))
}

Expand Down
14 changes: 7 additions & 7 deletions bus/client/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// Account returns the account for given id.
func (c *Client) Account(ctx context.Context, id rhpv3.Account, host types.PublicKey) (account api.Account, err error) {
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/accounts/%s", id), api.AccountHandlerPOST{
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/account/%s", id), api.AccountHandlerPOST{
HostKey: host,
}, &account)
return
Expand All @@ -27,7 +27,7 @@ func (c *Client) Accounts(ctx context.Context) (accounts []api.Account, err erro

// AddBalance adds the given amount to an account's balance, the amount can be negative.
func (c *Client) AddBalance(ctx context.Context, id rhpv3.Account, hk types.PublicKey, amount *big.Int) (err error) {
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/accounts/%s/add", id), api.AccountsAddBalanceRequest{
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/account/%s/add", id), api.AccountsAddBalanceRequest{
HostKey: hk,
Amount: amount,
}, nil)
Expand All @@ -37,7 +37,7 @@ func (c *Client) AddBalance(ctx context.Context, id rhpv3.Account, hk types.Publ
// LockAccount locks an account.
func (c *Client) LockAccount(ctx context.Context, id rhpv3.Account, hostKey types.PublicKey, exclusive bool, duration time.Duration) (account api.Account, lockID uint64, err error) {
var resp api.AccountsLockHandlerResponse
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/accounts/%s/lock", id), api.AccountsLockHandlerRequest{
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/account/%s/lock", id), api.AccountsLockHandlerRequest{
HostKey: hostKey,
Exclusive: exclusive,
Duration: api.DurationMS(duration),
Expand All @@ -47,13 +47,13 @@ func (c *Client) LockAccount(ctx context.Context, id rhpv3.Account, hostKey type

// ResetDrift resets the drift of an account to zero.
func (c *Client) ResetDrift(ctx context.Context, id rhpv3.Account) (err error) {
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/accounts/%s/resetdrift", id), nil, nil)
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/account/%s/resetdrift", id), nil, nil)
return
}

// SetBalance sets the given account's balance to a certain amount.
func (c *Client) SetBalance(ctx context.Context, id rhpv3.Account, hk types.PublicKey, amount *big.Int) (err error) {
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/accounts/%s/update", id), api.AccountsUpdateBalanceRequest{
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/account/%s/update", id), api.AccountsUpdateBalanceRequest{
HostKey: hk,
Amount: amount,
}, nil)
Expand All @@ -62,15 +62,15 @@ func (c *Client) SetBalance(ctx context.Context, id rhpv3.Account, hk types.Publ

// ScheduleSync sets the requiresSync flag of an account.
func (c *Client) ScheduleSync(ctx context.Context, id rhpv3.Account, hk types.PublicKey) (err error) {
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/accounts/%s/requiressync", id), api.AccountsRequiresSyncRequest{
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/account/%s/requiressync", id), api.AccountsRequiresSyncRequest{
HostKey: hk,
}, nil)
return
}

// UnlockAccount unlocks an account.
func (c *Client) UnlockAccount(ctx context.Context, id rhpv3.Account, lockID uint64) (err error) {
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/accounts/%s/unlock", id), api.AccountsUnlockHandlerRequest{
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/account/%s/unlock", id), api.AccountsUnlockHandlerRequest{
LockID: lockID,
}, nil)
return
Expand Down
4 changes: 2 additions & 2 deletions bus/client/autopilots.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Autopilot returns the autopilot with the given ID.
func (c *Client) Autopilot(ctx context.Context, id string) (autopilot api.Autopilot, err error) {
err = c.c.WithContext(ctx).GET(fmt.Sprintf("/autopilots/%s", id), &autopilot)
err = c.c.WithContext(ctx).GET(fmt.Sprintf("/autopilot/%s", id), &autopilot)
return
}

Expand All @@ -21,6 +21,6 @@ func (c *Client) Autopilots(ctx context.Context) (autopilots []api.Autopilot, er

// UpdateAutopilot updates the given autopilot in the store.
func (c *Client) UpdateAutopilot(ctx context.Context, autopilot api.Autopilot) (err error) {
err = c.c.WithContext(ctx).PUT(fmt.Sprintf("/autopilots/%s", autopilot.ID), autopilot)
err = c.c.WithContext(ctx).PUT(fmt.Sprintf("/autopilot/%s", autopilot.ID), autopilot)
return
}
28 changes: 14 additions & 14 deletions bus/client/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"go.sia.tech/renterd/api"
)

// Bucket returns information about a specific bucket.
func (c *Client) Bucket(ctx context.Context, bucketName string) (resp api.Bucket, err error) {
err = c.c.WithContext(ctx).GET(fmt.Sprintf("/bucket/%s", bucketName), &resp)
return
}

// CreateBucket creates a new bucket.
func (c *Client) CreateBucket(ctx context.Context, bucketName string, opts api.CreateBucketOptions) error {
return c.c.WithContext(ctx).POST("/buckets", api.BucketCreateRequest{
Expand All @@ -15,26 +21,20 @@ func (c *Client) CreateBucket(ctx context.Context, bucketName string, opts api.C
}, nil)
}

// UpdateBucketPolicy updates the policy of an existing bucket.
func (c *Client) UpdateBucketPolicy(ctx context.Context, bucketName string, policy api.BucketPolicy) error {
return c.c.WithContext(ctx).PUT(fmt.Sprintf("/buckets/%s/policy", bucketName), api.BucketUpdatePolicyRequest{
Policy: policy,
})
}

// DeleteBucket deletes an existing bucket. Fails if the bucket isn't empty.
func (c *Client) DeleteBucket(ctx context.Context, bucketName string) error {
return c.c.WithContext(ctx).DELETE(fmt.Sprintf("/buckets/%s", bucketName))
}

// Bucket returns information about a specific bucket.
func (c *Client) Bucket(ctx context.Context, bucketName string) (resp api.Bucket, err error) {
err = c.c.WithContext(ctx).GET(fmt.Sprintf("/buckets/%s", bucketName), &resp)
return
return c.c.WithContext(ctx).DELETE(fmt.Sprintf("/bucket/%s", bucketName))
}

// ListBuckets lists all available buckets.
func (c *Client) ListBuckets(ctx context.Context) (buckets []api.Bucket, err error) {
err = c.c.WithContext(ctx).GET("/buckets", &buckets)
return
}

// UpdateBucketPolicy updates the policy of an existing bucket.
func (c *Client) UpdateBucketPolicy(ctx context.Context, bucketName string, policy api.BucketPolicy) error {
return c.c.WithContext(ctx).PUT(fmt.Sprintf("/bucket/%s/policy", bucketName), api.BucketUpdatePolicyRequest{
Policy: policy,
})
}

0 comments on commit 3ea0663

Please sign in to comment.