github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/newrelic/resource_newrelic_alert_policy_channel_test.go (about)

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