Skip to content

Commit

Permalink
Upstream Bigtable seconds (#4282) (#2815)
Browse files Browse the repository at this point in the history
* adding support for duration lower than day

Signed-off-by: Kevin Labesse <kevin@labesse.me>

* adding support for duration lower than day

Signed-off-by: Kevin Labesse <kevin@labesse.me>

* set optional

* seconds optional

* review

* review

* go fmt

* duration

* duration

* fix ExactlyOneOf

* Fixes for compile, docs

* Fmt

Co-authored-by: Kevin Labesse <kevin@labesse.me>
Signed-off-by: Modular Magician <magic-modules@google.com>

Co-authored-by: Kevin Labesse <kevin@labesse.me>
  • Loading branch information
modular-magician and eraac authored Dec 23, 2020
1 parent 9b0e158 commit 5cb94e1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .changelog/4282.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
bigtable: added support for specifying `duration` for `bigtable_gc_policy` to allow durations shorter than a day
```
34 changes: 29 additions & 5 deletions google-beta/resource_bigtable_gc_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,22 @@ func resourceBigtableGCPolicy() *schema.Resource {
Optional: true,
ForceNew: true,
Description: `GC policy that applies to all cells older than the given age.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"days": {
Type: schema.TypeInt,
Required: true,
Description: `Number of days before applying GC policy.`,
Type: schema.TypeInt,
Optional: true,
Deprecated: "Deprecated in favor of duration",
Description: `Number of days before applying GC policy.`,
ExactlyOneOf: []string{"max_age.0.days", "max_age.0.duration"},
},
"duration": {
Type: schema.TypeString,
Optional: true,
Description: `Duration before applying GC policy`,
ValidateFunc: validateDuration(),
ExactlyOneOf: []string{"max_age.0.days", "max_age.0.duration"},
},
},
},
Expand Down Expand Up @@ -236,9 +246,12 @@ func generateBigtableGCPolicy(d *schema.ResourceData) (bigtable.GCPolicy, error)

if aok {
l, _ := ma.([]interface{})
d, _ := l[0].(map[string]interface{})["days"].(int)
d, err := getMaxAgeDuration(l[0].(map[string]interface{}))
if err != nil {
return nil, err
}

policies = append(policies, bigtable.MaxAgePolicy(time.Duration(d)*time.Hour*24))
policies = append(policies, bigtable.MaxAgePolicy(d))
}

if vok {
Expand All @@ -257,3 +270,14 @@ func generateBigtableGCPolicy(d *schema.ResourceData) (bigtable.GCPolicy, error)

return policies[0], nil
}

func getMaxAgeDuration(values map[string]interface{}) (time.Duration, error) {
d := values["duration"].(string)
if d != "" {
return time.ParseDuration(d)
}

days := values["days"].(int)

return time.Hour * 24 * time.Duration(days), nil
}
4 changes: 2 additions & 2 deletions google-beta/resource_bigtable_gc_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ resource "google_bigtable_gc_policy" "policy" {
column_family = "%s"
max_age {
days = 3
duration = "72h"
}
}
`, instanceName, instanceName, tableName, family, family)
Expand Down Expand Up @@ -195,7 +195,7 @@ resource "google_bigtable_gc_policy" "policy" {
mode = "UNION"
max_age {
days = 3
duration = "72h"
}
max_version {
Expand Down
2 changes: 1 addition & 1 deletion google-beta/resource_dataflow_flex_template_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/compute/v1"
)

func TestAccDataflowFlexTemplateJob_basic(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions website/docs/r/bigtable_gc_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ resource "google_bigtable_gc_policy" "policy" {
column_family = "name"
max_age {
days = 7
duration = "168h"
}
}
```
Expand All @@ -58,7 +58,7 @@ resource "google_bigtable_gc_policy" "policy" {
mode = "UNION"
max_age {
days = 7
duration = "168h" # 7 days
}
max_version {
Expand Down Expand Up @@ -89,7 +89,9 @@ The following arguments are supported:

`max_age` supports the following arguments:

* `days` - (Required) Number of days before applying GC policy.
* `days` - (Optional, Deprecated in favor of duration) Number of days before applying GC policy.

* `duration` - (Optional) Duration before applying GC policy (ex. "8h"). This is required when `days` isn't set

-----

Expand Down

0 comments on commit 5cb94e1

Please sign in to comment.