Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update website doc to depict the use of sets in members resource #767

Merged
merged 2 commits into from
Jan 19, 2023

Conversation

Uk1288
Copy link
Contributor

@Uk1288 Uk1288 commented Jan 13, 2023

Description

Related Issue: 761

Terraform creates an instance for each member of a given set, when for_each is used to configure a resource block.

If for_each is used with resources that do bulk management like the tfe_team_organization_members and tfe_team_members, it results in the creation of multiple instances managing the same set of resources. See the example config below:

locals {
  all_users = toset([
    "[email protected]",
    "[email protected]",
  ])
}

resource "tfe_team" "test" {
  name         = "my-team-name"
  organization = "my-org-name"
}

resource "tfe_organization_membership" "all_membership" {
  organization = "my-org-name"
  for_each     = local.all_users
  email        = each.key
}

resource "tfe_team_organization_members" "all-members" {
  team_id  = tfe_team.test.id
  for_each = local.all_users
  organization_membership_ids = [
    tfe_organization_membership.all_membership[each.key].id
  ]
}

The above config results in multiple instances of tfe_team_organization_members managing a set of memberships. Running an Apply will succeed. During the destroy phase, each instance of the tfe_team_organization_members resource attempts to delete the same organization members. The first delete request succeeds but the subsequent requests fail. If there are n number of local.all_users, there will be n DELETE API request made with the same membership id set (the current delete implementation attempts to delete all existing memberships).

What is needed here, is for Terraform to create just 1 instance of tfe_team_organization_members which will manage all the organization members in the set. The for expression will accomplish this, see the updated tfe_team_organization_members config below:

resource "tfe_team_organization_members" "all-members" {
  team_id = tfe_team.test.id
  organization_membership_ids = [for member in local.all_users : 
     tfe_organization_membership.all_membership[member].id]
}

After investigating this issue, the conclusion is that it is beneficial to add this use case example to the doc hence this PR.

Testing plan

  1. Apply and compare the listed config above

External links

Output from acceptance tests

@Uk1288 Uk1288 requested a review from a team as a code owner January 13, 2023 21:15
@Uk1288 Uk1288 force-pushed the uk1288-update-doc-to-show-sets-in-members-resource branch from a639f53 to 8c16712 Compare January 16, 2023 14:53
Copy link
Contributor

@laurenolivia laurenolivia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Really clear description! Smoke tested this and it works as expected.

@Uk1288 Uk1288 merged commit e973c3f into main Jan 19, 2023
@Uk1288 Uk1288 deleted the uk1288-update-doc-to-show-sets-in-members-resource branch January 19, 2023 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants