-
Notifications
You must be signed in to change notification settings - Fork 56
Use DynamicAttribute for query attribute #367
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
Open
simonhammes
wants to merge
9
commits into
hashicorp:main
Choose a base branch
from
simonhammes:dynamic-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
25d2d45
Use DynamicAttribute for query attribute
simonhammes 1a171ed
Clean up
simonhammes e5badae
Ignore null values to keep BC
simonhammes 0f768fe
Handle missing/null query
simonhammes 9187f35
Add tests
simonhammes 0fbb7be
Simplify marshalTuple()
simonhammes f425b94
Handle numbers
simonhammes 414931b
Remove indirection
simonhammes dea4070
Handle collection types
simonhammes 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,13 @@ import ( | |
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "math/big" | ||
| "os/exec" | ||
| "runtime" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" | ||
| "github.com/hashicorp/terraform-plugin-framework/attr" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||
|
|
@@ -72,11 +74,11 @@ func (n *externalDataSource) Schema(ctx context.Context, req datasource.SchemaRe | |
| Optional: true, | ||
| }, | ||
|
|
||
| "query": schema.MapAttribute{ | ||
| "query": schema.DynamicAttribute{ | ||
| // TODO: Update description | ||
| Description: "A map of string values to pass to the external program as the query " + | ||
| "arguments. If not supplied, the program will receive an empty object as its input.", | ||
| ElementType: types.StringType, | ||
| Optional: true, | ||
| Optional: true, | ||
| }, | ||
|
|
||
| "result": schema.MapAttribute{ | ||
|
|
@@ -128,31 +130,8 @@ func (n *externalDataSource) Read(ctx context.Context, req datasource.ReadReques | |
| return | ||
| } | ||
|
|
||
| var query map[string]types.String | ||
|
|
||
| diags = config.Query.ElementsAs(ctx, &query, false) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| filteredQuery := make(map[string]string) | ||
| for key, value := range query { | ||
| // Preserve v2.2.3 and earlier behavior of filtering whole map elements | ||
| // with null values. | ||
| // Reference: https://github.com/hashicorp/terraform-provider-external/issues/208 | ||
| // | ||
| // The external program protocol could be updated to support null values | ||
| // as a breaking change by marshaling map[string]*string to JSON. | ||
| // Reference: https://github.com/hashicorp/terraform-provider-external/issues/209 | ||
| if value.IsNull() { | ||
| continue | ||
| } | ||
|
|
||
| filteredQuery[key] = value.ValueString() | ||
| } | ||
| queryJson, err := marshalAttrValueToJSON(config.Query.UnderlyingValue()) | ||
|
|
||
| queryJson, err := json.Marshal(filteredQuery) | ||
| if err != nil { | ||
| resp.Diagnostics.AddAttributeError( | ||
| path.Root("query"), | ||
|
|
@@ -282,10 +261,92 @@ If the error is unclear, the output can be viewed by enabling Terraform's loggin | |
| resp.Diagnostics.Append(diags...) | ||
| } | ||
|
|
||
| func marshalNestedAttrValue(val attr.Value) (interface{}, error) { | ||
| switch v := val.(type) { | ||
| case types.String: | ||
| return v.ValueString(), nil | ||
| case types.Number: | ||
| return marshalNumber(*v.ValueBigFloat()), nil | ||
| case types.Bool: | ||
| return v.ValueBool(), nil | ||
| case types.Tuple: | ||
| return marshalTuple(v.Elements()) | ||
| case types.List: | ||
| return marshalTuple(v.Elements()) | ||
| case types.Set: | ||
| return marshalTuple(v.Elements()) | ||
| case types.Object: | ||
| return marshalObject(v.Attributes()) | ||
| case types.Map: | ||
| return marshalObject(v.Elements()) | ||
| default: | ||
| return nil, fmt.Errorf("unsupported type: %T", v) | ||
| } | ||
| } | ||
|
|
||
| func marshalNumber(number big.Float) interface{} { | ||
| if number.IsInt() { | ||
| intValue, _ := number.Int(nil) | ||
| return intValue | ||
| } | ||
|
|
||
| floatValue, _ := number.Float64() | ||
| return floatValue | ||
| } | ||
|
|
||
| func marshalTuple(tuple []attr.Value) ([]interface{}, error) { | ||
| result := make([]interface{}, len(tuple)) | ||
| for i, v := range tuple { | ||
| marshaledVal, err := marshalNestedAttrValue(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result[i] = marshaledVal | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func marshalObject(m map[string]attr.Value) (map[string]interface{}, error) { | ||
| result := make(map[string]interface{}) | ||
| for k, v := range m { | ||
| // Preserve v2.2.3 and earlier behavior of filtering whole map elements | ||
| // with null values. | ||
| // Reference: https://github.com/hashicorp/terraform-provider-external/issues/208 | ||
| // | ||
| // The external program protocol could be updated to support null values | ||
| // as a breaking change by marshaling map[string]*string to JSON. | ||
| // Reference: https://github.com/hashicorp/terraform-provider-external/issues/209 | ||
| if v.IsNull() { | ||
| continue | ||
| } | ||
|
|
||
| marshaledVal, err := marshalNestedAttrValue(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result[k] = marshaledVal | ||
|
|
||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func marshalAttrValueToJSON(val attr.Value) ([]byte, error) { | ||
| if val == nil || val.IsNull() { | ||
| emptyMap := make(map[string]interface{}) | ||
| return json.Marshal(emptyMap) | ||
| } | ||
|
|
||
| marshaledVal, err := marshalNestedAttrValue(val) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return json.Marshal(marshaledVal) | ||
| } | ||
|
|
||
| type externalDataSourceModelV0 struct { | ||
| Program types.List `tfsdk:"program"` | ||
| WorkingDir types.String `tfsdk:"working_dir"` | ||
| Query types.Map `tfsdk:"query"` | ||
| Result types.Map `tfsdk:"result"` | ||
| ID types.String `tfsdk:"id"` | ||
| Program types.List `tfsdk:"program"` | ||
| WorkingDir types.String `tfsdk:"working_dir"` | ||
| Query types.Dynamic `tfsdk:"query"` | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a breaking change? I tried to look up how Terraform handles schema changes, but the schema definition for data sources does not include the |
||
| Result types.Map `tfsdk:"result"` | ||
| ID types.String `tfsdk:"id"` | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some built-in way to serialize an
attr.Valueinto JSON?