|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go/aws/endpoints" |
| 9 | + "github.com/aws/aws-sdk-go/service/fms" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 13 | +) |
| 14 | + |
| 15 | +// Firewall Management Service admin APIs are only enabled in specific regions, otherwise: |
| 16 | +// InvalidOperationException: This operation is not supported in the 'us-west-2' region. |
| 17 | + |
| 18 | +// testAccFmsAdminRegion is the chosen Firewall Management Service testing region |
| 19 | +// |
| 20 | +// Cached to prevent issues should multiple regions become available. |
| 21 | +var testAccFmsAdminRegion string |
| 22 | + |
| 23 | +// testAccProviderFmsAdmin is the Firewall Management Service provider instance |
| 24 | +// |
| 25 | +// This Provider can be used in testing code for API calls without requiring |
| 26 | +// the use of saving and referencing specific ProviderFactories instances. |
| 27 | +// |
| 28 | +// testAccPreCheckFmsAdmin(t) must be called before using this provider instance. |
| 29 | +var testAccProviderFmsAdmin *schema.Provider |
| 30 | + |
| 31 | +// testAccProviderFmsAdminConfigure ensures the provider is only configured once |
| 32 | +var testAccProviderFmsAdminConfigure sync.Once |
| 33 | + |
| 34 | +// testAccPreCheckFmsAdmin verifies AWS credentials and that Firewall Management Service is supported |
| 35 | +func testAccPreCheckFmsAdmin(t *testing.T) { |
| 36 | + testAccPartitionHasServicePreCheck(fms.EndpointsID, t) |
| 37 | + |
| 38 | + // Since we are outside the scope of the Terraform configuration we must |
| 39 | + // call Configure() to properly initialize the provider configuration. |
| 40 | + testAccProviderFmsAdminConfigure.Do(func() { |
| 41 | + testAccProviderFmsAdmin = Provider() |
| 42 | + |
| 43 | + config := map[string]interface{}{ |
| 44 | + "region": testAccGetFmsAdminRegion(), |
| 45 | + } |
| 46 | + |
| 47 | + diags := testAccProviderFmsAdmin.Configure(context.Background(), terraform.NewResourceConfigRaw(config)) |
| 48 | + |
| 49 | + if diags != nil && diags.HasError() { |
| 50 | + for _, d := range diags { |
| 51 | + if d.Severity == diag.Error { |
| 52 | + t.Fatalf("error configuring Firewall Management Service provider: %s", d.Summary) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +// testAccFmsAdminRegionProviderConfig is the Terraform provider configuration for Firewall Management Service region testing |
| 60 | +// |
| 61 | +// Testing Firewall Management Service assumes no other provider configurations |
| 62 | +// are necessary and overwrites the "aws" provider configuration. |
| 63 | +func testAccFmsAdminRegionProviderConfig() string { |
| 64 | + return testAccRegionalProviderConfig(testAccGetFmsAdminRegion()) |
| 65 | +} |
| 66 | + |
| 67 | +// testAccGetFmsAdminRegion returns the Firewall Management Service region for testing |
| 68 | +func testAccGetFmsAdminRegion() string { |
| 69 | + if testAccFmsAdminRegion != "" { |
| 70 | + return testAccFmsAdminRegion |
| 71 | + } |
| 72 | + |
| 73 | + testAccFmsAdminRegion = endpoints.UsEast1RegionID |
| 74 | + |
| 75 | + return testAccFmsAdminRegion |
| 76 | +} |
0 commit comments