I have to add existing user-list for each group in oracle cloud (OCI), could you please help in doing it in a easy way
example:
iam_group = {
iamg1 = { group_name = "group_test", group_desc = "group test", user_list = ["test", "test1"] }
iamg2 = { group_name = "group_test", group_desc = "group test1", user_list = ["test", "test1"] }
}
variable:
variable "iam_group" {
type = map(object({
group_name = string
group_desc = string
user_list = list(string)
}))
}
I can create groups as below
resource "oci_identity_group" "this" {
for_each = var.iam_group
compartment_id = var.tenancy_ocid
name = each.value.group_name
description = each.value.group_desc
}
I am not getting how to add users to a group in this scenario.
resource "oci_identity_user_group_membership" "test_user_group_membership" {
<logic here>
}
(Massive disclaimer - this is untested so may need some tweaking to work)
I think the fundamental difficulty here is that Terraform only has limited support for complex loop techniques. Therefore you need a way in your
oci_identity_user_group_membershipto loop over a single object.Additionally, the
oci_identity_user_group_membershipresource takes the ID's of both aoci_identity_groupresource and aoci_identity_userresource. Therefore, before you can attempt to associate users with groups, you need to first create those users. Therefore, it looks like you need a variable to track the unique collection of users that may need to be granted access, so that you can createoci_identity_userresources. (In a more sophisticated solution you could likely generate that list from the contents ofiam_groups, but one step at a time :) )The variable
iam_group_usersdefined in thelocalssection is intended to produce a structure like:So a stab at the actual solution:
(Note I have pluralised your
iam_groupvar name)Note:
each.value.group_name&each.value.user_namerefers to the name that Terraform has associated with each instance of the resources, and is taken from thekeyused in thefor_eachstatements when creatingoci_identity_group&oci_identity_user, respectively. Also, given this, it is important that theuser_listin youriam_groupsvar contains the resource name (i.e. thekeyiniam_users) for the user.Some additional, possibly useful, reading:
Terraform 'flatten' docs
Terragrunt Blog post on Loops