opentofu/internal/command/output.go
Brandon Croft c33c8b013f
fix: have terraform output adhere to authorization w/ cloud
Normally, `terraform output` refreshes and reads the entire state in the command package before pulling output values out of it. This doesn't give Terraform Cloud the opportunity to apply the read state outputs org permission and instead applies the read state versions permission.

I decided to expand the state manager interface to provide a separate GetRootOutputValues function in order to give the cloud backend a more nuanced opportunity to fetch just the outputs. This required moving state Refresh/Read code that was previously in the command into the shared backend state as well as the filesystem state packages.
2022-07-25 10:04:43 -06:00

124 lines
3.0 KiB
Go

package command
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/command/views"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/tfdiags"
)
// OutputCommand is a Command implementation that reads an output
// from a Terraform state and prints it.
type OutputCommand struct {
Meta
}
func (c *OutputCommand) Run(rawArgs []string) int {
// Parse and apply global view arguments
common, rawArgs := arguments.ParseView(rawArgs)
c.View.Configure(common)
// Parse and validate flags
args, diags := arguments.ParseOutput(rawArgs)
if diags.HasErrors() {
c.View.Diagnostics(diags)
c.View.HelpPrompt("output")
return 1
}
view := views.NewOutput(args.ViewType, c.View)
// Fetch data from state
outputs, diags := c.Outputs(args.StatePath)
if diags.HasErrors() {
view.Diagnostics(diags)
return 1
}
// Render the view
viewDiags := view.Output(args.Name, outputs)
diags = diags.Append(viewDiags)
view.Diagnostics(diags)
if diags.HasErrors() {
return 1
}
return 0
}
func (c *OutputCommand) Outputs(statePath string) (map[string]*states.OutputValue, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
// Allow state path override
if statePath != "" {
c.Meta.statePath = statePath
}
// Load the backend
b, backendDiags := c.Backend(nil)
diags = diags.Append(backendDiags)
if diags.HasErrors() {
return nil, diags
}
// This is a read-only command
c.ignoreRemoteVersionConflict(b)
env, err := c.Workspace()
if err != nil {
diags = diags.Append(fmt.Errorf("Error selecting workspace: %s", err))
return nil, diags
}
// Get the state
stateStore, err := b.StateMgr(env)
if err != nil {
diags = diags.Append(fmt.Errorf("Failed to load state: %s", err))
return nil, diags
}
output, err := stateStore.GetRootOutputValues()
if err != nil {
return nil, diags.Append(err)
}
return output, diags
}
func (c *OutputCommand) Help() string {
helpText := `
Usage: terraform [global options] output [options] [NAME]
Reads an output variable from a Terraform state file and prints
the value. With no additional arguments, output will display all
the outputs for the root module. If NAME is not specified, all
outputs are printed.
Options:
-state=path Path to the state file to read. Defaults to
"terraform.tfstate". Ignored when remote
state is used.
-no-color If specified, output won't contain any color.
-json If specified, machine readable output will be
printed in JSON format.
-raw For value types that can be automatically
converted to a string, will print the raw
string directly, rather than a human-oriented
representation of the value.
`
return strings.TrimSpace(helpText)
}
func (c *OutputCommand) Synopsis() string {
return "Show output values from your root module"
}