github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/newrelic/resource_newrelic_alert_policy_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 TestAccNewRelicAlertPolicy_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: testAccCheckNewRelicAlertPolicyDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccCheckNewRelicAlertPolicyConfig(rName), 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckNewRelicAlertPolicyExists("newrelic_alert_policy.foo"), 25 resource.TestCheckResourceAttr( 26 "newrelic_alert_policy.foo", "name", fmt.Sprintf("tf-test-%s", rName)), 27 resource.TestCheckResourceAttr( 28 "newrelic_alert_policy.foo", "incident_preference", "PER_POLICY"), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccCheckNewRelicAlertPolicyConfigUpdated(rName), 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckNewRelicAlertPolicyExists("newrelic_alert_policy.foo"), 35 resource.TestCheckResourceAttr( 36 "newrelic_alert_policy.foo", "name", fmt.Sprintf("tf-test-updated-%s", rName)), 37 resource.TestCheckResourceAttr( 38 "newrelic_alert_policy.foo", "incident_preference", "PER_CONDITION"), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func testAccCheckNewRelicAlertPolicyDestroy(s *terraform.State) error { 46 client := testAccProvider.Meta().(*newrelic.Client) 47 for _, r := range s.RootModule().Resources { 48 if r.Type != "newrelic_alert_policy" { 49 continue 50 } 51 52 id, err := strconv.ParseInt(r.Primary.ID, 10, 32) 53 if err != nil { 54 return err 55 } 56 57 _, err = client.GetAlertPolicy(int(id)) 58 59 if err == nil { 60 return fmt.Errorf("Policy still exists") 61 } 62 63 } 64 return nil 65 } 66 67 func testAccCheckNewRelicAlertPolicyExists(n string) resource.TestCheckFunc { 68 return func(s *terraform.State) error { 69 rs, ok := s.RootModule().Resources[n] 70 if !ok { 71 return fmt.Errorf("Not found: %s", n) 72 } 73 if rs.Primary.ID == "" { 74 return fmt.Errorf("No policy ID is set") 75 } 76 77 client := testAccProvider.Meta().(*newrelic.Client) 78 79 id, err := strconv.ParseInt(rs.Primary.ID, 10, 32) 80 if err != nil { 81 return err 82 } 83 84 found, err := client.GetAlertPolicy(int(id)) 85 if err != nil { 86 return err 87 } 88 89 if strconv.Itoa(found.ID) != rs.Primary.ID { 90 return fmt.Errorf("Policy not found: %v - %v", rs.Primary.ID, found) 91 } 92 93 return nil 94 } 95 } 96 97 func testAccCheckNewRelicAlertPolicyConfig(rName string) string { 98 return fmt.Sprintf(` 99 resource "newrelic_alert_policy" "foo" { 100 name = "tf-test-%s" 101 } 102 `, rName) 103 } 104 105 func testAccCheckNewRelicAlertPolicyConfigUpdated(rName string) string { 106 return fmt.Sprintf(` 107 resource "newrelic_alert_policy" "foo" { 108 name = "tf-test-updated-%s" 109 incident_preference = "PER_CONDITION" 110 } 111 `, rName) 112 }