# Retrieve action apply/plan/destroy/refresh from plugin

**URL:** <https://discuss.hashicorp.com/t/retrieve-action-apply-plan-destroy-refresh-from-plugin/28170>\
**Category:** Plugin Development\
**Created:** [August 12, 2021, 11:13pm UTC](https://discuss.hashicorp.com/t/retrieve-action-apply-plan-destroy-refresh-from-plugin/28170 "2021-08-12T23:13:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tfmenard](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tfmenard/32/21544_2.png) [@Tfmenard](https://discuss.hashicorp.com/u/Tfmenard)\
**Post date:** [August 12, 2021, 11:13pm UTC](https://discuss.hashicorp.com/t/retrieve-action-apply-plan-destroy-refresh-from-plugin/28170/1 "2021-08-12T23:13:46Z")

</div>

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

> <https://github.com/hashicorp/terraform/blob/f200c5d0ef69563be6123cb9fee0849ea204d143/main.go#L146>

Output from plan:  
`[INFO] CLI args: []string{"/usr/local/bin/terraform", "plan"}`

And here too

> <https://github.com/hashicorp/terraform/blob/f200c5d0ef69563be6123cb9fee0849ea204d143/main.go#L340>

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.

---

<div class="post-metadata">

**Author:** ![paddy](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/paddy/32/4484_2.png) [@paddy](https://discuss.hashicorp.com/u/paddy)\
**Post date:** [August 16, 2021, 2:47pm UTC](https://discuss.hashicorp.com/t/retrieve-action-apply-plan-destroy-refresh-from-plugin/28170/2 "2021-08-16T14:47:35Z")

</div>

This is not currently possible. The [terraform issue tracker](https://github.com/hashicorp/terraform/issues/new/choose) is probably the right place to request this. 🙂

---

<div class="post-metadata">

**Author:** ![Tfmenard](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tfmenard/32/21544_2.png) [@Tfmenard](https://discuss.hashicorp.com/u/Tfmenard)\
**Post date:** [August 16, 2021, 3:50pm UTC](https://discuss.hashicorp.com/t/retrieve-action-apply-plan-destroy-refresh-from-plugin/28170/3 "2021-08-16T15:50:32Z")

</div>

Thanks @paddy  
Just made the FR and as mentioned in the FR I found a quick solution for this.

> <https://github.com/hashicorp/terraform/issues/29384>
>
> \### 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
