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

Add gc_rules to BigTable's Garbage Collection policy TF resource. #5800

Merged
merged 17 commits into from
Apr 12, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package google

import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"

"cloud.google.com/go/bigtable"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

Expand Down Expand Up @@ -56,9 +59,10 @@ func resourceBigtableGCPolicyCustomizeDiff(_ context.Context, d *schema.Resource

func resourceBigtableGCPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceBigtableGCPolicyCreate,
Create: resourceBigtableGCPolicyUpsert,
Read: resourceBigtableGCPolicyRead,
Delete: resourceBigtableGCPolicyDestroy,
Update: resourceBigtableGCPolicyUpsert,
CustomizeDiff: resourceBigtableGCPolicyCustomizeDiff,

Schema: map[string]*schema.Schema{
Expand All @@ -84,12 +88,24 @@ func resourceBigtableGCPolicy() *schema.Resource {
Description: `The name of the column family.`,
},

"gc_rules": {
hoangpham95 marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
Description: `Serialized JSON string for garbage collection policy. Conflicts with "mode", "max_age" and "max_version".`,
ValidateFunc: validation.StringIsJSON,
ConflictsWith: []string{"mode", "max_age", "max_version"},
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},
"mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `If multiple policies are set, you should choose between UNION OR INTERSECTION.`,
ValidateFunc: validation.StringInSlice([]string{GCPolicyModeIntersection, GCPolicyModeUnion}, false),
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `If multiple policies are set, you should choose between UNION OR INTERSECTION.`,
ValidateFunc: validation.StringInSlice([]string{GCPolicyModeIntersection, GCPolicyModeUnion}, false),
ConflictsWith: []string{"gc_rules"},
},

"max_age": {
Expand Down Expand Up @@ -120,6 +136,7 @@ func resourceBigtableGCPolicy() *schema.Resource {
},
},
},
ConflictsWith: []string{"gc_rules"},
},

"max_version": {
Expand All @@ -137,6 +154,7 @@ func resourceBigtableGCPolicy() *schema.Resource {
},
},
},
ConflictsWith: []string{"gc_rules"},
},

"project": {
Expand All @@ -151,7 +169,7 @@ func resourceBigtableGCPolicy() *schema.Resource {
}
}

