-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation to require support_hours when incident_urgency_rule is…
… "use_support_hours"
- Loading branch information
Showing
2 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |