Skip to content

Commit a4adca1

Browse files
add updates for big query kms changes (#4934) (#3406)
* add updates for big query kms changes * update docs for bigquery_table Signed-off-by: Modular Magician <[email protected]>
1 parent 1b818be commit a4adca1

File tree

7 files changed

+142
-36
lines changed

7 files changed

+142
-36
lines changed

.changelog/4934.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
bigquery: add `kms_key_version` as an output on `bigquery_table.encryption_configuration` and the `destination_encryption_configuration` blocks of `bigquery_job.query`, `bigquery_job.load`, and `bigquery_copy`.
3+
```

google-beta/resource_bigquery_job.go

Lines changed: 105 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ Creation, truncation and append actions occur as one atomic update upon job comp
114114
Description: `Describes the Cloud KMS encryption key that will be used to protect destination BigQuery table.
115115
The BigQuery Service Account associated with your project requires access to this encryption key.`,
116116
},
117+
"kms_key_version": {
118+
Type: schema.TypeString,
119+
Computed: true,
120+
Description: `Describes the Cloud KMS encryption key version used to protect destination BigQuery table.`,
121+
},
117122
},
118123
},
119124
},
@@ -407,6 +412,11 @@ Creation, truncation and append actions occur as one atomic update upon job comp
407412
Description: `Describes the Cloud KMS encryption key that will be used to protect destination BigQuery table.
408413
The BigQuery Service Account associated with your project requires access to this encryption key.`,
409414
},
415+
"kms_key_version": {
416+
Type: schema.TypeString,
417+
Computed: true,
418+
Description: `Describes the Cloud KMS encryption key version used to protect destination BigQuery table.`,
419+
},
410420
},
411421
},
412422
},
@@ -645,6 +655,11 @@ or of the form 'projects/{{project}}/datasets/{{dataset_id}}' if not.`,
645655
Description: `Describes the Cloud KMS encryption key that will be used to protect destination BigQuery table.
646656
The BigQuery Service Account associated with your project requires access to this encryption key.`,
647657
},
658+
"kms_key_version": {
659+
Type: schema.TypeString,
660+
Computed: true,
661+
Description: `Describes the Cloud KMS encryption key version used to protect destination BigQuery table.`,
662+
},
648663
},
649664
},
650665
},
@@ -1344,21 +1359,29 @@ func flattenBigQueryJobConfigurationQuerySchemaUpdateOptions(v interface{}, d *s
13441359
return v
13451360
}
13461361

1362+
// KmsKeyName switched from using a key name to a key version, this will separate the key name from the key version and save them
1363+
// separately in state. https://github.com/hashicorp/terraform-provider-google/issues/9208
13471364
func flattenBigQueryJobConfigurationQueryDestinationEncryptionConfiguration(v interface{}, d *schema.ResourceData, config *Config) interface{} {
13481365
if v == nil {
1349-
return nil
1366+
return []map[string]interface{}{}
13501367
}
1351-
original := v.(map[string]interface{})
1352-
if len(original) == 0 {
1353-
return nil
1368+
1369+
kmsKeyName := v.(map[string]interface{})["kmsKeyName"].(string)
1370+
re := regexp.MustCompile(`(projects/.*/locations/.*/keyRings/.*/cryptoKeys/.*)/cryptoKeyVersions/.*`)
1371+
paths := re.FindStringSubmatch(kmsKeyName)
1372+
1373+
if len(paths) > 0 {
1374+
return []map[string]interface{}{
1375+
{
1376+
"kms_key_name": paths[0],
1377+
"kms_key_version": kmsKeyName,
1378+
},
1379+
}
13541380
}
1355-
transformed := make(map[string]interface{})
1356-
transformed["kms_key_name"] =
1357-
flattenBigQueryJobConfigurationQueryDestinationEncryptionConfigurationKmsKeyName(original["kmsKeyName"], d, config)
1358-
return []interface{}{transformed}
1359-
}
1360-
func flattenBigQueryJobConfigurationQueryDestinationEncryptionConfigurationKmsKeyName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1361-
return v
1381+
1382+
// The key name was returned, no need to set the version
1383+
return []map[string]interface{}{{"kms_key_name": kmsKeyName, "kms_key_version": ""}}
1384+
13621385
}
13631386

