Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console api changed #102

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/database/gensql/metabase_metadata.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/database/queries/metabase_metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ WITH sources_in_same_dataset AS (
SELECT table_name FROM sources_in_same_dataset sds
JOIN metabase_metadata mbm
ON mbm.dataset_id = sds.dataset_id
WHERE mbm.collection_id IS null;
WHERE mbm.permission_group_id = 0;
44 changes: 29 additions & 15 deletions pkg/nc/nc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ type Client struct {
}

type Team struct {
Slug string `json:"slug"`
GoogleGroupEmail string `json:"googleGroupEmail"`
Environments []Environment `json:"environments"`
Slug string `json:"slug"`
ExternalResources ExternalResources `json:"externalResources"`
Environments []Environment `json:"environments"`
}

type ExternalResources struct {
GoogleGroup GoogleGroup `json:"googleGroup"`
}

type GoogleGroup struct {
Email string `json:"email"`
}

type Environment struct {
Expand All @@ -45,7 +53,8 @@ type Teams struct {
}

type PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
HasNextPage bool `json:"hasNextPage"`
EndCursor *string `json:"endCursor"`
}

type TeamOutput struct {
Expand All @@ -55,40 +64,45 @@ type TeamOutput struct {

func (c *Client) GetTeamGoogleProjects(ctx context.Context) (map[string]TeamOutput, error) {
gqlQuery := `
query GCPTeams($limit: Int, $offset: Int){
teams(limit: $limit, offset: $offset) {
query GCPTeams($first: Int, $after: Cursor){
teams(first: $first, after: $after){
nodes {
slug
googleGroupEmail
externalResources {
googleGroup {
email
}
}
environments {
name
gcpProjectID
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
`

const limit = 100
offset := 0
var offset *string

mapping := map[string]TeamOutput{}

for {
r := Response{}

payload := map[string]any{
"query": gqlQuery,
"variables": map[string]any{
"limit": limit,
"offset": offset,
"first": limit,
"after": offset,
},
}

r := Response{}

err := c.sendRequestAndDeserialize(ctx, http.MethodPost, "/query", payload, &r)
err := c.sendRequestAndDeserialize(ctx, http.MethodPost, "/graphql", payload, &r)
if err != nil {
return nil, fmt.Errorf("fetching team google projects: %w", err)
}
Expand All @@ -97,7 +111,7 @@ func (c *Client) GetTeamGoogleProjects(ctx context.Context) (map[string]TeamOutp
for _, env := range team.Environments {
if env.Name == c.naisClusterName {
mapping[team.Slug] = TeamOutput{
GroupEmail: team.GoogleGroupEmail,
GroupEmail: team.ExternalResources.GoogleGroup.Email,
GCPProjectID: env.GcpProjectID,
}
}
Expand All @@ -108,7 +122,7 @@ func (c *Client) GetTeamGoogleProjects(ctx context.Context) (map[string]TeamOutp
break
}

offset += limit
offset = r.Data.Teams.PageInfo.EndCursor
}

return mapping, nil
Expand Down
26 changes: 19 additions & 7 deletions pkg/nc/nc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ func TestClient_GetTeamGoogleProjects(t *testing.T) {
Teams: nc.Teams{
Nodes: []nc.Team{
{
Slug: "team-1",
GoogleGroupEmail: "team-1@nav.no",
Slug: "team-1",
ExternalResources: nc.ExternalResources{
GoogleGroup: nc.GoogleGroup{
Email: "team-1@nav.no",
},
},
Environments: []nc.Environment{
{
Name: "env-1",
Expand All @@ -38,8 +42,12 @@ func TestClient_GetTeamGoogleProjects(t *testing.T) {
},
},
{
Slug: "team-2",
GoogleGroupEmail: "team-1@nav.no",
Slug: "team-2",
ExternalResources: nc.ExternalResources{
GoogleGroup: nc.GoogleGroup{
Email: "team-1@nav.no",
},
},
Environments: []nc.Environment{
{
Name: "env-2",
Expand Down Expand Up @@ -69,8 +77,12 @@ func TestClient_GetTeamGoogleProjects(t *testing.T) {
Teams: nc.Teams{
Nodes: []nc.Team{
{
Slug: "team-1",
GoogleGroupEmail: "team-1@nav.no",
Slug: "team-1",
ExternalResources: nc.ExternalResources{
GoogleGroup: nc.GoogleGroup{
Email: "team-1@nav.no",
},
},
Environments: []nc.Environment{
{
Name: "env-2",
Expand All @@ -94,7 +106,7 @@ func TestClient_GetTeamGoogleProjects(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/query", r.URL.Path)
assert.Equal(t, "/graphql", r.URL.Path)
assert.Equal(t, "Bearer super-secret", r.Header.Get("Authorization"))
assert.Equal(t, http.MethodPost, r.Method)

Expand Down
2 changes: 1 addition & 1 deletion pkg/service/core/service_metabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ func (s *metabaseService) SyncTableVisibility(ctx context.Context, meta *service
}

func isRestrictedDatabase(meta *service.MetabaseMetadata) bool {
if meta.CollectionID != nil && *meta.CollectionID != 0 {
if meta.PermissionGroupID != nil && *meta.PermissionGroupID != 0 {
return true
}

Expand Down
Loading