func resourceBigtableGCPolicyCreate(d *schema.ResourceData, meta interface{}) error {
func resourceBigtableGCPolicyUpsert(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
Expand Down Expand Up @@ -288,13 +306,22 @@ func generateBigtableGCPolicy(d *schema.ResourceData) (bigtable.GCPolicy, error)
mode := d.Get("mode").(string)
ma, aok := d.GetOk("max_age")
mv, vok := d.GetOk("max_version")
gcRules, gok := d.GetOk("gc_rules")

if !aok && !vok {
if !aok && !vok && !gok {
return bigtable.NoGcPolicy(), nil
}

if mode == "" && aok && vok {
return nil, fmt.Errorf("If multiple policies are set, mode can't be empty")
return nil, fmt.Errorf("if multiple policies are set, mode can't be empty")
}

if gok {
hoangpham95 marked this conversation as resolved.
Show resolved Hide resolved
var j map[string]interface{}
if err := json.Unmarshal([]byte(gcRules.(string)), &j); err != nil {
return nil, err
}
return getGCPolicyFromJSON(j)
}

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

func getGCPolicyFromJSON(p map[string]interface{}) (bigtable.GCPolicy, error) {
hoangpham95 marked this conversation as resolved.
Show resolved Hide resolved
policy := []bigtable.GCPolicy{}
if p["rules"] == nil {
return nil, fmt.Errorf("missing `rules` from gc_rules")
}

if p["mode"] == nil && len(p["rules"].([]interface{})) != 1 {
return nil, fmt.Errorf("`rules` needs exactly 1 member if `mode` is not specified")
}

if p["mode"] != nil && len(p["rules"].([]interface{})) < 2 {
return nil, fmt.Errorf("need at least 2 member in `rules` when `mode` is specified")
}

for _, rule := range p["rules"].([]interface{}) {
r := rule.(map[string]interface{})
if r["max_age"] != nil {
maxAge := r["max_age"].(string)
duration, err := time.ParseDuration(maxAge)
if err != nil {
return nil, fmt.Errorf("invalid duration string: %v", maxAge)
}
policy = append(policy, bigtable.MaxAgePolicy(duration))
}

if r["max_version"] != nil {
version := r["max_version"].(float64)
policy = append(policy, bigtable.MaxVersionsPolicy(int(version)))
}

if r["mode"] != nil {
n, err := getGCPolicyFromJSON(r)
if err != nil {
return nil, err
}
policy = append(policy, n)
}
}

switch p["mode"] {
case strings.ToLower(GCPolicyModeUnion):
return bigtable.UnionPolicy(policy...), nil
case strings.ToLower(GCPolicyModeIntersection):
return bigtable.IntersectionPolicy(policy...), nil
default:
return policy[0], nil
hoangpham95 marked this conversation as resolved.
Show resolved Hide resolved
}
}

func getMaxAgeDuration(values map[string]interface{}) (time.Duration, error) {
d := values["duration"].(string)
if d != "" {
Expand Down
178 changes: 178 additions & 0 deletions mmv1/third_party/terraform/tests/resource_bigtable_gc_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"context"
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -120,6 +121,42 @@ func TestAccBigtableGCPolicy_multiplePolicies(t *testing.T) {
})
}

func TestAccBigtableGCPolicy_gcRulesPolicy(t *testing.T) {
skipIfVcr(t)
t.Parallel()

instanceName := fmt.Sprintf("tf-instance-%s", randString(t, 10))
tableName := fmt.Sprintf("tf-table-%s", randString(t, 10))
familyName := fmt.Sprintf("tf-family-%s", randString(t, 10))
hoangpham95 marked this conversation as resolved.
Show resolved Hide resolved

gcRulesOriginal := "{\"mode\":\"intersection\",\"rules\":[{\"max_age\":\"10h\"},{\"max_version\":2}]}"
gcRulesUpdate := "{\"mode\":\"intersection\",\"rules\":[{\"max_age\":\"16h\"},{\"max_version\":1}]}"

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBigtableGCPolicyDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigtableGCPolicy_gcRulesCreate(instanceName, tableName, familyName),
Check: resource.ComposeTestCheckFunc(
testAccBigtableGCPolicyExists(t, "google_bigtable_gc_policy.policy"),
resource.TestCheckResourceAttr("google_bigtable_gc_policy.policy", "gc_rules", gcRulesOriginal),
),
},
// Testing gc_rules update
hoangpham95 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Add test to verify no data loss
{
Config: testAccBigtableGCPolicy_gcRulesUpdate(instanceName, tableName, familyName),
Check: resource.ComposeTestCheckFunc(
testAccBigtableGCPolicyExists(t, "google_bigtable_gc_policy.policy"),
resource.TestCheckResourceAttr("google_bigtable_gc_policy.policy", "gc_rules", gcRulesUpdate),
),
},
},
})
}

