首页 > 后端开发 > Golang > 正文

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

王林
发布: 2024-02-09 10:50:29
转载
408 人浏览过

如何将 `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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!