github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_codecommit_trigger_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/codecommit"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSCodeCommitTrigger_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCodeCommitTriggerDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCodeCommitTrigger_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCodeCommitTriggerExists("aws_codecommit_trigger.test"),
    24  					resource.TestCheckResourceAttr(
    25  						"aws_codecommit_trigger.test", "trigger.#", "1"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckCodeCommitTriggerDestroy(s *terraform.State) error {
    33  	conn := testAccProvider.Meta().(*AWSClient).codecommitconn
    34  
    35  	for _, rs := range s.RootModule().Resources {
    36  		if rs.Type != "aws_codecommit_trigger" {
    37  			continue
    38  		}
    39  
    40  		_, err := conn.GetRepositoryTriggers(&codecommit.GetRepositoryTriggersInput{
    41  			RepositoryName: aws.String(rs.Primary.ID),
    42  		})
    43  
    44  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "RepositoryDoesNotExistException" {
    45  			continue
    46  		}
    47  		if err == nil {
    48  			return fmt.Errorf("Trigger still exists: %s", rs.Primary.ID)
    49  		}
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testAccCheckCodeCommitTriggerExists(name string) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[name]
    59  		if !ok {
    60  			return fmt.Errorf("Not found: %s", name)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No ID is set")
    65  		}
    66  
    67  		codecommitconn := testAccProvider.Meta().(*AWSClient).codecommitconn
    68  		out, err := codecommitconn.GetRepositoryTriggers(&codecommit.GetRepositoryTriggersInput{
    69  			RepositoryName: aws.String(rs.Primary.ID),
    70  		})
    71  
    72  		if err != nil {
    73  			return err
    74  		}
    75  
    76  		if len(out.Triggers) == 0 {
    77  			return fmt.Errorf("CodeCommit Trigger Failed: %q", out)
    78  		}
    79  
    80  		return nil
    81  	}
    82  }
    83  
    84  const testAccCodeCommitTrigger_basic = `
    85  provider "aws" {
    86    region = "us-east-1"
    87  }
    88  resource "aws_sns_topic" "test" {
    89    name = "tf-test-topic"
    90  }
    91  resource "aws_codecommit_repository" "test" {
    92    repository_name = "tf_test_repository"
    93    description = "This is a test description"
    94  }
    95  resource "aws_codecommit_trigger" "test" {
    96     depends_on = ["aws_codecommit_repository.test"]
    97     repository_name = "tf_test_repository"
    98      trigger {
    99      name = "tf-test-trigger"
   100      events = ["all"]
   101      destination_arn = "${aws_sns_topic.test.arn}"
   102    }
   103   }
   104  `