Fix the plan for one IAM user per developer

Review the AI's codeEasy12 minAmazon style

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 is noop, create, replace or destroy.

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 noop and a different name is replace. The user name cannot change in place, so the old user is destroyed and a new one is created.
  • A position only in after is create. A position only in before is destroy.
  • With "for_each", each user is keyed by name, the way toset does 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 in after is create, and a name only in before is destroy. There is never a replace.
  • 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
Inputterraform_user_plan(["alice", "bob", "carol", "dave"], ["alice", "carol", "dave"], "count")
Output["0=noop", "1=replace", "2=replace", "3=destroy"]
Example 2
Inputterraform_user_plan(["alice", "bob", "carol", "dave"], ["alice", "carol", "dave"], "for_each")
Output["alice=noop", "bob=destroy", "carol=noop", "dave=noop"]
Example 3
Inputterraform_user_plan(["alice", "bob"], ["alice", "bob", "erin"], "count")
Output["0=noop", "1=noop", "2=create"]