0

我正在使用 Terraform GitHub 提供程序为内部 GitHub Enterprise 实例定义 GitHub 存储库(尽管问题不是特定于提供程序的)。

现有github_repository资源工作正常,但我希望能够为其某些参数设置非标准默认值,并轻松地将其他参数分组到一个新参数下。

例如

  • github_repositoryprivate值默认为,false但我想默认为true
  • 许多 repos 只希望允许 squash 合并,所以有一个squash_merge_only参数来设置allow_squash_merge = true, allow_rebase_merge = false, allow_merge_commit = false

还有更多的案例,但这些都说明了这一点。目的是让人们更容易正确地配置新的 repos,并避免在每个 repo 中重复大量的配置。

我可以通过将变量传递到自定义模块来实现这一点,例如:

Foo/custom_repo/main.tf

resource "github_repository" "custom_repo" {
  name                 = ${var.repo_name}
  private              = true
  allow_squash_merge   = true
  allow_merge_commit   = ${var.squash_merge_only ? false : true}
  allow_rebase_merge   = ${var.squash_merge_only ? false : true}
} 

Foo/main.tf

provider "github" {
 ...
}

module "MyRepo_module" {
  source            = "./custom_repo"
  repo_name         = "MyRepo"
  squash_merge_only = true
}

不过这有点垃圾,因为我必须为github_repository使用该custom_repo模块的人可能想要设置的每个其他参数添加一个变量(这基本上是所有这些 - 我不是试图限制人们被允许做的事情)name-参见repo_name示例。然后这一切都需要单独记录,鉴于现有提供者有很好的文档,这也是一种耻辱。

有没有更好的模式来重用这样的现有资源,但可以控制参数如何传递给它们?

4

1 回答 1

3

我们在https://github.com/mineiros-io/terraform-github-repository为此创建了一个自以为是的模块(terraform 0.12+)

我们将所有默认值设置为我们认为最合适的值,但基本上您可以创建一组本地默认值并在多次调用模块时重用它们。

有趣的事实......您想要的默认值已经是模块的默认值,但是要清楚如何在此处明确设置这些是一个示例(未经测试):

locals {
  my_defaults = {
     # actually already the modules default to create private repositories
     private = true

     # also the modules default already and 
     # all other strategies are disabled by default
     allow_squash_merge = true
  }
}


module "repository" {
  source  = "mineiros-io/repository/github"
  version = "0.4.0"

  name = "my_new_repository"

  defaults = local.my_defaults
}

并非所有参数都被支持为默认值,但大多数参数是:https ://github.com/mineiros-io/terraform-github-repository#defaults-object-attributes

于 2020-05-31T00:55:35.377 回答