13641387
func flattenBigQueryJobConfigurationQueryScriptOptions(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -1578,21 +1601,29 @@ func flattenBigQueryJobConfigurationLoadTimePartitioningField(v interface{}, d *
15781601
return v
15791602
}
15801603

1604+
// KmsKeyName switched from using a key name to a key version, this will separate the key name from the key version and save them
1605+
// separately in state. https://github.com/hashicorp/terraform-provider-google/issues/9208
15811606
func flattenBigQueryJobConfigurationLoadDestinationEncryptionConfiguration(v interface{}, d *schema.ResourceData, config *Config) interface{} {
15821607
if v == nil {
1583-
return nil
1608+
return []map[string]interface{}{}
15841609
}
1585-
original := v.(map[string]interface{})
1586-
if len(original) == 0 {
1587-
return nil
1610+
1611+
kmsKeyName := v.(map[string]interface{})["kmsKeyName"].(string)
1612+
re := regexp.MustCompile(`(projects/.*/locations/.*/keyRings/.*/cryptoKeys/.*)/cryptoKeyVersions/.*`)
1613+
paths := re.FindStringSubmatch(kmsKeyName)
1614+
1615+
if len(paths) > 0 {
1616+
return []map[string]interface{}{
1617+
{
1618+
"kms_key_name": paths[0],
1619+
"kms_key_version": kmsKeyName,
1620+
},
1621+
}
15881622
}
1589-
transformed := make(map[string]interface{})
1590-
transformed["kms_key_name"] =
1591-
flattenBigQueryJobConfigurationLoadDestinationEncryptionConfigurationKmsKeyName(original["kmsKeyName"], d, config)
1592-
return []interface{}{transformed}
1593-
}
1594-
func flattenBigQueryJobConfigurationLoadDestinationEncryptionConfigurationKmsKeyName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1595-
return v
1623+
1624+
// The key name was returned, no need to set the version
1625+
return []map[string]interface{}{{"kms_key_name": kmsKeyName, "kms_key_version": ""}}
1626+
15961627
}
15971628

15981629
func flattenBigQueryJobConfigurationCopy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -1672,21 +1703,29 @@ func flattenBigQueryJobConfigurationCopyWriteDisposition(v interface{}, d *schem
16721703
return v
16731704
}
16741705

1706+
// KmsKeyName switched from using a key name to a key version, this will separate the key name from the key version and save them
1707+
// separately in state. https://github.com/hashicorp/terraform-provider-google/issues/9208
16751708
func flattenBigQueryJobConfigurationCopyDestinationEncryptionConfiguration(v interface{}, d *schema.ResourceData, config *Config) interface{} {
16761709
if v == nil {
1677-
return nil
1710+
return []map[string]interface{}{}
16781711
}
1679-
original := v.(map[string]interface{})
1680-
if len(original) == 0 {
1681-
return nil
1712+
1713+
kmsKeyName := v.(map[string]interface{})["kmsKeyName"].(string)
1714+
re := regexp.MustCompile(`(projects/.*/locations/.*/keyRings/.*/cryptoKeys/.*)/cryptoKeyVersions/.*`)
1715+
paths := re.FindStringSubmatch(kmsKeyName)
1716+
1717+
if len(paths) > 0 {
1718+
return []map[string]interface{}{
1719+
{
1720+
"kms_key_name": paths[0],
1721+
"kms_key_version": kmsKeyName,
1722+
},
1723+
}
16821724
}
1683-
transformed := make(map[string]interface{})
1684-
transformed["kms_key_name"] =
1685-
flattenBigQueryJobConfigurationCopyDestinationEncryptionConfigurationKmsKeyName(original["kmsKeyName"], d, config)
1686-
return []interface{}{transformed}
1687-
}
1688-
func flattenBigQueryJobConfigurationCopyDestinationEncryptionConfigurationKmsKeyName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1689-
return v
1725+
1726+
// The key name was returned, no need to set the version
1727+
return []map[string]interface{}{{"kms_key_name": kmsKeyName, "kms_key_version": ""}}
1728+
16901729
}
16911730

16921731
func flattenBigQueryJobConfigurationExtract(v interface{}, d *schema.ResourceData, config *Config) interface{} {
@@ -2259,13 +2298,24 @@ func expandBigQueryJobConfigurationQueryDestinationEncryptionConfiguration(v int
22592298
transformed["kmsKeyName"] = transformedKmsKeyName
22602299
}
22612300

2301+
transformedKmsKeyVersion, err := expandBigQueryJobConfigurationQueryDestinationEncryptionConfigurationKmsKeyVersion(original["kms_key_version"], d, config)
2302+
if err != nil {
2303+
return nil, err
2304+
} else if val := reflect.ValueOf(transformedKmsKeyVersion); val.IsValid() && !isEmptyValue(val) {
2305+
transformed["kmsKeyVersion"] = transformedKmsKeyVersion
2306+
}
2307+
22622308
return transformed, nil
22632309
}
22642310

22652311
func expandBigQueryJobConfigurationQueryDestinationEncryptionConfigurationKmsKeyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
22662312
return v, nil
22672313
}
22682314

