github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/pagerduty/resource_pagerduty_escalation_policy_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 TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccCheckPagerDutyEscalationPolicyConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
    22  					resource.TestCheckResourceAttr(
    23  						"pagerduty_escalation_policy.foo", "name", "foo"),
    24  					resource.TestCheckResourceAttr(
    25  						"pagerduty_escalation_policy.foo", "description", "foo"),
    26  					resource.TestCheckResourceAttr(
    27  						"pagerduty_escalation_policy.foo", "num_loops", "1"),
    28  					resource.TestCheckResourceAttr(
    29  						"pagerduty_escalation_policy.foo", "rule.#", "1"),
    30  					resource.TestCheckResourceAttr(
    31  						"pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"),
    32  				),
    33  			},
    34  			resource.TestStep{
    35  				Config: testAccCheckPagerDutyEscalationPolicyConfigUpdated,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
    38  					resource.TestCheckResourceAttr(
    39  						"pagerduty_escalation_policy.foo", "name", "bar"),
    40  					resource.TestCheckResourceAttr(
    41  						"pagerduty_escalation_policy.foo", "description", "bar"),
    42  					resource.TestCheckResourceAttr(
    43  						"pagerduty_escalation_policy.foo", "num_loops", "2"),
    44  					resource.TestCheckResourceAttr(
    45  						"pagerduty_escalation_policy.foo", "rule.#", "2"),
    46  					resource.TestCheckResourceAttr(
    47  						"pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"),
    48  					resource.TestCheckResourceAttr(
    49  						"pagerduty_escalation_policy.foo", "rule.1.escalation_delay_in_minutes", "20"),
    50  				),
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func TestAccPagerDutyEscalationPolicyWithTeams_Basic(t *testing.T) {
    57  	resource.Test(t, resource.TestCase{
    58  		PreCheck:     func() { testAccPreCheck(t) },
    59  		Providers:    testAccProviders,
    60  		CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy,
    61  		Steps: []resource.TestStep{
    62  			resource.TestStep{
    63  				Config: testAccCheckPagerDutyEscalationPolicyWithTeamsConfig,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
    66  					resource.TestCheckResourceAttr(
    67  						"pagerduty_escalation_policy.foo", "name", "foo"),
    68  					resource.TestCheckResourceAttr(
    69  						"pagerduty_escalation_policy.foo", "description", "foo"),
    70  					resource.TestCheckResourceAttr(
    71  						"pagerduty_escalation_policy.foo", "num_loops", "1"),
    72  					resource.TestCheckResourceAttr(
    73  						"pagerduty_escalation_policy.foo", "rule.#", "1"),
    74  					resource.TestCheckResourceAttr(
    75  						"pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"),
    76  					resource.TestCheckResourceAttr(
    77  						"pagerduty_escalation_policy.foo", "teams.#", "1"),
    78  				),
    79  			},
    80  			resource.TestStep{
    81  				Config: testAccCheckPagerDutyEscalationPolicyWithTeamsConfigUpdated,
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
    84  					resource.TestCheckResourceAttr(
    85  						"pagerduty_escalation_policy.foo", "name", "bar"),
    86  					resource.TestCheckResourceAttr(
    87  						"pagerduty_escalation_policy.foo", "description", "bar"),
    88  					resource.TestCheckResourceAttr(
    89  						"pagerduty_escalation_policy.foo", "num_loops", "2"),
    90  					resource.TestCheckResourceAttr(
    91  						"pagerduty_escalation_policy.foo", "rule.#", "2"),
    92  					resource.TestCheckResourceAttr(
    93  						"pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"),
    94  					resource.TestCheckResourceAttr(
    95  						"pagerduty_escalation_policy.foo", "rule.1.escalation_delay_in_minutes", "20"),
    96  					resource.TestCheckResourceAttr(
    97  						"pagerduty_escalation_policy.foo", "teams.#", "0"),
    98  				),
    99  			},
   100  		},
   101  	})
   102  }
   103  
   104  func testAccCheckPagerDutyEscalationPolicyDestroy(s *terraform.State) error {
   105  	client := testAccProvider.Meta().(*pagerduty.Client)
   106  	for _, r := range s.RootModule().Resources {
   107  		if r.Type != "pagerduty_escalation_policy" {
   108  			continue
   109  		}
   110  
   111  		_, err := client.GetEscalationPolicy(r.Primary.ID, &pagerduty.GetEscalationPolicyOptions{})
   112  
   113  		if err == nil {
   114  			return fmt.Errorf("Escalation Policy still exists")
   115  		}
   116  
   117  	}
   118  	return nil
   119  }
   120  
   121  func testAccCheckPagerDutyEscalationPolicyExists(n string) resource.TestCheckFunc {
   122  	return func(s *terraform.State) error {
   123  		rs, ok := s.RootModule().Resources[n]
   124  		if !ok {
   125  			return fmt.Errorf("Not found: %s", n)
   126  		}
   127  		if rs.Primary.ID == "" {
   128  			return fmt.Errorf("No Escalation Policy ID is set")
   129  		}
   130  
   131  		client := testAccProvider.Meta().(*pagerduty.Client)
   132  
   133  		found, err := client.GetEscalationPolicy(rs.Primary.ID, &pagerduty.GetEscalationPolicyOptions{})
   134  		if err != nil {
   135  			return err
   136  		}
   137  
   138  		if found.ID != rs.Primary.ID {
   139  			return fmt.Errorf("Escalation policy not found: %v - %v", rs.Primary.ID, found)
   140  		}
   141  
   142  		return nil
   143  	}
   144  }
   145  
   146  const testAccCheckPagerDutyEscalationPolicyConfig = `
   147  resource "pagerduty_user" "foo" {
   148    name        = "foo"
   149    email       = "foo@bar.com"
   150    color       = "green"
   151    role        = "user"
   152    job_title   = "foo"
   153    description = "foo"
   154  }
   155  
   156  resource "pagerduty_escalation_policy" "foo" {
   157    name        = "foo"
   158    description = "foo"
   159    num_loops   = 1
   160  
   161    rule {
   162      escalation_delay_in_minutes = 10
   163  
   164      target {
   165        type = "user_reference"
   166        id   = "${pagerduty_user.foo.id}"
   167      }
   168    }
   169  }
   170  `
   171  
   172  const testAccCheckPagerDutyEscalationPolicyConfigUpdated = `
   173  resource "pagerduty_user" "foo" {
   174    name        = "foo"
   175    email       = "foo@bar.com"
   176    color       = "green"
   177    role        = "user"
   178    job_title   = "foo"
   179    description = "foo"
   180  }
   181  
   182  resource "pagerduty_escalation_policy" "foo" {
   183    name        = "bar"
   184    description = "bar"
   185    num_loops   = 2
   186  
   187    rule {
   188      escalation_delay_in_minutes = 10
   189  
   190      target {
   191        type = "user_reference"
   192        id   = "${pagerduty_user.foo.id}"
   193      }
   194    }
   195  
   196    rule {
   197      escalation_delay_in_minutes = 20
   198  
   199      target {
   200        type = "user_reference"
   201        id   = "${pagerduty_user.foo.id}"
   202      }
   203    }
   204  }
   205  `
   206  
   207  const testAccCheckPagerDutyEscalationPolicyWithTeamsConfig = `
   208  resource "pagerduty_user" "foo" {
   209    name        = "foo"
   210    email       = "foo@bar.com"
   211    color       = "green"
   212    role        = "user"
   213    job_title   = "foo"
   214    description = "foo"
   215  }
   216  
   217  resource "pagerduty_team" "foo" {
   218    name        = "foo"
   219    description = "foo"
   220  }
   221  
   222  resource "pagerduty_escalation_policy" "foo" {
   223    name        = "foo"
   224    description = "foo"
   225    num_loops   = 1
   226  	teams       = ["${pagerduty_team.foo.id}"]
   227  
   228    rule {
   229      escalation_delay_in_minutes = 10
   230  
   231      target {
   232        type = "user_reference"
   233        id   = "${pagerduty_user.foo.id}"
   234      }
   235    }
   236  }
   237  `
   238  
   239  const testAccCheckPagerDutyEscalationPolicyWithTeamsConfigUpdated = `
   240  resource "pagerduty_user" "foo" {
   241    name        = "foo"
   242    email       = "foo@bar.com"
   243    color       = "green"
   244    role        = "user"
   245    job_title   = "foo"
   246    description = "foo"
   247  }
   248  
   249  resource "pagerduty_team" "foo" {
   250    name        = "foo"
   251    description = "foo"
   252  }
   253  
   254  resource "pagerduty_escalation_policy" "foo" {
   255    name        = "bar"
   256    description = "bar"
   257    num_loops   = 2
   258  
   259    rule {
   260      escalation_delay_in_minutes = 10
   261  
   262      target {
   263        type = "user_reference"
   264        id   = "${pagerduty_user.foo.id}"
   265      }
   266    }
   267  
   268    rule {
   269      escalation_delay_in_minutes = 20
   270  
   271      target {
   272        type = "user_reference"
   273        id   = "${pagerduty_user.foo.id}"
   274      }
   275    }
   276  }
   277  `