Skip to content

Commit 59a8047

Browse files
Chief-Rishabravisuhag
authored andcommitted
fix: use mutex locks while fetching resources in errgroup
1 parent 94113b9 commit 59a8047

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

plugins/providers/bigquery/model_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bigquery_test
33
import (
44
"testing"
55

6+
"github.com/odpf/guardian/core/resource"
67
"github.com/odpf/guardian/domain"
78
"github.com/odpf/guardian/plugins/providers/bigquery"
89
"github.com/stretchr/testify/assert"
@@ -26,6 +27,39 @@ func TestDataSet(t *testing.T) {
2627
URN: "p_id:d_id",
2728
},
2829
},
30+
{
31+
ds: &bigquery.Dataset{
32+
ProjectID: "p_id",
33+
DatasetID: "d_id",
34+
Labels: nil,
35+
},
36+
expectedResource: &domain.Resource{
37+
Type: bigquery.ResourceTypeDataset,
38+
Name: "d_id",
39+
URN: "p_id:d_id",
40+
},
41+
},
42+
{
43+
ds: &bigquery.Dataset{
44+
ProjectID: "p_id",
45+
DatasetID: "d_id",
46+
Labels: map[string]string{
47+
"key1": "value1",
48+
},
49+
},
50+
expectedResource: &domain.Resource{
51+
Type: bigquery.ResourceTypeDataset,
52+
Name: "d_id",
53+
URN: "p_id:d_id",
54+
Details: map[string]interface{}{
55+
resource.ReservedDetailsKeyMetadata: map[string]interface{}{
56+
"labels": map[string]string{
57+
"key1": "value1",
58+
},
59+
},
60+
},
61+
},
62+
},
2963
}
3064

3165
for _, tc := range testCases {
@@ -34,6 +68,7 @@ func TestDataSet(t *testing.T) {
3468
assert.Equal(t, tc.expectedResource.Type, actualResource.Type)
3569
assert.Equal(t, tc.expectedResource.Name, actualResource.Name)
3670
assert.Equal(t, tc.expectedResource.URN, actualResource.URN)
71+
assert.Equal(t, tc.expectedResource.Details, actualResource.Details)
3772
}
3873
})
3974
})
@@ -89,6 +124,41 @@ func TestTable(t *testing.T) {
89124
URN: "p_id:d_id.t_id",
90125
},
91126
},
127+
{
128+
tb: &bigquery.Table{
129+
TableID: "t_id",
130+
ProjectID: "p_id",
131+
DatasetID: "d_id",
132+
Labels: nil,
133+
},
134+
expectedResource: &domain.Resource{
135+
Type: bigquery.ResourceTypeTable,
136+
Name: "t_id",
137+
URN: "p_id:d_id.t_id",
138+
},
139+
},
140+
{
141+
tb: &bigquery.Table{
142+
TableID: "t_id",
143+
ProjectID: "p_id",
144+
DatasetID: "d_id",
145+
Labels: map[string]string{
146+
"key1": "value1",
147+
},
148+
},
149+
expectedResource: &domain.Resource{
150+
Type: bigquery.ResourceTypeTable,
151+
Name: "t_id",
152+
URN: "p_id:d_id.t_id",
153+
Details: map[string]interface{}{
154+
resource.ReservedDetailsKeyMetadata: map[string]interface{}{
155+
"labels": map[string]string{
156+
"key1": "value1",
157+
},
158+
},
159+
},
160+
},
161+
},
92162
}
93163

94164
for _, tc := range testCases {
@@ -97,6 +167,7 @@ func TestTable(t *testing.T) {
97167
assert.Equal(t, tc.expectedResource.Type, actualResource.Type)
98168
assert.Equal(t, tc.expectedResource.Name, actualResource.Name)
99169
assert.Equal(t, tc.expectedResource.URN, actualResource.URN)
170+
assert.Equal(t, tc.expectedResource.Details, actualResource.Details)
100171
}
101172
})
102173
})

plugins/providers/bigquery/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (p *Provider) GetResources(pc *domain.ProviderConfig) ([]*domain.Resource,
124124
resources := []*domain.Resource{}
125125
eg, ctx := errgroup.WithContext(context.TODO())
126126
eg.SetLimit(10)
127+
var mu sync.Mutex
127128

128129
datasets, err := client.GetDatasets(ctx)
129130
if err != nil {
@@ -137,6 +138,8 @@ func (p *Provider) GetResources(pc *domain.ProviderConfig) ([]*domain.Resource,
137138
dataset.ProviderURN = pc.URN
138139

139140
if containsString(resourceTypes, ResourceTypeDataset) {
141+
mu.Lock()
142+
defer mu.Unlock()
140143
resources = append(resources, dataset)
141144
}
142145

0 commit comments

Comments
 (0)