2315+
func expandBigQueryJobConfigurationQueryDestinationEncryptionConfigurationKmsKeyVersion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
2316+
return v, nil
2317+
}
2318+
22692319
func expandBigQueryJobConfigurationQueryScriptOptions(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
22702320
l := v.([]interface{})
22712321
if len(l) == 0 || l[0] == nil {
@@ -2614,13 +2664,24 @@ func expandBigQueryJobConfigurationLoadDestinationEncryptionConfiguration(v inte
26142664
transformed["kmsKeyName"] = transformedKmsKeyName
26152665
}
26162666

2667+
transformedKmsKeyVersion, err := expandBigQueryJobConfigurationLoadDestinationEncryptionConfigurationKmsKeyVersion(original["kms_key_version"], d, config)
2668+
if err != nil {
2669+
return nil, err
2670+
} else if val := reflect.ValueOf(transformedKmsKeyVersion); val.IsValid() && !isEmptyValue(val) {
2671+
transformed["kmsKeyVersion"] = transformedKmsKeyVersion
2672+
}
2673+
26172674
return transformed, nil
26182675
}
26192676

26202677
func expandBigQueryJobConfigurationLoadDestinationEncryptionConfigurationKmsKeyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
26212678
return v, nil
26222679
}
26232680

2681+
func expandBigQueryJobConfigurationLoadDestinationEncryptionConfigurationKmsKeyVersion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
2682+
return v, nil
2683+
}
2684+
26242685
func expandBigQueryJobConfigurationCopy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
26252686
l := v.([]interface{})
26262687
if len(l) == 0 || l[0] == nil {
@@ -2762,13 +2823,24 @@ func expandBigQueryJobConfigurationCopyDestinationEncryptionConfiguration(v inte
27622823
transformed["kmsKeyName"] = transformedKmsKeyName
27632824
}
27642825

2826+
transformedKmsKeyVersion, err := expandBigQueryJobConfigurationCopyDestinationEncryptionConfigurationKmsKeyVersion(original["kms_key_version"], d, config)
2827+
if err != nil {
2828+
return nil, err
2829+
} else if val := reflect.ValueOf(transformedKmsKeyVersion); val.IsValid() && !isEmptyValue(val) {
2830+
transformed["kmsKeyVersion"] = transformedKmsKeyVersion
2831+
}
2832+
27652833
return transformed, nil
27662834
}
27672835

27682836
func expandBigQueryJobConfigurationCopyDestinationEncryptionConfigurationKmsKeyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
27692837
return v, nil
27702838
}
27712839

