forked from PagerDuty/terraform-provider-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
petetanton
committed
Aug 16, 2021
1 parent
f7feaf3
commit c3a6023
Showing
1 changed file
with
55 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,74 +2,38 @@ 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("%[email protected]", username) | ||
service := fmt.Sprintf("tf-%s", acctest.RandString(5)) | ||
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) | ||
} |