isle/go/daemon/rpc.go

73 lines
2.2 KiB
Go
Raw Normal View History

2024-11-14 20:49:35 +00:00
//go:generate mockery --name RPC --inpackage --filename rpc_mock.go
package daemon
import (
"context"
2024-09-24 09:03:18 +00:00
"isle/bootstrap"
"isle/daemon/daecommon"
"isle/daemon/jsonrpc2"
"isle/daemon/network"
"isle/nebula"
"net/http"
)
// RPC defines the methods which the Daemon exposes over RPC (via the RPCHandler
// or HTTPHandler methods). Method documentation can be found on the Daemon
// type.
2024-09-04 19:24:45 +00:00
type RPC interface {
CreateNetwork(
ctx context.Context,
name string,
domain string,
ipNet nebula.IPNet,
hostName nebula.HostName,
) error
2024-09-04 19:24:45 +00:00
JoinNetwork(context.Context, network.JoiningBootstrap) error
2024-09-04 19:24:45 +00:00
2024-09-24 09:03:18 +00:00
GetNetworks(context.Context) ([]bootstrap.CreationParams, error)
// SetConfig extends the [network.RPC] method of the same name such that
// [ErrUserManagedNetworkConfig] is returned if the picked network is
// configured as part of the [daecommon.Config] which the Daemon was
// initialized with.
//
// See the `network.RPC` documentation in this interface for more usage
// details.
SetConfig(context.Context, daecommon.NetworkConfig) error
// All network.RPC methods are automatically implemented by Daemon using the
// currently joined network. If no network is joined then any call to these
// methods will return ErrNoNetwork.
//
// If more than one Network is joined then all calls to these methods must
// be accompanied with a context produced by WithNetwork, in order to choose
// the network. These methods may return these errors, in addition to those
// documented on the individual methods:
//
2024-09-24 09:03:18 +00:00
// Errors:
// - ErrNoNetwork
// - ErrNoMatchingNetworks
// - ErrMultipleMatchingNetworks
network.RPC
2024-09-04 19:24:45 +00:00
}
// RPCHandler returns a jsonrpc2.Handler which will use the Daemon to serve all
// methods defined on the RPC interface.
func (d *Daemon) RPCHandler() jsonrpc2.Handler {
rpc := RPC(d)
return jsonrpc2.Chain(
jsonrpc2.NewMLogMiddleware(d.logger.WithNamespace("rpc")),
jsonrpc2.ExposeServerSideErrorsMiddleware,
)(
jsonrpc2.NewDispatchHandler(&rpc),
)
}
// HTTPRPCHandler returns an http.Handler which will use the Daemon to serve all
// methods defined on the RPC interface via the JSONRPC2 protocol.
func (d *Daemon) HTTPRPCHandler() http.Handler {
return jsonrpc2.NewHTTPHandler(d.RPCHandler())
}