登录
首页 >  Golang >  Go问答

如何将 `tf.Apply()` 与 `hashicorp / terraform-exec` 中的 `ApplyConfig` 进行关联?

来源:stackoverflow

时间:2024-02-11 18:57:26 452浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《如何将 `tf.Apply()` 与 `hashicorp / terraform-exec` 中的 `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)
    }

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>