also return a net resource to groups index from GetGroups call

Signed-off-by: Dmitri Dolguikh <dmitri.external@netbird.io>
This commit is contained in:
Dmitri Dolguikh
2026-08-01 11:56:17 +02:00
parent b02736adc3
commit 600b0c752b
3 changed files with 13 additions and 8 deletions

View File

@@ -19,7 +19,7 @@ const (
)
type NetworkMapDBStore interface {
GetGroups(ctx context.Context, accountId string) ([]nmdata.Group, error)
GetGroups(ctx context.Context, accountId string) ([]nmdata.Group, map[string][]*nmdata.Group, error)
GetPeers(ctx context.Context, accountId string) ([]nmdata.Peer, error)
GetPolicies(ctx context.Context, accountId string) ([]nmdata.Policy, error)
GetRoutes(ctx context.Context, accountId string) ([]nmdata.Route, error)

View File

@@ -23,33 +23,38 @@ const (
`
)
func (pg *PgStore) GetGroups(ctx context.Context, accountId string) ([]nmdata.Group, error) {
func (pg *PgStore) GetGroups(ctx context.Context, accountId string) ([]nmdata.Group, map[string][]*nmdata.Group, error) {
c, err := pg.Pool.Acquire(ctx)
if err != nil {
return nil, err
return nil, nil, err
}
return GetGroupsViaPgxConnection(ctx, c.Conn(), accountId)
}
func GetGroupsViaPgxConnection(ctx context.Context, con *pgx.Conn, accountId string) ([]nmdata.Group, error) {
func GetGroupsViaPgxConnection(ctx context.Context, con *pgx.Conn, accountId string) ([]nmdata.Group, map[string][]*nmdata.Group, error) {
rows, err := con.Query(ctx, GetGroupsQuery, accountId)
if err != nil {
return nil, err
return nil, nil, err
}
groups, err := pgx.CollectRows(rows, pgx.RowToStructByName[group])
toret := make([]nmdata.Group, 0, len(groups))
toretidx := make(map[string][]*nmdata.Group)
for _, g := range groups {
dg := nmdata.Group{}
err := networkmapdb.FromSqlTypesToSharedTypes(
reflect.ValueOf(&g), reflect.ValueOf(&dg))
if err != nil {
return nil, err
return nil, nil, err
}
toret = append(toret, dg)
for _, resource := range dg.Resources {
toretidx[resource.ID] = append(toretidx[resource.ID], &dg)
}
}
return toret, err
return toret, toretidx, err
}
type group struct {

View File

@@ -22,7 +22,7 @@ func (pg *PgStore) GetNetworkMapData(ctx context.Context, accountId string) (*ne
// if err != nil {
// return rollbackAndReturnError(ctx, tx, err)
// }
groups, err := GetGroupsViaPgxConnection(ctx, tx.Conn(), accountId)
groups, netResourceToGroups, err := GetGroupsViaPgxConnection(ctx, tx.Conn(), accountId)
if err != nil {
return rollbackAndReturnError(ctx, tx, err)
}