From 40610586b008a268c2e79fc10be1cce47f7268d3 Mon Sep 17 00:00:00 2001 From: Abhishek Verma Date: Wed, 8 Nov 2023 11:42:48 +0530 Subject: [PATCH] =?UTF-8?q?fix(provider):=20bigquery=20provider=20with=20o?= =?UTF-8?q?nly=20resource=20type=20table=20insert=E2=80=A6=20(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(provider): bigquery provider with only resource type table inserts 0 resources to db * chore: add comments * chore: update provider test cases --- plugins/providers/bigquery/provider.go | 12 +++- plugins/providers/bigquery/provider_test.go | 62 +++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/plugins/providers/bigquery/provider.go b/plugins/providers/bigquery/provider.go index cdbd6fc1a..56a8a8bcc 100644 --- a/plugins/providers/bigquery/provider.go +++ b/plugins/providers/bigquery/provider.go @@ -143,10 +143,12 @@ func (p *Provider) GetResources(pc *domain.ProviderConfig) ([]*domain.Resource, dataset := d.ToDomain() dataset.ProviderType = pc.Type dataset.ProviderURN = pc.URN - var fetchTables bool - + fetchTables := true + //setting fetchTables to true here so that even if we try to get only tables we can do that, + //previously it wasn't possible as fetchTables was getting set to true only from within the dataset code block below. if containsString(resourceTypes, ResourceTypeDataset) { mu.Lock() + fetchTables = false defer mu.Unlock() if datasetFilter != "" { v, err := evaluator.Expression(datasetFilter).EvaluateWithStruct(dataset) @@ -175,7 +177,11 @@ func (p *Provider) GetResources(pc *domain.ProviderConfig) ([]*domain.Resource, table.ProviderURN = pc.URN children[i] = table } - dataset.Children = children + if containsString(resourceTypes, ResourceTypeDataset) { + dataset.Children = children + } else { + resources = append(resources, children...) + } } return nil }) diff --git a/plugins/providers/bigquery/provider_test.go b/plugins/providers/bigquery/provider_test.go index 277aff51a..eedb17b1a 100644 --- a/plugins/providers/bigquery/provider_test.go +++ b/plugins/providers/bigquery/provider_test.go @@ -366,6 +366,68 @@ func TestGetResources(t *testing.T) { assert.Nil(t, actualError) client.AssertExpectations(t) }) + + t.Run("should return table resource only", func(t *testing.T) { + encryptor := new(mocks.Encryptor) + client := new(mocks.BigQueryClient) + l := log.NewNoop() + p := bigquery.NewProvider("", encryptor, l) + p.Clients = map[string]bigquery.BigQueryClient{ + "resource-name": client, + } + validCredentials := bigquery.Credentials{ + ServiceAccountKey: base64.StdEncoding.EncodeToString([]byte("service-account-key-json")), + ResourceName: "projects/resource-name", + } + pc := &domain.ProviderConfig{ + Type: domain.ProviderTypeBigQuery, + URN: "test-project-id", + Credentials: validCredentials, + Resources: []*domain.ResourceConfig{ + { + Type: "table", + Roles: []*domain.Role{ + { + ID: "VIEWER", + Name: "VIEWER", + Permissions: []interface{}{"VIEWER"}, + }, + }, + }, + }, + } + expectedDatasets := []*bigquery.Dataset{ + { + ProjectID: "p_id", + DatasetID: "d_id", + }, + } + expectedTables := []*bigquery.Table{ + { + ProjectID: "p_id", + DatasetID: "d_id", + TableID: "t_id", + }, + } + client.On("GetDatasets", mock.Anything).Return(expectedDatasets, nil).Once() + client.On("GetTables", mock.Anything, mock.Anything).Return(expectedTables, nil).Once() + var resources []*domain.Resource + children := []*domain.Resource{ + { + ProviderType: domain.ProviderTypeBigQuery, + ProviderURN: "test-project-id", + Name: "t_id", + URN: "p_id:d_id.t_id", + Type: "table", + }, + } + expectedResources := append(resources, children...) + actualResources, actualError := p.GetResources(pc) + + assert.Equal(t, expectedResources, actualResources) + assert.Nil(t, actualError) + client.AssertExpectations(t) + }) } func TestGrantAccess(t *testing.T) {