Fix the plan for one IAM user per developer
Your task
You asked an assistant for one IAM user per developer, and it used count. A teammate warns that removing someone from the middle of the list will recreate everyone after them. To settle it, the team wants the plan for both ways of keying the users.
An assistant wrote terraform_user_plan(before, after, keying). It is in the editor. It has a bug. Find it and fix it.
Input and output:
before: the developer names in the list today, in order. Names are case sensitive and can repeat. The list can be empty.after: the names in the edited list, in the same format.keying:"count"or"for_each".- Return the plan as a list of strings, each
"<key>=<action>". The action isnoop,create,replaceordestroy.
Rules the tests check:
- With
"count", each user is keyed by its position in the list:"0","1", and so on. Go through every position from 0 to the length of the longer list minus 1, in that order. - For a position in both lists, the same name is
noopand a different name isreplace. The user name cannot change in place, so the old user is destroyed and a new one is created. - A position only in
afteriscreate. A position only inbeforeisdestroy. - With
"for_each", each user is keyed by name, the waytosetdoes it: a name that appears more than once counts once. - Take every distinct name from both lists. A name in both lists is
noop, a name only inafteriscreate, and a name only inbeforeisdestroy. There is never areplace. - The
"for_each"entries are sorted by name in ascending order, comparing character codes, so uppercase letters come before lowercase. - Any other
keying, such as"for-each"or"FOR_EACH", returns an empty list.
Examples
terraform_user_plan(before: list[str], after: list[str], keying: str) -> list[str]Example 1
Input
terraform_user_plan(["alice", "bob", "carol", "dave"], ["alice", "carol", "dave"], "count")Output
["0=noop", "1=replace", "2=replace", "3=destroy"]Example 2
Input
terraform_user_plan(["alice", "bob", "carol", "dave"], ["alice", "carol", "dave"], "for_each")Output
["alice=noop", "bob=destroy", "carol=noop", "dave=noop"]Example 3
Input
terraform_user_plan(["alice", "bob"], ["alice", "bob", "erin"], "count")Output
["0=noop", "1=noop", "2=create"]