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

huangapple go评论112阅读模式
英文:

How to pass `ApplyConfig` to `tf.Apply()` in `hashicorp / terraform-exec`?

问题

我正在尝试使用 Terraform 的 golang sdk(terraform-exec)为 terraform apply 命令添加一个 target。理想情况下,等效的 CLI 命令是 terraform apply --auto-approve --target 'module.example'。但是,当我将 targets 传递给 ApplyOptions{} 中的 Apply() 函数时,我遇到了以下错误。有人可以指出我在这里做错了什么吗?

错误信息是 invalid composite literal type tfexec.ApplyOptioncompiler

  1. package main
  2. import (
  3. "context"
  4. "github.com/hashicorp/terraform-exec/tfexec"
  5. )
  6. func main() {
  7. // 创建一个新的 tfexec.Executor 实例
  8. tf, err := tfexec.NewTerraform("/path/to/terraform/binary")
  9. if err != nil {
  10. panic(err)
  11. }
  12. err = tf.Init(context.Background(), tfexec.Upgrade(true))
  13. if err != nil {
  14. panic(err)
  15. }
  16. // 定义要应用的目标
  17. targets := []string{"module.example", "module.another_example"}
  18. // 创建一个包含目标的 ApplyOption
  19. applyOption := tfexec.ApplyOption{
  20. Targets: targets,
  21. }
  22. // 使用定义的目标应用 Terraform 配置
  23. err = tf.Apply(context.Background(), applyOption)
  24. if err != nil {
  25. panic(err)
  26. }
  27. }

错误提示是 go run test.go # command-line-arguments ./test.go:23:17: invalid composite literal type tfexec.ApplyOption

英文:

I am trying to add a target to terraform apply command using golang sdk for terraform in hashicorp/terraform-exec
Ideally, the equivalent command for CLI is terraform apply --auto-approve --target 'module.example'
But I am getting the following error when I pass the targets in ApplyOptions{} to the Apply() function.
Can anybody point me what I am doing wring here?

  1. package main
  2. import (
  3. "context"
  4. "github.com/hashicorp/terraform-exec/tfexec"
  5. )
  6. func main() {
  7. // Create a new tfexec.Executor instance
  8. tf, err := tfexec.NewTerraform("/path/to/terraform/binary")
  9. if err != nil {
  10. panic(err)
  11. }
  12. err = tf.Init(context.Background(), tfexec.Upgrade(true))
  13. if err != nil {
  14. panic(err)
  15. }
  16. // Define the targets you want to apply
  17. targets := []string{"module.example", "module.another_example"}
  18. // Create an ApplyOption with the targets
  19. applyOption := tfexec.ApplyOption{
  20. Targets: targets,
  21. }
  22. // Apply the Terraform configuration with the defined targets
  23. err = tf.Apply(context.Background(), applyOption)
  24. if err != nil {
  25. panic(err)
  26. }
  27. }

The error says, invalid composite literal type tfexec.ApplyOptioncompiler

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

答案1

得分: 0

我认为以下代码应该可以工作:

  1. targets := []tfexec.ApplyOption{
  2. tfexec.Target("module.example"),
  3. tfexec.Target("module.another_example"),
  4. }
  5. // 使用定义的目标应用 Terraform 配置
  6. err = tf.Apply(context.Background(), targets...)
  7. if err != nil {
  8. panic(err)
  9. }
英文:

I think the following should work:

  1. targets := []tfexec.ApplyOption{
  2. tfexec.Target("module.example"),
  3. tfexec.Target("module.another_example"),
  4. }
  5. // Apply the Terraform configuration with the defined targets
  6. err = tf.Apply(context.Background(), targets...)
  7. if err != nil {
  8. panic(err)
  9. }

huangapple
  • 本文由 发表于 2023年3月29日 15:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75874329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定