From 31cbe240d25967ea63913a0beb65b2a994db6760 Mon Sep 17 00:00:00 2001 From: Carlos Gajardo Date: Fri, 23 Feb 2024 11:18:15 -0300 Subject: [PATCH] Migrate data source pagerduty_business_service to terraform plugin framework --- .../data_source_pagerduty_business_service.go | 1 + pagerduty/provider.go | 1 + .../data_source_pagerduty_business_service.go | 94 +++++++++++++++++++ ..._source_pagerduty_business_service_test.go | 5 +- pagerdutyplugin/provider.go | 3 +- 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 pagerdutyplugin/data_source_pagerduty_business_service.go rename {pagerduty => pagerdutyplugin}/data_source_pagerduty_business_service_test.go (92%) diff --git a/pagerduty/data_source_pagerduty_business_service.go b/pagerduty/data_source_pagerduty_business_service.go index 953184301..a8e0b9718 100644 --- a/pagerduty/data_source_pagerduty_business_service.go +++ b/pagerduty/data_source_pagerduty_business_service.go @@ -11,6 +11,7 @@ import ( "github.com/heimweh/go-pagerduty/pagerduty" ) +// Deprecated: Migrated to pagerdutyplugin.dataSourceBusinessService. Kept for testing purposes. func dataSourcePagerDutyBusinessService() *schema.Resource { return &schema.Resource{ Read: dataSourcePagerDutyBusinessServiceRead, diff --git a/pagerduty/provider.go b/pagerduty/provider.go index ee30b93a7..914f3e800 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -151,6 +151,7 @@ func Provider(isMux bool) *schema.Provider { } if isMux { + delete(p.DataSourcesMap, "pagerduty_business_service") delete(p.ResourcesMap, "pagerduty_business_service") } diff --git a/pagerdutyplugin/data_source_pagerduty_business_service.go b/pagerdutyplugin/data_source_pagerduty_business_service.go new file mode 100644 index 000000000..81958235f --- /dev/null +++ b/pagerdutyplugin/data_source_pagerduty_business_service.go @@ -0,0 +1,94 @@ +package pagerduty + +import ( + "context" + "fmt" + "log" + "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 dataSourceBusinessService struct{ client *pagerduty.Client } + +var _ datasource.DataSourceWithConfigure = (*dataSourceBusinessService)(nil) + +func (*dataSourceBusinessService) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = "pagerduty_business_service" +} + +func (*dataSourceBusinessService) 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 *dataSourceBusinessService) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + resp.Diagnostics.Append(ConfigurePagerdutyClient(&d.client, req.ProviderData)...) +} + +func (d *dataSourceBusinessService) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + log.Println("[INFO] Reading PagerDuty business service") + + var searchName types.String + resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("name"), &searchName)...) + if resp.Diagnostics.HasError() { + return + } + + var found *pagerduty.BusinessService + err := retry.RetryContext(ctx, 5*time.Minute, func() *retry.RetryError { + list, err := d.client.ListBusinessServices(pagerduty.ListBusinessServiceOptions{}) + if err != nil { + if util.IsBadRequestError(err) { + return retry.NonRetryableError(err) + } + return retry.RetryableError(err) + } + + for _, bs := range list.BusinessServices { + if bs.Name == searchName.ValueString() { + found = bs + break + } + } + return nil + }) + if err != nil { + resp.Diagnostics.AddError( + fmt.Sprintf("Error reading Business Service %s", searchName), + err.Error(), + ) + } + + if found == nil { + resp.Diagnostics.AddError( + fmt.Sprintf("Unable to locate any business service with the name: %s", searchName), + "", + ) + return + } + + model := dataSourceBusinessServiceModel{ + ID: types.StringValue(found.ID), + Name: types.StringValue(found.Name), + Type: types.StringValue(found.Type), + } + resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) +} + +type dataSourceBusinessServiceModel struct { + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` + Type types.String `tfsdk:"type"` +} diff --git a/pagerduty/data_source_pagerduty_business_service_test.go b/pagerdutyplugin/data_source_pagerduty_business_service_test.go similarity index 92% rename from pagerduty/data_source_pagerduty_business_service_test.go rename to pagerdutyplugin/data_source_pagerduty_business_service_test.go index 746c1d4c2..312a57626 100644 --- a/pagerduty/data_source_pagerduty_business_service_test.go +++ b/pagerdutyplugin/data_source_pagerduty_business_service_test.go @@ -13,8 +13,8 @@ func TestAccDataSourcePagerDutyBusinessService_Basic(t *testing.T) { name := fmt.Sprintf("tf-%s", acctest.RandString(5)) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, + PreCheck: func() { testAccPreCheck(t) }, + ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(), Steps: []resource.TestStep{ { Config: testAccDataSourcePagerDutyBusinessServiceConfig(name), @@ -28,7 +28,6 @@ func TestAccDataSourcePagerDutyBusinessService_Basic(t *testing.T) { func testAccDataSourcePagerDutyBusinessService(src, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - srcR := s.RootModule().Resources[src] srcA := srcR.Primary.Attributes diff --git a/pagerdutyplugin/provider.go b/pagerdutyplugin/provider.go index 3fa80b6d4..f7fef54e2 100644 --- a/pagerdutyplugin/provider.go +++ b/pagerdutyplugin/provider.go @@ -49,9 +49,10 @@ 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 &dataSourceStandards{} }, + func() datasource.DataSource { return &dataSourceBusinessService{} }, func() datasource.DataSource { return &dataSourceStandardsResourceScores{} }, func() datasource.DataSource { return &dataSourceStandardsResourcesScores{} }, + func() datasource.DataSource { return &dataSourceStandards{} }, } }