Skip to content

Commit

Permalink
Bigtable: Validate gc policy top level (#6427) (#12351)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Aug 19, 2022
1 parent 6bf376e commit 56f2af7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changelog/6427.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigtable: passed `isTopeLevel` in getGCPolicyFromJSON() instead of hardcoding it to true.
```
28 changes: 14 additions & 14 deletions google/resource_bigtable_gc_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,11 @@ func generateBigtableGCPolicy(d *schema.ResourceData) (bigtable.GCPolicy, error)
}

if gok {
var j map[string]interface{}
if err := json.Unmarshal([]byte(gcRules.(string)), &j); err != nil {
var topLevelPolicy map[string]interface{}
if err := json.Unmarshal([]byte(gcRules.(string)), &topLevelPolicy); err != nil {
return nil, err
}
return getGCPolicyFromJSON(j)
return getGCPolicyFromJSON(topLevelPolicy /*isTopLevel=*/, true)
}

if aok {
Expand Down Expand Up @@ -351,16 +351,16 @@ func generateBigtableGCPolicy(d *schema.ResourceData) (bigtable.GCPolicy, error)
return policies[0], nil
}

func getGCPolicyFromJSON(topLevelPolicy map[string]interface{}) (bigtable.GCPolicy, error) {
func getGCPolicyFromJSON(inputPolicy map[string]interface{}, isTopLevel bool) (bigtable.GCPolicy, error) {
policy := []bigtable.GCPolicy{}

if err := validateNestedPolicy(topLevelPolicy, true); err != nil {
if err := validateNestedPolicy(inputPolicy, isTopLevel); err != nil {
return nil, err
}

for _, p := range topLevelPolicy["rules"].([]interface{}) {
for _, p := range inputPolicy["rules"].([]interface{}) {
childPolicy := p.(map[string]interface{})
if err := validateNestedPolicy(childPolicy, false); err != nil {
if err := validateNestedPolicy(childPolicy /*isTopLevel=*/, false); err != nil {
return nil, err
}

Expand All @@ -379,15 +379,15 @@ func getGCPolicyFromJSON(topLevelPolicy map[string]interface{}) (bigtable.GCPoli
}

if childPolicy["mode"] != nil {
n, err := getGCPolicyFromJSON(childPolicy)
n, err := getGCPolicyFromJSON(childPolicy /*isTopLevel=*/, false)
if err != nil {
return nil, err
}
policy = append(policy, n)
}
}

switch topLevelPolicy["mode"] {
switch inputPolicy["mode"] {
case strings.ToLower(GCPolicyModeUnion):
return bigtable.UnionPolicy(policy...), nil
case strings.ToLower(GCPolicyModeIntersection):
Expand All @@ -397,7 +397,7 @@ func getGCPolicyFromJSON(topLevelPolicy map[string]interface{}) (bigtable.GCPoli
}
}

func validateNestedPolicy(p map[string]interface{}, topLevel bool) error {
func validateNestedPolicy(p map[string]interface{}, isTopLevel bool) error {
if len(p) > 2 {
return fmt.Errorf("rules has more than 2 fields")
}
Expand All @@ -418,19 +418,19 @@ func validateNestedPolicy(p map[string]interface{}, topLevel bool) error {
return fmt.Errorf("`rules` need at least 2 GC rule when mode is specified")
}

if topLevel && !rulesOk {
if isTopLevel && !rulesOk {
return fmt.Errorf("invalid nested policy, need `rules`")
}

if topLevel && !modeOk && len(rules) != 1 {
if isTopLevel && !modeOk && len(rules) != 1 {
return fmt.Errorf("when `mode` is not specified, `rules` can only have 1 child rule")
}

if !topLevel && len(p) == 2 && (!modeOk || !rulesOk) {
if !isTopLevel && len(p) == 2 && (!modeOk || !rulesOk) {
return fmt.Errorf("need `mode` and `rules` for child nested policies")
}

if !topLevel && len(p) == 1 && !maxVersionOk && !maxAgeOk {
if !isTopLevel && len(p) == 1 && !maxVersionOk && !maxAgeOk {
return fmt.Errorf("need `max_version` or `max_age` for the rule")
}

Expand Down
6 changes: 3 additions & 3 deletions google/resource_bigtable_gc_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ var testUnitBigtableGCPolicyRulesTestCases = []testUnitBigtableGCPolicyJSONRules
func TestUnitBigtableGCPolicy_getGCPolicyFromJSON(t *testing.T) {
for _, tc := range testUnitBigtableGCPolicyRulesTestCases {
t.Run(tc.name, func(t *testing.T) {
var j map[string]interface{}
err := json.Unmarshal([]byte(tc.gcJSONString), &j)
var topLevelPolicy map[string]interface{}
err := json.Unmarshal([]byte(tc.gcJSONString), &topLevelPolicy)
if err != nil {
t.Fatalf("error unmarshalling JSON string: %v", err)
}
got, err := getGCPolicyFromJSON(j)
got, err := getGCPolicyFromJSON(topLevelPolicy /*isTopLevel=*/, true)
if tc.errorExpected && err == nil {
t.Fatal("expect error, got nil")
} else if !tc.errorExpected && err != nil {
Expand Down

0 comments on commit 56f2af7

Please sign in to comment.