From bfbff79ebc93d4d1cbbb585990772afdd1d4ab6f Mon Sep 17 00:00:00 2001 From: petetanton Date: Thu, 29 Jul 2021 11:20:28 +0100 Subject: [PATCH 1/8] add integration datasource --- .../data_source_pagerduty_integration.go | 100 ++++++++++++++++++ pagerduty/provider.go | 1 + 2 files changed, 101 insertions(+) create mode 100644 pagerduty/data_source_pagerduty_integration.go diff --git a/pagerduty/data_source_pagerduty_integration.go b/pagerduty/data_source_pagerduty_integration.go new file mode 100644 index 000000000..23cae6d61 --- /dev/null +++ b/pagerduty/data_source_pagerduty_integration.go @@ -0,0 +1,100 @@ +package pagerduty + +import ( + "fmt" + "log" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/heimweh/go-pagerduty/pagerduty" +) + +func dataSourcePagerDutyIntegration() *schema.Resource { + return &schema.Resource{ + Read: dataSourcePagerDutyIntegrationRead, + + Schema: map[string]*schema.Schema{ + "service_name": { + Type: schema.TypeString, + Required: true, + }, + "integration_type": { + Type: schema.TypeString, + Required: true, + Description: "examples 'Amazon CloudWatch', 'New Relic", + }, + + "integration_key": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + }, + } +} + +func dataSourcePagerDutyIntegrationRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*pagerduty.Client) + + log.Printf("[INFO] Reading PagerDuty service") + + searchName := d.Get("service_name").(string) + + o := &pagerduty.ListServicesOptions{ + Query: searchName, + } + + return resource.Retry(2*time.Minute, func() *resource.RetryError { + resp, _, err := client.Services.List(o) + if err != nil { + if isErrCode(err, 429) { + // Delaying retry by 30s as recommended by PagerDuty + // https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit + time.Sleep(30 * time.Second) + return resource.RetryableError(err) + } + + return resource.NonRetryableError(err) + } + + var found *pagerduty.Service + + for _, service := range resp.Services { + if service.Name == searchName { + found = service + break + } + } + + if found == nil { + return resource.NonRetryableError( + fmt.Errorf("Unable to locate any service with the name: %s", searchName), + ) + } + + integrationType := d.Get("integration_type").(string) + for _, integration := range found.Integrations { + if strings.EqualFold(integration.Summary, integrationType) { + integrationDetails, _, err := client.Services.GetIntegration(found.ID, integration.ID, &pagerduty.GetIntegrationOptions{}) + if err != nil { + if isErrCode(err, 429) { + time.Sleep(30 * time.Second) + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + d.SetId(integration.ID) + d.Set("service_name", found.Name) + d.Set("integration_key", integrationDetails.IntegrationKey) + + return nil + } + + } + return resource.NonRetryableError( + fmt.Errorf("Unable to locate any integration of type %s on service %s", integrationType, searchName), + ) + }) +} diff --git a/pagerduty/provider.go b/pagerduty/provider.go index 86f5aa945..42ae7ae40 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -36,6 +36,7 @@ func Provider() terraform.ResourceProvider { "pagerduty_vendor": dataSourcePagerDutyVendor(), "pagerduty_extension_schema": dataSourcePagerDutyExtensionSchema(), "pagerduty_service": dataSourcePagerDutyService(), + "pagerduty_integration": dataSourcePagerDutyIntegration(), "pagerduty_business_service": dataSourcePagerDutyBusinessService(), "pagerduty_priority": dataSourcePagerDutyPriority(), "pagerduty_ruleset": dataSourcePagerDutyRuleset(), From 8e14b0c3f89b71e54a71bdee066714e43df8ed5c Mon Sep 17 00:00:00 2001 From: petetanton Date: Thu, 29 Jul 2021 13:35:18 +0100 Subject: [PATCH 2/8] refactor error handling --- .../data_source_pagerduty_integration.go | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/pagerduty/data_source_pagerduty_integration.go b/pagerduty/data_source_pagerduty_integration.go index 23cae6d61..e0016b212 100644 --- a/pagerduty/data_source_pagerduty_integration.go +++ b/pagerduty/data_source_pagerduty_integration.go @@ -49,14 +49,7 @@ func dataSourcePagerDutyIntegrationRead(d *schema.ResourceData, meta interface{} return resource.Retry(2*time.Minute, func() *resource.RetryError { resp, _, err := client.Services.List(o) if err != nil { - if isErrCode(err, 429) { - // Delaying retry by 30s as recommended by PagerDuty - // https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit - time.Sleep(30 * time.Second) - return resource.RetryableError(err) - } - - return resource.NonRetryableError(err) + return handleError(err) } var found *pagerduty.Service @@ -79,11 +72,7 @@ func dataSourcePagerDutyIntegrationRead(d *schema.ResourceData, meta interface{} if strings.EqualFold(integration.Summary, integrationType) { integrationDetails, _, err := client.Services.GetIntegration(found.ID, integration.ID, &pagerduty.GetIntegrationOptions{}) if err != nil { - if isErrCode(err, 429) { - time.Sleep(30 * time.Second) - return resource.RetryableError(err) - } - return resource.NonRetryableError(err) + return handleError(err) } d.SetId(integration.ID) d.Set("service_name", found.Name) @@ -94,7 +83,16 @@ func dataSourcePagerDutyIntegrationRead(d *schema.ResourceData, meta interface{} } return resource.NonRetryableError( - fmt.Errorf("Unable to locate any integration of type %s on service %s", integrationType, searchName), + fmt.Errorf("unable to locate any integration of type %s on service %s", integrationType, searchName), ) }) } + +func handleError(err error) *resource.RetryError { + if isErrCode(err, 429) { + time.Sleep(30 * time.Second) + return resource.RetryableError(err) + } + + return resource.NonRetryableError(err) +} From a7ee9e129139b95466059ba35ec3d8abadfaf4a9 Mon Sep 17 00:00:00 2001 From: petetanton Date: Thu, 29 Jul 2021 15:57:53 +0100 Subject: [PATCH 3/8] rename and add test --- ...a_source_pagerduty_service_integration.go} | 2 +- ...urce_pagerduty_service_integration_test.go | 132 ++++++++++++++++++ pagerduty/provider.go | 2 +- 3 files changed, 134 insertions(+), 2 deletions(-) rename pagerduty/{data_source_pagerduty_integration.go => data_source_pagerduty_service_integration.go} (97%) create mode 100644 pagerduty/data_source_pagerduty_service_integration_test.go diff --git a/pagerduty/data_source_pagerduty_integration.go b/pagerduty/data_source_pagerduty_service_integration.go similarity index 97% rename from pagerduty/data_source_pagerduty_integration.go rename to pagerduty/data_source_pagerduty_service_integration.go index e0016b212..c027f57ae 100644 --- a/pagerduty/data_source_pagerduty_integration.go +++ b/pagerduty/data_source_pagerduty_service_integration.go @@ -63,7 +63,7 @@ func dataSourcePagerDutyIntegrationRead(d *schema.ResourceData, meta interface{} if found == nil { return resource.NonRetryableError( - fmt.Errorf("Unable to locate any service with the name: %s", searchName), + fmt.Errorf("unable to locate any service with the name: %s", searchName), ) } diff --git a/pagerduty/data_source_pagerduty_service_integration_test.go b/pagerduty/data_source_pagerduty_service_integration_test.go new file mode 100644 index 000000000..a94534160 --- /dev/null +++ b/pagerduty/data_source_pagerduty_service_integration_test.go @@ -0,0 +1,132 @@ +package pagerduty + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccDataSourcePagerDutyIntegration_Basic(t *testing.T) { + username := fmt.Sprintf("tf-%s", acctest.RandString(5)) + email := fmt.Sprintf("%s@foo.com", username) + service := fmt.Sprintf("tf-%s", acctest.RandString(5)) + escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) + serviceIntegration := fmt.Sprintf("tf-%s", acctest.RandString(5)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourcePagerDutyIntegrationConfigStep1(username, email, service, escalationPolicy, serviceIntegration), + Check: resource.ComposeTestCheckFunc( + testAccDataSourcePagerDutyIntegration("pagerduty_service.test", "data.pagerduty_service.by_name"), + ), + }, + { + Config: testAccDataSourcePagerDutyIntegrationConfigStep2(service, serviceIntegration), + Check: verifyOutput("output_id"), + }, + }, + }) +} + +func verifyOutput(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + ms := s.RootModule() + rs, ok := ms.Outputs[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + if rs.Type != "string" { + return fmt.Errorf("expected an error: %#v", rs) + } + + return nil + } +} + +func testAccDataSourcePagerDutyIntegration(src, n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + + srcR := s.RootModule().Resources[src] + srcA := srcR.Primary.Attributes + + r := s.RootModule().Resources[n] + a := r.Primary.Attributes + + if a["id"] == "" { + return fmt.Errorf("expected to get a service ID from PagerDuty") + } + + testAtts := []string{"id", "name"} + + for _, att := range testAtts { + if a[att] != srcA[att] { + return fmt.Errorf("Expected the service %s to be: %s, but got: %s", att, srcA[att], a[att]) + } + } + + return nil + } +} + +func testAccDataSourcePagerDutyIntegrationConfigStep1(username, email, service, escalationPolicy, serviceIntegration string) string { + return fmt.Sprintf(` +resource "pagerduty_user" "test" { + name = "%s" + email = "%s" +} + +resource "pagerduty_service" "test" { + name = "%s" + auto_resolve_timeout = 14400 + acknowledgement_timeout = 600 + escalation_policy = pagerduty_escalation_policy.test.id + alert_creation = "create_incidents" +} + +resource "pagerduty_escalation_policy" "test" { + name = "%s" + num_loops = 2 + rule { + escalation_delay_in_minutes = 10 + target { + type = "user_reference" + id = pagerduty_user.test.id + } + } +} + +resource "pagerduty_service_integration" "foo" { + name = "%s" + service = pagerduty_service.test.id + vendor = data.pagerduty_vendor.datadog.id +} + +data "pagerduty_vendor" "datadog" { + name = "datadog" +} + +data "pagerduty_service" "by_name" { + name = pagerduty_service.test.name +} +`, username, email, service, escalationPolicy, serviceIntegration) +} + +func testAccDataSourcePagerDutyIntegrationConfigStep2(service, serviceIntegration string) string { + return fmt.Sprintf(` +data "pagerduty_service_integration" "service_integration" { + service_name = "%s" + integration_type = "%s" +} + +output "output_id" { + value = data.pagerduty_service_integration.service_integration.integration_key +} +`, service, serviceIntegration) +} diff --git a/pagerduty/provider.go b/pagerduty/provider.go index 42ae7ae40..6b4d21c8f 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -36,7 +36,7 @@ func Provider() terraform.ResourceProvider { "pagerduty_vendor": dataSourcePagerDutyVendor(), "pagerduty_extension_schema": dataSourcePagerDutyExtensionSchema(), "pagerduty_service": dataSourcePagerDutyService(), - "pagerduty_integration": dataSourcePagerDutyIntegration(), + "pagerduty_service_integration": dataSourcePagerDutyIntegration(), "pagerduty_business_service": dataSourcePagerDutyBusinessService(), "pagerduty_priority": dataSourcePagerDutyPriority(), "pagerduty_ruleset": dataSourcePagerDutyRuleset(), From f7f16f15c4fa89143f00ce885c67b3d005a981b2 Mon Sep 17 00:00:00 2001 From: petetanton Date: Fri, 30 Jul 2021 13:53:51 +0100 Subject: [PATCH 4/8] testing! --- ...ta_source_pagerduty_service_integration.go | 8 +- ...urce_pagerduty_service_integration_test.go | 125 +++++++----------- 2 files changed, 53 insertions(+), 80 deletions(-) diff --git a/pagerduty/data_source_pagerduty_service_integration.go b/pagerduty/data_source_pagerduty_service_integration.go index c027f57ae..2754f4fc4 100644 --- a/pagerduty/data_source_pagerduty_service_integration.go +++ b/pagerduty/data_source_pagerduty_service_integration.go @@ -20,7 +20,7 @@ func dataSourcePagerDutyIntegration() *schema.Resource { Type: schema.TypeString, Required: true, }, - "integration_type": { + "integration_summary": { Type: schema.TypeString, Required: true, Description: "examples 'Amazon CloudWatch', 'New Relic", @@ -67,9 +67,9 @@ func dataSourcePagerDutyIntegrationRead(d *schema.ResourceData, meta interface{} ) } - integrationType := d.Get("integration_type").(string) + integrationSummary := d.Get("integration_summary").(string) for _, integration := range found.Integrations { - if strings.EqualFold(integration.Summary, integrationType) { + if strings.EqualFold(integration.Summary, integrationSummary) { integrationDetails, _, err := client.Services.GetIntegration(found.ID, integration.ID, &pagerduty.GetIntegrationOptions{}) if err != nil { return handleError(err) @@ -83,7 +83,7 @@ func dataSourcePagerDutyIntegrationRead(d *schema.ResourceData, meta interface{} } return resource.NonRetryableError( - fmt.Errorf("unable to locate any integration of type %s on service %s", integrationType, searchName), + fmt.Errorf("unable to locate any integration of type %s on service %s", integrationSummary, searchName), ) }) } diff --git a/pagerduty/data_source_pagerduty_service_integration_test.go b/pagerduty/data_source_pagerduty_service_integration_test.go index a94534160..212a6b243 100644 --- a/pagerduty/data_source_pagerduty_service_integration_test.go +++ b/pagerduty/data_source_pagerduty_service_integration_test.go @@ -2,30 +2,71 @@ package pagerduty import ( "fmt" + "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/heimweh/go-pagerduty/pagerduty" ) +func setupResources(service, serviceIntegration, escalationPolicy, username, email string) error { + client, err := pagerduty.NewClient(&pagerduty.Config{Token: os.Getenv("PAGERDUTY_TOKEN")}) + if err != nil { + return err + } + + createUser, _, err := client.Users.Create(&pagerduty.User{Name: username, Email: email}) + if err != nil { + return err + } + + createEp, _, err := client.EscalationPolicies.Create(&pagerduty.EscalationPolicy{ + Name: escalationPolicy, + EscalationRules: []*pagerduty.EscalationRule{{EscalationDelayInMinutes: 10, Targets: []*pagerduty.EscalationTargetReference{{Type: "user_reference", ID: createUser.ID}}}}, + }) + if err != nil { + return err + } + + createResponse, _, err := client.Services.Create(&pagerduty.Service{ + Name: service, + EscalationPolicy: &pagerduty.EscalationPolicyReference{ID: createEp.ID, Type: "escalation_policy_reference"}, + }) + if err != nil { + return err + } + + _, _, err = client.Services.CreateIntegration(createResponse.ID, &pagerduty.Integration{ + Summary: serviceIntegration, + Type: "service_integration_reference", + Service: &pagerduty.ServiceReference{ID: createResponse.ID, Type: "service_reference"}, + Vendor: &pagerduty.VendorReference{ID: "PAM4FGS", Type: "vendor_reference"}, + }) + if err != nil { + return fmt.Errorf("error creating integration: %v", err) + } + + return nil +} + func TestAccDataSourcePagerDutyIntegration_Basic(t *testing.T) { username := fmt.Sprintf("tf-%s", acctest.RandString(5)) email := fmt.Sprintf("%s@foo.com", username) service := fmt.Sprintf("tf-%s", acctest.RandString(5)) escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) - serviceIntegration := fmt.Sprintf("tf-%s", acctest.RandString(5)) + serviceIntegration := "Datadog" + + err := setupResources(service, serviceIntegration, escalationPolicy, username, email) + if err != nil { + t.Fatal(err) + } resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ - { - Config: testAccDataSourcePagerDutyIntegrationConfigStep1(username, email, service, escalationPolicy, serviceIntegration), - Check: resource.ComposeTestCheckFunc( - testAccDataSourcePagerDutyIntegration("pagerduty_service.test", "data.pagerduty_service.by_name"), - ), - }, { Config: testAccDataSourcePagerDutyIntegrationConfigStep2(service, serviceIntegration), Check: verifyOutput("output_id"), @@ -50,79 +91,11 @@ func verifyOutput(name string) resource.TestCheckFunc { } } -func testAccDataSourcePagerDutyIntegration(src, n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - - srcR := s.RootModule().Resources[src] - srcA := srcR.Primary.Attributes - - r := s.RootModule().Resources[n] - a := r.Primary.Attributes - - if a["id"] == "" { - return fmt.Errorf("expected to get a service ID from PagerDuty") - } - - testAtts := []string{"id", "name"} - - for _, att := range testAtts { - if a[att] != srcA[att] { - return fmt.Errorf("Expected the service %s to be: %s, but got: %s", att, srcA[att], a[att]) - } - } - - return nil - } -} - -func testAccDataSourcePagerDutyIntegrationConfigStep1(username, email, service, escalationPolicy, serviceIntegration string) string { - return fmt.Sprintf(` -resource "pagerduty_user" "test" { - name = "%s" - email = "%s" -} - -resource "pagerduty_service" "test" { - name = "%s" - auto_resolve_timeout = 14400 - acknowledgement_timeout = 600 - escalation_policy = pagerduty_escalation_policy.test.id - alert_creation = "create_incidents" -} - -resource "pagerduty_escalation_policy" "test" { - name = "%s" - num_loops = 2 - rule { - escalation_delay_in_minutes = 10 - target { - type = "user_reference" - id = pagerduty_user.test.id - } - } -} - -resource "pagerduty_service_integration" "foo" { - name = "%s" - service = pagerduty_service.test.id - vendor = data.pagerduty_vendor.datadog.id -} - -data "pagerduty_vendor" "datadog" { - name = "datadog" -} - -data "pagerduty_service" "by_name" { - name = pagerduty_service.test.name -} -`, username, email, service, escalationPolicy, serviceIntegration) -} - func testAccDataSourcePagerDutyIntegrationConfigStep2(service, serviceIntegration string) string { return fmt.Sprintf(` data "pagerduty_service_integration" "service_integration" { service_name = "%s" - integration_type = "%s" + integration_summary = "%s" } output "output_id" { From 2eb945078fd812b84c1a00905810d0c4ddf70bca Mon Sep 17 00:00:00 2001 From: petetanton Date: Fri, 30 Jul 2021 13:55:03 +0100 Subject: [PATCH 5/8] refactor function names --- pagerduty/data_source_pagerduty_service_integration.go | 6 +++--- pagerduty/provider.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pagerduty/data_source_pagerduty_service_integration.go b/pagerduty/data_source_pagerduty_service_integration.go index 2754f4fc4..67562112e 100644 --- a/pagerduty/data_source_pagerduty_service_integration.go +++ b/pagerduty/data_source_pagerduty_service_integration.go @@ -11,9 +11,9 @@ import ( "github.com/heimweh/go-pagerduty/pagerduty" ) -func dataSourcePagerDutyIntegration() *schema.Resource { +func dataSourcePagerDutyServiceIntegration() *schema.Resource { return &schema.Resource{ - Read: dataSourcePagerDutyIntegrationRead, + Read: dataSourcePagerDutyServiceIntegrationRead, Schema: map[string]*schema.Schema{ "service_name": { @@ -35,7 +35,7 @@ func dataSourcePagerDutyIntegration() *schema.Resource { } } -func dataSourcePagerDutyIntegrationRead(d *schema.ResourceData, meta interface{}) error { +func dataSourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) log.Printf("[INFO] Reading PagerDuty service") diff --git a/pagerduty/provider.go b/pagerduty/provider.go index 6b4d21c8f..80de9aae4 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -36,7 +36,7 @@ func Provider() terraform.ResourceProvider { "pagerduty_vendor": dataSourcePagerDutyVendor(), "pagerduty_extension_schema": dataSourcePagerDutyExtensionSchema(), "pagerduty_service": dataSourcePagerDutyService(), - "pagerduty_service_integration": dataSourcePagerDutyIntegration(), + "pagerduty_service_integration": dataSourcePagerDutyServiceIntegration(), "pagerduty_business_service": dataSourcePagerDutyBusinessService(), "pagerduty_priority": dataSourcePagerDutyPriority(), "pagerduty_ruleset": dataSourcePagerDutyRuleset(), From f7feaf36a89505c0331a5c332d5ff9407afa543c Mon Sep 17 00:00:00 2001 From: petetanton Date: Fri, 30 Jul 2021 14:07:06 +0100 Subject: [PATCH 6/8] update docs --- .../docs/d/service_integration.html.markdown | 30 +++++++++++++++++++ website/pagerduty.erb | 7 +++-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 website/docs/d/service_integration.html.markdown diff --git a/website/docs/d/service_integration.html.markdown b/website/docs/d/service_integration.html.markdown new file mode 100644 index 000000000..c114839c1 --- /dev/null +++ b/website/docs/d/service_integration.html.markdown @@ -0,0 +1,30 @@ +--- +layout: "pagerduty" +page_title: "PagerDuty: pagerduty_service_integration" +sidebar_current: "docs-pagerduty-datasource-service_integration" +description: |- + Get information about a service integration. +--- + +# pagerduty\_service\_integration + +Use this data source to get information about a specific service_integration. + +## Example Usage + +```hcl +data "pagerduty_service_integration" "example" { + service_name = "My Service" + integration_summary = "Datadog" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `service_name` - (Required) The service name to use to find a service in the PagerDuty API. +* `integration_summary` - (Required) The integration summary used to find the desired integration on the service + +## Attributes Reference +* `integration_key` - The integration key for the integration. This can be used to configure alerts. diff --git a/website/pagerduty.erb b/website/pagerduty.erb index 5a478472e..32594c590 100644 --- a/website/pagerduty.erb +++ b/website/pagerduty.erb @@ -34,6 +34,9 @@ > pagerduty_service + > + pagerduty_service_integration + > pagerduty_user @@ -75,7 +78,7 @@ > pagerduty_response_play - + > pagerduty_ruleset @@ -87,7 +90,7 @@ > pagerduty_service - + > pagerduty_service_event_rule From c3a60232073a0d25cfccb5493db33dc60b17654d Mon Sep 17 00:00:00 2001 From: petetanton Date: Mon, 16 Aug 2021 16:28:13 +0100 Subject: [PATCH 7/8] refactor test --- ...urce_pagerduty_service_integration_test.go | 107 +++++++++--------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/pagerduty/data_source_pagerduty_service_integration_test.go b/pagerduty/data_source_pagerduty_service_integration_test.go index 212a6b243..447c8cba0 100644 --- a/pagerduty/data_source_pagerduty_service_integration_test.go +++ b/pagerduty/data_source_pagerduty_service_integration_test.go @@ -2,55 +2,13 @@ package pagerduty import ( "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" - "github.com/heimweh/go-pagerduty/pagerduty" ) -func setupResources(service, serviceIntegration, escalationPolicy, username, email string) error { - client, err := pagerduty.NewClient(&pagerduty.Config{Token: os.Getenv("PAGERDUTY_TOKEN")}) - if err != nil { - return err - } - - createUser, _, err := client.Users.Create(&pagerduty.User{Name: username, Email: email}) - if err != nil { - return err - } - - createEp, _, err := client.EscalationPolicies.Create(&pagerduty.EscalationPolicy{ - Name: escalationPolicy, - EscalationRules: []*pagerduty.EscalationRule{{EscalationDelayInMinutes: 10, Targets: []*pagerduty.EscalationTargetReference{{Type: "user_reference", ID: createUser.ID}}}}, - }) - if err != nil { - return err - } - - createResponse, _, err := client.Services.Create(&pagerduty.Service{ - Name: service, - EscalationPolicy: &pagerduty.EscalationPolicyReference{ID: createEp.ID, Type: "escalation_policy_reference"}, - }) - if err != nil { - return err - } - - _, _, err = client.Services.CreateIntegration(createResponse.ID, &pagerduty.Integration{ - Summary: serviceIntegration, - Type: "service_integration_reference", - Service: &pagerduty.ServiceReference{ID: createResponse.ID, Type: "service_reference"}, - Vendor: &pagerduty.VendorReference{ID: "PAM4FGS", Type: "vendor_reference"}, - }) - if err != nil { - return fmt.Errorf("error creating integration: %v", err) - } - - return nil -} - func TestAccDataSourcePagerDutyIntegration_Basic(t *testing.T) { username := fmt.Sprintf("tf-%s", acctest.RandString(5)) email := fmt.Sprintf("%s@foo.com", username) @@ -58,18 +16,24 @@ func TestAccDataSourcePagerDutyIntegration_Basic(t *testing.T) { escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5)) serviceIntegration := "Datadog" - err := setupResources(service, serviceIntegration, escalationPolicy, username, email) - if err != nil { - t.Fatal(err) - } - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourcePagerDutyIntegrationConfigStep2(service, serviceIntegration), - Check: verifyOutput("output_id"), + Config: testAccDataSourcePagerDutyIntegrationConfigStep1(service, serviceIntegration, email, escalationPolicy), + //Destroy: false, + Check: func(state *terraform.State) error { + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourcePagerDutyIntegrationConfigStep2(service, serviceIntegration), + Check: verifyOutput("output_id"), + }}, + }) + return nil + }, }, }, }) @@ -91,15 +55,54 @@ func verifyOutput(name string) resource.TestCheckFunc { } } +func testAccDataSourcePagerDutyIntegrationConfigStep1(service, serviceIntegration, email, escalationPolicy string) string { + return fmt.Sprintf(` +resource "pagerduty_user" "pagerduty_user" { + email = "%s" + name = "test user" +} + +resource "pagerduty_escalation_policy" "escalation_policy" { + name = "%s" + rule { + escalation_delay_in_minutes = 5 + target { + type = "user_reference" + id = pagerduty_user.pagerduty_user.id + } + } + +} + +resource "pagerduty_service" "pagerduty_service" { + name = "%s" + escalation_policy = pagerduty_escalation_policy.escalation_policy.id +} + +resource "pagerduty_service_integration" "service_integration" { + name = "%s" + service = pagerduty_service.pagerduty_service.id + vendor = data.pagerduty_vendor.datadog.id +} + +data "pagerduty_vendor" "datadog" { + name = "datadog" +} + + +`, email, escalationPolicy, service, serviceIntegration) +} + func testAccDataSourcePagerDutyIntegrationConfigStep2(service, serviceIntegration string) string { return fmt.Sprintf(` + data "pagerduty_service_integration" "service_integration" { - service_name = "%s" - integration_summary = "%s" + service_name = "%s" + integration_summary = "%s" } output "output_id" { - value = data.pagerduty_service_integration.service_integration.integration_key + value = data.pagerduty_service_integration.service_integration.integration_key } `, service, serviceIntegration) } From 8e6f32c5fc495d3a61f58dd5c71e07df0b2b7935 Mon Sep 17 00:00:00 2001 From: petetanton Date: Mon, 16 Aug 2021 16:28:24 +0100 Subject: [PATCH 8/8] remove comment --- pagerduty/data_source_pagerduty_service_integration_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pagerduty/data_source_pagerduty_service_integration_test.go b/pagerduty/data_source_pagerduty_service_integration_test.go index 447c8cba0..0a348edc6 100644 --- a/pagerduty/data_source_pagerduty_service_integration_test.go +++ b/pagerduty/data_source_pagerduty_service_integration_test.go @@ -22,7 +22,6 @@ func TestAccDataSourcePagerDutyIntegration_Basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccDataSourcePagerDutyIntegrationConfigStep1(service, serviceIntegration, email, escalationPolicy), - //Destroy: false, Check: func(state *terraform.State) error { resource.Test(t, resource.TestCase{ Providers: testAccProviders,