Skip to content
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

Bigtable: Validate gc policy top level #6427

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions mmv1/third_party/terraform/resources/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
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