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