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