mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-08 15:13:56 -06:00
fca9216f53
This adds a singular data source in addition to the existing plural one. This allows retrieving data about a specific AZ. As a helper for writing reusable modules, the AZ letter (without its usual region name prefix) is exposed so that it can be used in region-agnostic mappings where a different value is used per AZ, such as for subnet numbering schemes.
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccDataSourceAwsAvailabilityZone(t *testing.T) {
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDataSourceAwsAvailabilityZoneConfig,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccDataSourceAwsAvailabilityZoneCheck("data.aws_availability_zone.by_name"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccDataSourceAwsAvailabilityZoneCheck(name string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[name]
|
|
if !ok {
|
|
return fmt.Errorf("root module has no resource called %s", name)
|
|
}
|
|
|
|
attr := rs.Primary.Attributes
|
|
|
|
if attr["name"] != "us-west-2a" {
|
|
return fmt.Errorf("bad name %s", attr["name"])
|
|
}
|
|
if attr["name_suffix"] != "a" {
|
|
return fmt.Errorf("bad name_suffix %s", attr["name_suffix"])
|
|
}
|
|
if attr["region"] != "us-west-2" {
|
|
return fmt.Errorf("bad region %s", attr["region"])
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
const testAccDataSourceAwsAvailabilityZoneConfig = `
|
|
provider "aws" {
|
|
region = "us-west-2"
|
|
}
|
|
|
|
data "aws_availability_zone" "by_name" {
|
|
name = "us-west-2a"
|
|
}
|
|
`
|