-
Notifications
You must be signed in to change notification settings - Fork 160
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
1 parent
8909002
commit ed5979e
Showing
4 changed files
with
98 additions
and
152 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,77 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validators | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type attributeValueConflictValidator struct { | ||
attributeName string | ||
conflictingValues []string | ||
} | ||
|
||
func (v attributeValueConflictValidator) Description(ctx context.Context) string { | ||
return fmt.Sprintf("Ensures the attribute is not set if %s is one of %v", v.attributeName, v.conflictingValues) | ||
} | ||
|
||
func (v attributeValueConflictValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
func (v attributeValueConflictValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) { | ||
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
var attributeValue types.String | ||
diags := req.Config.GetAttribute(ctx, path.Root(v.attributeName), &attributeValue) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
for _, conflictingValue := range v.conflictingValues { | ||
if attributeValue.ValueString() == conflictingValue { | ||
resp.Diagnostics.AddError( | ||
"Invalid Attribute Value", | ||
fmt.Sprintf("The attribute '%s' cannot be set when '%s' is '%s'", req.Path, v.attributeName, conflictingValue), | ||
) | ||
|
||
return | ||
} | ||
} | ||
} | ||
|
||
func (v attributeValueConflictValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { | ||
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
var attributeValue types.String | ||
diags := req.Config.GetAttribute(ctx, path.Root(v.attributeName), &attributeValue) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
for _, conflictingValue := range v.conflictingValues { | ||
if attributeValue.ValueString() == conflictingValue { | ||
resp.Diagnostics.AddError( | ||
"Invalid Attribute Value", | ||
fmt.Sprintf("The attribute '%s' cannot be set when '%s' is '%s'", req.Path, v.attributeName, conflictingValue), | ||
) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func AttributeValueConflictValidator(attributeName string, conflictingValues []string) attributeValueConflictValidator { | ||
return attributeValueConflictValidator{attributeName: attributeName, conflictingValues: conflictingValues} | ||
} |
54 changes: 0 additions & 54 deletions
54
internal/provider/validators/attribute_value_conflict_set.go
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
internal/provider/validators/attribute_value_conflict_string.go
This file was deleted.
Oops, something went wrong.