diff --git a/pagerduty/data_source_pagerduty_extension_schema.go b/pagerduty/data_source_pagerduty_extension_schema.go deleted file mode 100644 index 155d95442..000000000 --- a/pagerduty/data_source_pagerduty_extension_schema.go +++ /dev/null @@ -1,76 +0,0 @@ -package pagerduty - -import ( - "fmt" - "log" - "net/http" - "strings" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/heimweh/go-pagerduty/pagerduty" -) - -func dataSourcePagerDutyExtensionSchema() *schema.Resource { - return &schema.Resource{ - Read: dataSourcePagerDutyExtensionSchemaRead, - - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - }, - "type": { - Type: schema.TypeString, - Computed: true, - }, - }, - } -} - -func dataSourcePagerDutyExtensionSchemaRead(d *schema.ResourceData, meta interface{}) error { - client, err := meta.(*Config).Client() - if err != nil { - return err - } - - log.Printf("[INFO] Reading PagerDuty Extension Schema") - - searchName := d.Get("name").(string) - - return retry.Retry(5*time.Minute, func() *retry.RetryError { - resp, _, err := client.ExtensionSchemas.List(&pagerduty.ListExtensionSchemasOptions{Query: searchName}) - if err != nil { - if isErrCode(err, http.StatusBadRequest) { - return retry.NonRetryableError(err) - } - - // Delaying retry by 30s as recommended by PagerDuty - // https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit - time.Sleep(30 * time.Second) - return retry.RetryableError(err) - } - - var found *pagerduty.ExtensionSchema - - for _, schema := range resp.ExtensionSchemas { - if strings.EqualFold(schema.Label, searchName) { - found = schema - break - } - } - - if found == nil { - return retry.NonRetryableError( - fmt.Errorf("Unable to locate any extension schema with the name: %s", searchName), - ) - } - - d.SetId(found.ID) - d.Set("name", found.Label) - d.Set("type", found.Type) - - return nil - }) -} diff --git a/pagerduty/provider.go b/pagerduty/provider.go index 06609e24d..416331248 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -89,7 +89,6 @@ func Provider(isMux bool) *schema.Provider { "pagerduty_user_contact_method": dataSourcePagerDutyUserContactMethod(), "pagerduty_team": dataSourcePagerDutyTeam(), "pagerduty_vendor": dataSourcePagerDutyVendor(), - "pagerduty_extension_schema": dataSourcePagerDutyExtensionSchema(), "pagerduty_service": dataSourcePagerDutyService(), "pagerduty_service_integration": dataSourcePagerDutyServiceIntegration(), "pagerduty_business_service": dataSourcePagerDutyBusinessService(), diff --git a/pagerdutyplugin/data_source_pagerduty_extension_schema.go b/pagerdutyplugin/data_source_pagerduty_extension_schema.go new file mode 100644 index 000000000..74e8d27fd --- /dev/null +++ b/pagerdutyplugin/data_source_pagerduty_extension_schema.go @@ -0,0 +1,96 @@ +package pagerduty + +import ( + "context" + "fmt" + "log" + "strings" + "time" + + "github.com/PagerDuty/go-pagerduty" + "github.com/PagerDuty/terraform-provider-pagerduty/util" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" +) + +type dataSourceExtensionSchema struct{ client *pagerduty.Client } + +var _ datasource.DataSourceWithConfigure = (*dataSourceExtensionSchema)(nil) + +func (*dataSourceExtensionSchema) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = "pagerduty_extension_schema" +} + +func (*dataSourceExtensionSchema) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{Computed: true}, + "name": schema.StringAttribute{Required: true}, + "type": schema.StringAttribute{Computed: true}, + }, + } +} + +func (d *dataSourceExtensionSchema) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + resp.Diagnostics.Append(ConfigurePagerdutyClient(&d.client, req.ProviderData)...) +} + +func (d *dataSourceExtensionSchema) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + log.Println("[INFO] Reading PagerDuty extension schema") + + var searchName types.String + resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("name"), &searchName)...) + if resp.Diagnostics.HasError() { + return + } + + var found *pagerduty.ExtensionSchema + // TODO delete and comment in PR: changed to 2min because 5min/30s is 10 attempts + err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError { + list, err := d.client.ListExtensionSchemasWithContext(ctx, pagerduty.ListExtensionSchemaOptions{}) + if err != nil { + if util.IsBadRequestError(err) { + return retry.NonRetryableError(err) + } + return retry.RetryableError(err) + } + + for _, extensionSchema := range list.ExtensionSchemas { + if strings.EqualFold(extensionSchema.Label, searchName.ValueString()) { + found = &extensionSchema + break + } + } + return nil + }) + if err != nil { + resp.Diagnostics.AddError( + fmt.Sprintf("Error reading PagerDuty extension schema %s", searchName), + err.Error(), + ) + } + + if found == nil { + resp.Diagnostics.AddError( + fmt.Sprintf("Unable to locate any extension schema with the name: %s", searchName), + "", + ) + return + } + + model := dataSourceExtensionSchemaModel{ + ID: types.StringValue(found.ID), + Name: types.StringValue(found.Label), + Type: types.StringValue(found.Type), + } + resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) +} + +type dataSourceExtensionSchemaModel struct { + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` + Type types.String `tfsdk:"type"` +} diff --git a/pagerduty/data_source_pagerduty_extension_schema_test.go b/pagerdutyplugin/data_source_pagerduty_extension_schema_test.go similarity index 68% rename from pagerduty/data_source_pagerduty_extension_schema_test.go rename to pagerdutyplugin/data_source_pagerduty_extension_schema_test.go index c7880e087..eda6d5526 100644 --- a/pagerduty/data_source_pagerduty_extension_schema_test.go +++ b/pagerdutyplugin/data_source_pagerduty_extension_schema_test.go @@ -1,18 +1,20 @@ package pagerduty import ( + "context" "fmt" "testing" + "github.com/PagerDuty/go-pagerduty" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" ) func TestAccDataSourcePagerDutyExtensionSchema_Basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckPagerDutyScheduleDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(), + CheckDestroy: testAccCheckPagerDutyScheduleDestroy, Steps: []resource.TestStep{ { Config: testAccDataSourcePagerDutyExtensionSchemaConfig, @@ -26,7 +28,6 @@ func TestAccDataSourcePagerDutyExtensionSchema_Basic(t *testing.T) { func testAccDataSourcePagerDutyExtensionSchema(n string) resource.TestCheckFunc { return func(s *terraform.State) error { - r := s.RootModule().Resources[n] a := r.Primary.Attributes @@ -55,3 +56,19 @@ data "pagerduty_extension_schema" "foo" { name = "slack" } ` + +func testAccCheckPagerDutyScheduleDestroy(s *terraform.State) error { + for _, r := range s.RootModule().Resources { + if r.Type != "pagerduty_schedule" { + continue + } + + ctx := context.Background() + opts := pagerduty.GetScheduleOptions{} + if _, err := testAccProvider.client.GetScheduleWithContext(ctx, r.Primary.ID, opts); err == nil { + return fmt.Errorf("Schedule still exists") + } + + } + return nil +} diff --git a/pagerdutyplugin/import_pagerduty_extension_servicenow_test.go b/pagerdutyplugin/import_pagerduty_extension_servicenow_test.go index c0f0d05c5..901da0751 100644 --- a/pagerdutyplugin/import_pagerduty_extension_servicenow_test.go +++ b/pagerdutyplugin/import_pagerduty_extension_servicenow_test.go @@ -14,9 +14,9 @@ func TestAccPagerDutyExtensionServiceNow_import(t *testing.T) { url := "https://example.com/receive_a_pagerduty_webhook" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckPagerDutyExtensionServiceNowDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(), + CheckDestroy: testAccCheckPagerDutyExtensionServiceNowDestroy, Steps: []resource.TestStep{ { Config: testAccCheckPagerDutyExtensionServiceNowConfig(name, extension_name, url, "false", "any"), diff --git a/pagerdutyplugin/provider.go b/pagerdutyplugin/provider.go index 8797dce8c..59e3c2414 100644 --- a/pagerdutyplugin/provider.go +++ b/pagerdutyplugin/provider.go @@ -52,6 +52,7 @@ func (p *Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp func (p *Provider) DataSources(ctx context.Context) [](func() datasource.DataSource) { return [](func() datasource.DataSource){ func() datasource.DataSource { return &dataSourceBusinessService{} }, + func() datasource.DataSource { return &dataSourceExtensionSchema{} }, func() datasource.DataSource { return &dataSourceStandardsResourceScores{} }, func() datasource.DataSource { return &dataSourceStandardsResourcesScores{} }, func() datasource.DataSource { return &dataSourceStandards{} }, diff --git a/pagerdutyplugin/resource_pagerduty_extension_servicenow_test.go b/pagerdutyplugin/resource_pagerduty_extension_servicenow_test.go index ed1aea18b..03afbd9c7 100644 --- a/pagerdutyplugin/resource_pagerduty_extension_servicenow_test.go +++ b/pagerdutyplugin/resource_pagerduty_extension_servicenow_test.go @@ -1,15 +1,16 @@ package pagerduty import ( + "context" "fmt" "log" "strings" "testing" + "github.com/PagerDuty/go-pagerduty" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" - "github.com/heimweh/go-pagerduty/pagerduty" ) func init() { @@ -19,18 +20,10 @@ func init() { }) } -func testSweepExtensionServiceNow(region string) error { - config, err := sharedConfigForRegion(region) - if err != nil { - return err - } - - client, err := config.Client() - if err != nil { - return err - } +func testSweepExtensionServiceNow(_ string) error { + ctx := context.Background() - resp, _, err := client.Extensions.List(&pagerduty.ListExtensionsOptions{}) + resp, err := testAccProvider.client.ListExtensionsWithContext(ctx, pagerduty.ListExtensionOptions{}) if err != nil { return err } @@ -38,7 +31,7 @@ func testSweepExtensionServiceNow(region string) error { for _, extension := range resp.Extensions { if strings.HasPrefix(extension.Name, "test") || strings.HasPrefix(extension.Name, "tf-") { log.Printf("Destroying extension %s (%s)", extension.Name, extension.ID) - if _, err := client.Extensions.Delete(extension.ID); err != nil { + if err := testAccProvider.client.DeleteExtensionWithContext(ctx, extension.ID); err != nil { return err } } @@ -55,9 +48,9 @@ func TestAccPagerDutyExtensionServiceNow_Basic(t *testing.T) { url_updated := "https://example.com/webhook_foo" resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckPagerDutyExtensionServiceNowDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(), + CheckDestroy: testAccCheckPagerDutyExtensionServiceNowDestroy, Steps: []resource.TestStep{ { Config: testAccCheckPagerDutyExtensionServiceNowConfig(name, extension_name, url, "false", "any"), @@ -116,13 +109,14 @@ func TestAccPagerDutyExtensionServiceNow_Basic(t *testing.T) { } func testAccCheckPagerDutyExtensionServiceNowDestroy(s *terraform.State) error { - client, _ := testAccProvider.Meta().(*Config).Client() + ctx := context.Background() + for _, r := range s.RootModule().Resources { if r.Type != "pagerduty_extension_servicenow" { continue } - if _, _, err := client.Extensions.Get(r.Primary.ID); err == nil { + if _, err := testAccProvider.client.GetExtensionWithContext(ctx, r.Primary.ID); err == nil { return fmt.Errorf("Extension still exists") } @@ -131,6 +125,8 @@ func testAccCheckPagerDutyExtensionServiceNowDestroy(s *terraform.State) error { } func testAccCheckPagerDutyExtensionServiceNowExists(n string) resource.TestCheckFunc { + ctx := context.Background() + return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -141,9 +137,7 @@ func testAccCheckPagerDutyExtensionServiceNowExists(n string) resource.TestCheck return fmt.Errorf("No extension ID is set") } - client, _ := testAccProvider.Meta().(*Config).Client() - - found, _, err := client.Extensions.Get(rs.Primary.ID) + found, err := testAccProvider.client.GetExtensionWithContext(ctx, rs.Primary.ID) if err != nil { return err }