-
Notifications
You must be signed in to change notification settings - Fork 73
Add local_file ephemeral resource
#440
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
Open
yesdevnull
wants to merge
6
commits into
hashicorp:main
Choose a base branch
from
yesdevnull:feat-add-ephemeral
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1ef2dda
wip: add ephemeral local_file resource
yesdevnull 1f7e992
wip: cleanup and improved tests
yesdevnull ddda4f9
deps: move log to required imports
yesdevnull 6ea5f86
docs: work on doco
yesdevnull e4ec2db
wip: cleanup
yesdevnull c6de64e
wip: cleanup example/doc
yesdevnull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| --- | ||
| page_title: "local_file Ephemeral Resource - terraform-provider-local" | ||
| subcategory: "" | ||
| description: |- | ||
| Generates an ephemeral local file with the given content. | ||
| --- | ||
|
|
||
| # local_file (Ephemeral Resource) | ||
|
|
||
| Generates an ephemeral local file with the given content. | ||
|
|
||
| -> **Note**: Ephemeral resources are available in Terraform v1.10 and later. | ||
|
|
||
| ~> **Note about resource behaviour** | ||
| Ephemeral resources are considered to be sensitive so none of the arguments | ||
| can be displayed in output logs. This means if you use an ephemeral resource | ||
| in a provisioner the output will be suppressed. This cannot be overridden using | ||
| the `nonsensitive` function as while ephemeral values are considered sensitive | ||
| they are not actually sensitive values. | ||
|
|
||
| ~> **Note about file content** | ||
| File content must be specified with _exactly_ one of the arguments `content`, | ||
| `content_base64`, or `source`. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| ephemeral "local_file" "foo" { | ||
| content = "foo!" | ||
| filename = "foo.bar" | ||
| } | ||
|
|
||
| resource "terraform_data" "foo" { | ||
| provisioner "local-exec" { | ||
| command = "openssl sha256 ${ephemeral.local_file.foo.filename} > ${ephemeral.local_file.foo.filename}.sha256" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `filename` (String) The path to the file that will be created. | ||
| Missing parent directories will be created. | ||
| If the file already exists, it will be overridden with the given content. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `content` (String) Content to store in the file, expected to be a UTF-8 encoded string. | ||
| Conflicts with `content_base64` and `source`. | ||
| Exactly one of these three arguments must be specified. | ||
| - `content_base64` (String) Content to store in the file, expected to be binary encoded as base64 string. | ||
| Conflicts with `content` and `source`. | ||
| Exactly one of these three arguments must be specified. | ||
| - `directory_permission` (String) Permissions to set for directories created (before umask), expressed as string in | ||
| [numeric notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation). | ||
| Default value is `"0777"`. | ||
| - `file_permission` (String) Permissions to set for the output file (before umask), expressed as string in | ||
| [numeric notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation). | ||
| Default value is `"0777"`. | ||
| - `source` (String) Path to file to use as source for the one we are creating. | ||
| Conflicts with `content` and `content_base64`. | ||
| Exactly one of these three arguments must be specified. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `content_base64sha256` (String) Base64 encoded SHA256 checksum of file content. | ||
| - `content_base64sha512` (String) Base64 encoded SHA512 checksum of file content. | ||
| - `content_md5` (String) MD5 checksum of file content. | ||
| - `content_sha1` (String) SHA1 checksum of file content. | ||
| - `content_sha256` (String) SHA256 checksum of file content. | ||
| - `content_sha512` (String) SHA512 checksum of file content. | ||
| - `id` (String) The hexadecimal encoding of the SHA1 checksum of the file content. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ephemeral "local_file" "foo" { | ||
| content = "foo!" | ||
| filename = "foo.bar" | ||
| } | ||
|
|
||
| resource "terraform_data" "foo" { | ||
| provisioner "local-exec" { | ||
| command = "openssl sha256 ${ephemeral.local_file.foo.filename} > ${ephemeral.local_file.foo.filename}.sha256" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,265 @@ | ||||||
| // Copyright (c) HashiCorp, Inc. | ||||||
| // SPDX-License-Identifier: MPL-2.0 | ||||||
|
|
||||||
| package provider | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "encoding/base64" | ||||||
| "encoding/json" | ||||||
| "fmt" | ||||||
| "os" | ||||||
| "path/filepath" | ||||||
| "strconv" | ||||||
|
|
||||||
| "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/ephemeral" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/path" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/types/basetypes" | ||||||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||||||
| "github.com/terraform-providers/terraform-provider-local/internal/localtypes" | ||||||
| ) | ||||||
|
|
||||||
| var ( | ||||||
| _ ephemeral.EphemeralResource = (*localFileEphemeralResource)(nil) | ||||||
| ) | ||||||
|
|
||||||
| func NewLocalFileEphemeralResource() ephemeral.EphemeralResource { | ||||||
| return &localFileEphemeralResource{} | ||||||
| } | ||||||
|
|
||||||
| type localFileEphemeralResource struct{} | ||||||
|
|
||||||
| func (e *localFileEphemeralResource) Schema(_ context.Context, _ ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { | ||||||
| resp.Schema = schema.Schema{ | ||||||
| Description: "Generates an ephemeral local file with the given content.", | ||||||
| Attributes: map[string]schema.Attribute{ | ||||||
| "filename": schema.StringAttribute{ | ||||||
| Description: "The path to the file that will be created.\n " + | ||||||
| "Missing parent directories will be created.\n " + | ||||||
| "If the file already exists, it will be overridden with the given content.", | ||||||
| Required: true, | ||||||
| }, | ||||||
| "content": schema.StringAttribute{ | ||||||
| Description: "Content to store in the file, expected to be a UTF-8 encoded string.\n " + | ||||||
| "Conflicts with `content_base64` and `source`.\n " + | ||||||
| "Exactly one of these three arguments must be specified.", | ||||||
| Optional: true, | ||||||
| Validators: []validator.String{ | ||||||
| stringvalidator.ExactlyOneOf( | ||||||
| path.MatchRoot("content_base64"), | ||||||
| path.MatchRoot("source")), | ||||||
| }, | ||||||
| }, | ||||||
| "content_base64": schema.StringAttribute{ | ||||||
| Description: "Content to store in the file, expected to be binary encoded as base64 string.\n " + | ||||||
| "Conflicts with `content` and `source`.\n " + | ||||||
| "Exactly one of these three arguments must be specified.", | ||||||
| Optional: true, | ||||||
| Validators: []validator.String{ | ||||||
| stringvalidator.ExactlyOneOf( | ||||||
| path.MatchRoot("content"), | ||||||
| path.MatchRoot("source")), | ||||||
| }, | ||||||
| }, | ||||||
| "source": schema.StringAttribute{ | ||||||
| Description: "Path to file to use as source for the one we are creating.\n " + | ||||||
| "Conflicts with `content` and `content_base64`.\n " + | ||||||
| "Exactly one of these three arguments must be specified.", | ||||||
| Optional: true, | ||||||
| Validators: []validator.String{ | ||||||
| stringvalidator.ExactlyOneOf( | ||||||
| path.MatchRoot("content"), | ||||||
| path.MatchRoot("content_base64")), | ||||||
| }, | ||||||
| }, | ||||||
| "file_permission": schema.StringAttribute{ | ||||||
| CustomType: localtypes.NewFilePermissionType(), | ||||||
| Description: "Permissions to set for the output file (before umask), expressed as string in\n " + | ||||||
| "[numeric notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation).\n " + | ||||||
| "Default value is `\"0777\"`.", | ||||||
| Optional: true, | ||||||
| Computed: true, | ||||||
| // Can't set a default value for ephemeral resources, this is here as a fingers-crossed placeholder. | ||||||
| // Default: stringdefault.StaticString("0777"), | ||||||
| }, | ||||||
| "directory_permission": schema.StringAttribute{ | ||||||
| CustomType: localtypes.NewFilePermissionType(), | ||||||
| Description: "Permissions to set for directories created (before umask), expressed as string in\n " + | ||||||
| "[numeric notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation).\n " + | ||||||
| "Default value is `\"0777\"`.", | ||||||
| Optional: true, | ||||||
| Computed: true, | ||||||
| // Can't set a default value for ephemeral resources, this is here as a fingers-crossed placeholder. | ||||||
| // Default: stringdefault.StaticString("0777"), | ||||||
|
Comment on lines
+96
to
+97
|
||||||
| // Can't set a default value for ephemeral resources, this is here as a fingers-crossed placeholder. | |
| // Default: stringdefault.StaticString("0777"), |
yesdevnull marked this conversation as resolved.
Show resolved
Hide resolved
yesdevnull marked this conversation as resolved.
Show resolved
Hide resolved
yesdevnull marked this conversation as resolved.
Show resolved
Hide resolved
yesdevnull marked this conversation as resolved.
Show resolved
Hide resolved
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the commented-out code and the todo-style comment. If default values aren't supported for ephemeral resources, the comment explaining the '0777' default in the description is sufficient.