@@ -25,6 +25,19 @@ import (
2525 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2626)
2727
28+ var sensitiveParams = []string {"secret_access_key" }
29+
30+ func sensitiveParamCustomizeDiff (diff * schema.ResourceDiff , v interface {}) error {
31+ for _ , sp := range sensitiveParams {
32+ mapLabel := diff .Get ("params." + sp ).(string )
33+ authLabel := diff .Get ("sensitive_params.0." + sp ).(string )
34+ if mapLabel != "" && authLabel != "" {
35+ return fmt .Errorf ("Sensitive param [%s] cannot be set in both `params` and the `sensitive_params` block." , sp )
36+ }
37+ }
38+ return nil
39+ }
40+
2841func resourceBigqueryDataTransferConfig () * schema.Resource {
2942 return & schema.Resource {
3043 Create : resourceBigqueryDataTransferConfigCreate ,
@@ -42,6 +55,8 @@ func resourceBigqueryDataTransferConfig() *schema.Resource {
4255 Delete : schema .DefaultTimeout (4 * time .Minute ),
4356 },
4457
58+ CustomizeDiff : sensitiveParamCustomizeDiff ,
59+
4560 Schema : map [string ]* schema.Schema {
4661 "data_source_id" : {
4762 Type : schema .TypeString ,
@@ -106,6 +121,28 @@ about the format here:
106121https://cloud.google.com/appengine/docs/flexible/python/scheduling-jobs-with-cron-yaml#the_schedule_format
107122NOTE: the granularity should be at least 8 hours, or less frequent.` ,
108123 },
124+ "sensitive_params" : {
125+ Type : schema .TypeList ,
126+ Optional : true ,
127+ Description : `Different parameters are configured primarily using the the 'params' field on this
128+ resource. This block contains the parameters which contain secrets or passwords so that they can be marked
129+ sensitive and hidden from plan output. The name of the field, eg: secret_access_key, will be the key
130+ in the 'params' map in the api request.
131+
132+ Credentials may not be specified in both locations and will cause an error. Changing from one location
133+ to a different credential configuration in the config will require an apply to update state.` ,
134+ MaxItems : 1 ,
135+ Elem : & schema.Resource {
136+ Schema : map [string ]* schema.Schema {
137+ "secret_access_key" : {
138+ Type : schema .TypeString ,
139+ Required : true ,
140+ Description : `The Secret Access Key of the AWS account transferring data from.` ,
141+ Sensitive : true ,
142+ },
143+ },
144+ },
145+ },
109146 "service_account_name" : {
110147 Type : schema .TypeString ,
111148 Optional : true ,
@@ -186,6 +223,11 @@ func resourceBigqueryDataTransferConfigCreate(d *schema.ResourceData, meta inter
186223 obj ["params" ] = paramsProp
187224 }
188225
226+ obj , err = resourceBigqueryDataTransferConfigEncoder (d , meta , obj )
227+ if err != nil {
228+ return err
229+ }
230+
189231 url , err := replaceVars (d , config , "{{BigqueryDataTransferBasePath}}projects/{{project}}/locations/{{location}}/transferConfigs?serviceAccountName={{service_account_name}}" )
190232 if err != nil {
191233 return err
@@ -267,6 +309,18 @@ func resourceBigqueryDataTransferConfigRead(d *schema.ResourceData, meta interfa
267309 return handleNotFoundError (err , d , fmt .Sprintf ("BigqueryDataTransferConfig %q" , d .Id ()))
268310 }
269311
312+ res , err = resourceBigqueryDataTransferConfigDecoder (d , meta , res )
313+ if err != nil {
314+ return err
315+ }
316+
317+ if res == nil {
318+ // Decoding the object has resulted in it being gone. It may be marked deleted
319+ log .Printf ("[DEBUG] Removing BigqueryDataTransferConfig because it no longer exists." )
320+ d .SetId ("" )
321+ return nil
322+ }
323+
270324 if err := d .Set ("project" , project ); err != nil {
271325 return fmt .Errorf ("Error reading Config: %s" , err )
272326 }
@@ -351,6 +405,11 @@ func resourceBigqueryDataTransferConfigUpdate(d *schema.ResourceData, meta inter
351405 obj ["params" ] = paramsProp
352406 }
353407
408+ obj , err = resourceBigqueryDataTransferConfigEncoder (d , meta , obj )
409+ if err != nil {
410+ return err
411+ }
412+
354413 url , err := replaceVars (d , config , "{{BigqueryDataTransferBasePath}}{{name}}" )
355414 if err != nil {
356415 return err
@@ -547,3 +606,40 @@ func expandBigqueryDataTransferConfigParams(v interface{}, d TerraformResourceDa
547606 }
548607 return m , nil
549608}
609+
610+ func resourceBigqueryDataTransferConfigEncoder (d * schema.ResourceData , meta interface {}, obj map [string ]interface {}) (map [string ]interface {}, error ) {
611+ paramMap , ok := obj ["params" ]
612+ if ! ok {
613+ paramMap = make (map [string ]string )
614+ }
615+
616+ var params map [string ]string
617+ params = paramMap .(map [string ]string )
618+
619+ for _ , sp := range sensitiveParams {
620+ if auth , _ := d .GetOkExists ("sensitive_params.0." + sp ); auth != "" {
621+ params [sp ] = auth .(string )
622+ }
623+ }
624+
625+ obj ["params" ] = params
626+
627+ return obj , nil
628+ }
629+
630+ func resourceBigqueryDataTransferConfigDecoder (d * schema.ResourceData , meta interface {}, res map [string ]interface {}) (map [string ]interface {}, error ) {
631+ if paramMap , ok := res ["params" ]; ok {
632+ params := paramMap .(map [string ]interface {})
633+ for _ , sp := range sensitiveParams {
634+ if _ , apiOk := params [sp ]; apiOk {
635+ if _ , exists := d .GetOkExists ("sensitive_params.0." + sp ); exists {
636+ delete (params , sp )
637+ } else {
638+ params [sp ] = d .Get ("params." + sp )
639+ }
640+ }
641+ }
642+ }
643+
644+ return res , nil
645+ }
0 commit comments