Skip to content

Commit

Permalink
Fix service integration importation when more than 100 services exist (
Browse files Browse the repository at this point in the history
…#47)

* Update github.com/hashicorp/terraform/helper/resource dependency to v0.10.7 to get ImportStateIdFunc feature

* A service integration must belong to a service according to the API

* Service integration importation failed with more than 100 services. Change importation logic to use an ID formed as `service_id.integration_id`
  • Loading branch information
pdecat authored and heimweh committed Dec 4, 2017
1 parent 0b32f5e commit 5fb17ee
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 25 deletions.
6 changes: 6 additions & 0 deletions pagerduty/import_pagerduty_service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccPagerDutyServiceIntegration_import(t *testing.T) {
Expand All @@ -26,9 +27,14 @@ func TestAccPagerDutyServiceIntegration_import(t *testing.T) {

{
ResourceName: "pagerduty_service_integration.foo",
ImportStateIdFunc: testAccCheckPagerDutyServiceIntegrationId,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckPagerDutyServiceIntegrationId(s *terraform.State) (string, error) {
return fmt.Sprintf("%v.%v", s.RootModule().Resources["pagerduty_service.foo"].Primary.ID, s.RootModule().Resources["pagerduty_service_integration.foo"].Primary.ID), nil
}
29 changes: 12 additions & 17 deletions pagerduty/resource_pagerduty_service_integration.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package pagerduty

import (
"fmt"
"log"

"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
"strings"
)

func resourcePagerDutyServiceIntegration() *schema.Resource {
Expand All @@ -24,7 +25,7 @@ func resourcePagerDutyServiceIntegration() *schema.Resource {
},
"service": {
Type: schema.TypeString,
Optional: true,
Required: true,
},
"type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -188,26 +189,20 @@ func resourcePagerDutyServiceIntegrationDelete(d *schema.ResourceData, meta inte
func resourcePagerDutyServiceIntegrationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*pagerduty.Client)

resp, _, err := client.Services.List(&pagerduty.ListServicesOptions{Limit: 100})
if err != nil {
return []*schema.ResourceData{}, err
}

var serviceID string
ids := strings.Split(d.Id(), ".")

for _, service := range resp.Services {
for _, integration := range service.Integrations {
if integration.ID == d.Id() {
serviceID = service.ID
}
}
if len(ids) != 2 {
return []*schema.ResourceData{}, fmt.Errorf("Error importing pagerduty_service_integration. Expecting an importation ID formed as '<service_id>.<integration_id>'")
}
sid, id := ids[0], ids[1]

if serviceID == "" {
return []*schema.ResourceData{}, fmt.Errorf("Error importing pagerduty_service_integration. Could not locate a service ID for the integration")
_, _, err := client.Services.GetIntegration(sid, id, nil)
if err != nil {
return []*schema.ResourceData{}, err
}

d.Set("service", serviceID)
d.SetId(id)
d.Set("service", sid)

return []*schema.ResourceData{d}, nil
}

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.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,10 @@
"versionExact": "v0.10.0"
},
{
"checksumSHA1": "dhU2woQaSEI2OnbYLdkHxf7/nu8=",
"checksumSHA1": "CwIwANN1kyCaXfotQ3YU0NxQ+xE=",
"path": "github.com/hashicorp/terraform/helper/resource",
"revision": "2041053ee9444fa8175a298093b55a89586a1823",
"revisionTime": "2017-08-02T18:39:14Z",
"revision": "8ba8fc79c106dee098c625f4c40cd53d73660a92",
"revisionTime": "2017-10-02T18:46:49Z",
"version": "v0.10.0",
"versionExact": "v0.10.0"
},
Expand Down
10 changes: 9 additions & 1 deletion website/docs/r/service_integration.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ resource "pagerduty_service_integration" "cloudwatch" {

The following arguments are supported:

* `service` - (Required) The ID of the service the integration should belong to.
* `name` - (Optional) The name of the service integration.
* `type` - (Optional) The service type. Can be:
`aws_cloudwatch_inbound_integration`,
Expand All @@ -86,7 +87,6 @@ The following arguments are supported:
**Note:** This is meant for **generic** service integrations.
To integrate with a **vendor** (e.g Datadog or Amazon Cloudwatch) use the `vendor` field instead.

* `service` - (Optional) The ID of the service the integration should belong to.
* `vendor` - (Optional) The ID of the vendor the integration should integrate with (e.g Datadog or Amazon Cloudwatch).
* `integration_key` - (Optional) This is the unique key used to route events to this integration when received via the PagerDuty Events API.
* `integration_email` - (Optional) This is the unique fully-qualified email address used for routing emails to this integration for processing.
Expand All @@ -99,3 +99,11 @@ The following attributes are exported:
* `id` - The ID of the service integration.
* `integration_key` - This is the unique key used to route events to this integration when received via the PagerDuty Events API.
* `integration_email` - This is the unique fully-qualified email address used for routing emails to this integration for processing.

## Import

Services can be imported using their related `service` id and service integration `id` separated by a dot, e.g.

```
$ terraform import pagerduty_service.main PLSSSSS.PLIIIII
```

0 comments on commit 5fb17ee

Please sign in to comment.