目录
问题内容
解决方法
首页 后端开发 Golang 如何将 `ApplyConfig` 传递给 `hashicorp / terraform-exec` 中的 `tf.Apply()`?

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

Feb 09, 2024 am 10:50 AM

如何将 `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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Go语言包导入:带下划线和不带下划线的区别是什么? Go语言包导入:带下划线和不带下划线的区别是什么? Mar 03, 2025 pm 05:17 PM

Go语言包导入:带下划线和不带下划线的区别是什么?

Beego框架中NewFlash()函数如何实现页面间短暂信息传递? Beego框架中NewFlash()函数如何实现页面间短暂信息传递? Mar 03, 2025 pm 05:22 PM

Beego框架中NewFlash()函数如何实现页面间短暂信息传递?

如何编写模拟对象和存根以进行测试? 如何编写模拟对象和存根以进行测试? Mar 10, 2025 pm 05:38 PM

如何编写模拟对象和存根以进行测试?

如何使用跟踪工具了解GO应用程序的执行流? 如何使用跟踪工具了解GO应用程序的执行流? Mar 10, 2025 pm 05:36 PM

如何使用跟踪工具了解GO应用程序的执行流?

Go语言中如何将MySQL查询结果List转换为自定义结构体切片? Go语言中如何将MySQL查询结果List转换为自定义结构体切片? Mar 03, 2025 pm 05:18 PM

Go语言中如何将MySQL查询结果List转换为自定义结构体切片?

如何定义GO中仿制药的自定义类型约束? 如何定义GO中仿制药的自定义类型约束? Mar 10, 2025 pm 03:20 PM

如何定义GO中仿制药的自定义类型约束?

Go语言如何便捷地写入文件? Go语言如何便捷地写入文件? Mar 03, 2025 pm 05:15 PM

Go语言如何便捷地写入文件?

如何编写基准,以准确反映GO中的现实世界表现? 如何编写基准,以准确反映GO中的现实世界表现? Mar 10, 2025 pm 05:36 PM

如何编写基准,以准确反映GO中的现实世界表现?

See all articles