简体   繁体   English

在 terraform 提供程序中应用应用结果的回调

[英]callback with results of apply in terraform provider

I'm writing a custom terraform provider.我正在编写一个自定义 terraform 提供程序。 I need to wrap the calls to the backed API in a "session", opening it before any calls are made and then closing it once all the terraform calls have completed.我需要将对支持 API 的调用包装在“会话”中,在进行任何调用之前打开它,然后在所有 terraform 调用完成后关闭它。

Opening the session is straightforward, I can do that in the ConfigureContextFunc of the schema.Provider.打开会话很简单,我可以在 schema.Provider 的ConfigureContextFunc中做到这一点。 Is there a way to set up a callback (or something) at the end of the application so I can close/"finalize" the session?有没有办法在应用程序结束时设置回调(或其他东西),以便我可以关闭/“完成”会话? I can imagine something specific to my resources, but that seems hacky.我可以想象一些特定于我的资源的东西,但这似乎很老套。 In my dream world I'd also be able to fail the apply if the close had an error.在我的梦想世界中,如果关闭有错误,我也可以申请失败。

Absent a nice finalize call is there a way to access the plan that I could use to determine that the current call is the last needed for the apply?如果没有一个很好的 finalize 调用,是否有一种方法可以访问我可以用来确定当前调用是应用的最后一次调用的计划?

Update: I thought I could use a StopContext:更新:我想我可以使用 StopContext:

stopCtx, ok := schema.StopContext(ctx)
    ...
    go func(ctx context.Context) {
        // Wait for stop context cancellation
        <-stopCtx.Done()
        ...
    }

However, this is both deprecated and seems to only get called when stopping due to some outside trigger, like SIGINT, and not a regular exit (at least that's what I've been seeing).但是,这已被弃用,并且似乎仅在由于某些外部触发器(例如 SIGINT)而停止时才被调用,而不是常规退出(至少这是我所看到的)。

After a fair amount of floundering I have what I believe to be a reasonable solution, and it even matches @Ben Hoyt's comment.经过相当多的挣扎后,我有了我认为合理的解决方案,它甚至与@Ben Hoyt 的评论相匹配。 We can defer and teardown in main.我们可以在 main 中延迟和拆卸。


func main() {
    var prov *schema.Provider
    defer func() {
        xyz.PrividerTeardown(prov)
    }()
    plugin.Serve(&plugin.ServeOpts{
        ProviderFunc: func() *schema.Provider {
            prov = xyz.Provider()
            return prov
        },
    })
}

I'll note that my comment on the question about not being around when the provider is made is incorrect.我会注意到,我对关于在提供提供者时不在身边的问题的评论是不正确的。 The provider is made in a main function in the provider code.提供程序是在提供程序代码中的主要功能中制作的。 In my (flimsy) defense I used the example scaffolding so that code came pre-written.在我(脆弱的)辩护中,我使用了示例脚手架,以便预先编写代码。

One thing that threw me was the docs for plugin.Serve让我震惊的一件事是plugin.Serve的文档

Serve serves a plugin.服务提供一个插件。 This function never returns and should be the final function called in the main function of the plugin.这个函数永远不会返回,应该是插件主函数中调用的最终函数。

It turns out that when the terraform action is done and the plugin is no longer needed, Serve does return and allows our defer to run.事实证明,当 terraform 操作完成并且不再需要插件时,Serve 确实返回并允许我们的 defer 运行。 I did need to keep track of success or failure of all the calls that were made while Serve was active to know the status in my ProviderTeardown, but it is working.我确实需要跟踪在 Serve 处于活动状态时进行的所有调用的成功或失败,以了解我的 ProviderTeardown 中的状态,但它正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM