multinode/nodes: pass Node entity to Add method instead of set of a parameters

The Add method on the multinode DB interface accepts
a set of parameters which are already fields in the nodes.Node struct
excluding the name field.

When adding a new node, you're forced to call UpdateName() method
after calling the Add() method in order to save a node and update
the name.

This change allows passing the nodes.Node entity which includes
the name field. With this, a new node can be added together with
the name without invoking the UpdateName() method.

Change-Id: I281ec628dffaade35d6db4479a84f39636200072
This commit is contained in:
Clement Sam 2021-12-07 07:38:25 +00:00
parent 596124f0f6
commit 137641f090
6 changed files with 20 additions and 16 deletions

View File

@ -242,7 +242,12 @@ func cmdAdd(cmd *cobra.Command, args []string) (err error) {
} }
service := nodes.NewService(log, dialer, db.Nodes()) service := nodes.NewService(log, dialer, db.Nodes())
err = service.Add(ctx, node.NodeID, apiSecret[:], node.PublicAddress) err = service.Add(ctx, nodes.Node{
ID: node.NodeID,
APISecret: apiSecret[:],
PublicAddress: node.PublicAddress,
Name: node.Name,
})
if err != nil { if err != nil {
return err return err
} }

View File

@ -66,7 +66,7 @@ func (controller *Nodes) Add(w http.ResponseWriter, r *http.Request) {
return return
} }
if err = controller.service.Add(ctx, id, apiSecret[:], payload.PublicAddress); err != nil { if err = controller.service.Add(ctx, nodes.Node{ID: id, APISecret: apiSecret[:], PublicAddress: payload.PublicAddress}); err != nil {
switch { switch {
case nodes.ErrNodeNotReachable.Has(err): case nodes.ErrNodeNotReachable.Has(err):
controller.serveError(w, http.StatusNotFound, ErrNodes.Wrap(err)) controller.serveError(w, http.StatusNotFound, ErrNodes.Wrap(err))

View File

@ -102,15 +102,15 @@ func (n *nodesdb) Get(ctx context.Context, id storj.NodeID) (_ nodes.Node, err e
} }
// Add creates new node in NodesDB. // Add creates new node in NodesDB.
func (n *nodesdb) Add(ctx context.Context, id storj.NodeID, apiSecret []byte, publicAddress string) (err error) { func (n *nodesdb) Add(ctx context.Context, node nodes.Node) (err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
_, err = n.methods.Create_Node( _, err = n.methods.Create_Node(
ctx, ctx,
dbx.Node_Id(id.Bytes()), dbx.Node_Id(node.ID.Bytes()),
dbx.Node_Name(""), dbx.Node_Name(node.Name),
dbx.Node_PublicAddress(publicAddress), dbx.Node_PublicAddress(node.PublicAddress),
dbx.Node_ApiSecret(apiSecret), dbx.Node_ApiSecret(node.APISecret),
) )
return ErrNodesDB.Wrap(err) return ErrNodesDB.Wrap(err)

View File

@ -25,8 +25,7 @@ type DB interface {
// because paging doesn't necessarily mean pagination in computing. // because paging doesn't necessarily mean pagination in computing.
ListPaged(ctx context.Context, cursor Cursor) (page Page, err error) ListPaged(ctx context.Context, cursor Cursor) (page Page, err error)
// Add creates new node in NodesDB. // Add creates new node in NodesDB.
// TODO: pass Node entity instead of set of a parameters. Add(ctx context.Context, node Node) error
Add(ctx context.Context, id storj.NodeID, apiSecret []byte, publicAddress string) error
// Remove removed node from NodesDB. // Remove removed node from NodesDB.
Remove(ctx context.Context, id storj.NodeID) error Remove(ctx context.Context, id storj.NodeID) error
// UpdateName will update name of the specified node in database. // UpdateName will update name of the specified node in database.

View File

@ -26,7 +26,7 @@ func TestNodesDB(t *testing.T) {
apiSecret := []byte("secret") apiSecret := []byte("secret")
publicAddress := "228.13.38.1:8081" publicAddress := "228.13.38.1:8081"
err := nodesRepository.Add(ctx, nodeID, apiSecret, publicAddress) err := nodesRepository.Add(ctx, nodes.Node{ID: nodeID, APISecret: apiSecret, PublicAddress: publicAddress})
assert.NoError(t, err) assert.NoError(t, err)
node, err := nodesRepository.Get(ctx, nodeID) node, err := nodesRepository.Get(ctx, nodeID)
@ -72,7 +72,7 @@ func TestNodesDB(t *testing.T) {
Name: fmt.Sprintf("%d", i), Name: fmt.Sprintf("%d", i),
} }
nodeList = append(nodeList, node) nodeList = append(nodeList, node)
err := nodesRepository.Add(ctx, node.ID, node.APISecret, node.PublicAddress) err := nodesRepository.Add(ctx, node)
require.NoError(t, err) require.NoError(t, err)
} }
page, err := nodesRepository.ListPaged(ctx, nodes.Cursor{ page, err := nodesRepository.ListPaged(ctx, nodes.Cursor{

View File

@ -48,13 +48,13 @@ func NewService(log *zap.Logger, dialer rpc.Dialer, nodes DB) *Service {
} }
// Add adds new node to the system. // Add adds new node to the system.
func (service *Service) Add(ctx context.Context, id storj.NodeID, apiSecret []byte, publicAddress string) (err error) { func (service *Service) Add(ctx context.Context, node Node) (err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
// trying to connect to node to check its availability. // trying to connect to node to check its availability.
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{ conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
ID: id, ID: node.ID,
Address: publicAddress, Address: node.PublicAddress,
}) })
if err != nil { if err != nil {
return ErrNodeNotReachable.Wrap(err) return ErrNodeNotReachable.Wrap(err)
@ -65,7 +65,7 @@ func (service *Service) Add(ctx context.Context, id storj.NodeID, apiSecret []by
nodeClient := multinodepb.NewDRPCNodeClient(conn) nodeClient := multinodepb.NewDRPCNodeClient(conn)
header := &multinodepb.RequestHeader{ header := &multinodepb.RequestHeader{
ApiKey: apiSecret, ApiKey: node.APISecret,
} }
// making test request to check node api key. // making test request to check node api key.
@ -77,7 +77,7 @@ func (service *Service) Add(ctx context.Context, id storj.NodeID, apiSecret []by
return Error.Wrap(err) return Error.Wrap(err)
} }
return Error.Wrap(service.nodes.Add(ctx, id, apiSecret, publicAddress)) return Error.Wrap(service.nodes.Add(ctx, node))
} }
// List returns list of all nodes. // List returns list of all nodes.