Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating service_dependency to support technical service dependencies #238

Merged
merged 2 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-pagerduty
require (
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/heimweh/go-pagerduty v0.0.0-20200528011640-24a6d8472a24
github.com/heimweh/go-pagerduty v0.0.0-20200603015639-6677e9a50383
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ github.com/heimweh/go-pagerduty v0.0.0-20200429000711-fdd3a48907e7 h1:8groiuI/ki
github.com/heimweh/go-pagerduty v0.0.0-20200429000711-fdd3a48907e7/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20200528011640-24a6d8472a24 h1:ga84s0DaOnrYN7IL7jMug6ins5QpiMuNpXFxCKqFAvg=
github.com/heimweh/go-pagerduty v0.0.0-20200528011640-24a6d8472a24/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/heimweh/go-pagerduty v0.0.0-20200603015639-6677e9a50383 h1:IMMksFroOt5lul7trOUQZjP4nlCIE5XiRrJqCxZ07Jo=
github.com/heimweh/go-pagerduty v0.0.0-20200603015639-6677e9a50383/go.mod h1:6+bccpjQ/PM8uQY9m8avM4MJea+3vo3ta9r8kGQ4XFY=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/import_pagerduty_service_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func TestAccPagerDutyServiceDependency_import(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDependencyDestroy,
CheckDestroy: testAccCheckPagerDutyBusinessServiceDependencyDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyServiceDependencyConfig(service, businessService, username, email, escalationPolicy),
Config: testAccCheckPagerDutyBusinessServiceDependencyConfig(service, businessService, username, email, escalationPolicy),
},

{
Expand Down
14 changes: 7 additions & 7 deletions pagerduty/resource_pagerduty_service_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func resourcePagerDutyServiceDependencyDisassociate(d *schema.ResourceData, meta
log.Printf("[INFO] Disassociating PagerDuty dependency %s", dependency.DependentService.ID)

// listServiceRelationships by calling get dependencies using the serviceDependency.DependentService.ID
depResp, _, err := client.ServiceDependencies.GetBusinessServiceDependencies(dependency.DependentService.ID)
depResp, _, err := client.ServiceDependencies.GetServiceDependenciesForType(dependency.DependentService.ID, dependency.DependentService.Type)
if err != nil {
return err
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func resourcePagerDutyServiceDependencyRead(d *schema.ResourceData, meta interfa
serviceDependency, err := buildServiceDependencyStruct(d)
log.Printf("[INFO] Reading PagerDuty dependency %s", serviceDependency.ID)

if err = findDependencySetState(d.Id(), serviceDependency.DependentService.ID, d, meta); err != nil {
if err = findDependencySetState(d.Id(), serviceDependency.DependentService.ID, serviceDependency.DependentService.Type, d, meta); err != nil {
return err
}

Expand Down Expand Up @@ -230,13 +230,13 @@ func convertType(s string) string {
return s
}

func findDependencySetState(depID, busServiceID string, d *schema.ResourceData, meta interface{}) error {
func findDependencySetState(depID, serviceID, serviceType string, d *schema.ResourceData, meta interface{}) error {
client := meta.(*pagerduty.Client)

// Pausing to let the PD API sync.
time.Sleep(1 * time.Second)
retryErr := resource.Retry(30*time.Second, func() *resource.RetryError {
if dependencies, _, err := client.ServiceDependencies.GetBusinessServiceDependencies(busServiceID); err != nil {
if dependencies, _, err := client.ServiceDependencies.GetServiceDependenciesForType(serviceID, serviceType); err != nil {
if isErrCode(err, 404) || isErrCode(err, 500) || isErrCode(err, 429) {
return resource.RetryableError(err)
}
Expand Down Expand Up @@ -266,11 +266,11 @@ func resourcePagerDutyServiceDependencyImport(d *schema.ResourceData, meta inter
ids := strings.Split(d.Id(), ".")

if len(ids) != 2 {
return []*schema.ResourceData{}, fmt.Errorf("Error importing pagerduty_service_dependency. Expecting an importation ID formed as '<business_service_id>.<service_dependency_id>'")
return []*schema.ResourceData{}, fmt.Errorf("Error importing pagerduty_service_dependency. Expecting an importation ID formed as '<supporting_service_id>.<supporting_service_type>.<service_dependency_id>'")
}
sid, id := ids[0], ids[1]
sid, st, id := ids[0], ids[1], ids[2]

if err := findDependencySetState(id, sid, d, meta); err != nil {
if err := findDependencySetState(id, sid, st, d, meta); err != nil {
return []*schema.ResourceData{}, err
}

Expand Down
165 changes: 154 additions & 11 deletions pagerduty/resource_pagerduty_service_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/heimweh/go-pagerduty/pagerduty"
)

func TestAccPagerDutyServiceDependency_Basic(t *testing.T) {
// Testing Business Service Dependencies
func TestAccPagerDutyBusinessServiceDependency_Basic(t *testing.T) {
service := fmt.Sprintf("tf-%s", acctest.RandString(5))
businessService := fmt.Sprintf("tf-%s", acctest.RandString(5))
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
Expand All @@ -20,12 +21,12 @@ func TestAccPagerDutyServiceDependency_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDependencyDestroy,
CheckDestroy: testAccCheckPagerDutyBusinessServiceDependencyDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyServiceDependencyConfig(service, businessService, username, email, escalationPolicy),
Config: testAccCheckPagerDutyBusinessServiceDependencyConfig(service, businessService, username, email, escalationPolicy),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceDependencyExists("pagerduty_service_dependency.foo"),
testAccCheckPagerDutyBusinessServiceDependencyExists("pagerduty_service_dependency.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service_dependency.foo", "dependency.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -37,7 +38,7 @@ func TestAccPagerDutyServiceDependency_Basic(t *testing.T) {
},
})
}
func testAccCheckPagerDutyServiceDependencyExists(n string) resource.TestCheckFunc {
func testAccCheckPagerDutyBusinessServiceDependencyExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -47,12 +48,11 @@ func testAccCheckPagerDutyServiceDependencyExists(n string) resource.TestCheckFu
if rs.Primary.ID == "" {
return fmt.Errorf("No Service Relationship ID is set")
}

businessService, _ := s.RootModule().Resources["pagerduty_business_service.foo"]

client := testAccProvider.Meta().(*pagerduty.Client)

depResp, _, err := client.ServiceDependencies.GetBusinessServiceDependencies(businessService.Primary.ID)
depResp, _, err := client.ServiceDependencies.GetServiceDependenciesForType(businessService.Primary.ID, "business_service")
if err != nil {
return fmt.Errorf("Business Service not found: %v", err)
}
Expand All @@ -73,7 +73,7 @@ func testAccCheckPagerDutyServiceDependencyExists(n string) resource.TestCheckFu
}
}

func testAccCheckPagerDutyServiceDependencyDestroy(s *terraform.State) error {
func testAccCheckPagerDutyBusinessServiceDependencyDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_service_dependency" {
Expand All @@ -82,22 +82,22 @@ func testAccCheckPagerDutyServiceDependencyDestroy(s *terraform.State) error {
businessService, _ := s.RootModule().Resources["pagerduty_business_service.foo"]

// get business service
dependencies, _, err := client.ServiceDependencies.GetBusinessServiceDependencies(businessService.Primary.ID)
dependencies, _, err := client.ServiceDependencies.GetServiceDependenciesForType(businessService.Primary.ID, "business_service")
if err != nil {
// if the business service doesn't exist, that's okay
return nil
}
// get business service dependencies
for _, rel := range dependencies.Relationships {
if rel.ID == r.Primary.ID {
return fmt.Errorf("Business service relationship still exists")
return fmt.Errorf("supporting service relationship still exists")
}
}

}
return nil
}
func testAccCheckPagerDutyServiceDependencyConfig(service, businessService, username, email, escalationPolicy string) string {
func testAccCheckPagerDutyBusinessServiceDependencyConfig(service, businessService, username, email, escalationPolicy string) string {
return fmt.Sprintf(`
resource "pagerduty_business_service" "foo" {
name = "%s"
Expand Down Expand Up @@ -146,3 +146,146 @@ resource "pagerduty_service_dependency" "foo" {
}
`, businessService, username, email, escalationPolicy, service)
}

// Testing Technical Service Dependencies
func TestAccPagerDutyTechnicalServiceDependency_Basic(t *testing.T) {
dependentService := fmt.Sprintf("tf-%s", acctest.RandString(5))
supportingService := fmt.Sprintf("tf-%s", acctest.RandString(5))
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.com", username)
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyTechnicalServiceDependencyDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyTechnicalServiceDependencyConfig(dependentService, supportingService, username, email, escalationPolicy),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyTechnicalServiceDependencyExists("pagerduty_service_dependency.bar"),
resource.TestCheckResourceAttr(
"pagerduty_service_dependency.bar", "dependency.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service_dependency.bar", "dependency.0.supporting_service.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service_dependency.bar", "dependency.0.dependent_service.#", "1"),
),
},
},
})
}
func testAccCheckPagerDutyTechnicalServiceDependencyExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Service Relationship ID is set")
}
supportService, _ := s.RootModule().Resources["pagerduty_service.supportBar"]

client := testAccProvider.Meta().(*pagerduty.Client)

depResp, _, err := client.ServiceDependencies.GetServiceDependenciesForType(supportService.Primary.ID, "service")
if err != nil {
return fmt.Errorf("Technical Service not found: %v", err)
}
var foundRel *pagerduty.ServiceDependency

// loop serviceRelationships until relationship.IDs match
for _, rel := range depResp.Relationships {
if rel.ID == rs.Primary.ID {
foundRel = rel
break
}
}
if foundRel == nil {
return fmt.Errorf("Service Dependency not found: %v", rs.Primary.ID)
}

return nil
}
}

func testAccCheckPagerDutyTechnicalServiceDependencyDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_service_dependency" {
continue
}
supportService, _ := s.RootModule().Resources["pagerduty_service.supportBar"]

// get service dependencies
dependencies, _, err := client.ServiceDependencies.GetServiceDependenciesForType(supportService.Primary.ID, "service")
if err != nil {
// if the dependency doesn't exist, that's okay
return nil
}
// find desired dependency
for _, rel := range dependencies.Relationships {
if rel.ID == r.Primary.ID {
return fmt.Errorf("supporting service relationship still exists")
}
}

}
return nil
}
func testAccCheckPagerDutyTechnicalServiceDependencyConfig(dependentService, supportingService, username, email, escalationPolicy string) string {
return fmt.Sprintf(`


resource "pagerduty_user" "bar" {
name = "%s"
email = "%s"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}

resource "pagerduty_escalation_policy" "bar" {
name = "%s"
description = "bar-desc"
num_loops = 2
rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = pagerduty_user.bar.id
}
}
}
resource "pagerduty_service" "supportBar" {
name = "%s"
description = "supportBarDesc"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = pagerduty_escalation_policy.bar.id
alert_creation = "create_incidents"
}
resource "pagerduty_service" "dependBar" {
name = "%s"
description = "dependBarDesc"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = pagerduty_escalation_policy.bar.id
alert_creation = "create_incidents"
}
resource "pagerduty_service_dependency" "bar" {
dependency {
dependent_service {
id = pagerduty_service.dependBar.id
type = "service"
}
supporting_service {
id = pagerduty_service.supportBar.id
type = "service"
}
}
}
`, username, email, escalationPolicy, supportingService, dependentService)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ github.com/hashicorp/terraform-svchost/auth
github.com/hashicorp/terraform-svchost/disco
# github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/hashicorp/yamux
# github.com/heimweh/go-pagerduty v0.0.0-20200528011640-24a6d8472a24
# github.com/heimweh/go-pagerduty v0.0.0-20200603015639-6677e9a50383
github.com/heimweh/go-pagerduty/pagerduty
# github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af
github.com/jmespath/go-jmespath
Expand Down
Loading