88
99 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1211)
1312
1413const (
@@ -27,16 +26,13 @@ func dataSourceGKEPolicies() *schema.Resource {
2726 Schema : map [string ]* schema.Schema {
2827 fieldGKEPoliciesFeatures : {
2928 Description : "Provide a list of GCP feature names to include the necessary policies for them to work." ,
30- Type : schema .TypeList ,
29+ Type : schema .TypeMap ,
3130 ForceNew : true ,
3231 Optional : true ,
3332 Elem : & schema.Schema {
34- Type : schema .TypeString ,
35- ValidateFunc : validation .StringInSlice ([]string {
36- loadBalancersTargetBackendPoolsFeature ,
37- loadBalancersUnmanagedInstanceGroupsFeature ,
38- }, false ),
33+ Type : schema .TypeBool ,
3934 },
35+ ValidateFunc : validateFeatureKeys ,
4036 },
4137 fieldGKEPoliciesPolicy : {
4238 Type : schema .TypeList ,
@@ -49,7 +45,7 @@ func dataSourceGKEPolicies() *schema.Resource {
4945
5046func dataSourceGKEPoliciesRead (_ context.Context , data * schema.ResourceData , _ interface {}) diag.Diagnostics {
5147 // add policies per specified features
52- features , ok := data .Get (fieldGKEPoliciesFeatures ).([ ]interface {})
48+ featuresMap , ok := data .Get (fieldGKEPoliciesFeatures ).(map [ string ]interface {})
5349 if ! ok {
5450 return diag .FromErr (fmt .Errorf ("failed to retrieve features" ))
5551 }
@@ -58,7 +54,11 @@ func dataSourceGKEPoliciesRead(_ context.Context, data *schema.ResourceData, _ i
5854 policySet := make (map [string ]struct {})
5955
6056 // Process each feature
61- for _ , feature := range features {
57+ for feature , enabled := range featuresMap {
58+ if ! enabled .(bool ) {
59+ continue
60+ }
61+
6262 var err error
6363 var policies []string
6464
@@ -107,3 +107,17 @@ func appendArrayToMap(arr []string, m map[string]struct{}) map[string]struct{} {
107107 }
108108 return m
109109}
110+
111+ func validateFeatureKeys (val interface {}, key string ) (warns []string , errs []error ) {
112+ allowedFeatures := map [string ]struct {}{
113+ loadBalancersTargetBackendPoolsFeature : {},
114+ loadBalancersUnmanagedInstanceGroupsFeature : {},
115+ }
116+
117+ for k := range val .(map [string ]interface {}) {
118+ if _ , ok := allowedFeatures [k ]; ! ok {
119+ errs = append (errs , fmt .Errorf ("%q contains an invalid feature key: %s" , key , k ))
120+ }
121+ }
122+ return
123+ }
0 commit comments