def SetEndpointUpdateServiceAccounts(ref, arguments, req):
  """Modify the update request to add, remove, or clear endpoint service accounts."""
  del ref

  service_accounts = req.allocationEndpoint.serviceAccounts
  if arguments.clear_service_accounts:
    service_accounts = []
  if arguments.IsSpecified('remove_service_accounts'):
    to_remove = set(arguments.remove_service_accounts or [])
    service_accounts = [acc for acc in service_accounts if acc not in to_remove]
  if arguments.IsSpecified('add_service_accounts'):
    service_accounts += arguments.add_service_accounts

  req.allocationEndpoint.serviceAccounts = service_accounts
  return req