def prepare_user(username, password):
    """
    Create a new user account (admin) for Cloudera Director Server
    :param username: Username for the new account
    :param password: Password for the new account
    :return:         API exit code
    """
    # Cloudera Director server runs at http://127.0.0.1:7189
    try:
        logging.info('Creating new admin user for Cloudera Director Server')
        client = ApiClient("http://localhost:7189")
        AuthenticationApi(client).login(
            Login(username="******",
                  password="******"))  # create new login base on user input
        users_api = UsersApi(client)
        # Admin user by default has both roles
        users_api.create(
            User(username=username,
                 password=password,
                 enabled=True,
                 roles=["ROLE_ADMIN", "ROLE_READONLY"]))

        logging.info('Successfully created new admin user %s.' % dirUsername)
    except HTTPError, e:
        logging.error("Failed to create user '%s'. %s" % (username, e.msg))
        return ExitCodes.ERROR
Ejemplo n.º 2
0
def delete_user(args):
    """
    Delete an user account

    @param args: dict of parsed command line arguments that include
                 the username of the user to be deleted
    @return:     script exit code
    """
    users_api = UsersApi(get_authenticated_client(args))
    users_api.delete(args.username)

    return ExitCodes.OK
Ejemplo n.º 3
0
def delete_user(args):
    """
    Delete an user account

    @param args: dict of parsed command line arguments that include
                 the username of the user to be deleted
    @return:     script exit code
    """
    users_api = UsersApi(get_authenticated_client(args))
    users_api.delete(args.username)

    return ExitCodes.OK
Ejemplo n.º 4
0
def list_users(args):
    """
    List all the existing user accounts

    @param args: dict of parsed command line arguments only used to
                 authenticate as a admin
    @return:     script exit code
    """
    users_api = UsersApi(get_authenticated_client(args))
    for user in users_api.list():
        print user

    return ExitCodes.OK
Ejemplo n.º 5
0
def list_users(args):
    """
    List all the existing user accounts

    @param args: dict of parsed command line arguments only used to
                 authenticate as a admin
    @return:     script exit code
    """
    users_api = UsersApi(get_authenticated_client(args))
    for user in users_api.list():
        print user

    return ExitCodes.OK
Ejemplo n.º 6
0
def add_user(args):
    """
    Create a new user account

    @param args: dict of parsed command line arguments that include
                 an username and a password for the new account
    @return:     script exit code
    """
    try:
        users_api = UsersApi(get_authenticated_client(args))
        users_api.create(User(username=args.username, password=args.password,
                          enabled=True, roles=["ROLE_ADMIN"]))
        return ExitCodes.OK

    except HTTPError, e:
        if  e.code == 302:  # found
            sys.stderr.write("Cannot create duplicate user '%s'.\n" % (args.username,))
            return ExitCodes.DUPLICATE_USER
        else:
            raise e
Ejemplo n.º 7
0
def add_user(args):
    """
    Create a new user account

    @param args: dict of parsed command line arguments that include
                 an username and a password for the new account
    @return:     script exit code
    """
    try:
        users_api = UsersApi(get_authenticated_client(args))
        users_api.create(User(username=args.username, password=args.password,
                              enabled=True, roles=["ROLE_ADMIN"]))
        return ExitCodes.OK

    except ApiException as exc:
        if exc.status == 409:  # conflict
            sys.stderr.write("Cannot create duplicate user '%s'.\n" % (args.username,))
            return ExitCodes.DUPLICATE_USER
        else:
            raise exc
def prepare_user(username, password):
    """
    Create a new user account (admin) for Cloudera Director Server
    :param username: Username for the new account
    :param password: Password for the new account
    :return:         API exit code
    """
    # Cloudera Director server runs at http://127.0.0.1:7189
    try:
        logging.info('Creating new admin user for Cloudera Director Server')
        client = ApiClient("http://localhost:7189")
        AuthenticationApi(client).login(
            Login(username="******", password="******"))  # create new login base on user input
        users_api = UsersApi(client)
        # Admin user by default has both roles
        users_api.create(User(username=username, password=password, enabled=True,
                              roles=["ROLE_ADMIN", "ROLE_READONLY"]))

        logging.info('Successfully created new admin user %s.' % dirUsername)
    except HTTPError, e:
        logging.error("Failed to create user '%s'. %s" % (username, e.msg))
        return ExitCodes.ERROR
                 password=password,
                 enabled=True,
                 roles=["ROLE_ADMIN", "ROLE_READONLY"]))

        logging.info('Successfully created new admin user %s.' % dirUsername)
    except HTTPError, e:
        logging.error("Failed to create user '%s'. %s" % (username, e.msg))
        return ExitCodes.ERROR

    # delete existing admin user using the new account
    try:
        logging.info(
            "Deleting default user 'admin' for Cloudera Director Server")
        client = ApiClient("http://localhost:7189")
        AuthenticationApi(client).login(
            Login(username=username, password=password))
        users_api = UsersApi(client)
        users_api.delete("admin")

        logging.info("Successfully deleted default user 'admin'")
        return ExitCodes.OK
    except HTTPError, e:
        logging.error("Failed to delete default user 'admin'. %s" % e.msg)
        return ExitCodes.ERROR


dirUsername = sys.argv[1]
dirPassword = sys.argv[2]

sys.exit(prepare_user(dirUsername, dirPassword))
        AuthenticationApi(client).login(
            Login(username="******", password="******"))  # create new login base on user input
        users_api = UsersApi(client)
        # Admin user by default has both roles
        users_api.create(User(username=username, password=password, enabled=True,
                              roles=["ROLE_ADMIN", "ROLE_READONLY"]))

        logging.info('Successfully created new admin user %s.' % dirUsername)
    except HTTPError, e:
        logging.error("Failed to create user '%s'. %s" % (username, e.msg))
        return ExitCodes.ERROR

    # delete existing admin user using the new account
    try:
        logging.info("Deleting default user 'admin' for Cloudera Director Server")
        client = ApiClient("http://localhost:7189")
        AuthenticationApi(client).login(Login(username=username, password=password))
        users_api = UsersApi(client)
        users_api.delete("admin")

        logging.info("Successfully deleted default user 'admin'")
        return ExitCodes.OK
    except HTTPError, e:
        logging.error("Failed to delete default user 'admin'. %s" % e.msg)
        return ExitCodes.ERROR

dirUsername = sys.argv[1]
dirPassword = sys.argv[2]

sys.exit(prepare_user(dirUsername, dirPassword))