def add_iam_binding(policy, member, role):
  """Adds binding to given policy.

  Args:
    policy: Policy.
    member: Account, e.g. user:joe@doe.com, serviceAccount:..., etc.
    role: Role

  Returns:
    True if binding was added. False, if binding was already present in policy.
  """
  # Check if member is already bound to the role
  target_binding = None
  for binding in policy['bindings']:
    if binding['role'] == role:
      if member in binding['members']:
        # Member is already bound to role. Nothing further to do.
        return False
      else:
        # We found the role we are looking for, stop traversing the rest.
        # Just keep a reference to avoid looping again.
        target_binding = binding
        break

  if target_binding:
    target_binding['members'].append(member)
  else:
    # Create a new binding if role wasn't in the policy before
    policy['bindings'].append(dict(role=role, members=[member]))

  return True