Skip to content

Commit

Permalink
Migrate data source priority
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed Jul 4, 2024
1 parent 9103f30 commit 78c3f01
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
1 change: 1 addition & 0 deletions pagerduty/data_source_pagerduty_priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/heimweh/go-pagerduty/pagerduty"
)

// Deprecated: Migrated to pagerdutyplugin.dataSourcePriority. Kept for testing purposes.
func dataSourcePagerDutyPriority() *schema.Resource {
return &schema.Resource{
Read: dataSourcePagerDutyPriorityRead,
Expand Down
1 change: 1 addition & 0 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func Provider(isMux bool) *schema.Provider {
if isMux {
delete(p.DataSourcesMap, "pagerduty_business_service")
delete(p.DataSourcesMap, "pagerduty_licenses")
delete(p.DataSourcesMap, "pagerduty_priority")
delete(p.DataSourcesMap, "pagerduty_service")
delete(p.DataSourcesMap, "pagerduty_service_integration")

Expand Down
97 changes: 97 additions & 0 deletions pagerdutyplugin/data_source_pagerduty_priority.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package pagerduty

import (
"context"
"fmt"
"log"
"strings"

"github.com/PagerDuty/go-pagerduty"
"github.com/PagerDuty/terraform-provider-pagerduty/util/apiutil"
"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"
)

type dataSourcePriority struct{ client *pagerduty.Client }

var _ datasource.DataSourceWithConfigure = (*dataSourcePriority)(nil)

func (*dataSourcePriority) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "pagerduty_priority"
}

func (*dataSourcePriority) 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,
Description: "The name of the priority to find in the PagerDuty API",
},
"description": schema.StringAttribute{Computed: true},
},
}
}

func (d *dataSourcePriority) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
resp.Diagnostics.Append(ConfigurePagerdutyClient(&d.client, req.ProviderData)...)
}

func (d *dataSourcePriority) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
log.Println("[INFO] Reading PagerDuty priority")

var searchName types.String
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("name"), &searchName)...)
if resp.Diagnostics.HasError() {
return
}

var found *pagerduty.Priority
err := apiutil.All(ctx, apiutil.AllFunc(func(offset int) (bool, error) {
list, err := d.client.ListPrioritiesWithContext(ctx, pagerduty.ListPrioritiesOptions{
Limit: apiutil.Limit,
Offset: uint(offset),
})
if err != nil {
return false, err
}

for _, priority := range list.Priorities {
if strings.EqualFold(priority.Name, searchName.ValueString()) {
found = &priority
return false, nil
}
}

return list.More, nil
}))
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error reading PagerDuty priority %s", searchName),
err.Error(),
)
}

if found == nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to locate any priority with the name: %s", searchName),
"",
)
return
}

model := dataSourcePriorityModel{
ID: types.StringValue(found.ID),
Name: types.StringValue(found.Name),
Description: types.StringValue(found.Description),
}
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}

type dataSourcePriorityModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func TestAccDataSourcePagerDutyPriority_Basic(t *testing.T) {
dataSourceName := "data.pagerduty_priority.p1"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyPriorityConfig,
Expand All @@ -22,11 +22,12 @@ func TestAccDataSourcePagerDutyPriority_Basic(t *testing.T) {
},
})
}

func TestAccDataSourcePagerDutyPriority_P2(t *testing.T) {
dataSourceName := "data.pagerduty_priority.p2"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourcePagerDutyP2Config,
Expand Down
1 change: 1 addition & 0 deletions pagerdutyplugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (p *Provider) DataSources(_ context.Context) [](func() datasource.DataSourc
func() datasource.DataSource { return &dataSourceIntegration{} },
func() datasource.DataSource { return &dataSourceLicense{} },
func() datasource.DataSource { return &dataSourceLicenses{} },
func() datasource.DataSource { return &dataSourcePriority{} },
func() datasource.DataSource { return &dataSourceService{} },
func() datasource.DataSource { return &dataSourceStandardsResourceScores{} },
func() datasource.DataSource { return &dataSourceStandardsResourcesScores{} },
Expand Down

0 comments on commit 78c3f01

Please sign in to comment.