2840+
func expandBigQueryJobConfigurationCopyDestinationEncryptionConfigurationKmsKeyVersion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
2841+
return v, nil
2842+
}
2843+
27722844
func expandBigQueryJobConfigurationExtract(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
27732845
l := v.([]interface{})
27742846
if len(l) == 0 || l[0] == nil {

google-beta/resource_bigquery_table.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log"
9+
"regexp"
910
"sort"
1011
"strconv"
1112
"strings"
@@ -820,6 +821,11 @@ func resourceBigQueryTable() *schema.Resource {
820821
Required: true,
821822
Description: `The self link or full name of a key which should be used to encrypt this table. Note that the default bigquery service account will need to have encrypt/decrypt permissions on this key - you may want to see the google_bigquery_default_service_account datasource and the google_kms_crypto_key_iam_binding resource.`,
822823
},
824+
"kms_key_version": {
825+
Type: schema.TypeString,
826+
Computed: true,
827+
Description: `The self link or full name of the kms key version used to encrypt this table.`,
828+
},
823829
},
824830
},
825831
},
@@ -1543,7 +1549,20 @@ func expandRangePartitioning(configured interface{}) (*bigquery.RangePartitionin
15431549
}
15441550

15451551
func flattenEncryptionConfiguration(ec *bigquery.EncryptionConfiguration) []map[string]interface{} {
1546-
return []map[string]interface{}{{"kms_key_name": ec.KmsKeyName}}
1552+
re := regexp.MustCompile(`(projects/.*/locations/.*/keyRings/.*/cryptoKeys/.*)/cryptoKeyVersions/.*`)
1553+
paths := re.FindStringSubmatch(ec.KmsKeyName)
1554+
1555+
if len(paths) > 0 {
1556+
return []map[string]interface{}{
1557+
{
1558+
"kms_key_name": paths[0],
1559+
"kms_key_version": ec.KmsKeyName,
1560+
},
1561+
}
1562+
}
1563+
1564+
// The key name was returned, no need to set the version
1565+
return []map[string]interface{}{{"kms_key_name": ec.KmsKeyName, "kms_key_version": ""}}
15471566
}
15481567

15491568
func flattenTimePartitioning(tp *bigquery.TimePartitioning) []map[string]interface{} {

google-beta/resource_dataproc_cluster_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1515

16-
dataproc "google.golang.org/api/dataproc/v1beta2"
1716
"google.golang.org/api/googleapi"
17+
18+
dataproc "google.golang.org/api/dataproc/v1beta2"
1819
)
1920

2021
func TestDataprocExtractInitTimeout(t *testing.T) {

google-beta/resource_gke_hub_feature_membership_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8-
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
8+
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
99
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

website/docs/r/bigquery_job.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ The `destination_encryption_configuration` block supports:
539539
Describes the Cloud KMS encryption key that will be used to protect destination BigQuery table.
540540
The BigQuery Service Account associated with your project requires access to this encryption key.
541541

542+
* `kms_key_version` -
543+
Describes the Cloud KMS encryption key version used to protect destination BigQuery table.
544+
542545
The `script_options` block supports:
543546

544547
* `statement_timeout_ms` -
@@ -731,6 +734,9 @@ The `destination_encryption_configuration` block supports:
731734
Describes the Cloud KMS encryption key that will be used to protect destination BigQuery table.
732735
The BigQuery Service Account associated with your project requires access to this encryption key.
733736

737+
* `kms_key_version` -
738+
Describes the Cloud KMS encryption key version used to protect destination BigQuery table.
739+
734740
The `copy` block supports:
735741

736742
* `source_tables` -
@@ -806,6 +812,9 @@ The `destination_encryption_configuration` block supports:
806812
Describes the Cloud KMS encryption key that will be used to protect destination BigQuery table.
807813
The BigQuery Service Account associated with your project requires access to this encryption key.
808814

815+
* `kms_key_version` -
816+
Describes the Cloud KMS encryption key version used to protect destination BigQuery table.
817+
809818
The `extract` block supports:
810819

811820
* `destination_uris` -

website/docs/r/bigquery_table.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ exported:
322322

323323
* `etag` - A hash of the resource.
324324

325+
* `kms_key_version` - The self link or full name of the kms key version used to encrypt this table.
326+
325327
* `last_modified_time` - The time when this table was last modified, in milliseconds since the epoch.
326328

327329
* `location` - The geographic location where the table resides. This value is inherited from the dataset.

0 commit comments

Comments
 (0)