-
Notifications
You must be signed in to change notification settings - Fork 1
chore: rework validation imported from the provider #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cfc2017
chore: bad default values should throw errors
Emyrk db24679
add more error context
Emyrk ed766d0
add more error context
Emyrk 4453f33
Merge remote-tracking branch 'origin/main' into stevenmasley/opts_val…
Emyrk 3fcc847
terraform provider dep update
Emyrk 1a0b055
accept empty default values
Emyrk 4f4463b
populate required field
Emyrk dd15cc7
make gen
Emyrk 5104c52
fixup default usage
Emyrk 3ba59d2
fix AsString reference
Emyrk ddcd0c6
Merge remote-tracking branch 'origin/main' into stevenmasley/opts_val…
Emyrk 0876a7d
fix AsString to account for other types
Emyrk b20a5bc
add skip until provider validation is merged and released
Emyrk 6cf3c10
remove unused code
Emyrk 154d86b
update provider
Emyrk 2d4f4de
Merge remote-tracking branch 'origin/main' into stevenmasley/opts_val…
Emyrk 753772a
Update testdata/emptydefault/skipe2e
aslilac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
terraform { | ||
required_providers { | ||
coder = { | ||
source = "coder/coder" | ||
version = "2.4.0-pre0" | ||
} | ||
} | ||
} | ||
|
||
data "coder_parameter" "word" { | ||
name = "word" | ||
description = "Select something" | ||
type = "string" | ||
order = 1 | ||
# No default selected | ||
|
||
option { | ||
name = "Bird" | ||
value = "bird" | ||
description = "An animal that can fly." | ||
} | ||
option { | ||
name = "Boat" | ||
value = "boat" | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
Skipping until https://github.com/coder/terraform-provider-coder/pull/381 is merged and released | ||
This file contains hidden or 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,90 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/aquasecurity/trivy/pkg/iac/terraform" | ||
hcty "github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
|
||
"github.com/coder/terraform-provider-coder/v2/provider" | ||
) | ||
|
||
func providerValidations(vals []*ParameterValidation) []provider.Validation { | ||
cpy := make([]provider.Validation, 0, len(vals)) | ||
for _, val := range vals { | ||
cpy = append(cpy, providerValidation(val)) | ||
} | ||
return cpy | ||
} | ||
|
||
func providerValidation(v *ParameterValidation) provider.Validation { | ||
return provider.Validation{ | ||
Min: int(orZero(v.Min)), | ||
MinDisabled: v.Min == nil, | ||
Max: int(orZero(v.Max)), | ||
MaxDisabled: v.Max == nil, | ||
Monotonic: orZero(v.Monotonic), | ||
Regex: orZero(v.Regex), | ||
Error: v.Error, | ||
} | ||
} | ||
|
||
func providerOptions(opts []*ParameterOption) []provider.Option { | ||
cpy := make([]provider.Option, 0, len(opts)) | ||
for _, opt := range opts { | ||
cpy = append(cpy, providerOption(opt)) | ||
} | ||
return cpy | ||
} | ||
|
||
func providerOption(opt *ParameterOption) provider.Option { | ||
return provider.Option{ | ||
Name: opt.Name, | ||
Description: opt.Description, | ||
Value: opt.Value.AsString(), | ||
Icon: opt.Icon, | ||
} | ||
} | ||
|
||
func hclDiagnostics(diagnostics diag.Diagnostics, source *terraform.Block) hcl.Diagnostics { | ||
cpy := make(hcl.Diagnostics, 0, len(diagnostics)) | ||
for _, d := range diagnostics { | ||
cpy = append(cpy, hclDiagnostic(d, source)) | ||
} | ||
return cpy | ||
} | ||
|
||
func hclDiagnostic(d diag.Diagnostic, source *terraform.Block) *hcl.Diagnostic { | ||
sev := hcl.DiagInvalid | ||
switch d.Severity { | ||
case diag.Error: | ||
sev = hcl.DiagError | ||
case diag.Warning: | ||
sev = hcl.DiagWarning | ||
} | ||
|
||
// This is an imperfect way to finding the source code of the error. There is 2 | ||
// different `cty` types at place here, the hashicorp fork and the original. So a | ||
// more general solution is difficult. This is good enough for now to add more | ||
// context to an error. | ||
var subject *hcl.Range | ||
if len(d.AttributePath) == 1 && source != nil { | ||
if attr, ok := d.AttributePath[0].(hcty.GetAttrStep); ok { | ||
src := source.GetAttribute(attr.Name) | ||
if src != nil { | ||
subject = &(src.HCLAttribute().Range) | ||
} | ||
} | ||
} | ||
|
||
return &hcl.Diagnostic{ | ||
Severity: sev, | ||
Summary: d.Summary, | ||
Detail: d.Detail, | ||
Subject: subject, | ||
Context: nil, | ||
Expression: nil, | ||
EvalContext: nil, | ||
Extra: nil, | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.