@@ -5,11 +5,14 @@ import (
55 "fmt"
66 "log"
77 "os"
8+ "regexp"
89 "strings"
910 "testing"
1011
1112 "github.com/aws/aws-sdk-go-v2/config"
1213 "github.com/aws/aws-sdk-go-v2/service/sts"
14+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
15+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1316 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1417 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1518 redshiftdatasqldriver "github.com/mmichaelb/redshift-data-sql-driver"
2326func init () {
2427 testAccProvider = Provider ()
2528 testAccProviders = map [string ]func () (* schema.Provider , error ){
26- "redshift" : func () (* schema.Provider , error ) { return testAccProvider , nil },
29+ "redshift" : func () (* schema.Provider , error ) { return testAccProvider , nil },
30+ "testvalues" : getTestValuesProvider ,
2731 }
2832}
2933
@@ -191,21 +195,6 @@ func Test_getConfigFromResourceData(t *testing.T) {
191195 },
192196 false ,
193197 },
194- {
195- "Data API config missing region" ,
196- args {
197- d : schema .TestResourceDataRaw (t , Provider ().Schema , map [string ]interface {}{
198- "database" : "some-database" ,
199- "data_api" : []interface {}{
200- map [string ]interface {}{
201- "workgroup_name" : "some-workgroup" ,
202- },
203- },
204- }),
205- },
206- nil ,
207- true ,
208- },
209198 {
210199 "PQ config" ,
211200 args {
@@ -251,19 +240,6 @@ func Test_getConfigFromResourceData(t *testing.T) {
251240 },
252241 false ,
253242 },
254- {
255- "PQ config - missing host" ,
256- args {
257- d : schema .TestResourceDataRaw (t , Provider ().Schema , map [string ]interface {}{
258- "username" : "some-user" ,
259- "port" : 4122 ,
260- "database" : "some-database" ,
261- "sslmode" : "require" ,
262- }),
263- },
264- nil ,
265- true ,
266- },
267243 }
268244 for _ , tt := range tests {
269245 t .Run (tt .name , func (t * testing.T ) {
@@ -291,6 +267,63 @@ func Test_getConfigFromResourceData(t *testing.T) {
291267 }
292268}
293269
270+ func TestAccProviderCalculatedValues_HostConfig (t * testing.T ) {
271+ testHostValue := generateRandomObjectName ("tf_acc_calc_val_host" )
272+ providerConfig := fmt .Sprintf (`
273+ provider "redshift" {
274+ host = testvalues_value.calculated_host.result
275+ password = "somepassword"
276+ }
277+
278+ resource "testvalues_value" "calculated_host" {
279+ value = %[1]q
280+ }
281+ ` , testHostValue )
282+ expectedError := fmt .Sprintf (`dial tcp: lookup %s: no such host` , testHostValue )
283+ // no such host error should occur, not a missing attribute error
284+ testCalculatedProviderValues (t , providerConfig , expectedError )
285+ }
286+
287+ func TestAccProviderCalculatedValues_RedshiftDataConfig (t * testing.T ) {
288+ testWorkgroupValue := generateRandomObjectName ("tf_acc_calc_val_host" )
289+ providerConfig := fmt .Sprintf (`
290+ provider "redshift" {
291+ database = "somedb"
292+
293+ data_api {
294+ workgroup_name = testvalues_value.calculated_workgroup.result
295+ region = "us-west-2"
296+ }
297+ }
298+
299+ resource "testvalues_value" "calculated_workgroup" {
300+ value = %[1]q
301+ }
302+ ` , testWorkgroupValue )
303+ // redshift endpoint doesn't exist in this region error should occur, not a missing attribute error
304+ expectedError := "ValidationException: Redshift endpoint doesn't exist in this region."
305+ testCalculatedProviderValues (t , providerConfig , expectedError )
306+ }
307+
308+ func testCalculatedProviderValues (t * testing.T , providerConfig string , expectedError string ) {
309+ defer unsetAndSetEnvVars ("REDSHIFT_DATABASE" , "REDSHIFT_HOST" , "REDSHIFT_USER" , "REDSHIFT_PASSWORD" , "REDSHIFT_DATA_API_SERVERLESS_WORKGROUP_NAME" )()
310+ testDbName := generateRandomObjectName ("tf_acc_calc_val_db" )
311+ testDbConfig := testAccDataSourceRedshiftDatabaseConfigBasic (testDbName )
312+ cfg := fmt .Sprintf (`
313+ %[1]s
314+ %[2]s
315+ ` , providerConfig , testDbConfig )
316+ resource .ParallelTest (t , resource.TestCase {
317+ ProviderFactories : testAccProviders ,
318+ Steps : []resource.TestStep {
319+ {
320+ Config : cfg ,
321+ ExpectError : regexp .MustCompile (expectedError ),
322+ },
323+ },
324+ })
325+ }
326+
294327func unsetAndSetEnvVars (envName ... string ) func () {
295328 envValues := make (map [string ]string )
296329 for _ , env := range envName {
@@ -308,3 +341,56 @@ func unsetAndSetEnvVars(envName ...string) func() {
308341 }
309342 }
310343}
344+
345+ type testValuesProvider struct {
346+ testValues map [string ]interface {}
347+ }
348+
349+ func (p * testValuesProvider ) getProvider () * schema.Provider {
350+ return & schema.Provider {
351+ ResourcesMap : map [string ]* schema.Resource {
352+ "testvalues_value" : {
353+ CreateContext : func (ctx context.Context , data * schema.ResourceData , i interface {}) diag.Diagnostics {
354+ value := data .Get ("value" ).(string )
355+ data .Set ("result" , value )
356+ data .SetId (value )
357+ p .testValues [value ] = & struct {}{}
358+ return nil
359+ },
360+ DeleteContext : func (ctx context.Context , data * schema.ResourceData , i interface {}) diag.Diagnostics {
361+ value := data .Get ("value" ).(string )
362+ delete (p .testValues , value )
363+ data .SetId ("" )
364+ return nil
365+ },
366+ ReadContext : func (ctx context.Context , data * schema.ResourceData , i interface {}) diag.Diagnostics {
367+ value := data .Get ("value" ).(string )
368+
369+ if _ , ok := p .testValues [value ]; ! ok {
370+ data .SetId ("" )
371+ return nil
372+ } else {
373+ data .SetId (value )
374+ data .Set ("result" , value )
375+ }
376+ return nil
377+ },
378+ Schema : map [string ]* schema.Schema {
379+ "value" : {
380+ Type : schema .TypeString ,
381+ Required : true ,
382+ ForceNew : true ,
383+ },
384+ "result" : {
385+ Type : schema .TypeString ,
386+ Computed : true ,
387+ },
388+ },
389+ },
390+ },
391+ }
392+ }
393+
394+ func getTestValuesProvider () (* schema.Provider , error ) {
395+ return (& testValuesProvider {testValues : make (map [string ]interface {})}).getProvider (), nil
396+ }
0 commit comments