forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
57 lines (49 loc) · 1.39 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package digitalocean
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
type commonFilter struct {
key string
values []string
}
func filterSchema(allowedKeys []string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(allowedKeys, false),
},
"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
Optional: true,
Description: "One or more key/value pairs to filter droplet size results.",
}
}
func expandFilters(rawFilters []interface{}) []commonFilter {
expandedFilters := make([]commonFilter, len(rawFilters))
for i, rawFilter := range rawFilters {
f := rawFilter.(map[string]interface{})
expandedFilter := commonFilter{
key: f["key"].(string),
values: expandFilterValues(f["values"].([]interface{})),
}
expandedFilters[i] = expandedFilter
}
return expandedFilters
}
func expandFilterValues(rawFilterValues []interface{}) []string {
expandedFilterValues := make([]string, len(rawFilterValues))
for i, v := range rawFilterValues {
expandedFilterValues[i] = v.(string)
}
return expandedFilterValues
}