Skip to content

Commit

Permalink
Merge pull request #14568 from terraform-providers/td-hardaz-func-config
Browse files Browse the repository at this point in the history
r/instance: Function for availability zone config
  • Loading branch information
YakDriver authored Aug 13, 2020
2 parents c824d22 + a574b53 commit 0a61ba7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
26 changes: 15 additions & 11 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3280,6 +3280,19 @@ data "aws_availability_zones" "available" {
`, strings.Join(excludeZoneIds, "\", \""))
}

func testAccAvailableAZsNoOptInConfig() string {
return `
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
`
}

func testAccInstanceConfigInDefaultVpcBySgName(rName string) string {
return testAccAvailableAZsNoOptInDefaultExcludeConfig() +
testAccLatestAmazonLinuxHvmEbsAmiConfig() +
Expand Down Expand Up @@ -4886,16 +4899,7 @@ resource "aws_subnet" "test" {
}

func testAccAwsInstanceVpcConfigBasic(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
Expand All @@ -4913,7 +4917,7 @@ resource "aws_subnet" "test" {
Name = %[1]q
}
}
`, rName)
`, rName))
}

// testAccAwsInstanceVpcSecurityGroupConfig returns the configuration for tests that create
Expand Down
25 changes: 16 additions & 9 deletions docs/contributing/running-and-writing-acceptance-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -1012,18 +1012,25 @@ The below are location-based items that _may_ be noted during review and are rec
}
```
- [ ] __Uses aws_availability_zones Data Source__: Any hardcoded AWS Availability Zone configuration, e.g. `us-west-2a`, should be replaced with the [`aws_availability_zones` data source](https://www.terraform.io/docs/providers/aws/d/availability_zones.html). A common pattern is declaring `data "aws_availability_zones" "available" {...}` and referencing it via `data.aws_availability_zones.available.names[0]` or `data.aws_availability_zones.available.names[count.index]` in resources utilizing `count`.
- [ ] __Uses aws_availability_zones Data Source__: Any hardcoded AWS Availability Zone configuration, e.g. `us-west-2a`, should be replaced with the [`aws_availability_zones` data source](https://www.terraform.io/docs/providers/aws/d/availability_zones.html). Use the convenience function called `testAccAvailableAZsNoOptInConfig()` (defined in `resource_aws_instance_test.go`) to declare `data "aws_availability_zones" "available" {...}`. You can then reference the data source via `data.aws_availability_zones.available.names[0]` or `data.aws_availability_zones.available.names[count.index]` in resources utilizing `count`.
```hcl
data "aws_availability_zones" "available" {
state = "available"
Here's an example of using `testAccAvailableAZsNoOptInConfig()` and `data.aws_availability_zones.available.names[0]`:

filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
```go
func testAccAwsInstanceVpcConfigBasic(rName string) string {
return testAccAvailableAZsNoOptInConfig() + fmt.Sprintf(`
resource "aws_subnet" "test" {
availability_zone = data.aws_availability_zones.available.names[0]
cidr_block = "10.0.0.0/24"
vpc_id = aws_vpc.test.id

tags = {
Name = %[1]q
}
```
}
`, rName)
}
```

- [ ] __Uses aws_region Data Source__: Any hardcoded AWS Region configuration, e.g. `us-west-2`, should be replaced with the [`aws_region` data source](https://www.terraform.io/docs/providers/aws/d/region.html). A common pattern is declaring `data "aws_region" "current" {}` and referencing it via `data.aws_region.current.name`
- [ ] __Uses aws_partition Data Source__: Any hardcoded AWS Partition configuration, e.g. the `aws` in a `arn:aws:SERVICE:REGION:ACCOUNT:RESOURCE` ARN, should be replaced with the [`aws_partition` data source](https://www.terraform.io/docs/providers/aws/d/partition.html). A common pattern is declaring `data "aws_partition" "current" {}` and referencing it via `data.aws_partition.current.partition`
Expand Down

0 comments on commit 0a61ba7

Please sign in to comment.