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

如何将 `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 mainimport (    "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()`?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2357693.html

(0)
上一篇 2025年3月1日 16:35:02
下一篇 2025年3月1日 16:35:22

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论