added support for account network

Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>
This commit is contained in:
Dmitri Dolguikh
2026-07-29 18:57:08 +02:00
parent e620c86cd4
commit 42ce83a8f3
6 changed files with 76 additions and 4 deletions

View File

@@ -25,6 +25,7 @@ type NetworkMapDBStore interface {
GetNameServerGroups(ctx context.Context, accountId string) ([]nmdata.NameServerGroup, error)
GetNetworkResources(ctx context.Context, accountId string) ([]nmdata.NetworkResource, error)
GetNetworkRouters(ctx context.Context, accountId string) ([]nmdata.NetworkRouter, error)
GetNetwork(ctx context.Context, accountId string) (nmdata.Network, error)
}
type NetworkMapDBStoreImpl struct {

View File

@@ -171,6 +171,28 @@ func TestGetNetworkRouters(t *testing.T) {
// )
}
func TestGetNetwork(t *testing.T) {
ctx := context.TODO()
s, err := NewPostgresqlStore(ctx, "postgresql://root:netbird@localhost:5432/netbird")
assert.NoError(t, err)
// err = loadSQL(ctx, s.pool, initDb)
//assert.NoError(t, err)
n, err := s.GetNetwork(ctx, "d29f99jl0ubs73cm8ce0") //"ckd7ee2fic3c73dtendg")
assert.NoError(t, err)
fmt.Print(n)
// assert.Contains(t,
// groups,
// nmdata.Group{Name: "test-group-1", PublicID: "public-id-1", Resources: []nmdata.Resource{{ID: "cui7q2jl0ubs73d8qpi0", Type: "host"}}},
// )
// assert.Contains(t,
// groups,
// nmdata.Group{Name: "All", PublicID: "d9aejspvcsu517nkh4a0", Resources: []nmdata.Resource{{ID: "cui7olrl0ubs73d8qpe0", Type: "subnet"}, {ID: "cui7q2jl0ubs73d8qpi0", Type: "host"}}},
// )
}
func loadSQL(ctx context.Context, pool *pgxpool.Pool, initdb string) error {
queries := strings.Split(string(initdb), ";")

View File

@@ -0,0 +1,49 @@
package networkmap_pgsql
import (
"context"
"database/sql"
"encoding/json"
"reflect"
"github.com/jackc/pgx/v5"
networkmapdb "github.com/netbirdio/netbird/management/internals/network_map_db"
"github.com/netbirdio/netbird/shared/management/networkmap/nmdata"
)
const (
GetNetworkQuery = `
select network_identifier as identifier, network_net as net, network_net_v6 as net_v6, network_dns as dns, network_serial as serial
from accounts
where id=$1
`
)
func (pg *PgStore) GetNetwork(ctx context.Context, accountId string) (nmdata.Network, error) {
rows, err := pg.pool.Query(ctx, GetNetworkQuery, accountId)
if err != nil {
return nmdata.Network{}, err
}
n, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[network])
if err != nil {
return nmdata.Network{}, err
}
toret := nmdata.Network{}
err = networkmapdb.FromSqlTypesToSharedTypes(
reflect.ValueOf(&n).Elem(), reflect.ValueOf(&toret).Elem())
if err != nil {
return nmdata.Network{}, err
}
return toret, nil
}
type network struct {
Identifier sql.NullString
Net json.RawMessage
NetV6 json.RawMessage
Dns sql.NullString
Serial sql.NullInt64
}

View File

@@ -332,7 +332,7 @@ func TwinNetwork(n *Network) *nmdata.Network {
Net: nc.Net,
NetV6: nc.NetV6,
Dns: nc.Dns,
Serial: nc.Serial,
Serial: int64(nc.Serial),
}
}

View File

@@ -291,7 +291,7 @@ func decodeAccountNetwork(an *proto.AccountNetwork) *nmdata.Network {
n := &nmdata.Network{
Identifier: an.Identifier,
Dns: an.Dns,
Serial: an.Serial,
Serial: int64(an.Serial),
}
if an.NetCidr != "" {
if _, ipnet, err := net.ParseCIDR(an.NetCidr); err == nil && ipnet != nil {

View File

@@ -8,9 +8,9 @@ type Network struct {
Net net.IPNet
NetV6 net.IPNet
Dns string
Serial uint64
Serial int64
}
func (n *Network) CurrentSerial() uint64 {
return n.Serial
return uint64(n.Serial)
}