Is it possible to fetch the action being performed (apply/plan/destroy/refresh) from the plugin SDK or any other way so I can use it in a provider code?
I was looking at this part of terraform code and saw the action is retrieve from the CLI args
logging.RegisterSink(f)
} else {
log.Printf("[ERROR] Could not open temp log file: %v", err)
}
}
log.Printf(
"[INFO] Terraform version: %s %s",
Version, VersionPrerelease)
log.Printf("[INFO] Go runtime version: %s", runtime.Version())
log.Printf("[INFO] CLI args: %#v", os.Args)
// This is the recieving end of our workaround to retain the metadata
// about the real stderr even though we're talking to it via the panicwrap
// pipe. See the call to StateForAfterPanicWrap above for the producer
// part of this.
var streamState *terminal.PrePanicwrapState
if raw := os.Getenv(envTerminalPanicwrapWorkaround); raw != "" {
streamState = &terminal.PrePanicwrapState{}
if _, err := fmt.Sscanf(raw, "%t:%d", &streamState.StderrIsTerminal, &streamState.StderrWidth); err != nil {
log.Printf("[WARN] %s is set but is incorrectly-formatted: %s", envTerminalPanicwrapWorkaround, err)
Output from plan:
[INFO] CLI args: []string{"/usr/local/bin/terraform", "plan"}
And here too
if arg == "-v" || arg == "-version" || arg == "--version" {
newArgs := make([]string, len(args)+1)
newArgs[0] = "version"
copy(newArgs[1:], args)
args = newArgs
break
}
}
// Rebuild the CLI with any modified args.
log.Printf("[INFO] CLI command args: %#v", args)
cliRunner = &cli.CLI{
Name: binName,
Args: args,
Commands: Commands,
HelpFunc: helpFunc,
HelpWriter: os.Stdout,
Autocomplete: true,
AutocompleteInstall: "install-autocomplete",
AutocompleteUninstall: "uninstall-autocomplete",
Output
[INFO] CLI command args: []string{"plan"}
However if I try to print it os.Args from a provider code I get an empty a string array
log.Printf("[INFO] MORE_CLI_ARGS = %#v", os.Args[1:])
Output
[INFO] MORE_CLI_ARGS = []string{}
Is it possible to retrieve the command line args that were used from somewhere (undocumented env var or what not)?
Is there a FR already open for this? If not I can make one.
paddy
August 16, 2021, 2:47pm
2
This is not currently possible. The terraform issue tracker is probably the right place to request this.
Thanks @paddy
Just made the FR and as mentioned in the FR I found a quick solution for this.
opened 03:43PM - 16 Aug 21 UTC
enhancement
new
### Current Terraform Version
```
Terraform v1.0.4 on linux amd64
```
##… # Use-cases
As a provider developer I would like to be able to retrieve what verb is being run by terraform from the provider/plugin process.
This can enable custom actions on reads for example (plan vs refresh).
This can also enable tracking of state transitions and actions performed from outside of just TF logs or build pipelines.
For example, provider developers could send a custom to a logging system that gives:
- the verb (apply/refresh/plan/destroy)
- command start time, command end time. Successful execution
Finally, provider developers could also embed the verb information in some request header or just enhance the user-agent to help with tracking and debugging purposes.
The last 2 use cases can be very useful to debug perma diffs and repro work as we could align state transition with provider API changes for example.
### Attempted Solutions
I was able to make a couple that retrive the command line arguments
https://github.com/shirou/gopsutil/blob/v3.21.7/process/process.go#L25
```
processes, err := gops.Processes()
if err != nil {
return err
}
for _, p := range processes {
processName, err := p.Name()
if err != nil {
return err
}
if processName == "terraform" {
log.Printf("[INFO] found terraform process")
cmdLine, err := p.Cmdline()
if err != nil {
return err
}
log.Printf("[INFO] CmdArgs: %v", cmdLine)
}
log.Printf("[INFO] process name: %v", processName)
}
```
And it works.
### Proposal
But I don't think provider developer should have to develop their own code to get execution/verb contexts from terraform.
I think this should be part of the SDK. Fetching verbs is just for my use cases, but there might be more info that could be fetched by providers here.
I'm pretty new to plugin SDK but I think a new RPC is needed here.
### References
I asked for this FR here
https://discuss.hashicorp.com/t/retrieve-action-apply-plan-destroy-refresh-from-plugin/28170?u=tfmenard