github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func testAccCheckAWSDBSecurityGroupDestroy(s *terraform.State) error {
    42  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
    43  
    44  	for _, rs := range s.RootModule().Resources {
    45  		if rs.Type != "aws_db_security_group" {
    46  			continue
    47  		}
    48  
    49  		// Try to find the Group
    50  		resp, err := conn.DescribeDBSecurityGroups(
    51  			&rds.DescribeDBSecurityGroupsInput{
    52  				DBSecurityGroupName: aws.String(rs.Primary.ID),
    53  			})
    54  
    55  		if err == nil {
    56  			if len(resp.DBSecurityGroups) != 0 &&
    57  				*resp.DBSecurityGroups[0].DBSecurityGroupName == rs.Primary.ID {
    58  				return fmt.Errorf("DB Security Group still exists")
    59  			}
    60  		}
    61  
    62  		// Verify the error
    63  		newerr, ok := err.(awserr.Error)
    64  		if !ok {
    65  			return err
    66  		}
    67  		if newerr.Code() != "InvalidDBSecurityGroup.NotFound" {
    68  			return err
    69  		}
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func testAccCheckAWSDBSecurityGroupAttributes(group *rds.DBSecurityGroup) resource.TestCheckFunc {
    76  	return func(s *terraform.State) error {
    77  		if len(group.IPRanges) == 0 {
    78  			return fmt.Errorf("no cidr: %#v", group.IPRanges)
    79  		}
    80  
    81  		if *group.IPRanges[0].CIDRIP != "10.0.0.1/24" {
    82  			return fmt.Errorf("bad cidr: %#v", group.IPRanges)
    83  		}
    84  
    85  		statuses := make([]string, 0, len(group.IPRanges))
    86  		for _, ips := range group.IPRanges {
    87  			statuses = append(statuses, *ips.Status)
    88  		}
    89  
    90  		if statuses[0] != "authorized" {
    91  			return fmt.Errorf("bad status: %#v", statuses)
    92  		}
    93  
    94  		if *group.DBSecurityGroupName != "secgroup-terraform" {
    95  			return fmt.Errorf("bad name: %#v", *group.DBSecurityGroupName)
    96  		}
    97  
    98  		if *group.DBSecurityGroupDescription != "just cuz" {
    99  			return fmt.Errorf("bad description: %#v", *group.DBSecurityGroupDescription)
   100  		}
   101  
   102  		return nil
   103  	}
   104  }
   105  
   106  func testAccCheckAWSDBSecurityGroupExists(n string, v *rds.DBSecurityGroup) resource.TestCheckFunc {
   107  	return func(s *terraform.State) error {
   108  		rs, ok := s.RootModule().Resources[n]
   109  		if !ok {
   110  			return fmt.Errorf("Not found: %s", n)
   111  		}
   112  
   113  		if rs.Primary.ID == "" {
   114  			return fmt.Errorf("No DB Security Group ID is set")
   115  		}
   116  
   117  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   118  
   119  		opts := rds.DescribeDBSecurityGroupsInput{
   120  			DBSecurityGroupName: aws.String(rs.Primary.ID),
   121  		}
   122  
   123  		resp, err := conn.DescribeDBSecurityGroups(&opts)
   124  
   125  		if err != nil {
   126  			return err
   127  		}
   128  
   129  		if len(resp.DBSecurityGroups) != 1 ||
   130  			*resp.DBSecurityGroups[0].DBSecurityGroupName != rs.Primary.ID {
   131  			return fmt.Errorf("DB Security Group not found")
   132  		}
   133  
   134  		*v = *resp.DBSecurityGroups[0]
   135  
   136  		return nil
   137  	}
   138  }
   139  
   140  const testAccAWSDBSecurityGroupConfig = `
   141  provider "aws" {
   142          region = "us-east-1"
   143  }
   144  
   145  resource "aws_db_security_group" "bar" {
   146      name = "secgroup-terraform"
   147      description = "just cuz"
   148  
   149      ingress {
   150          cidr = "10.0.0.1/24"
   151      }
   152  }
   153  `