Skip to content

Commit 6e808e9

Browse files
Fix bigquery dataset access iam roles with primative equivalent (#3471) (#2039)
* account for iam vs primative bigquery roles * add test, fix description * s/EDITOR/WRITER * fix test * comments Signed-off-by: Modular Magician <[email protected]>
1 parent 2dce264 commit 6e808e9

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

.changelog/3471.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
bigquery: Fixed error where `google_bigquery_dataset_access` resources could not be found post-creation if role was set to a predefined IAM role with an equivalent primative role (e.g. `roles/bigquery.dataOwner` and `OWNER`)
3+
```

google-beta/resource_big_query_dataset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ domain specified will be granted the specified access`,
243243
member of the access object. Primitive, Predefined and custom
244244
roles are supported. Predefined roles that have equivalent
245245
primitive roles are swapped by the API to their Primitive
246-
counterparts, and will show a diff post-create. See
246+
counterparts. See
247247
[official docs](https://cloud.google.com/bigquery/docs/access-control).`,
248248
},
249249
"special_group": {

google-beta/resource_big_query_dataset_access.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ import (
2424
"google.golang.org/api/googleapi"
2525
)
2626

27+
var bigqueryAccessRoleToPrimitiveMap = map[string]string{
28+
"roles/bigquery.dataOwner": "OWNER",
29+
"roles/bigquery.dataEditor": "WRITER",
30+
"roles/bigquery.dataViewer": "VIEWER",
31+
}
32+
33+
func resourceBigQueryDatasetAccessRoleDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
34+
if primitiveRole, ok := bigqueryAccessRoleToPrimitiveMap[new]; ok {
35+
return primitiveRole == old
36+
}
37+
return false
38+
}
39+
2740
func resourceBigQueryDatasetAccess() *schema.Resource {
2841
return &schema.Resource{
2942
Create: resourceBigQueryDatasetAccessCreate,
@@ -68,9 +81,10 @@ group, domain, or special group. For example: 'allUsers'`,
6881
ExactlyOneOf: []string{"user_by_email", "group_by_email", "domain", "special_group", "iam_member", "view"},
6982
},
7083
"role": {
71-
Type: schema.TypeString,
72-
Optional: true,
73-
ForceNew: true,
84+
Type: schema.TypeString,
85+
Optional: true,
86+
ForceNew: true,
87+
DiffSuppressFunc: resourceBigQueryDatasetAccessRoleDiffSuppress,
7488
Description: `Describes the rights granted to the user specified by the other
7589
member of the access object. Primitive, Predefined and custom
7690
roles are supported. Predefined roles that have equivalent
@@ -396,6 +410,13 @@ func expandNestedBigQueryDatasetAccessDatasetId(v interface{}, d TerraformResour
396410
}
397411

398412
func expandNestedBigQueryDatasetAccessRole(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
413+
if v == nil {
414+
return nil, nil
415+
}
416+
417+
if primitiveRole, ok := bigqueryAccessRoleToPrimitiveMap[v.(string)]; ok {
418+
return primitiveRole, nil
419+
}
399420
return v, nil
400421
}
401422

google-beta/resource_bigquery_dataset_access_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,37 @@ func TestAccBigQueryDatasetAccess_multiple(t *testing.T) {
106106
})
107107
}
108108

109+
func TestAccBigQueryDatasetAccess_predefinedRole(t *testing.T) {
110+
t.Parallel()
111+
112+
datasetID := fmt.Sprintf("tf_test_%s", randString(t, 10))
113+
114+
expected1 := map[string]interface{}{
115+
"role": "WRITER",
116+
"domain": "google.com",
117+
}
118+
119+
vcrTest(t, resource.TestCase{
120+
PreCheck: func() { testAccPreCheck(t) },
121+
Providers: testAccProviders,
122+
Steps: []resource.TestStep{
123+
{
124+
Config: testAccBigQueryDatasetAccess_predefinedRole(datasetID),
125+
Check: resource.ComposeTestCheckFunc(
126+
testAccCheckBigQueryDatasetAccessPresent(t, "google_bigquery_dataset.dataset", expected1),
127+
),
128+
},
129+
{
130+
// Destroy step instead of CheckDestroy so we can check the access is removed without deleting the dataset
131+
Config: testAccBigQueryDatasetAccess_destroy(datasetID, "dataset"),
132+
Check: resource.ComposeTestCheckFunc(
133+
testAccCheckBigQueryDatasetAccessAbsent(t, "google_bigquery_dataset.dataset", expected1),
134+
),
135+
},
136+
},
137+
})
138+
}
139+
109140
func testAccCheckBigQueryDatasetAccessPresent(t *testing.T, n string, expected map[string]interface{}) resource.TestCheckFunc {
110141
return testAccCheckBigQueryDatasetAccess(t, n, expected, true)
111142
}
@@ -224,3 +255,17 @@ resource "google_bigquery_dataset" "dataset" {
224255
}
225256
`, datasetID)
226257
}
258+
259+
func testAccBigQueryDatasetAccess_predefinedRole(datasetID string) string {
260+
return fmt.Sprintf(`
261+
resource "google_bigquery_dataset_access" "access" {
262+
dataset_id = google_bigquery_dataset.dataset.dataset_id
263+
role = "roles/bigquery.dataEditor"
264+
domain = "google.com"
265+
}
266+
267+
resource "google_bigquery_dataset" "dataset" {
268+
dataset_id = "%s"
269+
}
270+
`, datasetID)
271+
}

website/docs/r/bigquery_dataset.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ The `access` block supports:
206206
member of the access object. Primitive, Predefined and custom
207207
roles are supported. Predefined roles that have equivalent
208208
primitive roles are swapped by the API to their Primitive
209-
counterparts, and will show a diff post-create. See
209+
counterparts. See
210210
[official docs](https://cloud.google.com/bigquery/docs/access-control).
211211

212212
* `special_group` -

0 commit comments

Comments
 (0)