Exemplo n.º 1
0
def get_entity(ident, entity_type, db):
    """Find a group, given its identity.

    This is much like what _get_group()/_get_account() in bofhd does... except simpler
    """

    const = Factory.get("Constants")()
    if entity_type in ('group', const.entity_group):
        obj = Factory.get("Group")(db)
    elif entity_type in ('account', const.entity_account):
        obj = Account(db)

    finder = None
    if isinstance(ident, str):
        if ident.isdigit():
            finder = obj.find
        else:
            finder = obj.find_by_name
    elif isinstance(ident, int):
        finder = obj.find
    else:
        assert False

    finder(ident)
    return obj
Exemplo n.º 2
0
def create_user(uname, db):
    """Helper function to create some system users we need in VH.

    uname will be owned by the system group andit will have been created by
    the system account.
    """

    account = Account(db)
    constants = Factory.get("Constants")()
    try:
        account.find_by_name(uname)
        logger.debug("Found account with name=%s, id=%s", account.account_name,
                     account.entity_id)
        return
    except Errors.NotFoundError:
        sys_group = get_system_group(db)
        sys_account = get_system_account(db)
        account.populate(
            uname,
            constants.entity_group,  # owned by 
            sys_group.entity_id,  # ... system group
            constants.account_program,
            sys_account.entity_id,  # created by system account
            None)  # no expire (duh!)
        account.write_db()
        logger.debug("Created account uname=%s, id=%s", account.account_name,
                     account.entity_id)
        logger.debug("Don't forget to set a password on uname=%s",
                     account.account_name)
def prepare_empty(db, args):
    """This function constructs a destination group
    if it doesn't exist."""
    co = Factory.get('Constants')(db)
    gr = Factory.get('Group')(db)
    try:
        gr.find_by_name(args.destination_group)
    except Errors.NotFoundError:
        bootstrap_ac = Account(db)
        bootstrap_ac.find_by_name(cereconf.INITIAL_ACCOUNTNAME)
        gr.populate(
            creator_id=bootstrap_ac.entity_id,
            visibility=co.group_visibility_all,
            name=args.destination_group,
            description=('Flattened variant of %s' % args.target_group),
            group_type=co.group_type_derived,
        )
    gr.write_db()
Exemplo n.º 4
0
    def setUpClass(cls):
        """ Set up this TestCase module.

        This setup code sets up shared objects between each tests. This is done
        *once* before running any of the tests within this class.
        """

        # TODO: We might want this basic class setup in other TestCases. Maybe
        #       set up a generic TestCase class to inherit common stuff from?
        cls._db = Factory.get('Database')()
        cls._db.cl_init(change_program='nosetests')
        cls._db.commit = cls._db.rollback  # Let's try not to screw up the db

        cls._ac = Factory.get('Account')(cls._db)
        cls._ac = Account(cls._db)
        cls._co = Factory.get('Constants')(cls._db)

        # Data sources
        cls.account_ds = BasicAccountSource()
        cls.person_ds = BasicPersonSource()

        # Tools for creating and destroying temporary db items
        cls.db_tools = DatabaseTools(cls._db)
        cls.db_tools._ac = cls._ac