【问题标题】:we created okta_group_memberships using terraform. we are creating multple users but we want only selected user我们使用 terraform 创建 okta_group_memberships。我们正在创建多个用户,但我们只想要选定的用户
【发布时间】:2022-06-10 20:45:14
【问题描述】:

我们使用 terraform 创建了 okta 用户和 okta 组

这是变量.tf 用于用户和组的变量列表对象

variable "users" {
  type = list(object
    ({
      first_name   = string
      last_name    = string
      email        = string
      organization = string
      role         = string
      okta_admin   = bool
      team         = string
  }))
}

variable "groups" {
  type = list(object
    ({
      name        = string
      description = string
  }))
}

这是 terraform.tfvars

groups = [
  { name = "dev", description = "This group for Devlopers" },
  { name = "qa", description = "This group for QA" },
  { name = "per", description = "This group for Per" }
  { name = "devops", description = "This group for Devops" }
  { name = "admins", description = "This group for administators" }
]

users = [
  { first_name = "a", last_name = "a", email = "a@gmail.com", role = "Engineer", organization = "organization", okta_admin = true, team = "a" },
  { first_name = "b", last_name = "b", email = "b@gmail.com", role = "Lead", organization = "organization", okta_admin = true, team = "a" },
  { first_name = "c", last_name = "c", email = "c@gmail.com", role = "Devloper", organization = "organization", okta_admin = false, team ="b" },
  { first_name = "d", last_name = "d", email = "d@gmail.com", role = "Engineer", organization = "organization", okta_admin = true, team = "b" },
  { first_name = "e", last_name = "e", email = "e@gmail.com", role = "Lead", organization = "organization", okta_admin = true, team ="b" },
  { first_name = "f", last_name = "f", email = "f@gmail.com", role = "Devloper", organization = "organization", okta_admin = false, team ="b" },
],

这是 main.tf

locals {
  #groups
  dev_group         = [for dev in var.groups : dev if apollo.name == "dev"][0]
  devops_group       = [for devops in var.groups : devops if devops.name == "devops"][0]
 # users
  a_user         = [for a in var.users : a if a.team == "a"][0]
  b_user       = [for bin var.users : b if b.team == "b"][0]
}

获取用于_each的值

resource "okta_group" "groups" {
  for_each    = { for group in var.groups : join("-", [group.name, group.description]) => group }
  name        = each.value.name
  description = each.value.description
}



resource "okta_user" "okta_user_add" {
  for_each = { for user in var.users : join("-", [user.first_name, user.last_name]) => user }

  title        = each.value.role
  email        = each.value.email
  first_name   = each.value.first_name
  last_name    = each.value.last_name
  login        = each.value.email
  organization = each.value.organization
}

当我们尝试获取 id 时,我们尝试了多种方法,但对我们不起作用。 无法获取组 id 和用户 id 的

resource "okta_group_memberships" "b_member_group" {

  group_id = okta_group.groups[join("-", [local.dev.name, local.dev.description])].id
  users = [ okta_user.okta_user_add[join("-", [local.b_user.first_name, local.b_user.last_name])].id ]
}

我的问题是

当我们创建 okta_group_memberships 时,我们只会从中获得一个用户。在当地人中,我选择了 b_user select b_user = [for bin var.users : b if b.team == "b"][0] 这是有效的,但它只选择了一个用户。

如果我插入 [*] 我会从中得到错误。

Error: Unsupported attribute
│
│   on main.tf line 55, in resource "okta_group_memberships" "b_member_group":
│   55:     okta_user.okta_user_add[join("-", [local.b_user.first_name, local.b_user.last_name])].id    
│     ├────────────────
│     │ local.b_user is tuple with 4 elements
│
│ This value does not have any attributes.

【问题讨论】:

    标签: terraform tuples okta


    【解决方案1】:

    您的表达式 [for bin var.users : b if b.team == "b"] 构造如下:

    [
      { first_name = "c", last_name = "c", email = "c@gmail.com", role = "Devloper", organization = "organization", okta_admin = false, team ="b" },
      { first_name = "d", last_name = "d", email = "d@gmail.com", role = "Engineer", organization = "organization", okta_admin = true, team = "b" },
      { first_name = "e", last_name = "e", email = "e@gmail.com", role = "Lead", organization = "organization", okta_admin = true, team ="b" },
      { first_name = "f", last_name = "f", email = "f@gmail.com", role = "Devloper", organization = "organization", okta_admin = false, team ="b" },
    ]
    

    然后您使用[0] 访问此列表的第零个元素,这将返回:

    { first_name = "c", last_name = "c", email = "c@gmail.com", role = "Devloper", organization = "organization", okta_admin = false, team ="b" }
    

    这就是为什么只有一个用户从该值返回。您需要删除 [0] 元素访问器:

    b_user = [for bin var.users : b if b.team == "b"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-28
      • 2016-09-25
      • 2023-01-16
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      相关资源
      最近更新 更多