terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Add function to parse duration into seconds/milliseconds

Open yegle opened this issue 5 years ago • 8 comments

Current Terraform Version

v0.13.5

Use-cases

Be able to specify duration like "3h" and have a utility function to convert to seconds or milliseconds.

Attempted Solutions

None

Proposal

Have duration_in_s() that convert a string representation of duration to seconds, and duration_in_ms() that convert a string representation of duration to milliseconds.

References

Related proposal: https://github.com/hashicorp/terraform/issues/23429

yegle avatar Dec 01 '20 17:12 yegle

Hi @yegle, does https://github.com/hashicorp/terraform/pull/27048 does what you need?

remilapeyre avatar Dec 01 '20 17:12 remilapeyre

@remilapeyre Yes that's exactly what I wanted!

Another idea is to return an object instead of a number, something like this:

duration("1h") => { hour = 1, minute = 60, second = 3600, ms = 3600000 }

You can then reference the duration in different unit like duration("1h").second.

yegle avatar Dec 01 '20 19:12 yegle

Hi @yegle! Thanks for this feature request.

The use-case you wrote here seems less like a use-case and more like a description of the behavior of what you proposed. Can you say a little more about why it would be useful to parse a duration like this? It'd be great to see a full example of how you'd use this as part of a larger system so we can understand how it might fit in with other Terraform features.

apparentlymart avatar Dec 02 '20 01:12 apparentlymart

Hi @apparentlymart

Here's a concrete example: https://github.com/google/exposure-notifications-server/blob/1013999583a94da7775f5bff5fc1dd7bbd8dadc9/terraform/alerting/alerts.tf#L16-L31

In order to generate the correct MQL to use with Google Cloud Monitoring API, I'll have to use threshold in milliseconds. But defining them using duration string would be more convenient and more readable.

yegle avatar Dec 02 '20 02:12 yegle

Using time provider we can use the following workaround

locals {
  duration       = "1 years"
  # duration     = "1 months"
  # duration     = "1 days"
  # duration     = "1 hours"
  duration_split = split(" ", local.duration)
}

resource "time_static" "now" {}

resource "time_offset" "future" {
  base_rfc3339   = time_static.now.rfc3339
  offset_years   = contains(local.duration_split, "years") ? element(local.duration_split, 0) : null
  offset_months  = contains(local.duration_split, "months") ? element(local.duration_split, 0) : null
  offset_days    = contains(local.duration_split, "days") ? element(local.duration_split, 0) : null
  offset_hours   = contains(local.duration_split, "hours") ? element(local.duration_split, 0) : null
  offset_minutes = contains(local.duration_split, "minutes") ? element(local.duration_split, 0) : null
}

output "seconds" {
  value = time_offset.future.unix - time_static.now.unix
}

output "milliseconds" {
  value = (time_offset.future.unix - time_static.now.unix) * 1000
}
milliseconds = 31622400000
seconds = 31622400

It will be good to have a short function for that and to be able to parse something like 1d 1m 1h 1s or 1 years 1 months 1 days 1 hours.

Mentioned workaround with some modifications was used for the google_compute_region_autoscaler where we should specify the duration in seconds

    scaling_schedules {
      ...
      duration_sec = time_offset.duration.unix - time_offset.start.unix
    }

And in the variables, for the convenience, we specify the duration in years, months, days and hours.

ghost avatar Mar 12 '23 22:03 ghost

I have another use case for this kind of function, in the vault provider if you need to set the min_seconds_remaining, you need to add beside the value as a documentation, the representation in something more human readable. It's even what it is done in the official documentation for the default value of 7 days.

This kind of situation where you feel the need to comment the value to fully explained what the value represents, doesn't seem right, it's the kind of thing that could lead to misunderstanding.

https://registry.terraform.io/providers/hashicorp/vault/latest/docs/resources/pki_secret_backend_cert#min_seconds_remaining

antham avatar Mar 13 '23 07:03 antham

Thank you for your continued interest in this issue.

Terraform version 1.8 launches with support of provider-defined functions. It is now possible to implement your own functions! We would love to see this implemented as a provider-defined function.

Please see the provider-defined functions documentation to learn how to implement functions in your providers. If you are new to provider development, learn how to create a new provider with the Terraform Plugin Framework. If you have any questions, please visit the Terraform Plugin Development category in our official forum.

We hope this feature unblocks future function development and provides more flexibility for the Terraform community. Thank you for your continued support of Terraform!

crw avatar Mar 07 '24 00:03 crw

https://github.com/hashicorp/terraform-provider-time/pull/350

coming soon 🚀

dongho-jung avatar Feb 28 '25 04:02 dongho-jung