storj/multinode/mutlinodedb/nodes.go
crawter 126450f7dd multinode/database: nodes repository
Change-Id: I327afa1b4bde44f7c2f0d4df10a0792dd5c588a3
2020-10-14 12:46:17 +00:00

90 lines
2.1 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package mutlinodedb
import (
"context"
"github.com/zeebo/errs"
"storj.io/common/storj"
"storj.io/storj/multinode/console"
"storj.io/storj/multinode/mutlinodedb/dbx"
)
// NodesDBError indicates about internal NodesDB error.
var NodesDBError = errs.Class("NodesDB error")
// ensures that nodes implements console.Nodes.
var _ console.Nodes = (*nodes)(nil)
// nodes exposes needed by MND NodesDB functionality.
// dbx implementation of console.Nodes.
//
// architecture: Database
type nodes struct {
methods dbx.Methods
db *multinodeDB
}
// Add creates new node in NodesDB.
func (n *nodes) Add(ctx context.Context, id storj.NodeID, apiSecret []byte, publicAddress string) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = n.methods.Create_Node(
ctx,
dbx.Node_Id(id.Bytes()),
dbx.Node_Name(""),
dbx.Node_Tag(""),
dbx.Node_PublicAddress(publicAddress),
dbx.Node_ApiSecret(apiSecret),
dbx.Node_Logo(nil),
)
return NodesDBError.Wrap(err)
}
// Remove removed node from NodesDB.
func (n *nodes) Remove(ctx context.Context, id storj.NodeID) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = n.methods.Delete_Node_By_Id(ctx, dbx.Node_Id(id.Bytes()))
return NodesDBError.Wrap(err)
}
// GetByID return node from NodesDB by its id.
func (n *nodes) GetByID(ctx context.Context, id storj.NodeID) (_ console.Node, err error) {
defer mon.Task()(&ctx)(&err)
dbxNode, err := n.methods.Get_Node_By_Id(ctx, dbx.Node_Id(id.Bytes()))
if err != nil {
return console.Node{}, NodesDBError.Wrap(err)
}
node, err := fromDBXNode(ctx, dbxNode)
return node, NodesDBError.Wrap(err)
}
// fromDBXNode converts dbx.Node to console.Node.
func fromDBXNode(ctx context.Context, node *dbx.Node) (_ console.Node, err error) {
defer mon.Task()(&ctx)(&err)
id, err := storj.NodeIDFromBytes(node.Id)
if err != nil {
return console.Node{}, err
}
result := console.Node{
ID: id,
APISecret: node.ApiSecret,
PublicAddress: node.PublicAddress,
Logo: node.Logo,
Tag: node.Tag,
}
return result, nil
}