github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/newrelic/resource_newrelic_alert_condition_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 TestAccNewRelicAlertCondition_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: testAccCheckNewRelicAlertConditionDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCheckNewRelicAlertConditionConfig(rName), 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckNewRelicAlertConditionExists("newrelic_alert_condition.foo"), 24 resource.TestCheckResourceAttr( 25 "newrelic_alert_condition.foo", "name", fmt.Sprintf("tf-test-%s", rName)), 26 resource.TestCheckResourceAttr( 27 "newrelic_alert_condition.foo", "type", "apm_app_metric"), 28 resource.TestCheckResourceAttr( 29 "newrelic_alert_condition.foo", "runbook_url", "https://foo.example.com"), 30 resource.TestCheckResourceAttr( 31 "newrelic_alert_condition.foo", "entities.#", "1"), 32 resource.TestCheckResourceAttr( 33 "newrelic_alert_condition.foo", "term.#", "1"), 34 resource.TestCheckResourceAttr( 35 "newrelic_alert_condition.foo", "term.0.duration", "5"), 36 resource.TestCheckResourceAttr( 37 "newrelic_alert_condition.foo", "term.0.operator", "below"), 38 resource.TestCheckResourceAttr( 39 "newrelic_alert_condition.foo", "term.0.priority", "critical"), 40 resource.TestCheckResourceAttr( 41 "newrelic_alert_condition.foo", "term.0.threshold", "0.75"), 42 resource.TestCheckResourceAttr( 43 "newrelic_alert_condition.foo", "term.0.time_function", "all"), 44 ), 45 }, 46 resource.TestStep{ 47 Config: testAccCheckNewRelicAlertConditionConfigUpdated(rName), 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckNewRelicAlertConditionExists("newrelic_alert_condition.foo"), 50 resource.TestCheckResourceAttr( 51 "newrelic_alert_condition.foo", "name", fmt.Sprintf("tf-test-updated-%s", rName)), 52 resource.TestCheckResourceAttr( 53 "newrelic_alert_condition.foo", "runbook_url", "https://bar.example.com"), 54 resource.TestCheckResourceAttr( 55 "newrelic_alert_condition.foo", "entities.#", "1"), 56 resource.TestCheckResourceAttr( 57 "newrelic_alert_condition.foo", "term.#", "1"), 58 resource.TestCheckResourceAttr( 59 "newrelic_alert_condition.foo", "term.0.duration", "10"), 60 resource.TestCheckResourceAttr( 61 "newrelic_alert_condition.foo", "term.0.operator", "below"), 62 resource.TestCheckResourceAttr( 63 "newrelic_alert_condition.foo", "term.0.priority", "critical"), 64 resource.TestCheckResourceAttr( 65 "newrelic_alert_condition.foo", "term.0.threshold", "0.65"), 66 resource.TestCheckResourceAttr( 67 "newrelic_alert_condition.foo", "term.0.time_function", "all"), 68 ), 69 }, 70 }, 71 }) 72 } 73 74 // TODO: func TestAccNewRelicAlertCondition_Multi(t *testing.T) { 75 76 func testAccCheckNewRelicAlertConditionDestroy(s *terraform.State) error { 77 client := testAccProvider.Meta().(*newrelic.Client) 78 for _, r := range s.RootModule().Resources { 79 if r.Type != "newrelic_alert_condition" { 80 continue 81 } 82 83 ids, err := parseIDs(r.Primary.ID, 2) 84 if err != nil { 85 return err 86 } 87 88 policyID := ids[0] 89 id := ids[1] 90 91 _, err = client.GetAlertCondition(policyID, id) 92 if err == nil { 93 return fmt.Errorf("Alert condition still exists") 94 } 95 96 } 97 return nil 98 } 99 100 func testAccCheckNewRelicAlertConditionExists(n string) resource.TestCheckFunc { 101 return func(s *terraform.State) error { 102 rs, ok := s.RootModule().Resources[n] 103 if !ok { 104 return fmt.Errorf("Not found: %s", n) 105 } 106 if rs.Primary.ID == "" { 107 return fmt.Errorf("No alert condition ID is set") 108 } 109 110 client := testAccProvider.Meta().(*newrelic.Client) 111 112 ids, err := parseIDs(rs.Primary.ID, 2) 113 if err != nil { 114 return err 115 } 116 117 policyID := ids[0] 118 id := ids[1] 119 120 found, err := client.GetAlertCondition(policyID, id) 121 if err != nil { 122 return err 123 } 124 125 if found.ID != id { 126 return fmt.Errorf("Alert condition not found: %v - %v", id, found) 127 } 128 129 return nil 130 } 131 } 132 133 func testAccCheckNewRelicAlertConditionConfig(rName string) string { 134 return fmt.Sprintf(` 135 data "newrelic_application" "app" { 136 name = "%[2]s" 137 } 138 139 resource "newrelic_alert_policy" "foo" { 140 name = "tf-test-%[1]s" 141 } 142 143 resource "newrelic_alert_condition" "foo" { 144 policy_id = "${newrelic_alert_policy.foo.id}" 145 146 name = "tf-test-%[1]s" 147 type = "apm_app_metric" 148 entities = ["${data.newrelic_application.app.id}"] 149 metric = "apdex" 150 runbook_url = "https://foo.example.com" 151 condition_scope = "application" 152 153 term { 154 duration = 5 155 operator = "below" 156 priority = "critical" 157 threshold = "0.75" 158 time_function = "all" 159 } 160 } 161 `, rName, testAccExpectedApplicationName) 162 } 163 164 func testAccCheckNewRelicAlertConditionConfigUpdated(rName string) string { 165 return fmt.Sprintf(` 166 data "newrelic_application" "app" { 167 name = "%[2]s" 168 } 169 170 resource "newrelic_alert_policy" "foo" { 171 name = "tf-test-updated-%[1]s" 172 } 173 174 resource "newrelic_alert_condition" "foo" { 175 policy_id = "${newrelic_alert_policy.foo.id}" 176 177 name = "tf-test-updated-%[1]s" 178 type = "apm_app_metric" 179 entities = ["${data.newrelic_application.app.id}"] 180 metric = "apdex" 181 runbook_url = "https://bar.example.com" 182 condition_scope = "application" 183 184 term { 185 duration = 10 186 operator = "below" 187 priority = "critical" 188 threshold = "0.65" 189 time_function = "all" 190 } 191 } 192 `, rName, testAccExpectedApplicationName) 193 } 194 195 // TODO: const testAccCheckNewRelicAlertConditionConfigMulti = `