Skip to content

Commit 8dd1c9d

Browse files
authored
Add condition and accessPolicyVersion to BQ dataset access (#12475)
1 parent 483e328 commit 8dd1c9d

File tree

6 files changed

+207
-17
lines changed

6 files changed

+207
-17
lines changed

mmv1/products/bigquery/Dataset.yaml

+29-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ docs:
2828
base_url: 'projects/{{project}}/datasets'
2929
self_link: 'projects/{{project}}/datasets/{{dataset_id}}'
3030
has_self_link: true
31+
create_url: 'projects/{{project}}/datasets?accessPolicyVersion=3'
32+
update_url: 'projects/{{project}}/datasets/{{dataset_id}}?accessPolicyVersion=3'
3133
delete_url: 'projects/{{project}}/datasets/{{dataset_id}}?deleteContents={{delete_contents_on_destroy}}'
3234
import_format:
3335
- 'projects/{{project}}/datasets/{{dataset_id}}'
@@ -37,6 +39,7 @@ timeouts:
3739
delete_minutes: 20
3840
custom_code:
3941
constants: 'templates/terraform/constants/bigquery_dataset.go.tmpl'
42+
pre_read: 'templates/terraform/pre_read/bigquery_dataset.go.tmpl'
4043
exclude_sweeper: true
4144
examples:
4245
- name: 'bigquery_dataset_basic'
@@ -83,11 +86,6 @@ examples:
8386
dataset_id: 'example_dataset'
8487
account_name: 'bqowner'
8588
exclude_docs: true
86-
- name: 'bigquery_dataset_external_reference_aws_test'
87-
primary_resource_id: 'dataset'
88-
vars:
89-
dataset_id: 'example_dataset'
90-
exclude_docs: true
9189
- name: 'bigquery_dataset_external_reference_aws'
9290
primary_resource_id: 'dataset'
9391
vars:
@@ -242,6 +240,32 @@ properties:
242240
A-Z), numbers (0-9), or underscores (_). The maximum length
243241
is 256 characters.
244242
required: true
243+
- name: 'condition'
244+
type: NestedObject
245+
description: |
246+
Condition for the binding. If CEL expression in this field is true, this
247+
access binding will be considered.
248+
properties:
249+
- name: expression
250+
type: String
251+
required: true
252+
description: |
253+
Textual representation of an expression in Common Expression Language syntax.
254+
- name: title
255+
type: String
256+
description: |
257+
Title for the expression, i.e. a short string describing its purpose.
258+
This can be used e.g. in UIs which allow to enter the expression.
259+
- name: description
260+
type: String
261+
description: |
262+
Description of the expression. This is a longer text which describes the expression,
263+
e.g. when hovered over it in a UI.
264+
- name: location
265+
type: String
266+
description: |
267+
String indicating the location of the expression for error reporting, e.g. a file
268+
name and a position in the file.
245269
- name: 'creationTime'
246270
type: Integer
247271
description: |

mmv1/products/bigquery/DatasetAccess.yaml

+28-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ docs:
3333
The API does accept both formats but it will always return the legacy format which results in Terraform
3434
showing permanent diff on each plan and apply operation.
3535
base_url: 'projects/{{project}}/datasets/{{dataset_id}}'
36-
self_link: 'projects/{{project}}/datasets/{{dataset_id}}'
36+
id_format: 'projects/{{project}}/datasets/{{dataset_id}}'
37+
self_link: 'projects/{{project}}/datasets/{{dataset_id}}?accessPolicyVersion=3'
3738
create_verb: 'PATCH'
3839
delete_verb: 'PATCH'
3940
immutable: true
@@ -301,3 +302,29 @@ properties:
301302
A-Z), numbers (0-9), or underscores (_). The maximum length
302303
is 256 characters.
303304
required: true
305+
- name: 'condition'
306+
type: NestedObject
307+
description: |
308+
Condition for the binding. If CEL expression in this field is true, this
309+
access binding will be considered.
310+
properties:
311+
- name: expression
312+
type: String
313+
required: true
314+
description: |
315+
Textual representation of an expression in Common Expression Language syntax.
316+
- name: title
317+
type: String
318+
description: |
319+
Title for the expression, i.e. a short string describing its purpose.
320+
This can be used e.g. in UIs which allow to enter the expression.
321+
- name: description
322+
type: String
323+
description: |
324+
Description of the expression. This is a longer text which describes the expression,
325+
e.g. when hovered over it in a UI.
326+
- name: location
327+
type: String
328+
description: |
329+
String indicating the location of the expression for error reporting, e.g. a file
330+
name and a position in the file.

