首页  >  文章  >  后端开发  >  如何将 `ApplyConfig` 传递给 `hashicorp / terraform-exec` 中的 `tf.Apply()`?

如何将 `ApplyConfig` 传递给 `hashicorp / terraform-exec` 中的 `tf.Apply()`?

王林
王林转载
2024-02-09 10:50:29408浏览

如何将 `ApplyConfig` 传递给 `hashicorp / terraform-exec` 中的 `tf.Apply()`?

php小编柚子为您解答如何将 `ApplyConfig` 传递给 `hashicorp / terraform-exec` 中的 `tf.Apply()`。在使用 `hashicorp / terraform-exec` 进行 Terraform 部署时,可以通过创建一个 `ApplyConfig` 对象来配置 `tf.Apply()` 的行为。然后,将该对象传递给 `tf.Apply()` 方法,以便进行相应的部署操作。通过这种方式,您可以灵活地控制部署过程,并实现自定义的配置需求。在实际应用中,您可以根据具体的业务需求来设置 `ApplyConfig` 对象的属性,以达到最佳的部署效果。

问题内容

我正在尝试使用 golang sdk 在 hashicorp/terraform-exec 中使用 golang sdk 将目标添加到 terraform apply 命令中

理想情况下,cli 的等效命令是 terraform apply --auto-approve --target 'module.example'

但是,当我将 applyoptions{} 中的 targets 传递给 apply() 函数时,出现以下错误。

有人能指出我在这里做什么吗?

package main

import (
    "context"

    "github.com/hashicorp/terraform-exec/tfexec"
)

func main() {
    // create a new tfexec.executor instance
    tf, err := tfexec.newterraform("/path/to/terraform/binary")
    if err != nil {
        panic(err)
    }
    err = tf.init(context.background(), tfexec.upgrade(true))
    if err != nil {
        panic(err)
    }
    // define the targets you want to apply
    targets := []string{"module.example", "module.another_example"}

    // create an applyoption with the targets
    applyoption := tfexec.applyoption{
        targets: targets,
    }

    // apply the terraform configuration with the defined targets
    err = tf.apply(context.background(), applyoption)
    if err != nil {
        panic(err)
    }
}

错误显示,invalid 复合文字类型 tfexec.applyoptioncompiler

go run test.go # command-line-arguments ./test.go:23:17: invalid composite literal type tfexec.ApplyOption

解决方法

我认为以下应该有效:

targets := []tfexec.ApplyOption{
        tfexec.Target("module.example"),
        tfexec.Target("module.another_example"),
    }

    // Apply the Terraform configuration with the defined targets
    err = tf.Apply(context.Background(), targets...)
    if err != nil {
        panic(err)
    }

以上是如何将 `ApplyConfig` 传递给 `hashicorp / terraform-exec` 中的 `tf.Apply()`?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除