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

Data source for groups #483

Closed
roryprimrose opened this issue Nov 16, 2021 · 2 comments · Fixed by #484
Closed

Data source for groups #483

roryprimrose opened this issue Nov 16, 2021 · 2 comments · Fixed by #484

Comments

@roryprimrose
Copy link
Contributor

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

There is a data source for referencing existing users but not existing groups. The only data source is for referencing a single group which throws an exception if the group is not found.

I am using json data to refer to users and groups from which I want to configure things like permissions. This currently is difficult because there is no clean way to find matches between json data and groups.

New or Affected Resource(s)

New: data azuredevops_groups

Potential Terraform Configuration

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key.

References

  • #0000
@roryprimrose
Copy link
Contributor Author

My current workaround is to use a local that is built by json configuration (members) that do not appear to be a user (no @ character found). I then use that local data set to load each group using data azuredevops_group. The difference between this and data azuredevops_users is that this throws an exception resulting in a hard fail if a group isn't found. By comparison if users are not found then they are just skipped over.

resource "azuredevops_group" "groups" {
  for_each     = { for group in local.groups : group.key => group }
  display_name = each.value.name
  description  = each.value.description
}

locals {
  groupMembers = flatten(
    [
      for group in local.groups :
      [
        for member in group.members :
        {
          group  = group
          member = member
        }
        if(length(regexall("@", member)) == 0)
      ]
  ])
}

data "azuredevops_group" "group_members" {
  for_each = { for item in local.groupMembers : format("%s|%s", item.group.key, item.member) => item.member }
  name     = each.value
}

resource "azuredevops_group_membership" "membership" {
  for_each = { for item in local.groups : item.key => item }
  group    = azuredevops_group.groups[each.key].descriptor
  mode     = "overwrite"
  members = flatten(
    concat(
      [
        for user in data.azuredevops_users.all_users.users :
        [
          for member in each.value.members : user.descriptor if lower(user.principal_name) == lower(member)
        ]
      ],
      [
        for group in data.azuredevops_group.group_members :
        [
          for member in each.value.members : group.descriptor if lower(group.name) == lower(member)
        ]
    ])
  )
}

@tmeckel
Copy link
Contributor

tmeckel commented Nov 17, 2021

@xuzhang3 this is a low hanging fruit and a feature what I wanted to implement for a long time. I've some code prepared already for this. I'll file a pull request for this till the end of the week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants