Skip to content

Commit

Permalink
Add custom flatten to transform json to string -> string map for para…
Browse files Browse the repository at this point in the history
  • Loading branch information
slevenick authored and JanMa committed Oct 25, 2019
1 parent 482f093 commit 273354a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
2 changes: 2 additions & 0 deletions products/bigquerydatatransfer/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
properties:
location: !ruby/object:Overrides::Terraform::PropertyOverride
ignore_read: true
params: !ruby/object:Overrides::Terraform::PropertyOverride
custom_flatten: templates/terraform/custom_flatten/json_to_string_map.go.erb
examples:
- !ruby/object:Provider::Terraform::Examples
skip_test: true
Expand Down
27 changes: 27 additions & 0 deletions templates/terraform/custom_flatten/json_to_string_map.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%- # the license inside this block applies to this file
# Copyright 2019 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
func flatten<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
}

kv := v.(map[string]interface{})

res := make(map[string]string)
for key, value := range kv {
res[key] = fmt.Sprintf("%v", value)
}
return res
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
// but it will get deleted by parallel tests, so they need to be ran serially.
func TestAccBigqueryDataTransferConfig(t *testing.T) {
testCases := map[string]func(t *testing.T){
"basic": testAccBigqueryDataTransferConfig_scheduledQuery_basic,
"update": testAccBigqueryDataTransferConfig_scheduledQuery_update,
"basic": testAccBigqueryDataTransferConfig_scheduledQuery_basic,
"update": testAccBigqueryDataTransferConfig_scheduledQuery_update,
"booleanParam": testAccBigqueryDataTransferConfig_copy_booleanParam,
}

for name, tc := range testCases {
Expand Down Expand Up @@ -75,6 +76,27 @@ func testAccBigqueryDataTransferConfig_scheduledQuery_update(t *testing.T) {
})
}

func testAccBigqueryDataTransferConfig_copy_booleanParam(t *testing.T) {
random_suffix := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBigqueryDataTransferConfigDestroy,
Steps: []resource.TestStep{
{
Config: testAccBigqueryDataTransferConfig_booleanParam(random_suffix),
},
{
ResourceName: "google_bigquery_data_transfer_config.copy_config",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location"},
},
},
})
}

func testAccCheckBigqueryDataTransferConfigDestroy(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_bigquery_data_transfer_config" {
Expand Down Expand Up @@ -135,3 +157,47 @@ resource "google_bigquery_data_transfer_config" "query_config" {
}
`, random_suffix, random_suffix, schedule, letter)
}

func testAccBigqueryDataTransferConfig_booleanParam(random_suffix string) string {
return fmt.Sprintf(`
data "google_project" "project" {}
resource "google_project_iam_member" "permissions" {
role = "roles/iam.serviceAccountShortTermTokenMinter"
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-bigquerydatatransfer.iam.gserviceaccount.com"
}
resource "google_bigquery_dataset" "source_dataset" {
depends_on = [google_project_iam_member.permissions]
dataset_id = "source_%s"
friendly_name = "foo"
description = "bar"
location = "asia-northeast1"
}
resource "google_bigquery_dataset" "destination_dataset" {
depends_on = [google_project_iam_member.permissions]
dataset_id = "destination_%s"
friendly_name = "foo"
description = "bar"
location = "asia-northeast1"
}
resource "google_bigquery_data_transfer_config" "copy_config" {
depends_on = [google_project_iam_member.permissions]
location = "asia-northeast1"
display_name = "Copy test %s"
data_source_id = "cross_region_copy"
destination_dataset_id = google_bigquery_dataset.destination_dataset.dataset_id
params = {
overwrite_destination_table = "true"
source_dataset_id = google_bigquery_dataset.source_dataset.dataset_id
source_project_id = data.google_project.project.project_id
}
}
`, random_suffix, random_suffix, random_suffix)
}

0 comments on commit 273354a

Please sign in to comment.