Skip to content

Commit

Permalink
Require explicit resource_type in standards_resource(s)_scores
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed Feb 8, 2024
1 parent fb85073 commit 4348821
Show file tree
Hide file tree
Showing 34 changed files with 2,441 additions and 23 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/hashicorp/terraform-exec v0.20.0
github.com/hashicorp/terraform-json v0.20.0
github.com/hashicorp/terraform-plugin-framework v1.5.0
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.20.0
github.com/hashicorp/terraform-plugin-mux v0.13.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/hashicorp/terraform-json v0.20.0 h1:cJcvn4gIOTi0SD7pIy+xiofV1zFA3hza+
github.com/hashicorp/terraform-json v0.20.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
github.com/hashicorp/terraform-plugin-framework v1.5.0 h1:8kcvqJs/x6QyOFSdeAyEgsenVOUeC/IyKpi2ul4fjTg=
github.com/hashicorp/terraform-plugin-framework v1.5.0/go.mod h1:6waavirukIlFpVpthbGd2PUNYaFedB0RwW3MDzJ/rtc=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg=
github.com/hashicorp/terraform-plugin-go v0.20.0 h1:oqvoUlL+2EUbKNsJbIt3zqqZ7wi6lzn4ufkn/UA51xQ=
github.com/hashicorp/terraform-plugin-go v0.20.0/go.mod h1:Rr8LBdMlY53a3Z/HpP+ZU3/xCDqtKNCkeI9qOyT10QE=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
21 changes: 10 additions & 11 deletions pagerdutyplugin/data_source_pagerduty_standards_resource_scores.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"

"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand All @@ -24,8 +26,13 @@ func (d *dataSourceStandardsResourceScores) Metadata(ctx context.Context, req da
func (d *dataSourceStandardsResourceScores) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{Required: true},
"resource_type": schema.StringAttribute{Required: true},
"id": schema.StringAttribute{Required: true},
"resource_type": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf("technical_services"),
},
},
"score": schema.ObjectAttribute{
AttributeTypes: resourceScoresObjectType.AttrTypes,
Computed: true,
Expand All @@ -48,12 +55,8 @@ func (d *dataSourceStandardsResourceScores) Read(ctx context.Context, req dataso

id := data.ID.ValueString()
rt := data.ResourceType.ValueString()
rtUrl, ok := resourceStandardsToURLMapping[rt]
if !ok {
rtUrl = rt
}

scores, err := d.client.ListResourceStandardScores(ctx, id, rtUrl)
scores, err := d.client.ListResourceStandardScores(ctx, id, rt)
if err != nil {
resp.Diagnostics.Append(diag.NewErrorDiagnostic(
"Error calling ListResourceStandardScores",
Expand Down Expand Up @@ -124,7 +127,3 @@ func resourceStandardsToModel(standards []pagerduty.ResourceStandard) (types.Lis
diags.Append(di...)
return modelStandards, diags
}

var resourceStandardsToURLMapping = map[string]string{
"technical_service": "technical_services",
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ resource "pagerduty_service" "example" {
}
}
data "pagerduty_standards" "all" {}
data "pagerduty_standards_resource_scores" "%s" {
resource_type = data.pagerduty_standards.all.standards.0.resource_type
resource_type = "technical_services"
id = pagerduty_service.example.id
}`, name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"

"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand All @@ -28,7 +30,12 @@ func (d *dataSourceStandardsResourcesScores) Schema(ctx context.Context, req dat
ElementType: types.StringType,
Required: true,
},
"resource_type": schema.StringAttribute{Required: true},
"resource_type": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf("technical_services"),
},
},
"resources": schema.ListAttribute{
ElementType: resourceStandardScoreObjectType,
Computed: true,
Expand All @@ -46,15 +53,11 @@ func (d *dataSourceStandardsResourcesScores) Read(ctx context.Context, req datas
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

rt := data.ResourceType.ValueString()
rtUrl, ok := resourceStandardsToURLMapping[rt]
if !ok {
rtUrl = rt
}
ids := make([]string, 0)
resp.Diagnostics.Append(data.IDs.ElementsAs(ctx, &ids, true)...)

opt := pagerduty.ListMultiResourcesStandardScoresOptions{IDs: ids}
scores, err := d.client.ListMultiResourcesStandardScores(ctx, rtUrl, opt)
scores, err := d.client.ListMultiResourcesStandardScores(ctx, rt, opt)
if err != nil {
resp.Diagnostics.Append(diag.NewErrorDiagnostic(
"Error calling ListResourceStandardScores",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ resource "pagerduty_service" "example" {
}
}
data "pagerduty_standards" "all" {}
data "pagerduty_standards_resources_scores" "%s" {
resource_type = data.pagerduty_standards.all.standards.0.resource_type
resource_type = "technical_services"
ids = [pagerduty_service.example.id]
}`, name)
}
Loading

0 comments on commit 4348821

Please sign in to comment.