Skip to content

Commit

Permalink
Add validation to require support_hours when incident_urgency_rule is…
Browse files Browse the repository at this point in the history
… "use_support_hours"
  • Loading branch information
cjgajard committed Apr 4, 2024
1 parent aeaea4b commit 3082ba4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pagerdutyplugin/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

"github.com/PagerDuty/go-pagerduty"
"github.com/PagerDuty/terraform-provider-pagerduty/util"
"github.com/PagerDuty/terraform-provider-pagerduty/util/validate"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
Expand All @@ -22,22 +24,31 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/terraform-providers/terraform-provider-pagerduty/util"
)

type resourceService struct {
client *pagerduty.Client
}

var (
_ resource.ResourceWithConfigure = (*resourceService)(nil)
_ resource.ResourceWithImportState = (*resourceService)(nil)
_ resource.ResourceWithConfigure = (*resourceService)(nil)
_ resource.ResourceWithConfigValidators = (*resourceService)(nil)
_ resource.ResourceWithImportState = (*resourceService)(nil)
)

func (r *resourceService) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
resp.Diagnostics.Append(ConfigurePagerdutyClient(&r.client, req.ProviderData)...)
}

func (r *resourceService) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
return []resource.ConfigValidator{
validate.RequireAIfBEqual(
path.Root("support_hours"),
path.Root("incident_urgency_rule").AtListIndex(0).AtName("type"),
types.StringValue("use_support_hours")),
}
}

func (r *resourceService) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = "pagerduty_service"
}
Expand Down
57 changes: 57 additions & 0 deletions util/validate/required_support_hours.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package validate

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

// RequireAIfBEqual checks path `a` is not null when path `b` is equal to `expected`.
func RequireAIfBEqual(a, b path.Path, expected attr.Value) resource.ConfigValidator {
return &requireIfEqual{
dst: a,
src: b,
expected: expected,
}
}

type requireIfEqual struct {
dst path.Path
src path.Path
expected attr.Value
}

func (v *requireIfEqual) Description(ctx context.Context) string { return "" }
func (v *requireIfEqual) MarkdownDescription(ctx context.Context) string { return "" }

func (v *requireIfEqual) ValidateResource(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
var src attr.Value
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, v.src, &src)...)
if resp.Diagnostics.HasError() {
return
}

if src.IsNull() || src.IsUnknown() {
return
}

if src.Equal(v.expected) {
var dst attr.Value
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, v.dst, &dst)...)
if resp.Diagnostics.HasError() {
return
}

if dst.IsNull() || dst.IsUnknown() {
resp.Diagnostics.AddAttributeError(
v.dst,
fmt.Sprintf("Required %s", v.dst),
fmt.Sprintf("When the value of %s equals %s, field %s must have an explicit value", v.src, v.expected, v.dst),
)
return
}
}
}

0 comments on commit 3082ba4

Please sign in to comment.