示例#1
0
    def create_user(self, user, project):
        """Create a new OpenStack user from an addusers.User
        
        This function assumes you have already verfied the user doesn't exist.
        """
        print "Creating user {}".format(user.name)
        password = random_password(16)
        fullname = "{} {}".format(user.first_name, user.last_name)
        ks_user = self.keystone.users.create(name=user.name,
                                             email=user.email,
                                             password=password,
                                             domain='default',
                                             description=fullname)

        setpass_token = self.setpass.get_token(ks_user.id, password, user.pin)
        password_url = self.setpass.get_url(setpass_token)

        usr_cfg = email_defaults.copy()
        usr_cfg.update(dict(config.items('welcome_email')))
        welcome_email = message.TemplateMessage(email=ks_user.email,
                                                username=ks_user.name,
                                                fullname=fullname,
                                                project=project.name,
                                                **usr_cfg)
        pwd_cfg = email_defaults.copy()
        pwd_cfg.update(dict(config.items('password_email')))
        password_email = message.TemplateMessage(
            email=ks_user.email,
            fullname=fullname,
            setpass_token_url=password_url,
            **pwd_cfg)

        try:
            welcome_email.send()
            password_email.send()
        except:
            # Save both emails if either throws an error, just in case
            path = config.get('output', 'email_path')
            welcome_email.dump_to_file(target_path=path, label="welcome")
            password_email.dump_to_file(target_path=path, label="password")
            raise

        return ks_user
    sess = session.Session(auth=auth)

    setpass = SetpassClient(sess, setpass_url)
    keystone = client.Client(session=sess)

    user = [
        usr for usr in keystone.users.list()
        if usr.name.lower() == args.username.lower()
    ]
    if not user:
        print "User {} not found".format(args.username)
        sys.exit(1)
    else:
        user = user[0]

    newpass = random_password(16)

    keystone.users.update(user, password=newpass)
    token = setpass.get_token(user.id, newpass, args.PIN)

    url = setpass.get_url(token)

    email_config = dict(config.items('email_defaults'))
    email_config.update(dict(config.items('password_reset_email')))

    email = TemplateMessage(email=args.username,
                            fullname=args.username,
                            setpass_token_url=url,
                            **email_config)

    try: