-
Notifications
You must be signed in to change notification settings - Fork 9
Fix Data Source Tag Filtering #143
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
base: master
Are you sure you want to change the base?
Changes from all commits
47df007
b609e9a
45e960e
ba35e44
50548a1
18ad909
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ package cloudscale | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "reflect" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -29,14 +30,35 @@ func dataSourceResourceRead( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var foundItems []map[string]any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Filter resources: each set attribute must match (maps use subset semantics). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, m := range resources { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match := true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for key := range sourceSchema { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if attr, ok := d.GetOk(key); ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if m[key] != attr { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for key, schemaEntry := range sourceSchema { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| attr, ok := d.GetOk(key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue // not a filter criterion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if schemaEntry.Type == schema.TypeMap { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Tags: all filter key-value pairs must be present in the resource (subset, not exact). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filterMap := attr.(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resourceMap, _ := m[key].(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for fk, fv := range filterMap { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if resourceMap[fk] != fv { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break // one tag mismatch is sufficient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if schemaEntry.Type == schema.TypeList || schemaEntry.Type == schema.TypeSet { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Gather functions return []string from the SDK struct; d.GetOk returns []interface{}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Normalise before comparing so reflect.DeepEqual sees the same dynamic type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !reflect.DeepEqual(toInterfaceSlice(m[key]), attr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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 equal match right or subset match here too? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if !reflect.DeepEqual(m[key], attr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !match { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break // skip remaining attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if match { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -57,6 +79,21 @@ func dataSourceResourceRead( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func toInterfaceSlice(v any) []interface{} { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch s := v.(type) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case []string: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result := make([]interface{}, len(s)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i, str := range s { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result[i] = str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case []interface{}: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+82
to
+96
Contributor
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. nit: we probably should use either
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func getFetchFunc[TResource any]( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| listFunc func(d *schema.ResourceData, meta any) ([]TResource, error), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gatherFunc func(resource *TResource) ResourceDataRaw, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
TypeSet
TypeSet returns a
*schema.Setwhich isn't handled bytoInterfaceSlice.