-
Notifications
You must be signed in to change notification settings - Fork 33
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
Automate recreation of future dates prior to the date #131
Comments
Thanks for raising this, @Noel-Jones 👍 Over in the resource "time_rotating" "example" {
early_rotation_months = 1
rotation_months = 12
} This would requiring saving another computed attribute, such as One challenge we have in this area is that this type of logic could be subjective. For example, what should happen if you subtract a month from March 31st, is it February 28/29 or March 2? The Go standard library That being said, you may be able to approximate date manipulation functionality using the existing terraform {
required_providers {
time = {
source = "hashicorp/time"
version = "0.9.0"
}
}
}
resource "time_rotating" "eleven_months" {
rotation_months = 11
}
output "one_year" {
# Cannot specify 1 month directly, so approximate with 30 days:
# 24 hours * 30 days = 720 hours
value = timeadd(time_rotating.eleven_months.rotation_rfc3339, "720h")
} Would something like this also be an acceptable solution in your case? Of course it'd be great if Terraform supported something like a "dateadd()" function to handle calendar-based date manipulation since the We could consider adding something like a data "time_dateadd" "one_year" {
rfc3339 = time_rotating.example.rfc3339
years = 1
} It'd be interesting if we could do something like: resource "time_rotating" "one_year_with_one_month_early_replacement" {
rotation_months = 12
lifecycle {
replace_triggered_by = [
timecmp(self.rotation_rfc3339, timeadd(self.rotation_rfc3339, "-720h")),
]
}
} However, those $ terraform apply
╷
│ Error: Invalid reference in replace_triggered_by expression
│
│ on main.tf line 15, in resource "time_rotating" "one_year_with_one_month_early_replacement":
│ 15: timecmp(self.rotation_rfc3339, timeadd(self.rotation_rfc3339, "-720h")),
│
│ Only resources, count.index, and each.key may be used in replace_triggered_by.
╵ Supporting similar functionality that automatically triggers replacement if any of the elements are true would be interesting, such as: resource "time_rotating" "one_year_with_one_month_early_replacement" {
rotation_months = 12
lifecycle {
# replace_when is a theoretical lifecycle addition that is not supported today.
replace_when = [
timecmp(self.rotation_rfc3339, timeadd(self.rotation_rfc3339, "-720h")) > 0,
]
}
} Which is something that could also be proposed upstream. |
Thanks for the well considered response Brian. I agree that it could be added to the existing rotating provider. I'd also note that the timing logic is already somewhat subjective since the resource currently add months and so on but it is a point well made. I'm finding it hard to thik how to explain how the early_rotation_months would be used, maybe recreate_after_months = 11 would be more intuitive. However, like you I came up with the implementation using timeadd per your second example; rotate after 11 months and use timeadd to create the 12 month expiry date. This does suffer from the expiry date being somewhat random with the hours being an approximation as you note. It is also up to the coder to ensure that the expiry date expressed in hours is greater than the rotation period expressed possibly in another unit. But it works and does not require change. I'm inclined to cancel this request given the alternative solution. I wonder if it is worth documenting this as a use case? |
Community Note
Description
I would like a time resource that produces a future date that is recalcuated after a period that is prior to that date. This would assist with creation of resources that should have an expiry date and allow them to be automatically updated prior to expiry (and if put in a keyvault then they can be fetched by the application transparently). This would mean that if terraform is run between the recreate time and the expiry date then the resource will be recreated.
This can be done using two resources now:
But it would be tidier to have one resource as the above does not check that the rotation occurs before the expiry date.
New or Affected Resource(s)
time_offset
Potential Terraform Configuration
There would need to be a recreate_after_xxx attributes of each offset_xxx attribute. Like the offset, the recreate_after would be relative to the base time.
References
none
The text was updated successfully, but these errors were encountered: