简体   繁体   English

如何在 for_each 中的字符串列表上添加额外的循环

[英]how to add an additional loop over a stringlist within a for_each

i've setted up multiple github repos within my.tf configuration with a simple for_each on "github" repository resource.我已经在 my.tf 配置中设置了多个 github 存储库,并在“github”存储库资源上使用了一个简单的 for_each。 In addition i tried to create several branches (a branch per env) for each newly created repo.此外,我尝试为每个新创建的回购创建几个分支(每个环境一个分支)。

My first intention was to use a module (./modules/github_repo/repo.tf) which includes我的第一个意图是使用一个模块(./modules/github_repo/repo.tf),其中包括

    locals {
      environments = var.environments
    }

    resource "github_branch" "branches" {
      for_each      = toset(setsubtract(local.environments, ["prod"]))
      repository    = "orgName/${var.REPO_NAME}"
      branch        = lookup(var.environment_to_branch_map, each.key, each.key)
      source_branch = "master" 
    }

with following variables具有以下变量

    variable "REPO_NAME" {
      type = string
    }
    
    variable "environments" {
      type    = list(string)
    }

    variable "environment_to_branch_map" {
      type = map(any)
      default = {
        "prod" = "master"
        "dev"  = "develop"
      }

calling like this from main.tf从 main.tf 这样调用


    provider "github" {
      token = var.GITHUB_TOKEN
      owner = "orgName"
    }    

    locals {
      environments = ["dev", "prod", "staging", "test"]
      microServices  = tomap({ "service1" : "fnApp", "service2" : "fnApp" })
      default_branch = "master"
    }

    module "branches_per_microservice" {
      for_each     = local.microServices
      source       = "./modules/github_repo"
      REPO_NAME    = each.key
      environments = local.environments
      depends_on   = [github_repository.microservices]
    } 

unfortunately i get an 404 for each branch and repo combination like this不幸的是,我为每个分支和回购组合得到一个 404,就像这样

Error: Error querying GitHub branch reference /orgName/service1 (refs/heads/master): GET https://api.github.com/repos//orgName/service1/git/ref/heads/master : 404 Not Found [] with module.branches_per_microservice["service1"].github_branch.branches["test"] on modules/github_repo/repo.tf line 23, in resource "github_branch" "branches":错误:查询 GitHub 分支参考 /orgName/service1 (refs/heads/master) 时出错:GET https://api.github.com/repos//orgName/service1/git/ref/heads/master : 404 Not Found []使用 module.branches_per_microservice["service1"].github_branch.branches["test"] 在 modules/github_repo/repo.tf 第 23 行,在资源“github_branch”“branches”中:

i guess it's a "provider" thing, cause if i try to create a branch directly in the main.tf, it will work.我想这是一个“提供者”的东西,因为如果我尝试直接在 main.tf 中创建一个分支,它就会起作用。 but the problem is, that i only can use one loop within a resource.但问题是,我只能在一个资源中使用一个循环。 (i already know that providers are not possible in modules with count or for_each loops, as written in terraform docs) (我已经知道在带有 count 或 for_each 循环的模块中提供程序是不可能的,如 terraform 文档中所写)

resource "github_branch" "branches" {
  for_each      = toset(setsubtract(local.environments, ["prod"]))
  repository    = github_repository.microservices["service1"].name 
  branch        = lookup(var.environment_to_branch_map, each.key, each.key)
  source_branch = "master" 
}

in this case, i have to create a resource for each "MicroService" manually, which i want to avoid heavily... Are there any ideas how i could "nest" the second loop over the environments to create my branches for each Micorservice repos?在这种情况下,我必须手动为每个“微服务”创建一个资源,我想极力避免这种情况……有没有什么想法可以“嵌套”环境中的第二个循环来为每个微服务回购创建我的分支?

Many thx in advance for any hint, idea or approach here...对于这里的任何提示、想法或方法,请提前致谢......

Nested loop can be replaced with a single loop over the setproduct of two sets.嵌套循环可以用两个集合的setproduct上的单个循环代替。 The documentation for setproduct can be found here https://www.terraform.io/language/functions/setproduct setproduct的文档可以在这里找到https://www.terraform.io/language/functions/setproduct

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform - 如何在对象列表上使用 for_each 循环来创建资源 - Terraform - how to use for_each loop on a list of objects to create resources 如何跳过在根模块中声明值(for_each 循环) - How to skip declaring values in root module (for_each loop) 使用FOR循环在每次循环迭代中添加其他字符或符号 - Use FOR loop to add additional characters or symbols each loop iteration 如何在特定于每次迭代的“for循环”中添加字符串? - How to add a string within a "for loop" that is specific to each iteration? HAML每个循环向第​​n个元素添加其他类 - HAML each loop add additional classes to nth elements 通过 YAML 查询并为 for_each 循环创建自定义本地 - Query through YAML and create custom local for for_each loop 在多个数据集和每个数据集中的多个列上循环一个函数 - Loop a function over multiple datasets and multiple columns within each dataset 向foreach循环添加其他参数 - add additional parameters to foreach loop 是否可以循环遍历多个对象并调用循环中每个对象内的元素 - Is it possible to loop over multiple objects and call to elements within each object in the loop 如何在系统上使用并行 std::for_each<execution> 不见了?</execution> - How to use a parallel std::for_each on systems where <execution> is missing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM