provider/pagerduty: Import support for service integrations (#12141)

* Remove custom imports

* Add import support for service integrations
This commit is contained in:
Alexander 2017-02-23 20:19:35 +01:00 committed by Paul Stack
parent cd56e6f071
commit 229a1343b4
8 changed files with 67 additions and 51 deletions

View File

@ -0,0 +1,28 @@
package pagerduty
import (
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccPagerDutyServiceIntegration_import(t *testing.T) {
resourceName := "pagerduty_service_integration.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceIntegrationDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckPagerDutyServiceIntegrationConfig,
},
resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

View File

@ -14,7 +14,7 @@ func resourcePagerDutyAddon() *schema.Resource {
Update: resourcePagerDutyAddonUpdate,
Delete: resourcePagerDutyAddonDelete,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyAddonImport,
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
@ -105,10 +105,3 @@ func resourcePagerDutyAddonDelete(d *schema.ResourceData, meta interface{}) erro
return nil
}
func resourcePagerDutyAddonImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourcePagerDutyAddonRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

View File

@ -14,7 +14,7 @@ func resourcePagerDutyEscalationPolicy() *schema.Resource {
Update: resourcePagerDutyEscalationPolicyUpdate,
Delete: resourcePagerDutyEscalationPolicyDelete,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyEscalationPolicyImport,
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
@ -167,10 +167,3 @@ func resourcePagerDutyEscalationPolicyDelete(d *schema.ResourceData, meta interf
return nil
}
func resourcePagerDutyEscalationPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourcePagerDutyEscalationPolicyRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

View File

@ -14,7 +14,7 @@ func resourcePagerDutySchedule() *schema.Resource {
Update: resourcePagerDutyScheduleUpdate,
Delete: resourcePagerDutyScheduleDelete,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyScheduleImport,
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
@ -196,10 +196,3 @@ func resourcePagerDutyScheduleDelete(d *schema.ResourceData, meta interface{}) e
return nil
}
func resourcePagerDutyScheduleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourcePagerDutyScheduleRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

View File

@ -14,7 +14,7 @@ func resourcePagerDutyService() *schema.Resource {
Update: resourcePagerDutyServiceUpdate,
Delete: resourcePagerDutyServiceDelete,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyServiceImport,
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
@ -303,11 +303,3 @@ func resourcePagerDutyServiceDelete(d *schema.ResourceData, meta interface{}) er
return nil
}
func resourcePagerDutyServiceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourcePagerDutyServiceRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

View File

@ -1,6 +1,7 @@
package pagerduty
import (
"fmt"
"log"
pagerduty "github.com/PagerDuty/go-pagerduty"
@ -13,6 +14,9 @@ func resourcePagerDutyServiceIntegration() *schema.Resource {
Read: resourcePagerDutyServiceIntegrationRead,
Update: resourcePagerDutyServiceIntegrationUpdate,
Delete: resourcePagerDutyServiceIntegrationDelete,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyServiceIntegrationImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
@ -130,8 +134,8 @@ func resourcePagerDutyServiceIntegrationRead(d *schema.ResourceData, meta interf
d.Set("name", serviceIntegration.Name)
d.Set("type", serviceIntegration.Type)
d.Set("service", serviceIntegration.Service)
d.Set("vendor", serviceIntegration.Vendor)
d.Set("service", serviceIntegration.Service.ID)
d.Set("vendor", serviceIntegration.Vendor.ID)
d.Set("integration_key", serviceIntegration.IntegrationKey)
d.Set("integration_email", serviceIntegration.IntegrationEmail)
@ -173,3 +177,30 @@ func resourcePagerDutyServiceIntegrationDelete(d *schema.ResourceData, meta inte
return nil
}
func resourcePagerDutyServiceIntegrationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*pagerduty.Client)
resp, err := client.ListServices(pagerduty.ListServiceOptions{})
if err != nil {
return []*schema.ResourceData{}, err
}
var serviceID string
for _, service := range resp.Services {
for _, integration := range service.Integrations {
if integration.ID == d.Id() {
serviceID = service.ID
}
}
}
if serviceID == "" {
return []*schema.ResourceData{}, fmt.Errorf("Error importing pagerduty_service_integration. Could not locate a service ID for the integration")
}
d.Set("service", serviceID)
return []*schema.ResourceData{d}, nil
}

View File

@ -14,7 +14,7 @@ func resourcePagerDutyTeam() *schema.Resource {
Update: resourcePagerDutyTeamUpdate,
Delete: resourcePagerDutyTeamDelete,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyTeamImport,
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
@ -105,10 +105,3 @@ func resourcePagerDutyTeamDelete(d *schema.ResourceData, meta interface{}) error
return nil
}
func resourcePagerDutyTeamImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourcePagerDutyTeamRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

View File

@ -14,7 +14,7 @@ func resourcePagerDutyUser() *schema.Resource {
Update: resourcePagerDutyUserUpdate,
Delete: resourcePagerDutyUserDelete,
Importer: &schema.ResourceImporter{
State: resourcePagerDutyUserImport,
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
@ -227,10 +227,3 @@ func resourcePagerDutyUserDelete(d *schema.ResourceData, meta interface{}) error
return nil
}
func resourcePagerDutyUserImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourcePagerDutyUserRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}