github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/newrelic/resource_newrelic_alert_channel_test.go (about)

     1  package newrelic
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	newrelic "github.com/paultyng/go-newrelic/api"
    12  )
    13  
    14  func TestAccNewRelicAlertChannel_Basic(t *testing.T) {
    15  	rName := acctest.RandString(5)
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckNewRelicAlertChannelDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccCheckNewRelicAlertChannelConfig(rName),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckNewRelicAlertChannelExists("newrelic_alert_channel.foo"),
    25  					resource.TestCheckResourceAttr(
    26  						"newrelic_alert_channel.foo", "name", fmt.Sprintf("tf-test-%s", rName)),
    27  					resource.TestCheckResourceAttr(
    28  						"newrelic_alert_channel.foo", "type", "email"),
    29  					resource.TestCheckResourceAttr(
    30  						"newrelic_alert_channel.foo", "configuration.recipients", "foo@example.com"),
    31  					resource.TestCheckResourceAttr(
    32  						"newrelic_alert_channel.foo", "configuration.include_json_attachment", "1"),
    33  				),
    34  			},
    35  			resource.TestStep{
    36  				Config: testAccCheckNewRelicAlertChannelConfigUpdated(rName),
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckNewRelicAlertChannelExists("newrelic_alert_channel.foo"),
    39  					resource.TestCheckResourceAttr(
    40  						"newrelic_alert_channel.foo", "name", fmt.Sprintf("tf-test-updated-%s", rName)),
    41  					resource.TestCheckResourceAttr(
    42  						"newrelic_alert_channel.foo", "type", "email"),
    43  					resource.TestCheckResourceAttr(
    44  						"newrelic_alert_channel.foo", "configuration.recipients", "bar@example.com"),
    45  					resource.TestCheckResourceAttr(
    46  						"newrelic_alert_channel.foo", "configuration.include_json_attachment", "0"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testAccCheckNewRelicAlertChannelDestroy(s *terraform.State) error {
    54  	client := testAccProvider.Meta().(*newrelic.Client)
    55  	for _, r := range s.RootModule().Resources {
    56  		if r.Type != "newrelic_alert_channel" {
    57  			continue
    58  		}
    59  
    60  		id, err := strconv.ParseInt(r.Primary.ID, 10, 32)
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		_, err = client.GetAlertChannel(int(id))
    66  
    67  		if err == nil {
    68  			return fmt.Errorf("Alert channel still exists")
    69  		}
    70  
    71  	}
    72  	return nil
    73  }
    74  
    75  func testAccCheckNewRelicAlertChannelExists(n string) resource.TestCheckFunc {
    76  	return func(s *terraform.State) error {
    77  		rs, ok := s.RootModule().Resources[n]
    78  		if !ok {
    79  			return fmt.Errorf("Not found: %s", n)
    80  		}
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("No channel ID is set")
    83  		}
    84  
    85  		client := testAccProvider.Meta().(*newrelic.Client)
    86  
    87  		id, err := strconv.ParseInt(rs.Primary.ID, 10, 32)
    88  		if err != nil {
    89  			return err
    90  		}
    91  
    92  		found, err := client.GetAlertChannel(int(id))
    93  		if err != nil {
    94  			return err
    95  		}
    96  
    97  		if strconv.Itoa(found.ID) != rs.Primary.ID {
    98  			return fmt.Errorf("Channel not found: %v - %v", rs.Primary.ID, found)
    99  		}
   100  
   101  		return nil
   102  	}
   103  }
   104  
   105  func testAccCheckNewRelicAlertChannelConfig(rName string) string {
   106  	return fmt.Sprintf(`
   107  resource "newrelic_alert_channel" "foo" {
   108    name = "tf-test-%s"
   109  	type = "email"
   110  	
   111  	configuration = {
   112  		recipients = "foo@example.com"
   113  		include_json_attachment = "1"
   114  	}
   115  }
   116  `, rName)
   117  }
   118  
   119  func testAccCheckNewRelicAlertChannelConfigUpdated(rName string) string {
   120  	return fmt.Sprintf(`
   121  resource "newrelic_alert_channel" "foo" {
   122    name = "tf-test-updated-%s"
   123  	type = "email"
   124  	
   125  	configuration = {
   126  		recipients = "bar@example.com"
   127  		include_json_attachment = "0"
   128  	}
   129  }
   130  `, rName)
   131  }