From f04bce9abf2d85b14a9a28939d683bf7f216f113 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Mon, 15 Apr 2024 18:07:09 +0800 Subject: [PATCH] feat: add function to get group project IDs from Lagoon API DB --- internal/lagoondb/client.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/internal/lagoondb/client.go b/internal/lagoondb/client.go index d5ae232..198b108 100644 --- a/internal/lagoondb/client.go +++ b/internal/lagoondb/client.go @@ -1,3 +1,4 @@ +// Package lagoondb implements a client for the Lagoon API database. package lagoondb import ( @@ -20,6 +21,13 @@ type Project struct { Name string `db:"name"` } +// groupProjectMapping maps Lagoon group ID to project ID. +// This type is only used for database unmarshalling. +type groupProjectMapping struct { + GroupID string `db:"group_id"` + ProjectID int `db:"project_id"` +} + // ErrNoResult is returned by client methods if there is no result. var ErrNoResult = errors.New("no rows in result set") @@ -36,8 +44,7 @@ func NewClient(ctx context.Context, dsn string) (*Client, error) { return &Client{db: db}, nil } -// Projects returns the Environment associated with the given -// Namespace name (on Openshift this is the project name). +// Projects returns a slice of all Projects in the Lagoon API DB. func (c *Client) Projects(ctx context.Context) ([]Project, error) { // run query var projects []Project @@ -52,3 +59,28 @@ func (c *Client) Projects(ctx context.Context) ([]Project, error) { } return projects, nil } + +// GroupProjectsMap returns a map of Group (UU)IDs to Project IDs. +// This denotes Project Group membership in Lagoon. +func (c *Client) GroupProjectsMap( + ctx context.Context, +) (map[string][]int, error) { + var gpms []groupProjectMapping + err := c.db.SelectContext(ctx, &gpms, ` + SELECT group_id, project_id + FROM kc_group_projects`) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, ErrNoResult + } + return nil, err + } + groupProjectsMap := map[string][]int{} + // no need to check for duplicates here since the table has: + // UNIQUE KEY `group_project` (`group_id`,`project_id`) + for _, gpm := range gpms { + groupProjectsMap[gpm.GroupID] = + append(groupProjectsMap[gpm.GroupID], gpm.ProjectID) + } + return groupProjectsMap, nil +}