github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/pagerduty/resource_pagerduty_service_test.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/PagerDuty/go-pagerduty"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccPagerDutyService_Basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckPagerDutyServiceDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccCheckPagerDutyServiceConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
    22  					resource.TestCheckResourceAttr(
    23  						"pagerduty_service.foo", "name", "foo"),
    24  					resource.TestCheckResourceAttr(
    25  						"pagerduty_service.foo", "description", "foo"),
    26  					resource.TestCheckResourceAttr(
    27  						"pagerduty_service.foo", "auto_resolve_timeout", "1800"),
    28  					resource.TestCheckResourceAttr(
    29  						"pagerduty_service.foo", "acknowledgement_timeout", "1800"),
    30  				),
    31  			},
    32  			resource.TestStep{
    33  				Config: testAccCheckPagerDutyServiceConfigUpdated,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
    36  					resource.TestCheckResourceAttr(
    37  						"pagerduty_service.foo", "name", "bar"),
    38  					resource.TestCheckResourceAttr(
    39  						"pagerduty_service.foo", "description", "bar"),
    40  					resource.TestCheckResourceAttr(
    41  						"pagerduty_service.foo", "auto_resolve_timeout", "3600"),
    42  					resource.TestCheckResourceAttr(
    43  						"pagerduty_service.foo", "acknowledgement_timeout", "3600"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func testAccCheckPagerDutyServiceDestroy(s *terraform.State) error {
    51  	client := testAccProvider.Meta().(*pagerduty.Client)
    52  	for _, r := range s.RootModule().Resources {
    53  		if r.Type != "pagerduty_service" {
    54  			continue
    55  		}
    56  
    57  		_, err := client.GetService(r.Primary.ID, &pagerduty.GetServiceOptions{})
    58  
    59  		if err == nil {
    60  			return fmt.Errorf("Service still exists")
    61  		}
    62  
    63  	}
    64  	return nil
    65  }
    66  
    67  func testAccCheckPagerDutyServiceExists(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 Service ID is set")
    75  		}
    76  
    77  		client := testAccProvider.Meta().(*pagerduty.Client)
    78  
    79  		found, err := client.GetService(rs.Primary.ID, &pagerduty.GetServiceOptions{})
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		if found.ID != rs.Primary.ID {
    85  			return fmt.Errorf("Service not found: %v - %v", rs.Primary.ID, found)
    86  		}
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  const testAccCheckPagerDutyServiceConfig = `
    93  resource "pagerduty_user" "foo" {
    94    name        = "foo"
    95    email       = "foo@bar.com"
    96    color       = "green"
    97    role        = "user"
    98    job_title   = "foo"
    99    description = "foo"
   100  }
   101  
   102  resource "pagerduty_escalation_policy" "foo" {
   103    name        = "bar"
   104    description = "bar"
   105    num_loops   = 2
   106  
   107    rule {
   108      escalation_delay_in_minutes = 10
   109  
   110      target {
   111        type = "user_reference"
   112        id   = "${pagerduty_user.foo.id}"
   113      }
   114    }
   115  }
   116  
   117  resource "pagerduty_service" "foo" {
   118    name                    = "foo"
   119    description             = "foo"
   120    auto_resolve_timeout    = 1800
   121    acknowledgement_timeout = 1800
   122    escalation_policy       = "${pagerduty_escalation_policy.foo.id}"
   123  }
   124  `
   125  
   126  const testAccCheckPagerDutyServiceConfigUpdated = `
   127  resource "pagerduty_user" "foo" {
   128    name        = "foo"
   129    email       = "foo@bar.com"
   130    color       = "green"
   131    role        = "user"
   132    job_title   = "foo"
   133    description = "foo"
   134  }
   135  
   136  resource "pagerduty_escalation_policy" "foo" {
   137    name        = "bar"
   138    description = "bar"
   139    num_loops   = 2
   140  
   141    rule {
   142      escalation_delay_in_minutes = 10
   143  
   144      target {
   145        type = "user_reference"
   146        id   = "${pagerduty_user.foo.id}"
   147      }
   148    }
   149  }
   150  
   151  resource "pagerduty_service" "foo" {
   152    name                    = "bar"
   153    description             = "bar"
   154    auto_resolve_timeout    = 3600
   155    acknowledgement_timeout = 3600
   156    escalation_policy       = "${pagerduty_escalation_policy.foo.id}"
   157  }
   158  `