Skip to content

Commit

Permalink
create file
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyclenerd committed Oct 14, 2024
1 parent ce3f1d7 commit 3757e4c
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 0 deletions.
22 changes: 22 additions & 0 deletions terraform/create-file/.terraform-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
formatter: "markdown"
output:
file: "README.md"
mode: inject
template: |-
<!-- BEGIN_TF_DOCS -->
{{ .Content }}
<!-- END_TF_DOCS -->
settings:
indent: 2
content: |-
{{ .Requirements }}
{{ .Providers }}
{{ .Modules }}
{{ .Resources }}
{{ .Inputs }}
{{ .Outputs }}
45 changes: 45 additions & 0 deletions terraform/create-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Creates a local file

Creates a single file if the file name does **not** exist.

If the file already exists or has been modified, the Terraform state is only updated during `terraform apply`.

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.7 |
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 2.4.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_local"></a> [local](#provider\_local) | >= 2.4.1 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [local_file.this](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_content"></a> [content](#input\_content) | Content to store in the file, expected to be a UTF-8 encoded string. | `string` | n/a | yes |
| <a name="input_directory_permission"></a> [directory\_permission](#input\_directory\_permission) | Permissions to set for directories created (before umask), expressed as string in numeric notation. Default value is 0755. | `string` | `"0755"` | no |
| <a name="input_file_permission"></a> [file\_permission](#input\_file\_permission) | Permissions to set for the output file (before umask), expressed as string in numeric notation. Default value is 0644. | `string` | `"0644"` | no |
| <a name="input_filename"></a> [filename](#input\_filename) | The path to the file that will be created. Missing parent directories will be created. If the file already exists, it will NOT be overridden. | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_file_id"></a> [file\_id](#output\_file\_id) | The hexadecimal encoding of the SHA1 checksum of the file content. |
<!-- END_TF_DOCS -->
1 change: 1 addition & 0 deletions terraform/create-file/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Examples
25 changes: 25 additions & 0 deletions terraform/create-file/examples/file/.terraform-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
formatter: "markdown"
output:
file: "README.md"
mode: inject
template: |-
<!-- BEGIN_TF_DOCS -->
{{ .Content }}
<!-- END_TF_DOCS -->
settings:
indent: 2
content: |-
```hcl
{{ include "main.tf" }}
```
{{ .Providers }}
{{ .Modules }}
{{ .Resources }}
{{ .Inputs }}
{{ .Outputs }}
73 changes: 73 additions & 0 deletions terraform/create-file/examples/file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Creates a local file

Creates a single file.

Run:

```bash
echo "Test" > /tmp/test
chmod 644 /tmp/test
terraform init
terraform plan
terraform apply
```

## Example

<!-- BEGIN_TF_DOCS -->

```hcl
# Example
locals {
new_file_name = "/tmp/${random_pet.this.id}"
existing_file_name = "/tmp/test"
}
# Creates new file
module "new_file" {
source = "../../"
filename = local.new_file_name
content = "Test"
}
# Does not create a file (file name already exists)
# Note: Terraform state will be updated if necessary (first apply).
module "existing_file" {
source = "../../"
filename = local.existing_file_name
content = "Overwrite"
}
# Helper
resource "random_pet" "this" {
length = 2
}
```

## Providers

| Name | Version |
|------|---------|
| <a name="provider_random"></a> [random](#provider\_random) | 3.6.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_existing_file"></a> [existing\_file](#module\_existing\_file) | ../../ | n/a |
| <a name="module_new_file"></a> [new\_file](#module\_new\_file) | ../../ | n/a |

## Resources

| Name | Type |
|------|------|
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |

## Inputs

No inputs.

## Outputs

No outputs.
<!-- END_TF_DOCS -->
25 changes: 25 additions & 0 deletions terraform/create-file/examples/file/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Example
locals {
new_file_name = "/tmp/${random_pet.this.id}"
existing_file_name = "/tmp/test"
}

# Creates new file
module "new_file" {
source = "../../"
filename = local.new_file_name
content = "Test"
}

# Does not create a file (file name already exists)
# Note: Terraform state will be updated if necessary (first apply).
module "existing_file" {
source = "../../"
filename = local.existing_file_name
content = "Overwrite"
}

# Helper
resource "random_pet" "this" {
length = 2
}
10 changes: 10 additions & 0 deletions terraform/create-file/examples/file/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.5.7"

required_providers {
random = {
source = "hashicorp/random"
version = ">= 3.6"
}
}
}
11 changes: 11 additions & 0 deletions terraform/create-file/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
locals {
content = fileexists(var.filename) ? file(var.filename) : var.content
}

resource "local_file" "this" {
# Create file only if file does not exists
file_permission = var.file_permission
directory_permission = var.directory_permission
filename = var.filename
content = local.content
}
4 changes: 4 additions & 0 deletions terraform/create-file/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "file_id" {
description = "The hexadecimal encoding of the SHA1 checksum of the file content."
value = try(local_file.this.id, null)
}
21 changes: 21 additions & 0 deletions terraform/create-file/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
variable "filename" {
description = "The path to the file that will be created. Missing parent directories will be created. If the file already exists, it will NOT be overridden."
type = string
}

variable "content" {
description = "Content to store in the file, expected to be a UTF-8 encoded string."
type = string
}

variable "directory_permission" {
description = "Permissions to set for directories created (before umask), expressed as string in numeric notation. Default value is 0755."
type = string
default = "0755"
}

variable "file_permission" {
description = "Permissions to set for the output file (before umask), expressed as string in numeric notation. Default value is 0644."
type = string
default = "0644"
}
10 changes: 10 additions & 0 deletions terraform/create-file/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.5.7"

required_providers {
local = {
source = "hashicorp/local"
version = ">= 2.4.1"
}
}
}

0 comments on commit 3757e4c

Please sign in to comment.