mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-15 10:03:44 -06:00
120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
"github.com/hashicorp/aws-sdk-go/gen/rds"
|
|
)
|
|
|
|
func TestAccAWSDBSubnetGroup(t *testing.T) {
|
|
var v rds.DBSubnetGroup
|
|
|
|
testCheck := func(*terraform.State) error {
|
|
return nil
|
|
}
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDBSubnetGroupDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDBSubnetGroupConfig,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckDBSubnetGroupExists(
|
|
"aws_db_subnet_group.foo", &v),
|
|
testCheck,
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error {
|
|
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "aws_db_subnet_group" {
|
|
continue
|
|
}
|
|
|
|
// Try to find the resource
|
|
resp, err := conn.DescribeDBSubnetGroups(
|
|
&rds.DescribeDBSubnetGroupsMessage{DBSubnetGroupName: aws.String(rs.Primary.ID)})
|
|
if err == nil {
|
|
if len(resp.DBSubnetGroups) > 0 {
|
|
return fmt.Errorf("still exist.")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Verify the error is what we want
|
|
rdserr, ok := err.(aws.APIError)
|
|
if !ok {
|
|
return err
|
|
}
|
|
if rdserr.Code != "DBSubnetGroupNotFoundFault" {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[n]
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", n)
|
|
}
|
|
|
|
if rs.Primary.ID == "" {
|
|
return fmt.Errorf("No ID is set")
|
|
}
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
|
resp, err := conn.DescribeDBSubnetGroups(
|
|
&rds.DescribeDBSubnetGroupsMessage{DBSubnetGroupName: aws.String(rs.Primary.ID)})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(resp.DBSubnetGroups) == 0 {
|
|
return fmt.Errorf("DbSubnetGroup not found")
|
|
}
|
|
|
|
*v = resp.DBSubnetGroups[0]
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
const testAccDBSubnetGroupConfig = `
|
|
resource "aws_vpc" "foo" {
|
|
cidr_block = "10.1.0.0/16"
|
|
}
|
|
|
|
resource "aws_subnet" "foo" {
|
|
cidr_block = "10.1.1.0/24"
|
|
availability_zone = "us-west-2a"
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
}
|
|
|
|
resource "aws_subnet" "bar" {
|
|
cidr_block = "10.1.2.0/24"
|
|
availability_zone = "us-west-2b"
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
}
|
|
|
|
resource "aws_db_subnet_group" "foo" {
|
|
name = "foo"
|
|
description = "foo description"
|
|
subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
|
|
}
|
|
`
|