github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/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/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccPagerDutyServiceIntegration_Basic(t *testing.T) {
    14  	username := fmt.Sprintf("tf-%s", acctest.RandString(5))
    15  	email := fmt.Sprintf("%s@foo.com", username)
    16  	escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
    17  	service := fmt.Sprintf("tf-%s", acctest.RandString(5))
    18  	serviceIntegration := fmt.Sprintf("tf-%s", acctest.RandString(5))
    19  	serviceIntegrationUpdated := fmt.Sprintf("tf-%s", acctest.RandString(5))
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckPagerDutyServiceIntegrationDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: testAccCheckPagerDutyServiceIntegrationConfig(username, email, escalationPolicy, service, serviceIntegration),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"),
    30  					resource.TestCheckResourceAttr(
    31  						"pagerduty_service_integration.foo", "name", serviceIntegration),
    32  					resource.TestCheckResourceAttr(
    33  						"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
    34  					resource.TestCheckResourceAttr(
    35  						"pagerduty_service_integration.foo", "vendor", "PAM4FGS"),
    36  				),
    37  			},
    38  			{
    39  				Config: testAccCheckPagerDutyServiceIntegrationConfigUpdated(username, email, escalationPolicy, service, serviceIntegrationUpdated),
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"),
    42  					resource.TestCheckResourceAttr(
    43  						"pagerduty_service_integration.foo", "name", serviceIntegrationUpdated),
    44  					resource.TestCheckResourceAttr(
    45  						"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
    46  					resource.TestCheckResourceAttr(
    47  						"pagerduty_service_integration.foo", "vendor", "PAM4FGS"),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func TestAccPagerDutyServiceIntegrationGeneric_Basic(t *testing.T) {
    55  	username := fmt.Sprintf("tf-%s", acctest.RandString(5))
    56  	email := fmt.Sprintf("%s@foo.com", username)
    57  	escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
    58  	service := fmt.Sprintf("tf-%s", acctest.RandString(5))
    59  	serviceIntegration := fmt.Sprintf("tf-%s", acctest.RandString(5))
    60  	serviceIntegrationUpdated := fmt.Sprintf("tf-%s", acctest.RandString(5))
    61  
    62  	resource.Test(t, resource.TestCase{
    63  		PreCheck:     func() { testAccPreCheck(t) },
    64  		Providers:    testAccProviders,
    65  		CheckDestroy: testAccCheckPagerDutyServiceIntegrationDestroy,
    66  		Steps: []resource.TestStep{
    67  			{
    68  				Config: testAccCheckPagerDutyServiceIntegrationGenericConfig(username, email, escalationPolicy, service, serviceIntegration),
    69  				Check: resource.ComposeTestCheckFunc(
    70  					testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"),
    71  					resource.TestCheckResourceAttr(
    72  						"pagerduty_service_integration.foo", "name", serviceIntegration),
    73  					resource.TestCheckResourceAttr(
    74  						"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
    75  				),
    76  			},
    77  			{
    78  				Config: testAccCheckPagerDutyServiceIntegrationGenericConfigUpdated(username, email, escalationPolicy, service, serviceIntegrationUpdated),
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckPagerDutyServiceIntegrationExists("pagerduty_service_integration.foo"),
    81  					resource.TestCheckResourceAttr(
    82  						"pagerduty_service_integration.foo", "name", serviceIntegrationUpdated),
    83  					resource.TestCheckResourceAttr(
    84  						"pagerduty_service_integration.foo", "type", "generic_events_api_inbound_integration"),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func testAccCheckPagerDutyServiceIntegrationDestroy(s *terraform.State) error {
    92  	client := testAccProvider.Meta().(*pagerduty.Client)
    93  	for _, r := range s.RootModule().Resources {
    94  		if r.Type != "pagerduty_service_integration" {
    95  			continue
    96  		}
    97  
    98  		service, _ := s.RootModule().Resources["pagerduty_service.foo"]
    99  
   100  		_, err := client.GetIntegration(service.Primary.ID, r.Primary.ID, pagerduty.GetIntegrationOptions{})
   101  
   102  		if err == nil {
   103  			return fmt.Errorf("Service Integration still exists")
   104  		}
   105  
   106  	}
   107  	return nil
   108  }
   109  
   110  func testAccCheckPagerDutyServiceIntegrationExists(n string) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		rs, ok := s.RootModule().Resources[n]
   113  
   114  		if !ok {
   115  			return fmt.Errorf("Not found: %s", n)
   116  		}
   117  		if rs.Primary.ID == "" {
   118  			return fmt.Errorf("No Service Integration ID is set")
   119  		}
   120  
   121  		service, _ := s.RootModule().Resources["pagerduty_service.foo"]
   122  
   123  		client := testAccProvider.Meta().(*pagerduty.Client)
   124  
   125  		found, err := client.GetIntegration(service.Primary.ID, rs.Primary.ID, pagerduty.GetIntegrationOptions{})
   126  		if err != nil {
   127  			return fmt.Errorf("Service integration not found: %v", rs.Primary.ID)
   128  		}
   129  
   130  		if found.ID != rs.Primary.ID {
   131  			return fmt.Errorf("Service Integration not found: %v - %v", rs.Primary.ID, found)
   132  		}
   133  
   134  		return nil
   135  	}
   136  }
   137  
   138  func testAccCheckPagerDutyServiceIntegrationConfig(username, email, escalationPolicy, service, serviceIntegration string) string {
   139  	return fmt.Sprintf(`
   140  resource "pagerduty_user" "foo" {
   141    name        = "%s"
   142    email       = "%s"
   143  }
   144  
   145  resource "pagerduty_escalation_policy" "foo" {
   146    name        = "%s"
   147    description = "foo"
   148    num_loops   = 1
   149  
   150    rule {
   151      escalation_delay_in_minutes = 10
   152  
   153      target {
   154        type = "user_reference"
   155        id   = "${pagerduty_user.foo.id}"
   156      }
   157    }
   158  }
   159  
   160  resource "pagerduty_service" "foo" {
   161    name                    = "%s"
   162    description             = "foo"
   163    auto_resolve_timeout    = 1800
   164    acknowledgement_timeout = 1800
   165    escalation_policy       = "${pagerduty_escalation_policy.foo.id}"
   166  
   167    incident_urgency_rule {
   168      type = "constant"
   169      urgency = "high"
   170    }
   171  }
   172  
   173  data "pagerduty_vendor" "datadog" {
   174    name = "datadog"
   175  }
   176  
   177  resource "pagerduty_service_integration" "foo" {
   178    name    = "%s"
   179    service = "${pagerduty_service.foo.id}"
   180    vendor  = "${data.pagerduty_vendor.datadog.id}"
   181  }
   182  `, username, email, escalationPolicy, service, serviceIntegration)
   183  }
   184  
   185  func testAccCheckPagerDutyServiceIntegrationConfigUpdated(username, email, escalationPolicy, service, serviceIntegration string) string {
   186  	return fmt.Sprintf(`
   187  resource "pagerduty_user" "foo" {
   188    name        = "%s"
   189    email       = "%s"
   190    color       = "green"
   191    role        = "user"
   192    job_title   = "foo"
   193    description = "foo"
   194  }
   195  
   196  resource "pagerduty_escalation_policy" "foo" {
   197    name        = "%s"
   198    description = "bar"
   199    num_loops   = 2
   200  
   201    rule {
   202      escalation_delay_in_minutes = 10
   203  
   204      target {
   205        type = "user_reference"
   206        id   = "${pagerduty_user.foo.id}"
   207      }
   208    }
   209  }
   210  
   211  resource "pagerduty_service" "foo" {
   212    name                    = "%s"
   213    description             = "bar"
   214    auto_resolve_timeout    = 3600
   215    acknowledgement_timeout = 3600
   216    escalation_policy       = "${pagerduty_escalation_policy.foo.id}"
   217  
   218    incident_urgency_rule {
   219      type    = "constant"
   220      urgency = "high"
   221    }
   222  }
   223  
   224  data "pagerduty_vendor" "datadog" {
   225    name = "datadog"
   226  }
   227  
   228  resource "pagerduty_service_integration" "foo" {
   229    name    = "%s"
   230    service = "${pagerduty_service.foo.id}"
   231    vendor  = "${data.pagerduty_vendor.datadog.id}"
   232  }
   233  `, username, email, escalationPolicy, service, serviceIntegration)
   234  }
   235  
   236  func testAccCheckPagerDutyServiceIntegrationGenericConfig(username, email, escalationPolicy, service, serviceIntegration string) string {
   237  	return fmt.Sprintf(`
   238  resource "pagerduty_user" "foo" {
   239    name        = "%s"
   240    email       = "%s"
   241  }
   242  
   243  resource "pagerduty_escalation_policy" "foo" {
   244    name        = "%s"
   245    description = "foo"
   246    num_loops   = 1
   247  
   248    rule {
   249      escalation_delay_in_minutes = 10
   250  
   251      target {
   252        type = "user_reference"
   253        id   = "${pagerduty_user.foo.id}"
   254      }
   255    }
   256  }
   257  
   258  resource "pagerduty_service" "foo" {
   259    name                    = "%s"
   260    description             = "foo"
   261    auto_resolve_timeout    = 1800
   262    acknowledgement_timeout = 1800
   263    escalation_policy       = "${pagerduty_escalation_policy.foo.id}"
   264  
   265    incident_urgency_rule {
   266      type = "constant"
   267      urgency = "high"
   268    }
   269  }
   270  
   271  resource "pagerduty_service_integration" "foo" {
   272    name    = "%s"
   273    service = "${pagerduty_service.foo.id}"
   274    type    = "generic_events_api_inbound_integration"
   275  }
   276  `, username, email, escalationPolicy, service, serviceIntegration)
   277  }
   278  
   279  func testAccCheckPagerDutyServiceIntegrationGenericConfigUpdated(username, email, escalationPolicy, service, serviceIntegration string) string {
   280  	return fmt.Sprintf(`
   281  resource "pagerduty_user" "foo" {
   282    name        = "%s"
   283    email       = "%s"
   284    color       = "green"
   285    role        = "user"
   286    job_title   = "foo"
   287    description = "foo"
   288  }
   289  
   290  resource "pagerduty_escalation_policy" "foo" {
   291    name        = "%s"
   292    description = "bar"
   293    num_loops   = 2
   294  
   295    rule {
   296      escalation_delay_in_minutes = 10
   297  
   298      target {
   299        type = "user_reference"
   300        id   = "${pagerduty_user.foo.id}"
   301      }
   302    }
   303  }
   304  
   305  resource "pagerduty_service" "foo" {
   306    name                    = "%s"
   307    description             = "bar"
   308    auto_resolve_timeout    = 3600
   309    acknowledgement_timeout = 3600
   310    escalation_policy       = "${pagerduty_escalation_policy.foo.id}"
   311  
   312    incident_urgency_rule {
   313      type    = "constant"
   314      urgency = "high"
   315    }
   316  }
   317  
   318  resource "pagerduty_service_integration" "foo" {
   319    name    = "%s"
   320    service = "${pagerduty_service.foo.id}"
   321    type    = "generic_events_api_inbound_integration"
   322  }
   323  `, username, email, escalationPolicy, service, serviceIntegration)
   324  }