Skip to content

Commit 96f80a1

Browse files
committed
KUBE-997: refactor
1 parent d3fe826 commit 96f80a1

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

castai/data_source_gke.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
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

1413
const (
@@ -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

5046
func 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+
}

castai/data_source_gke_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ import (
1313
func Test_dataSourceGKEPoliciesRead(t *testing.T) {
1414
tests := []struct {
1515
name string
16-
features []interface{}
16+
features map[string]bool
1717
expected int
1818
hasError bool
1919
}{
2020
{
2121
name: "all features",
22-
features: []interface{}{
23-
loadBalancersTargetBackendPoolsFeature,
24-
loadBalancersUnmanagedInstanceGroupsFeature,
22+
features: map[string]bool{
23+
loadBalancersTargetBackendPoolsFeature: true,
24+
loadBalancersUnmanagedInstanceGroupsFeature: true,
2525
},
2626
expected: 42, // -1 for the duplicate policy
2727
hasError: false,
2828
},
2929
{
3030
name: "loadBalancersTargetBackendPoolsFeature",
31-
features: []interface{}{
32-
loadBalancersTargetBackendPoolsFeature,
31+
features: map[string]bool{
32+
loadBalancersTargetBackendPoolsFeature: true,
3333
},
3434
expected: 41, // -1 for the duplicate policy
3535
hasError: false,

docs/data-sources/gke_user_policies.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)