def map_roles(role):
    """ Transform roles into the format needed by the api """
    if type(role) is str:
        return dict(roleName=role, databaseName='admin')
    elif role.get('db') is not None and role.get('role') is not None:
        # this makes the playbook look better. I.e. the roles section looks
        # like:
        # mongo_atlas_user:
        #   user: foo
        #   roles:
        #       db: mydb
        #       role: readWrite
        #
        # Instead of having the roleName and databaseName fields like:
        # mongo_atlas_user:
        #   user: foo
        #   roles:
        #       databaseName: mydb
        #       roleName: readWrite
        return dict(roleName=role['role'], databaseName=role['db'])
    else:
        return role