github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_db_security_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/aws-sdk-go/aws" 8 "github.com/hashicorp/aws-sdk-go/gen/rds" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSDBSecurityGroup(t *testing.T) { 14 var v rds.DBSecurityGroup 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAWSDBSecurityGroupDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSDBSecurityGroupConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAWSDBSecurityGroupExists("aws_db_security_group.bar", &v), 25 testAccCheckAWSDBSecurityGroupAttributes(&v), 26 resource.TestCheckResourceAttr( 27 "aws_db_security_group.bar", "name", "secgroup-terraform"), 28 resource.TestCheckResourceAttr( 29 "aws_db_security_group.bar", "description", "just cuz"), 30 resource.TestCheckResourceAttr( 31 "aws_db_security_group.bar", "ingress.3363517775.cidr", "10.0.0.1/24"), 32 resource.TestCheckResourceAttr( 33 "aws_db_security_group.bar", "ingress.#", "1"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func testAccCheckAWSDBSecurityGroupDestroy(s *terraform.State) error { 41 conn := testAccProvider.Meta().(*AWSClient).rdsconn 42 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "aws_db_security_group" { 45 continue 46 } 47 48 // Try to find the Group 49 resp, err := conn.DescribeDBSecurityGroups( 50 &rds.DescribeDBSecurityGroupsMessage{ 51 DBSecurityGroupName: aws.String(rs.Primary.ID), 52 }) 53 54 if err == nil { 55 if len(resp.DBSecurityGroups) != 0 && 56 *resp.DBSecurityGroups[0].DBSecurityGroupName == rs.Primary.ID { 57 return fmt.Errorf("DB Security Group still exists") 58 } 59 } 60 61 // Verify the error 62 newerr, ok := err.(aws.APIError) 63 if !ok { 64 return err 65 } 66 if newerr.Code != "InvalidDBSecurityGroup.NotFound" { 67 return err 68 } 69 } 70 71 return nil 72 } 73 74 func testAccCheckAWSDBSecurityGroupAttributes(group *rds.DBSecurityGroup) resource.TestCheckFunc { 75 return func(s *terraform.State) error { 76 if len(group.IPRanges) == 0 { 77 return fmt.Errorf("no cidr: %#v", group.IPRanges) 78 } 79 80 if *group.IPRanges[0].CIDRIP != "10.0.0.1/24" { 81 return fmt.Errorf("bad cidr: %#v", group.IPRanges) 82 } 83 84 statuses := make([]string, 0, len(group.IPRanges)) 85 for _, ips := range group.IPRanges { 86 statuses = append(statuses, *ips.Status) 87 } 88 89 if statuses[0] != "authorized" { 90 return fmt.Errorf("bad status: %#v", statuses) 91 } 92 93 if *group.DBSecurityGroupName != "secgroup-terraform" { 94 return fmt.Errorf("bad name: %#v", *group.DBSecurityGroupName) 95 } 96 97 if *group.DBSecurityGroupDescription != "just cuz" { 98 return fmt.Errorf("bad description: %#v", *group.DBSecurityGroupDescription) 99 } 100 101 return nil 102 } 103 } 104 105 func testAccCheckAWSDBSecurityGroupExists(n string, v *rds.DBSecurityGroup) resource.TestCheckFunc { 106 return func(s *terraform.State) error { 107 rs, ok := s.RootModule().Resources[n] 108 if !ok { 109 return fmt.Errorf("Not found: %s", n) 110 } 111 112 if rs.Primary.ID == "" { 113 return fmt.Errorf("No DB Security Group ID is set") 114 } 115 116 conn := testAccProvider.Meta().(*AWSClient).rdsconn 117 118 opts := rds.DescribeDBSecurityGroupsMessage{ 119 DBSecurityGroupName: aws.String(rs.Primary.ID), 120 } 121 122 resp, err := conn.DescribeDBSecurityGroups(&opts) 123 124 if err != nil { 125 return err 126 } 127 128 if len(resp.DBSecurityGroups) != 1 || 129 *resp.DBSecurityGroups[0].DBSecurityGroupName != rs.Primary.ID { 130 return fmt.Errorf("DB Security Group not found") 131 } 132 133 *v = resp.DBSecurityGroups[0] 134 135 return nil 136 } 137 } 138 139 const testAccAWSDBSecurityGroupConfig = ` 140 resource "aws_db_security_group" "bar" { 141 name = "secgroup-terraform" 142 description = "just cuz" 143 144 ingress { 145 cidr = "10.0.0.1/24" 146 } 147 } 148 `