github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/pagerduty/resource_pagerduty_service_integration_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 TestAccPagerDutyServiceIntegration_Basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckPagerDutyServiceIntegrationDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccCheckPagerDutyServiceIntegrationConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"),
    22  					resource.TestCheckResourceAttr(
    23  						"pagerduty_service_integration.foo", "name", "foo"),
    24  					resource.TestCheckResourceAttr(
    25  						"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
    26  					resource.TestCheckResourceAttr(
    27  						"pagerduty_service_integration.foo", "vendor", "PAM4FGS"),
    28  				),
    29  			},
    30  			resource.TestStep{
    31  				Config: testAccCheckPagerDutyServiceIntegrationConfigUpdated,
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"),
    34  					resource.TestCheckResourceAttr(
    35  						"pagerduty_service_integration.foo", "name", "bar"),
    36  					resource.TestCheckResourceAttr(
    37  						"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
    38  					resource.TestCheckResourceAttr(
    39  						"pagerduty_service_integration.foo", "vendor", "PAM4FGS"),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func testAccCheckPagerDutyServiceIntegrationDestroy(s *terraform.State) error {
    47  	client := testAccProvider.Meta().(*pagerduty.Client)
    48  	for _, r := range s.RootModule().Resources {
    49  		if r.Type != "pagerduty_service_integration" {
    50  			continue
    51  		}
    52  
    53  		service, _ := s.RootModule().Resources["pagerduty_service.foo"]
    54  
    55  		_, err := client.GetIntegration(service.Primary.ID, r.Primary.ID, pagerduty.GetIntegrationOptions{})
    56  
    57  		if err == nil {
    58  			return fmt.Errorf("Service Integration still exists")
    59  		}
    60  
    61  	}
    62  	return nil
    63  }
    64  
    65  func testAccCheckPagerDutyServiceIntegrationExists(n string) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		rs, ok := s.RootModule().Resources[n]
    68  
    69  		if !ok {
    70  			return fmt.Errorf("Not found: %s", n)
    71  		}
    72  		if rs.Primary.ID == "" {
    73  			return fmt.Errorf("No Service Integration ID is set")
    74  		}
    75  
    76  		service, _ := s.RootModule().Resources["pagerduty_service.foo"]
    77  
    78  		client := testAccProvider.Meta().(*pagerduty.Client)
    79  
    80  		found, err := client.GetIntegration(service.Primary.ID, rs.Primary.ID, pagerduty.GetIntegrationOptions{})
    81  		if err != nil {
    82  			return fmt.Errorf("Service integration not found: %v", rs.Primary.ID)
    83  		}
    84  
    85  		if found.ID != rs.Primary.ID {
    86  			return fmt.Errorf("Service Integration not found: %v - %v", rs.Primary.ID, found)
    87  		}
    88  
    89  		return nil
    90  	}
    91  }
    92  
    93  const testAccCheckPagerDutyServiceIntegrationConfig = `
    94  resource "pagerduty_user" "foo" {
    95    name        = "foo"
    96    email       = "foo@bar.com"
    97  }
    98  
    99  resource "pagerduty_escalation_policy" "foo" {
   100    name        = "foo"
   101    description = "foo"
   102    num_loops   = 1
   103  
   104    rule {
   105      escalation_delay_in_minutes = 10
   106  
   107      target {
   108        type = "user_reference"
   109        id   = "${pagerduty_user.foo.id}"
   110      }
   111    }
   112  }
   113  
   114  resource "pagerduty_service" "foo" {
   115    name                    = "foo"
   116    description             = "foo"
   117    auto_resolve_timeout    = 1800
   118    acknowledgement_timeout = 1800
   119    escalation_policy       = "${pagerduty_escalation_policy.foo.id}"
   120  }
   121  
   122  data "pagerduty_vendor" "datadog" {
   123    name_regex = "datadog"
   124  }
   125  
   126  resource "pagerduty_service_integration" "foo" {
   127    name    = "foo"
   128    type    = "generic_events_api_inbound_integration"
   129    service = "${pagerduty_service.foo.id}"
   130    vendor  = "${data.pagerduty_vendor.datadog.id}"
   131  }
   132  `
   133  
   134  const testAccCheckPagerDutyServiceIntegrationConfigUpdated = `
   135  resource "pagerduty_user" "foo" {
   136    name        = "foo"
   137    email       = "foo@bar.com"
   138    color       = "green"
   139    role        = "user"
   140    job_title   = "foo"
   141    description = "foo"
   142  }
   143  
   144  resource "pagerduty_escalation_policy" "foo" {
   145    name        = "bar"
   146    description = "bar"
   147    num_loops   = 2
   148  
   149    rule {
   150      escalation_delay_in_minutes = 10
   151  
   152      target {
   153        type = "user_reference"
   154        id   = "${pagerduty_user.foo.id}"
   155      }
   156    }
   157  }
   158  
   159  resource "pagerduty_service" "foo" {
   160    name                    = "bar"
   161    description             = "bar"
   162    auto_resolve_timeout    = 3600
   163    acknowledgement_timeout = 3600
   164    escalation_policy       = "${pagerduty_escalation_policy.foo.id}"
   165  }
   166  
   167  data "pagerduty_vendor" "datadog" {
   168    name_regex = "datadog"
   169  }
   170  
   171  resource "pagerduty_service_integration" "foo" {
   172    name    = "bar"
   173    type    = "generic_events_api_inbound_integration"
   174    service = "${pagerduty_service.foo.id}"
   175    vendor  = "${data.pagerduty_vendor.datadog.id}"
   176  }
   177  `