An open API service indexing awesome lists of open source software.

https://github.com/libre-devops/terraform-external-timestamp

A module used to generate a timestamp. This is useful due to the fact it runs at plan time rather than apply ⌚
https://github.com/libre-devops/terraform-external-timestamp

Last synced: about 1 year ago
JSON representation

A module used to generate a timestamp. This is useful due to the fact it runs at plan time rather than apply ⌚

Awesome Lists containing this project

README

          

```hcl
###############################################################################
# Detect the OS
###############################################################################

# Full path to the helper that exists only on Windows
locals {
windows_helper = "${abspath(path.module)}\\printf.cmd"
}

# If the helper file exists, we’re on Windows; otherwise assume Linux
data "external" "detect_os" {
program = fileexists(local.windows_helper) ? [local.windows_helper, "{\"os\":\"Windows\"}"] : ["printf", "{\"os\":\"Linux\"}"]
}

locals {
os = data.external.detect_os.result.os
is_windows = lower(local.os) == "windows"
is_linux = lower(local.os) == "linux"
}

# Linux branch
data "external" "generate_linux_timestamp" {
count = local.is_linux ? 1 : 0
working_dir = var.working_dir == null ? path.module : var.working_dir

program = [
"bash",
"-c",
"DATE=$(date '${var.linux_timestamp_format}'); printf '{\"id\":\"%s\",\"timestamp\":\"%s\"}' \"$DATE\" \"$DATE\""
]
}

# Windows branch
data "external" "generate_windows_timestamp" {
count = local.is_windows ? 1 : 0
working_dir = var.working_dir == null ? path.module : var.working_dir

program = [
"powershell",
"-Command",
"$date = Get-Date -Format '${var.windows_timestamp_format}'; $json = @{ id = $date; timestamp = $date } | ConvertTo-Json -Compress; Write-Output $json"
]

}

###############################################################################
# Normalise the result
###############################################################################

locals {
timestamp = local.is_linux ? lower(data.external.generate_linux_timestamp[0].result.timestamp) : lower(data.external.generate_windows_timestamp[0].result.timestamp)
}
```
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| [external](#provider\_external) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [external_external.detect_os](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
| [external_external.generate_linux_timestamp](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
| [external_external.generate_windows_timestamp](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| [linux\_timestamp\_format](#input\_linux\_timestamp\_format) | The format of the timestamp to generate on Linux | `string` | `"+%d-%m-%Y:%H:%M"` | no |
| [windows\_timestamp\_format](#input\_windows\_timestamp\_format) | The format of the timestamp to generate on Windows | `string` | `"dd-MM-yyyy:HH:mm"` | no |
| [working\_dir](#input\_working\_dir) | The working directory for the module | `string` | `null` | no |

## Outputs

| Name | Description |
|------|-------------|
| [is\_linux](#output\_is\_linux) | True if the OS is Linux |
| [is\_windows](#output\_is\_windows) | True if the OS is Windows |
| [os](#output\_os) | The OS that is running the commands |
| [timestamp](#output\_timestamp) | A random string generated by the external data source |