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