mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-09 15:09:08 +02:00
also build proxy-cluster to peer idx
Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>
This commit is contained in:
@@ -22,7 +22,7 @@ const (
|
|||||||
|
|
||||||
type NetworkMapDBStore interface {
|
type NetworkMapDBStore interface {
|
||||||
GetGroups(ctx context.Context, accountId string) ([]nmdata.Group, map[string]map[string]any, error)
|
GetGroups(ctx context.Context, accountId string) ([]nmdata.Group, map[string]map[string]any, error)
|
||||||
GetPeers(ctx context.Context, accountId string) ([]nmdata.Peer, error)
|
GetPeers(ctx context.Context, accountId string) ([]nmdata.Peer, map[string]*nmdata.Peer, error)
|
||||||
GetPolicies(ctx context.Context, accountId string) ([]nmdata.Policy, map[string]map[string]any, map[string]map[string]any, error)
|
GetPolicies(ctx context.Context, accountId string) ([]nmdata.Policy, map[string]map[string]any, map[string]map[string]any, error)
|
||||||
GetRoutes(ctx context.Context, accountId string) ([]nmdata.Route, error)
|
GetRoutes(ctx context.Context, accountId string) ([]nmdata.Route, error)
|
||||||
GetNameServerGroups(ctx context.Context, accountId string) ([]nmdata.NameServerGroup, error)
|
GetNameServerGroups(ctx context.Context, accountId string) ([]nmdata.NameServerGroup, error)
|
||||||
|
|||||||
@@ -43,8 +43,9 @@ func TestGetPeers(t *testing.T) {
|
|||||||
// err = loadSQL(ctx, s.pool, initDb)
|
// err = loadSQL(ctx, s.pool, initDb)
|
||||||
//assert.NoError(t, err)
|
//assert.NoError(t, err)
|
||||||
|
|
||||||
peers, err := s.GetPeers(ctx, "ck7bnf2t2r9s739pkug0") //"ckd7ee2fic3c73dtendg")
|
peers, clusterToPeerIdx, err := s.GetPeers(ctx, "d8pqjvbl0ubs73e8cjkg") //"ckd7ee2fic3c73dtendg")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, clusterToPeerIdx)
|
||||||
|
|
||||||
fmt.Print(peers)
|
fmt.Print(peers)
|
||||||
// assert.Contains(t,
|
// assert.Contains(t,
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func (pg *PgStore) GetNetworkMapData(ctx context.Context, accountId string) (*ne
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return rollbackAndReturnError(ctx, tx, err)
|
return rollbackAndReturnError(ctx, tx, err)
|
||||||
}
|
}
|
||||||
peers, err := GetPeersViaPgxConnection(ctx, tx.Conn(), accountId)
|
peers, _, err := GetPeersViaPgxConnection(ctx, tx.Conn(), accountId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rollbackAndReturnError(ctx, tx, err)
|
return rollbackAndReturnError(ctx, tx, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
GetPeersQuery = `
|
GetPeersQuery = `
|
||||||
select id, key, ssh_key, dns_label, user_id, ssh_enabled, login_expiration_enabled, last_login, ip, ipv6,
|
select id, key, ssh_key, dns_label, user_id, ssh_enabled, login_expiration_enabled, last_login, ip, ipv6,
|
||||||
peer_status_requires_approval, proxy_meta_embedded,
|
peer_status_requires_approval, proxy_meta_embedded, proxy_meta_cluster,
|
||||||
meta_wt_version, meta_go_os, meta_os_version, meta_kernel_version, meta_network_addresses, meta_files, meta_capabilities, meta_flags,
|
meta_wt_version, meta_go_os, meta_os_version, meta_kernel_version, meta_network_addresses, meta_files, meta_capabilities, meta_flags,
|
||||||
location_country_code, location_city_name, location_connection_ip
|
location_country_code, location_city_name, location_connection_ip
|
||||||
from peers
|
from peers
|
||||||
@@ -22,37 +22,41 @@ const (
|
|||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
func (pg *PgStore) GetPeers(ctx context.Context, accountId string) ([]nmdata.Peer, error) {
|
func (pg *PgStore) GetPeers(ctx context.Context, accountId string) ([]nmdata.Peer, map[string]*nmdata.Peer, error) {
|
||||||
c, err := pg.Pool.Acquire(ctx)
|
c, err := pg.Pool.Acquire(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
return GetPeersViaPgxConnection(ctx, c.Conn(), accountId)
|
return GetPeersViaPgxConnection(ctx, c.Conn(), accountId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPeersViaPgxConnection(ctx context.Context, con *pgx.Conn, accountId string) ([]nmdata.Peer, error) {
|
func GetPeersViaPgxConnection(ctx context.Context, con *pgx.Conn, accountId string) ([]nmdata.Peer, map[string]*nmdata.Peer, error) {
|
||||||
rows, err := con.Query(ctx, GetPeersQuery, accountId)
|
rows, err := con.Query(ctx, GetPeersQuery, accountId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
peers, err := pgx.CollectRows(rows, pgx.RowToStructByName[peer])
|
peers, err := pgx.CollectRows(rows, pgx.RowToStructByName[peer])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
toret := make([]nmdata.Peer, 0, len(peers))
|
toret := make([]nmdata.Peer, 0, len(peers))
|
||||||
|
clusterToPeerIdx := make(map[string]*nmdata.Peer)
|
||||||
for _, p := range peers {
|
for _, p := range peers {
|
||||||
dp := nmdata.Peer{}
|
dp := nmdata.Peer{}
|
||||||
err := networkmapdb.FromSqlTypesToSharedTypes(
|
err := networkmapdb.FromSqlTypesToSharedTypes(
|
||||||
reflect.ValueOf(&p), reflect.ValueOf(&dp))
|
reflect.ValueOf(&p), reflect.ValueOf(&dp))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.ProxyMetaEmbedded.Valid {
|
if p.ProxyMetaEmbedded.Valid {
|
||||||
dp.ProxyMeta.Embedded = p.ProxyMetaEmbedded.Bool
|
dp.ProxyMeta.Embedded = p.ProxyMetaEmbedded.Bool
|
||||||
}
|
}
|
||||||
|
if dp.ProxyMeta.Embedded {
|
||||||
|
clusterToPeerIdx[p.ProxyMetaCluster.String] = &dp
|
||||||
|
}
|
||||||
if p.MetaWtVersion.Valid {
|
if p.MetaWtVersion.Valid {
|
||||||
dp.Meta.WtVersion = p.MetaWtVersion.String
|
dp.Meta.WtVersion = p.MetaWtVersion.String
|
||||||
}
|
}
|
||||||
@@ -74,36 +78,36 @@ func GetPeersViaPgxConnection(ctx context.Context, con *pgx.Conn, accountId stri
|
|||||||
if p.LocationConnectionIp != nil {
|
if p.LocationConnectionIp != nil {
|
||||||
err := json.Unmarshal(p.LocationConnectionIp, &dp.Location.ConnectionIP)
|
err := json.Unmarshal(p.LocationConnectionIp, &dp.Location.ConnectionIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return toret, err
|
return toret, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p.MetaFiles != nil {
|
if p.MetaFiles != nil {
|
||||||
err := json.Unmarshal(p.MetaFiles, &dp.Meta.Files)
|
err := json.Unmarshal(p.MetaFiles, &dp.Meta.Files)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return toret, err
|
return toret, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p.MetaCapabilities != nil {
|
if p.MetaCapabilities != nil {
|
||||||
err := json.Unmarshal(p.MetaCapabilities, &dp.Meta.Capabilities)
|
err := json.Unmarshal(p.MetaCapabilities, &dp.Meta.Capabilities)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return toret, err
|
return toret, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p.MetaFlags != nil {
|
if p.MetaFlags != nil {
|
||||||
err := json.Unmarshal(p.MetaFlags, &dp.Meta.Flags)
|
err := json.Unmarshal(p.MetaFlags, &dp.Meta.Flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return toret, err
|
return toret, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p.MetaNetworkAddresses != nil {
|
if p.MetaNetworkAddresses != nil {
|
||||||
err := json.Unmarshal(p.MetaNetworkAddresses, &dp.Meta.NetworkAddresses)
|
err := json.Unmarshal(p.MetaNetworkAddresses, &dp.Meta.NetworkAddresses)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return toret, err
|
return toret, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return toret, nil
|
return toret, clusterToPeerIdx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO add support for creating struct fields from denormalized fields
|
// TODO add support for creating struct fields from denormalized fields
|
||||||
@@ -116,8 +120,9 @@ type peer struct {
|
|||||||
LastLogin sql.NullTime
|
LastLogin sql.NullTime
|
||||||
SSHEnabled sql.NullBool
|
SSHEnabled sql.NullBool
|
||||||
LoginExpirationEnabled sql.NullBool
|
LoginExpirationEnabled sql.NullBool
|
||||||
PeerStatusRequiresApproval sql.NullBool `nmap:"mapTo=RequiresApproval"`
|
PeerStatusRequiresApproval sql.NullBool `nmap:"map_to:RequiresApproval"`
|
||||||
ProxyMetaEmbedded sql.NullBool `nmap:"skip"`
|
ProxyMetaEmbedded sql.NullBool `nmap:"skip"`
|
||||||
|
ProxyMetaCluster sql.NullString `nmap:"skip"`
|
||||||
IP json.RawMessage
|
IP json.RawMessage
|
||||||
IPv6 json.RawMessage
|
IPv6 json.RawMessage
|
||||||
LocationConnectionIp json.RawMessage `nmap:"skip"`
|
LocationConnectionIp json.RawMessage `nmap:"skip"`
|
||||||
|
|||||||
Reference in New Issue
Block a user