mmv1/templates/terraform/examples/bigquery_dataset_external_reference_aws_test.tf.tmpl

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{/*
2+
The license inside this block applies to this file
3+
Copyright 2024 Google Inc.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/ -}}
13+
url = url + "?accessPolicyVersion=3";

mmv1/third_party/terraform/services/bigquery/resource_bigquery_dataset_access_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,40 @@ func TestAccBigQueryDatasetAccess_userByEmailWithMixedCase(t *testing.T) {
292292
})
293293
}
294294

295+
func TestAccBigQueryDatasetAccess_withCondition(t *testing.T) {
296+
t.Parallel()
297+
298+
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
299+
saID := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
300+
301+
expected := map[string]interface{}{
302+
"condition": map[string]interface{}{
303+
"description": "Request after midnight of 2019-12-31",
304+
"expression": "request.time > timestamp(\"2020-01-01T00:00:00Z\")",
305+
"location": "any.file.anywhere",
306+
"title": "test-condition",
307+
},
308+
"role": "OWNER",
309+
"userByEmail": fmt.Sprintf("%s@%s.iam.gserviceaccount.com", saID, envvar.GetTestProjectFromEnv()),
310+
}
311+
312+
acctest.VcrTest(t, resource.TestCase{
313+
PreCheck: func() { acctest.AccTestPreCheck(t) },
314+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
315+
Steps: []resource.TestStep{
316+
{
317+
Config: testAccBigQueryDatasetAccess_withCondition(datasetID, saID),
318+
Check: testAccCheckBigQueryDatasetAccessPresent(t, "google_bigquery_dataset.dataset", expected),
319+
},
320+
{
321+
// Destroy step instead of CheckDestroy so we can check the access is removed without deleting the dataset
322+
Config: testAccBigQueryDatasetAccess_destroy(datasetID, "dataset"),
323+
Check: testAccCheckBigQueryDatasetAccessAbsent(t, "google_bigquery_dataset.dataset", expected),
324+
},
325+
},
326+
})
327+
}
328+
295329
func TestAccBigQueryDatasetAccess_groupByEmailWithMixedCase(t *testing.T) {
296330
t.Parallel()
297331

@@ -575,3 +609,27 @@ resource "google_bigquery_dataset" "dataset" {
575609
}
576610
`, accessType, email, datasetID)
577611
}
612+
613+
func testAccBigQueryDatasetAccess_withCondition(datasetID, saID string) string {
614+
return fmt.Sprintf(`
615+
resource "google_bigquery_dataset_access" "withCondition" {
616+
dataset_id = google_bigquery_dataset.dataset.dataset_id
617+
role = "OWNER"
618+
user_by_email = google_service_account.bqowner.email
619+
condition {
620+
title = "test-condition"
621+
description = "Request after midnight of 2019-12-31"
622+
expression = "request.time > timestamp(\"2020-01-01T00:00:00Z\")"
623+
location = "any.file.anywhere"
624+
}
625+
}
626+
627+
resource "google_bigquery_dataset" "dataset" {
628+
dataset_id = "%s"
629+
}
630+
631+
resource "google_service_account" "bqowner" {
632+
account_id = "%s"
633+
}
634+
`, datasetID, saID)
635+
}

mmv1/third_party/terraform/services/bigquery/resource_bigquery_dataset_test.go.tmpl

+79
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ func TestAccBigQueryDataset_access(t *testing.T) {
297297
ImportStateVerify: true,
298298
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
299299
},
300+
{
301+
Config: testAccBigQueryDatasetWithConditionAccess(datasetID),
302+
},
303+
{
304+
ResourceName: "google_bigquery_dataset.access_test",
305+
ImportState: true,
306+
ImportStateVerify: true,
307+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
308+
},
300309
{
301310
Config: testAccBigQueryDatasetWithViewAccess(datasetID, otherDatasetID, otherTableID),
302311
},
@@ -454,6 +463,31 @@ func TestAccBigQueryDataset_bigqueryDatasetResourceTags_update(t *testing.T) {
454463
})
455464
}
456465

466+
func TestAccBigQueryDataset_bigqueryDatasetExternalReferenceAws(t *testing.T) {
467+
t.Parallel()
468+
469+
context := map[string]interface{}{
470+
"random_suffix": acctest.RandString(t, 10),
471+
}
472+
473+
acctest.VcrTest(t, resource.TestCase{
474+
PreCheck: func() { acctest.AccTestPreCheck(t) },
475+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
476+
CheckDestroy: testAccCheckBigQueryDatasetDestroyProducer(t),
477+
Steps: []resource.TestStep{
478+
{
479+
Config: testAccBigQueryDataset_bigqueryDatasetExternalReferenceAws(context),
480+
},
481+
{
482+
ResourceName: "google_bigquery_dataset.dataset",
483+
ImportState: true,
484+
ImportStateVerify: true,
485+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "etag", "last_modified_time"},
486+
},
487+
},
488+
})
489+
}
490+
457491
{{- if ne $.TargetVersionName "ga" }}
458492
func TestAccBigQueryDataset_externalCatalogDatasetOptions_update(t *testing.T) {
459493
t.Parallel()
@@ -706,6 +740,35 @@ resource "google_bigquery_dataset" "access_test" {
706740
`, datasetID)
707741
}
708742

743+
func testAccBigQueryDatasetWithConditionAccess(datasetID string) string {
744+
return fmt.Sprintf(`
745+
resource "google_bigquery_dataset" "access_test" {
746+
dataset_id = "%s"
747+
748+
access {
749+
role = "OWNER"
750+
user_by_email = "[email protected]"
751+
}
752+
753+
access {
754+
role = "READER"
755+
user_by_email = "[email protected]"
756+
condition {
757+
title = "test-condition"
758+
description = "Request after midnight of 2019-12-31"
759+
expression = "request.time > timestamp(\"2020-01-01T00:00:00Z\")"
760+
location = "any.file.anywhere"
761+
}
762+
}
763+
764+
labels = {
765+
env = "foo"
766+
default_table_expiration_ms = 3600000
767+
}
768+
}
769+
`, datasetID)
770+
}
771+
709772
func testAccBigQueryDatasetWithThreeAccess(datasetID string) string {
710773
return fmt.Sprintf(`
711774
resource "google_bigquery_dataset" "access_test" {
@@ -939,3 +1002,19 @@ resource "google_bigquery_dataset" "dataset" {
9391002
}
9401003
`, context)
9411004
}
1005+
1006+
func testAccBigQueryDataset_bigqueryDatasetExternalReferenceAws(context map[string]interface{}) string {
1007+
return acctest.Nprintf(`
1008+
resource "google_bigquery_dataset" "dataset" {
1009+
dataset_id = "dataset%{random_suffix}"
1010+
friendly_name = "test"
1011+
description = "This is a test description"
1012+
location = "aws-us-east-1"
1013+
1014+
external_dataset_reference {
1015+
external_source = "aws-glue://arn:aws:glue:us-east-1:772042918353:database/db_other_formats_external"
1016+
connection = "projects/bigquerytestdefault/locations/aws-us-east-1/connections/external_test-connection"
1017+
}
1018+
}
1019+
`, context)
1020+
}

0 commit comments

Comments
 (0)