func TestUnitBigtableGCPolicy_customizeDiff(t *testing.T) {
for _, tc := range testUnitBigtableGCPolicyCustomizeDiffTestcases {
tc.check(t)
Expand Down Expand Up @@ -150,6 +187,81 @@ func (testcase *testUnitBigtableGCPolicyCustomizeDiffTestcase) check(t *testing.
}
}

type testUnitBigtableGCPolicyJSONRules struct {
name string
gcJSONString string
want string
errorExpected bool
}

var testUnitBigtableGCPolicyRulesTestCases = []testUnitBigtableGCPolicyJSONRules{
{
name: "Simple policy",
gcJSONString: `{"rules":[{"max_age":"10h"}]}`,
want: "age() > 10h",
errorExpected: false,
},
{
name: "Simple multiple policies",
gcJSONString: `{"mode":"union", "rules":[{"max_age":"10h"},{"max_version":2}]}`,
want: "(age() > 10h || versions() > 2)",
errorExpected: false,
},
{
name: "Nested policy",
gcJSONString: `{"mode":"union", "rules":[{"max_age":"10h"},{"mode": "intersection", "rules":[{"max_age":"2h"}, {"max_version":2}]}]}`,
want: "(age() > 10h || (age() > 2h && versions() > 2))",
errorExpected: false,
},
{
name: "JSON with no `rules`",
gcJSONString: `{"mode": "union"}`,
errorExpected: true,
},
{
name: "Empty JSON",
gcJSONString: "{}",
errorExpected: true,
},
{
name: "Invalid duration string",
errorExpected: true,
gcJSONString: `{"mode":"union","rules":[{"max_age":"12o"},{"max_version":2}]}`,
},
{
name: "Empty mode policy with more than 1 rules",
gcJSONString: `{"rules":[{"max_age":"10h"}, {"max_version":2}]}`,
errorExpected: true,
},
{
name: "Less than 2 rules with mode specified",
gcJSONString: `{"mode":"union", "rules":[{"max_version":2}]}`,
errorExpected: true,
},
}

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)
if err != nil {
t.Fatalf("error unmarshalling JSON string: %v", err)
}
got, err := getGCPolicyFromJSON(j)
if tc.errorExpected && err == nil {
t.Fatal("expect error, got nil")
} else if !tc.errorExpected && err != nil {
t.Fatalf("unexpected error: %v", err)
} else {
if got != nil && got.String() != tc.want {
t.Errorf("error getting policy from JSON, got: %v, want: %v", tc.want, got)
}
}
})
}
}

type testUnitBigtableGCPolicyCustomizeDiffTestcase struct {
testName string
arraySize int
Expand Down Expand Up @@ -437,3 +549,69 @@ resource "google_bigtable_gc_policy" "policyC" {
}
`, instanceName, instanceName, tableName, family, family, family, family)
}

func testAccBigtableGCPolicy_gcRulesCreate(instanceName, tableName, family string) string {
return fmt.Sprintf(`
resource "google_bigtable_instance" "instance" {
name = "%s"

cluster {
cluster_id = "%s"
zone = "us-central1-b"
}

instance_type = "DEVELOPMENT"
deletion_protection = false
}

resource "google_bigtable_table" "table" {
name = "%s"
instance_name = google_bigtable_instance.instance.id

column_family {
family = "%s"
}
}

resource "google_bigtable_gc_policy" "policy" {
instance_name = google_bigtable_instance.instance.id
table = google_bigtable_table.table.name
column_family = "%s"

gc_rules = "{\"mode\":\"intersection\", \"rules\":[{\"max_age\":\"10h\"},{\"max_version\":2}]}"
}
`, instanceName, instanceName, tableName, family, family)
}

func testAccBigtableGCPolicy_gcRulesUpdate(instanceName, tableName, family string) string {
hoangpham95 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf(`
resource "google_bigtable_instance" "instance" {
name = "%s"

cluster {
cluster_id = "%s"
zone = "us-central1-b"
}

instance_type = "DEVELOPMENT"
deletion_protection = false
}

resource "google_bigtable_table" "table" {
name = "%s"
instance_name = google_bigtable_instance.instance.id

column_family {
family = "%s"
}
}

resource "google_bigtable_gc_policy" "policy" {
instance_name = google_bigtable_instance.instance.id
table = google_bigtable_table.table.name
column_family = "%s"

gc_rules = "{\"mode\":\"intersection\", \"rules\":[{\"max_age\":\"16h\"},{\"max_version\":1}]}"
}
`, instanceName, instanceName, tableName, family, family)
}
Loading