|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "os/exec" |
| 11 | + "runtime" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 18 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + _ datasource.DataSource = (*localCommandDataSource)(nil) |
| 23 | +) |
| 24 | + |
| 25 | +func NewLocalCommandDataSource() datasource.DataSource { |
| 26 | + return &localCommandDataSource{} |
| 27 | +} |
| 28 | + |
| 29 | +type localCommandDataSource struct{} |
| 30 | + |
| 31 | +func (a *localCommandDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 32 | + resp.TypeName = req.ProviderTypeName + "_command" |
| 33 | +} |
| 34 | + |
| 35 | +func (a *localCommandDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 36 | + resp.Schema = schema.Schema{ |
| 37 | + MarkdownDescription: "Runs an executable on the local machine and returns the exit code, standard output data (`stdout`), and standard error data (`stderr`). " + |
| 38 | + "All environment variables visible to the Terraform process are passed through to the child process. Both `stdout` and `stderr` returned by this data source " + |
| 39 | + "are UTF-8 strings, which can be decoded into [Terraform values](https://developer.hashicorp.com/terraform/language/expressions/types) for use elsewhere in the Terraform configuration. " + |
| 40 | + "There are built-in decoding functions such as [`jsondecode`](https://developer.hashicorp.com/terraform/language/functions/jsondecode) or [`yamldecode`](https://developer.hashicorp.com/terraform/language/functions/yamldecode), " + |
| 41 | + "and more specialized [decoding functions](https://developer.hashicorp.com/terraform/plugin/framework/functions/concepts) can be built with a Terraform provider." + |
| 42 | + "\n\n" + |
| 43 | + "Any non-zero exit code returned by the command will be treated as an error and will return a diagnostic to Terraform containing the `stderr` message if available. " + |
| 44 | + "If a non-zero exit code is expected by the command, set `allow_non_zero_exit_code` to `true`." + |
| 45 | + "\n\n" + |
| 46 | + "~> **Warning** This mechanism is provided as an \"escape hatch\" for exceptional situations where a first-class Terraform provider is not more appropriate. " + |
| 47 | + "Its capabilities are limited in comparison to a true data source, and implementing a data source via a local executable is likely to hurt the " + |
| 48 | + "portability of your Terraform configuration by creating dependencies on external programs and libraries that may not be available (or may need to be used differently) " + |
| 49 | + "on different operating systems." + |
| 50 | + "\n\n" + |
| 51 | + "~> **Warning** HCP Terraform and Terraform Enterprise do not guarantee availability of any particular language runtimes or external programs beyond standard shell utilities, " + |
| 52 | + "so it is not recommended to use this data source within configurations that are applied within either.", |
| 53 | + Attributes: map[string]schema.Attribute{ |
| 54 | + "command": schema.StringAttribute{ |
| 55 | + Description: "Executable name to be discovered on the PATH or absolute path to executable.", |
| 56 | + Required: true, |
| 57 | + }, |
| 58 | + "arguments": schema.ListAttribute{ |
| 59 | + MarkdownDescription: "Arguments to be passed to the given command. Any `null` arguments will be removed from the list.", |
| 60 | + ElementType: types.StringType, |
| 61 | + Optional: true, |
| 62 | + }, |
| 63 | + "stdin": schema.StringAttribute{ |
| 64 | + MarkdownDescription: "Data to be passed to the given command's standard input as a UTF-8 string. [Terraform values](https://developer.hashicorp.com/terraform/language/expressions/types) can be encoded " + |
| 65 | + "by any Terraform encode function, for example, [`jsonencode`](https://developer.hashicorp.com/terraform/language/functions/jsonencode).", |
| 66 | + Optional: true, |
| 67 | + }, |
| 68 | + "working_directory": schema.StringAttribute{ |
| 69 | + Description: "The directory path where the command should be executed, either an absolute path or relative to the Terraform working directory. If not provided, defaults to the Terraform working directory.", |
| 70 | + Optional: true, |
| 71 | + }, |
| 72 | + "allow_non_zero_exit_code": schema.BoolAttribute{ |
| 73 | + MarkdownDescription: "Indicates that the command returning a non-zero exit code should be treated as a successful execution. " + |
| 74 | + "Further assertions can be made of the `exit_code` value with the [`check` block](https://developer.hashicorp.com/terraform/language/block/check). Defaults to false.", |
| 75 | + Optional: true, |
| 76 | + }, |
| 77 | + "exit_code": schema.Int64Attribute{ |
| 78 | + MarkdownDescription: "The exit code returned by the command. By default, if the exit code is non-zero, the data source will return a diagnostic to Terraform. " + |
| 79 | + "If a non-zero exit code is expected by the command, set `allow_non_zero_exit_code` to `true`.", |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "stdout": schema.StringAttribute{ |
| 83 | + MarkdownDescription: "Data returned from the command's standard output stream. The data is returned directly from the command as a UTF-8 string, " + |
| 84 | + "which can then be decoded by any Terraform decode function, for example, [`jsondecode`](https://developer.hashicorp.com/terraform/language/functions/jsondecode).", |
| 85 | + Computed: true, |
| 86 | + }, |
| 87 | + "stderr": schema.StringAttribute{ |
| 88 | + Description: "Data returned from the command's standard error stream. The data is returned directly from the command as a UTF-8 string and will be " + |
| 89 | + "populated regardless of the exit code returned.", |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +type localCommandDataSourceModel struct { |
| 97 | + Command types.String `tfsdk:"command"` |
| 98 | + Arguments types.List `tfsdk:"arguments"` |
| 99 | + Stdin types.String `tfsdk:"stdin"` |
| 100 | + WorkingDirectory types.String `tfsdk:"working_directory"` |
| 101 | + AllowNonZeroExitCode types.Bool `tfsdk:"allow_non_zero_exit_code"` |
| 102 | + ExitCode types.Int64 `tfsdk:"exit_code"` |
| 103 | + Stdout types.String `tfsdk:"stdout"` |
| 104 | + Stderr types.String `tfsdk:"stderr"` |
| 105 | +} |
| 106 | + |
| 107 | +func (a *localCommandDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 108 | + var state localCommandDataSourceModel |
| 109 | + resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) |
| 110 | + if resp.Diagnostics.HasError() { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + // Prep the command |
| 115 | + command := state.Command.ValueString() |
| 116 | + if _, err := exec.LookPath(command); err != nil { |
| 117 | + resp.Diagnostics.AddAttributeError( |
| 118 | + path.Root("command"), |
| 119 | + "Command Lookup Failed", |
| 120 | + "The data source received an unexpected error while attempting to find the command."+ |
| 121 | + "\n\n"+ |
| 122 | + "The command must be accessible according to the platform where Terraform is running."+ |
| 123 | + "\n\n"+ |
| 124 | + "If the expected command should be automatically found on the platform where Terraform is running, "+ |
| 125 | + "ensure that the command is in an expected directory. On Unix-based platforms, these directories are "+ |
| 126 | + "typically searched based on the '$PATH' environment variable. On Windows-based platforms, these directories "+ |
| 127 | + "are typically searched based on the '%PATH%' environment variable."+ |
| 128 | + "\n\n"+ |
| 129 | + "If the expected command is relative to the Terraform configuration, it is recommended that the command name includes "+ |
| 130 | + "the interpolated value of 'path.module' before the command name to ensure that it is compatible with varying module usage. For example: \"${path.module}/my-command\""+ |
| 131 | + "\n\n"+ |
| 132 | + "The command must also be executable according to the platform where Terraform is running. On Unix-based platforms, the file on the filesystem must have the executable bit set. "+ |
| 133 | + "On Windows-based platforms, no action is typically necessary."+ |
| 134 | + "\n\n"+ |
| 135 | + fmt.Sprintf("Platform: %s\n", runtime.GOOS)+ |
| 136 | + fmt.Sprintf("Command: %s\n", command)+ |
| 137 | + fmt.Sprintf("Error: %s", err), |
| 138 | + ) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + arguments := make([]string, 0) |
| 143 | + for _, element := range state.Arguments.Elements() { |
| 144 | + strElement, ok := element.(types.String) |
| 145 | + // Mirroring the underlying os/exec Command support for args (no nil arguments, but does support empty strings) |
| 146 | + if element.IsNull() || !ok { |
| 147 | + continue |
| 148 | + } |
| 149 | + |
| 150 | + arguments = append(arguments, strElement.ValueString()) |
| 151 | + } |
| 152 | + |
| 153 | + cmd := exec.CommandContext(ctx, command, arguments...) |
| 154 | + |
| 155 | + cmd.Dir = state.WorkingDirectory.ValueString() |
| 156 | + |
| 157 | + if !state.Stdin.IsNull() { |
| 158 | + cmd.Stdin = bytes.NewReader([]byte(state.Stdin.ValueString())) |
| 159 | + } |
| 160 | + |
| 161 | + var stderr strings.Builder |
| 162 | + cmd.Stderr = &stderr |
| 163 | + var stdout strings.Builder |
| 164 | + cmd.Stdout = &stdout |
| 165 | + |
| 166 | + tflog.Trace(ctx, "Executing local command", map[string]interface{}{"command": cmd.String()}) |
| 167 | + |
| 168 | + // Run the command |
| 169 | + commandErr := cmd.Run() |
| 170 | + stdoutStr := stdout.String() |
| 171 | + stderrStr := stderr.String() |
| 172 | + |
| 173 | + if len(stderrStr) > 0 { |
| 174 | + state.Stderr = types.StringValue(stderrStr) |
| 175 | + } |
| 176 | + |
| 177 | + if len(stdoutStr) > 0 { |
| 178 | + state.Stdout = types.StringValue(stdoutStr) |
| 179 | + } |
| 180 | + |
| 181 | + // ProcessState will always be populated if the command has been was successfully started (regardless of exit code) |
| 182 | + if cmd.ProcessState != nil { |
| 183 | + exitCode := cmd.ProcessState.ExitCode() |
| 184 | + state.ExitCode = types.Int64Value(int64(exitCode)) |
| 185 | + } |
| 186 | + |
| 187 | + tflog.Trace(ctx, "Executed local command", map[string]interface{}{"command": cmd.String(), "stdout": stdoutStr, "stderr": stderrStr}) |
| 188 | + |
| 189 | + // Set all of the data to state |
| 190 | + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) |
| 191 | + if commandErr == nil { |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + // If running the command returned an exit error, we need to check and see if we should explicitly raise a diagnostic |
| 196 | + if exitError, ok := commandErr.(*exec.ExitError); ok { |
| 197 | + // We won't return a diagnostic because the command was successfully started and then exited |
| 198 | + // with a non-zero exit code (which the user has indicated they will handle in configuration). |
| 199 | + // |
| 200 | + // All data has already been saved to state, so we just return. |
| 201 | + if state.AllowNonZeroExitCode.ValueBool() { |
| 202 | + return |
| 203 | + } |
| 204 | + |
| 205 | + resp.Diagnostics.AddAttributeError( |
| 206 | + path.Root("command"), |
| 207 | + "Command Execution Failed", |
| 208 | + "The data source executed the command but received a non-zero exit code. If a non-zero exit code is expected "+ |
| 209 | + "and can be handled in configuration, set \"allow_non_zero_exit_code\" to true."+ |
| 210 | + "\n\n"+ |
| 211 | + fmt.Sprintf("Command: %s\n", cmd.String())+ |
| 212 | + fmt.Sprintf("Command Error: %s\n", stderrStr)+ |
| 213 | + fmt.Sprintf("State: %s", exitError), |
| 214 | + ) |
| 215 | + return |
| 216 | + } |
| 217 | + |
| 218 | + // We need to raise a diagnostic because the command wasn't successfully started and we have no exit code. |
| 219 | + resp.Diagnostics.AddAttributeError( |
| 220 | + path.Root("command"), |
| 221 | + "Command Execution Failed", |
| 222 | + "The data source received an unexpected error while attempting to execute the command."+ |
| 223 | + "\n\n"+ |
| 224 | + fmt.Sprintf("Command: %s\n", cmd.String())+ |
| 225 | + fmt.Sprintf("State: %s", commandErr), |
| 226 | + ) |
| 227 | +} |
0 commit comments