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 support for multiple offsets to time_offset module #189

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
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20231130-135645.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'resource/time_offset: Fix bug preventing multiple offset arguments from being
set'
time: 2023-11-30T13:56:45.120869-05:00
custom:
Issue: "189"
13 changes: 13 additions & 0 deletions docs/resources/offset.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ output "one_week_from_now" {
}
```

### Multiple Offsets Usage

```terraform
resource "time_offset" "example" {
offset_years = 1
offset_months = 1
}

output "one_year_and_month_from_now" {
value = time_offset.example.rfc3339
}
```

### Triggers Usage

```terraform
Expand Down
8 changes: 8 additions & 0 deletions examples/resources/time_offset/resource_multiple_offset.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "time_offset" "example" {
offset_years = 1
offset_months = 1
}

output "one_year_and_month_from_now" {
value = time_offset.example.rfc3339
}
14 changes: 7 additions & 7 deletions internal/provider/resource_time_offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,33 +391,33 @@ type timeOffsetModelV0 struct {
}

func setOffsetValues(plan *timeOffsetModelV0, timestamp time.Time) {
var offsetTimestamp time.Time
var offsetTimestamp = timestamp

if plan.OffsetDays.ValueInt64() != 0 {
offsetTimestamp = timestamp.AddDate(0, 0, int(plan.OffsetDays.ValueInt64()))
offsetTimestamp = offsetTimestamp.AddDate(0, 0, int(plan.OffsetDays.ValueInt64()))
}

if plan.OffsetHours.ValueInt64() != 0 {
hours := time.Duration(plan.OffsetHours.ValueInt64()) * time.Hour
offsetTimestamp = timestamp.Add(hours)
offsetTimestamp = offsetTimestamp.Add(hours)
}

if plan.OffsetMinutes.ValueInt64() != 0 {
minutes := time.Duration(plan.OffsetMinutes.ValueInt64()) * time.Minute
offsetTimestamp = timestamp.Add(minutes)
offsetTimestamp = offsetTimestamp.Add(minutes)
}

if plan.OffsetMonths.ValueInt64() != 0 {
offsetTimestamp = timestamp.AddDate(0, int(plan.OffsetMonths.ValueInt64()), 0)
offsetTimestamp = offsetTimestamp.AddDate(0, int(plan.OffsetMonths.ValueInt64()), 0)
}

if plan.OffsetSeconds.ValueInt64() != 0 {
seconds := time.Duration(plan.OffsetSeconds.ValueInt64()) * time.Second
offsetTimestamp = timestamp.Add(seconds)
offsetTimestamp = offsetTimestamp.Add(seconds)
}

if plan.OffsetYears.ValueInt64() != 0 {
offsetTimestamp = timestamp.AddDate(int(plan.OffsetYears.ValueInt64()), 0, 0)
offsetTimestamp = offsetTimestamp.AddDate(int(plan.OffsetYears.ValueInt64()), 0, 0)
}

plan.BaseRFC3339 = timetypes.NewRFC3339TimeValue(timestamp)
Expand Down
62 changes: 62 additions & 0 deletions internal/provider/resource_time_offset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,58 @@ func TestAccTimeOffset_OffsetYears(t *testing.T) {
})
}

func TestAccTimeOffset_OffsetYearsAndMonths(t *testing.T) {
resourceName := "time_offset.test"
timestamp := time.Now().UTC()
offsetTimestamp := timestamp.AddDate(3, 3, 0)
offsetTimestampUpdated := timestamp.AddDate(4, 4, 0)

resource.UnitTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccConfigTimeOffsetOffsetYearsAndMonths(timestamp.Format(time.RFC3339), 3, 3),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "base_rfc3339", timestamp.Format(time.RFC3339)),
resource.TestCheckResourceAttr(resourceName, "day", strconv.Itoa(offsetTimestamp.Day())),
resource.TestCheckResourceAttr(resourceName, "hour", strconv.Itoa(offsetTimestamp.Hour())),
resource.TestCheckResourceAttr(resourceName, "minute", strconv.Itoa(offsetTimestamp.Minute())),
resource.TestCheckResourceAttr(resourceName, "month", strconv.Itoa(int(offsetTimestamp.Month()))),
resource.TestCheckResourceAttr(resourceName, "offset_years", "3"),
resource.TestCheckResourceAttr(resourceName, "offset_months", "3"),
resource.TestCheckResourceAttr(resourceName, "rfc3339", offsetTimestamp.Format(time.RFC3339)),
resource.TestCheckResourceAttr(resourceName, "second", strconv.Itoa(offsetTimestamp.Second())),
resource.TestCheckResourceAttr(resourceName, "unix", strconv.Itoa(int(offsetTimestamp.Unix()))),
resource.TestCheckResourceAttr(resourceName, "year", strconv.Itoa(offsetTimestamp.Year())),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccTimeOffsetImportStateIdFunc(),
ImportStateVerify: true,
},
{
Config: testAccConfigTimeOffsetOffsetYearsAndMonths(timestamp.Format(time.RFC3339), 4, 4),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "base_rfc3339", timestamp.Format(time.RFC3339)),
resource.TestCheckResourceAttr(resourceName, "day", strconv.Itoa(offsetTimestampUpdated.Day())),
resource.TestCheckResourceAttr(resourceName, "hour", strconv.Itoa(offsetTimestampUpdated.Hour())),
resource.TestCheckResourceAttr(resourceName, "minute", strconv.Itoa(offsetTimestampUpdated.Minute())),
resource.TestCheckResourceAttr(resourceName, "month", strconv.Itoa(int(offsetTimestampUpdated.Month()))),
resource.TestCheckResourceAttr(resourceName, "offset_years", "4"),
resource.TestCheckResourceAttr(resourceName, "offset_months", "4"),
resource.TestCheckResourceAttr(resourceName, "rfc3339", offsetTimestampUpdated.Format(time.RFC3339)),
resource.TestCheckResourceAttr(resourceName, "second", strconv.Itoa(offsetTimestampUpdated.Second())),
resource.TestCheckResourceAttr(resourceName, "unix", strconv.Itoa(int(offsetTimestampUpdated.Unix()))),
resource.TestCheckResourceAttr(resourceName, "year", strconv.Itoa(offsetTimestampUpdated.Year())),
),
},
},
})
}

func TestAccTimeOffset_Upgrade(t *testing.T) {
resourceName := "time_offset.test"
timestamp := time.Now().UTC()
Expand Down Expand Up @@ -508,3 +560,13 @@ resource "time_offset" "test" {
}
`, baseRfc3339, offsetYears)
}

func testAccConfigTimeOffsetOffsetYearsAndMonths(baseRfc3339 string, offsetYears int, offsetMonths int) string {
return fmt.Sprintf(`
resource "time_offset" "test" {
base_rfc3339 = %[1]q
offset_years = %[2]d
offset_months = %[3]d
}
`, baseRfc3339, offsetYears, offsetMonths)
}
4 changes: 4 additions & 0 deletions templates/resources/offset.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ description: |-

{{ tffile "examples/resources/time_offset/resource.tf" }}

### Multiple Offsets Usage

{{ tffile "examples/resources/time_offset/resource_multiple_offset.tf" }}

### Triggers Usage

{{ tffile "examples/resources/time_offset/resource_triggers.tf" }}
Expand Down
Loading