modern-nest-74116
04/14/2022, 6:44 PMfunc createUsers(ctx *pulumi.Context,
group mcfg.Group,
globalTags map[string]string) ([]pulumi.StringOutput, error) {
// the order of users in the list is important to avoid
// unnecessary updates of GroupMembership and to serialize its update
// with NewUsers exectued in a loop.
sort.Strings(group.Users)
var newUsersNames []pulumi.StringOutput
var newUser *iam.User
var err error
for _, user := range group.Users {
newUser, err = iam.NewUser(ctx, user, &iam.UserArgs{
Name: pulumi.String(user),
Path: pulumi.String("/perf/"),
Tags: pulumi.ToStringMap(globalTags),
})
if err != nil {
return nil, err
}
newUsersNames = append(newUsersNames, newUser.Name)
}
if len(newUsersNames) > 0 {
ngMemberName := fmt.Sprintf("%s-%s", group.Name, "mozart")
// a hack to serialize NewUsers with NewGroupMembership
_, err := iam.NewGroupMembership(ctx, ngMemberName, &iam.GroupMembershipArgs{
Users: pulumi.ToStringArray(group.Users),
Group: pulumi.String(group.Name),
}, pulumi.DependsOn([]pulumi.Resource{newUser}))
if err != nil {
return nil, err
}
}
return newUsersNames, nil
}
My initial attempt was to convert newUsersNames - []pulumi.StringOutput
to []string
using ApplyT on each of the list elements. It works but then the creation of NewUsers
and NewGroupMembership
is not serialized.
In the above version I just pass the list of users which is provided by a function argument to GroupMembershipArgs
and try to create a dependency on the last NewUser
resource created in the loop. Is there a better way of doing this?No matter how you like to participate in developer communities, Pulumi wants to meet you there. If you want to meet other Pulumi users to share use-cases and best practices, contribute code or documentation, see us at an event, or just tell a story about something cool you did with Pulumi, you are part of our community.
Powered by