Skip to content

Commit

Permalink
Merge pull request #304 from bkabrda/fix-individual-pd-services
Browse files Browse the repository at this point in the history
Make sure PD services don't get removed by updating PD resource
  • Loading branch information
Slavek Kabrda authored Aug 29, 2019
2 parents 4eb7082 + dafe595 commit d8cf584
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
18 changes: 10 additions & 8 deletions datadog/resource_datadog_integration_pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,18 @@ func resourceDatadogIntegrationPagerdutyUpdate(d *schema.ResourceData, meta inte
// if there are none currently configured services, we actually
// have to remove them explicitly, otherwise the underlying API client
// would not send the "services" key at all and they wouldn't get deleted
currentServices := d.Get("services").([]interface{})
if len(currentServices) == 0 {
pd, err := client.GetIntegrationPD()
if err != nil {
return fmt.Errorf("Error while deleting Pagerduty integration service object: %v", err)
}
for _, service := range pd.Services {
if err := client.DeleteIntegrationPDService(*service.ServiceName); err != nil {
if value, ok := d.GetOk("individual_services"); !ok || !value.(bool) {
currentServices := d.Get("services").([]interface{})
if len(currentServices) == 0 {
pd, err := client.GetIntegrationPD()
if err != nil {
return fmt.Errorf("Error while deleting Pagerduty integration service object: %v", err)
}
for _, service := range pd.Services {
if err := client.DeleteIntegrationPDService(*service.ServiceName); err != nil {
return fmt.Errorf("Error while deleting Pagerduty integration service object: %v", err)
}
}
}
}
return resourceDatadogIntegrationPagerdutyRead(d, meta)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog

import (
"strings"
"testing"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -47,6 +48,10 @@ func TestAccDatadogIntegrationPagerdutyServiceObject_Basic(t *testing.T) {
"datadog_integration_pagerduty_service_object.testing_bar", "service_key", "54321098765432109876_2"),
),
},
{
// make sure that updating the PD resource itself doesn't delete the individual service objects
Config: strings.Replace(testAccCheckDatadogIntegrationPagerdutyServiceObjectUpdatedConfig, "testdomain", "testdomain2", -1),
},
},
})
}
Expand Down
15 changes: 15 additions & 0 deletions datadog/resource_datadog_integration_pagerduty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func TestAccDatadogIntegrationPagerduty_Migrate2ServiceObjects(t *testing.T) {
"datadog_integration_pagerduty.pd", "services.1.service_key", "*****"),
),
},
{
// this represents the intermediary step which will ensure the old
// inline-defined service objects get removed
Config: testAccCheckDatadogIntegrationPagerdutyConfigDuringMigration,
},
{
Config: testAccCheckDatadogIntegrationPagerdutyConfigAfterMigration,
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -200,6 +205,16 @@ resource "datadog_integration_pagerduty" "pd" {
api_token = "*****"
}`

const testAccCheckDatadogIntegrationPagerdutyConfigDuringMigration = `
resource "datadog_integration_pagerduty" "pd" {
schedules = [
"https://ddog.pagerduty.com/schedules/X123VF",
"https://ddog.pagerduty.com/schedules/X321XX"
]
subdomain = "ddog"
api_token = "*****"
}`

const testAccCheckDatadogIntegrationPagerdutyConfigAfterMigration = `
resource "datadog_integration_pagerduty" "pd" {
individual_services = true
Expand Down
23 changes: 18 additions & 5 deletions website/docs/r/integration_pagerduty.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ resource "datadog_integration_pagerduty" "pd" {

### Migrating from Inline Services to Individual Resources

Migrating from usage of inline services to individual resources is very simple. The following example shows how to convert an existing inline services configuration to configuration using individual resources. Doing analogous change and running `terraform apply` is all that's necessary to migrate.
Migrating from usage of inline services to individual resources is very simple. The following example shows how to convert an existing inline services configuration to configuration using individual resources. Doing analogous change and running `terraform apply` after every step is all that's necessary to migrate.

```
# Before
# First step - this is what the configuration looked like initially
locals {
pd_services = {
Expand All @@ -127,11 +127,24 @@ resource "datadog_integration_pagerduty" "pd" {
```

```
# After
# Second step - this will remove the inline-defined service objects
# Note that during this step, `individual_services` must not be defined
resource "datadog_integration_pagerduty" "pd" {
# `individual_services` was added
# `services` was removed
schedules = [
"https://ddog.pagerduty.com/schedules/X123VF",
"https://ddog.pagerduty.com/schedules/X321XX"
]
subdomain = "ddog"
api_token = "38457822378273432587234242874"
}
```

```
# Third step - this will reintroduce the service objects as individual resources
resource "datadog_integration_pagerduty" "pd" {
# `individual_services = true` was added
individual_services = true
schedules = [
"https://ddog.pagerduty.com/schedules/X123VF",
Expand Down

0 comments on commit d8cf584

Please sign in to comment.