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 timediff datetime function #23429

Closed
adongy opened this issue Nov 20, 2019 · 3 comments
Closed

Add timediff datetime function #23429

adongy opened this issue Nov 20, 2019 · 3 comments

Comments

@adongy
Copy link

adongy commented Nov 20, 2019

Current Terraform Version

Terraform v0.12.14

Use-cases

In Vault, many resources accept a ttl kind of argument which is usually a number of seconds a token can be valid for. I'd like to create a policy to allow users to be able to generate tokens up until a certain point in time.

Example: a contractor joins my company for a contract ending on Dec 31th, I'll set max_ttl to Dec 31th - Current date for tokens issued to the contractor.

Terraform currently provides a few datetime related functions, including timeadd, but it only supports a datetime and a duration as arguments.

Attempted Solutions

Did not find any solution to this, except computing the ttl myself.

Proposal

Add a timediff(timestamp, timestamp) function which would return a number (the number of seconds between the two timestamps). Returning a duration would be preferable to avoid huge durations converting into a big number, but there are no duration utils in Terraform which would make further usage complex.
https://github.com/hashicorp/terraform/blob/master/lang/funcs/datetime.go

References

@apparentlymart
Copy link
Contributor

Hi @adongy! Sorry for the long silence here.

I ended up implementing something related to this for a different reason, and only afterwards found this older issue covering the use-case. The forthcoming Terraform v1.3.0 will have a timecmp function which takes two timestamps and determines their relative ordering: is the first earlier than the second, is the second earlier than the first, or are they equal?

That doesn't directly address your use-case here, but it can then be combined with timeadd and other functions or providers that generate timestamps in the conventional format to determine whether a timestamp is before or after some other timestamp.

As you noted, Terraform doesn't have much existing functionality for computing with durations in particular, and so the design of timecmp is a compromise that can compose with the other functions to answer boolean questions about timestamps being in range, but it cannot directly answer the question of exactly how much time there is between two time stamps.

I'm going to close this issue not because I believe timecmp fully solves the problem, but because I think it addresses as much of the problem as we're likely to address with built-in functions. In the longer term we are hoping to allow provider plugins to contribute additional functions (#2771) so that they can be maintained independently of the main Terraform Language runtime, so for now we are aiming to minimize the built-in functions to only what seems to have a large impact (lots of use-cases solved).

In the meantime, the hashicorp/time provider includes some resource types for performing some different kinds of operations based on time which may help fill some gaps that the built-in functions can't support.

Thanks!

@apparentlymart apparentlymart closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2022
@adongy
Copy link
Author

adongy commented Aug 30, 2022

Hello,

I left the company that wanted to leverage that usecase, but good to hear an API for providing custom functions is getting some traction 👍

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants