From 229a1343b4111880ab02e71ffc22ffcee7c639cc Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 23 Feb 2017 20:19:35 +0100 Subject: [PATCH] provider/pagerduty: Import support for service integrations (#12141) * Remove custom imports * Add import support for service integrations --- ...port_pagerduty_service_integration_test.go | 28 +++++++++++++++ .../pagerduty/resource_pagerduty_addon.go | 9 +---- .../resource_pagerduty_escalation_policy.go | 9 +---- .../pagerduty/resource_pagerduty_schedule.go | 9 +---- .../pagerduty/resource_pagerduty_service.go | 10 +----- .../resource_pagerduty_service_integration.go | 35 +++++++++++++++++-- .../pagerduty/resource_pagerduty_team.go | 9 +---- .../pagerduty/resource_pagerduty_user.go | 9 +---- 8 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 builtin/providers/pagerduty/import_pagerduty_service_integration_test.go diff --git a/builtin/providers/pagerduty/import_pagerduty_service_integration_test.go b/builtin/providers/pagerduty/import_pagerduty_service_integration_test.go new file mode 100644 index 000000000..38c3a060b --- /dev/null +++ b/builtin/providers/pagerduty/import_pagerduty_service_integration_test.go @@ -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, + }, + }, + }) +} diff --git a/builtin/providers/pagerduty/resource_pagerduty_addon.go b/builtin/providers/pagerduty/resource_pagerduty_addon.go index 36d037ef4..04190ecf8 100644 --- a/builtin/providers/pagerduty/resource_pagerduty_addon.go +++ b/builtin/providers/pagerduty/resource_pagerduty_addon.go @@ -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 -} diff --git a/builtin/providers/pagerduty/resource_pagerduty_escalation_policy.go b/builtin/providers/pagerduty/resource_pagerduty_escalation_policy.go index d6fb1a69c..f8c4e37bb 100644 --- a/builtin/providers/pagerduty/resource_pagerduty_escalation_policy.go +++ b/builtin/providers/pagerduty/resource_pagerduty_escalation_policy.go @@ -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 -} diff --git a/builtin/providers/pagerduty/resource_pagerduty_schedule.go b/builtin/providers/pagerduty/resource_pagerduty_schedule.go index 609a37c45..38b1031b0 100644 --- a/builtin/providers/pagerduty/resource_pagerduty_schedule.go +++ b/builtin/providers/pagerduty/resource_pagerduty_schedule.go @@ -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 -} diff --git a/builtin/providers/pagerduty/resource_pagerduty_service.go b/builtin/providers/pagerduty/resource_pagerduty_service.go index dbcbdde12..8405f3361 100644 --- a/builtin/providers/pagerduty/resource_pagerduty_service.go +++ b/builtin/providers/pagerduty/resource_pagerduty_service.go @@ -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 -} diff --git a/builtin/providers/pagerduty/resource_pagerduty_service_integration.go b/builtin/providers/pagerduty/resource_pagerduty_service_integration.go index 496e60e6f..fb12dc675 100644 --- a/builtin/providers/pagerduty/resource_pagerduty_service_integration.go +++ b/builtin/providers/pagerduty/resource_pagerduty_service_integration.go @@ -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 +} diff --git a/builtin/providers/pagerduty/resource_pagerduty_team.go b/builtin/providers/pagerduty/resource_pagerduty_team.go index f322890b1..6bbe1ca46 100644 --- a/builtin/providers/pagerduty/resource_pagerduty_team.go +++ b/builtin/providers/pagerduty/resource_pagerduty_team.go @@ -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 -} diff --git a/builtin/providers/pagerduty/resource_pagerduty_user.go b/builtin/providers/pagerduty/resource_pagerduty_user.go index 744523d93..dc1bd71d2 100644 --- a/builtin/providers/pagerduty/resource_pagerduty_user.go +++ b/builtin/providers/pagerduty/resource_pagerduty_user.go @@ -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 -}