github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_rds_cluster_instance_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  
    13  	"github.com/aws/aws-sdk-go/aws"
    14  	"github.com/aws/aws-sdk-go/aws/awserr"
    15  	"github.com/aws/aws-sdk-go/service/rds"
    16  )
    17  
    18  func TestAccAWSRDSClusterInstance_basic(t *testing.T) {
    19  	var v rds.DBInstance
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckAWSClusterDestroy,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: testAccAWSClusterInstanceConfig,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
    30  					testAccCheckAWSDBClusterInstanceAttributes(&v),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccCheckAWSClusterInstanceDestroy(s *terraform.State) error {
    38  	for _, rs := range s.RootModule().Resources {
    39  		if rs.Type != "aws_rds_cluster" {
    40  			continue
    41  		}
    42  
    43  		// Try to find the Group
    44  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
    45  		var err error
    46  		resp, err := conn.DescribeDBInstances(
    47  			&rds.DescribeDBInstancesInput{
    48  				DBInstanceIdentifier: aws.String(rs.Primary.ID),
    49  			})
    50  
    51  		if err == nil {
    52  			if len(resp.DBInstances) != 0 &&
    53  				*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
    54  				return fmt.Errorf("DB Cluster Instance %s still exists", rs.Primary.ID)
    55  			}
    56  		}
    57  
    58  		// Return nil if the Cluster Instance is already destroyed
    59  		if awsErr, ok := err.(awserr.Error); ok {
    60  			if awsErr.Code() == "DBInstanceNotFound" {
    61  				return nil
    62  			}
    63  		}
    64  
    65  		return err
    66  
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  func testAccCheckAWSDBClusterInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  
    75  		if *v.Engine != "aurora" {
    76  			return fmt.Errorf("bad engine, expected \"aurora\": %#v", *v.Engine)
    77  		}
    78  
    79  		if !strings.HasPrefix(*v.DBClusterIdentifier, "tf-aurora-cluster") {
    80  			return fmt.Errorf("Bad Cluster Identifier prefix:\nexpected: %s\ngot: %s", "tf-aurora-cluster", *v.DBClusterIdentifier)
    81  		}
    82  
    83  		return nil
    84  	}
    85  }
    86  
    87  func testAccCheckAWSClusterInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		rs, ok := s.RootModule().Resources[n]
    90  		if !ok {
    91  			return fmt.Errorf("Not found: %s", n)
    92  		}
    93  
    94  		if rs.Primary.ID == "" {
    95  			return fmt.Errorf("No DB Instance ID is set")
    96  		}
    97  
    98  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
    99  		resp, err := conn.DescribeDBInstances(&rds.DescribeDBInstancesInput{
   100  			DBInstanceIdentifier: aws.String(rs.Primary.ID),
   101  		})
   102  
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		for _, d := range resp.DBInstances {
   108  			if *d.DBInstanceIdentifier == rs.Primary.ID {
   109  				*v = *d
   110  				return nil
   111  			}
   112  		}
   113  
   114  		return fmt.Errorf("DB Cluster (%s) not found", rs.Primary.ID)
   115  	}
   116  }
   117  
   118  // Add some random to the name, to avoid collision
   119  var testAccAWSClusterInstanceConfig = fmt.Sprintf(`
   120  resource "aws_rds_cluster" "default" {
   121    cluster_identifier = "tf-aurora-cluster-test-%d"
   122    availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
   123    database_name = "mydb"
   124    master_username = "foo"
   125    master_password = "mustbeeightcharaters"
   126  }
   127  
   128  resource "aws_rds_cluster_instance" "cluster_instances" {
   129    identifier = "aurora-cluster-test-instance"
   130          cluster_identifier = "${aws_rds_cluster.default.id}"
   131    instance_class = "db.r3.large"
   132  }
   133  
   134  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int())