mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-03 20:19:07 +02:00
support for GetNetworkRouters in sqlite
Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>
This commit is contained in:
@@ -6,15 +6,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/management/server/types"
|
|
||||||
"github.com/netbirdio/netbird/shared/management/networkmap/nmdata"
|
"github.com/netbirdio/netbird/shared/management/networkmap/nmdata"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetNetworkRouters(t *testing.T) {
|
func TestGetNetworkRouters(t *testing.T) {
|
||||||
if engine == string(types.SqliteStoreEngine) {
|
|
||||||
t.Skip()
|
|
||||||
}
|
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
execQuery(t, ctx,
|
execQuery(t, ctx,
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package networkmap_sqlite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
networkmapdb "github.com/netbirdio/netbird/management/internals/network_map_db"
|
||||||
|
"github.com/netbirdio/netbird/shared/management/networkmap/nmdata"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetNetworkRouterQuery = `
|
||||||
|
select public_id, peer, network_id, masquerade, metric, enabled, peer_groups, group_peers.peer_id
|
||||||
|
from network_routers, json_each(peer_groups)
|
||||||
|
left join group_peers on group_peers.account_id=? and group_peers.group_id=json_each.value
|
||||||
|
where network_routers.account_id=?
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
func (sc *SqliteStoreConn) GetNetworkRouters(ctx context.Context, accountId string) (map[string]map[string]*nmdata.NetworkRouter, error) {
|
||||||
|
rows, err := sc.Conn.QueryContext(ctx, GetNetworkRouterQuery, accountId, accountId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
routers, err := networkmapdb.CollectRowsForSqlite[networkrouter](rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
toret := make(map[string]map[string]*nmdata.NetworkRouter)
|
||||||
|
for _, router := range routers {
|
||||||
|
if !router.Enabled.Bool {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
networkId := router.NetworkID.String
|
||||||
|
if networkId == "" {
|
||||||
|
return nil, fmt.Errorf("router with public_id %s doesn't have network_id set", router.PublicID.String)
|
||||||
|
}
|
||||||
|
|
||||||
|
nmdatarouter := nmdata.NetworkRouter{}
|
||||||
|
err := networkmapdb.FromSqlTypesToSharedTypes(reflect.ValueOf(&router), reflect.ValueOf(&nmdatarouter))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if toret[networkId] == nil {
|
||||||
|
toret[networkId] = make(map[string]*nmdata.NetworkRouter)
|
||||||
|
}
|
||||||
|
if router.Peer.String != "" {
|
||||||
|
toret[networkId][router.Peer.String] = &nmdatarouter
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if router.PeerViaGroups.String != "" {
|
||||||
|
toret[networkId][router.PeerViaGroups.String] = &nmdatarouter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type networkrouter struct {
|
||||||
|
PublicID sql.NullString
|
||||||
|
Peer sql.NullString `nmap:"skip"`
|
||||||
|
NetworkID sql.NullString `nmap:"skip"`
|
||||||
|
Masquerade sql.NullBool
|
||||||
|
Metric sql.NullInt64
|
||||||
|
Enabled sql.NullBool
|
||||||
|
PeerGroups []byte `nmap:"json"`
|
||||||
|
PeerViaGroups sql.NullString `nmap:"skip"`
|
||||||
|
}
|
||||||
@@ -91,9 +91,6 @@ func (s *SqliteStoreConn) GetPolicies(ctx context.Context, accountId string) ([]
|
|||||||
func (s *SqliteStoreConn) GetRoutes(ctx context.Context, accountId string) ([]nmdata.Route, error) {
|
func (s *SqliteStoreConn) GetRoutes(ctx context.Context, accountId string) ([]nmdata.Route, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
func (s *SqliteStoreConn) GetNetworkRouters(ctx context.Context, accountId string) (map[string]map[string]*nmdata.NetworkRouter, error) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
func (s *SqliteStoreConn) GetNetwork(ctx context.Context, accountId string) (nmdata.Network, error) {
|
func (s *SqliteStoreConn) GetNetwork(ctx context.Context, accountId string) (nmdata.Network, error) {
|
||||||
return nmdata.Network{}, nil
|
return nmdata.Network{}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user