Skip to content

Commit

Permalink
fix(provider): bigquery provider with only resource type table insert… (
Browse files Browse the repository at this point in the history
#104)

* fix(provider): bigquery provider with only resource type table inserts 0 resources to db

* chore: add comments

* chore: update provider test cases
  • Loading branch information
abhishekv24 authored Nov 8, 2023
1 parent 2c3fdbc commit 4061058
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
12 changes: 9 additions & 3 deletions plugins/providers/bigquery/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
})
Expand Down
62 changes: 62 additions & 0 deletions plugins/providers/bigquery/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4061058

Please sign in to comment.