github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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", "Managed by Terraform"),
    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  		return nil
   101  	}
   102  }
   103  
   104  func testAccCheckAWSDBSecurityGroupExists(n string, v *rds.DBSecurityGroup) resource.TestCheckFunc {
   105  	return func(s *terraform.State) error {
   106  		rs, ok := s.RootModule().Resources[n]
   107  		if !ok {
   108  			return fmt.Errorf("Not found: %s", n)
   109  		}
   110  
   111  		if rs.Primary.ID == "" {
   112  			return fmt.Errorf("No DB Security Group ID is set")
   113  		}
   114  
   115  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   116  
   117  		opts := rds.DescribeDBSecurityGroupsInput{
   118  			DBSecurityGroupName: aws.String(rs.Primary.ID),
   119  		}
   120  
   121  		resp, err := conn.DescribeDBSecurityGroups(&opts)
   122  
   123  		if err != nil {
   124  			return err
   125  		}
   126  
   127  		if len(resp.DBSecurityGroups) != 1 ||
   128  			*resp.DBSecurityGroups[0].DBSecurityGroupName != rs.Primary.ID {
   129  			return fmt.Errorf("DB Security Group not found")
   130  		}
   131  
   132  		*v = *resp.DBSecurityGroups[0]
   133  
   134  		return nil
   135  	}
   136  }
   137  
   138  const testAccAWSDBSecurityGroupConfig = `
   139  provider "aws" {
   140          region = "us-east-1"
   141  }
   142  
   143  resource "aws_db_security_group" "bar" {
   144      name = "secgroup-terraform"
   145  
   146      ingress {
   147          cidr = "10.0.0.1/24"
   148      }
   149  
   150      tags {
   151  		foo = "bar"
   152      }
   153  }
   154  `