-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
17 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
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
106 changes: 106 additions & 0 deletions
106
internal/provider/netlify_validators/environment_variable_context_parameter_validator.go
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,106 @@ | ||
package netlify_validators | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" | ||
) | ||
|
||
var ( | ||
_ validator.String = EnvironmentVariableContextParameterValidator{} | ||
) | ||
|
||
type EnvironmentVariableContextParameterValidator struct { | ||
ContextPathExpression path.Expression | ||
} | ||
|
||
type EnvironmentVariableContextParameterValidatorRequest struct { | ||
Config tfsdk.Config | ||
ConfigValue attr.Value | ||
Path path.Path | ||
PathExpression path.Expression | ||
} | ||
|
||
type EnvironmentVariableContextParameterValidatorResponse struct { | ||
Diagnostics diag.Diagnostics | ||
} | ||
|
||
func (av EnvironmentVariableContextParameterValidator) Description(ctx context.Context) string { | ||
return av.MarkdownDescription(ctx) | ||
} | ||
|
||
func (av EnvironmentVariableContextParameterValidator) MarkdownDescription(_ context.Context) string { | ||
return fmt.Sprintf("Ensure that an attribute is a non-empty string iff %q is set to \"branch\"", av.ContextPathExpression) | ||
} | ||
|
||
func (av EnvironmentVariableContextParameterValidator) Validate(ctx context.Context, req EnvironmentVariableContextParameterValidatorRequest, res *EnvironmentVariableContextParameterValidatorResponse) { | ||
// Delay validation until all involved attributes have a known value | ||
if req.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
isNonEmpty := !req.ConfigValue.IsNull() && !req.ConfigValue.Equal(types.StringValue("")) | ||
|
||
matchedPaths, diags := req.Config.PathMatches(ctx, req.PathExpression.Merge(av.ContextPathExpression)) | ||
|
||
res.Diagnostics.Append(diags...) | ||
if diags.HasError() { | ||
return | ||
} | ||
|
||
for _, mp := range matchedPaths { | ||
var mpVal attr.Value | ||
diags := req.Config.GetAttribute(ctx, mp, &mpVal) | ||
res.Diagnostics.Append(diags...) | ||
|
||
// Collect all errors | ||
if diags.HasError() { | ||
continue | ||
} | ||
|
||
// Delay validation until all involved attributes have a known value | ||
if mpVal.IsUnknown() { | ||
return | ||
} | ||
|
||
var listValue basetypes.ListValue | ||
listValue, diags = types.ListValue(types.StringType, []attr.Value{types.StringValue("branch")}) | ||
res.Diagnostics.Append(diags...) | ||
|
||
// Collect all errors | ||
if diags.HasError() { | ||
continue | ||
} | ||
|
||
isBranch := !mpVal.IsNull() && mpVal.Equal(listValue) | ||
|
||
if isNonEmpty != isBranch { | ||
res.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic( | ||
req.Path, | ||
fmt.Sprintf("Attribute %q must be a non-empty string iff %q is specified", req.Path, mp), | ||
)) | ||
} | ||
} | ||
} | ||
|
||
func (av EnvironmentVariableContextParameterValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { | ||
validateReq := EnvironmentVariableContextParameterValidatorRequest{ | ||
Config: req.Config, | ||
ConfigValue: req.ConfigValue, | ||
Path: req.Path, | ||
PathExpression: req.PathExpression, | ||
} | ||
validateResp := &EnvironmentVariableContextParameterValidatorResponse{} | ||
|
||
av.Validate(ctx, validateReq, validateResp) | ||
|
||
resp.Diagnostics.Append(validateResp.Diagnostics...) | ||
} |
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