Esempio n. 1
0
 def __init__(self, **kwargs):
     """Construct a DAO from **kwargs. Client can also create from a dictionary, d, using AbacoDAO(**d)"""
     for name, source, attr, typ, help, default in self.PARAMS:
         pname = name
         # When using camel case, it is possible a given argument will come in in camel
         # case, for instance when creating an object directly from parameters passed in
         # a POST request.
         if name not in kwargs and Config.get('web', 'case') == 'camel':
             # derived attributes always create the attribute with underscores:
             if source == 'derived':
                 pname = name
             else:
                 pname = under_to_camel(name)
         if source == 'required':
             try:
                 value = kwargs[pname]
             except KeyError:
                 logger.debug("required missing field: {}. ".format(pname))
                 raise errors.DAOError("Required field {} missing.".format(pname))
         elif source == 'optional':
             value = kwargs.get(pname, default)
         elif source == 'provided':
             try:
                 value = kwargs[pname]
             except KeyError:
                 logger.debug("provided field missing: {}.".format(pname))
                 raise errors.DAOError("Required field {} missing.".format(pname))
         else:
             # derived value - check to see if already computed
             if hasattr(self, pname):
                 value = getattr(self, pname)
             else:
                 value = self.get_derived_value(pname, kwargs)
         setattr(self, attr, value)
Esempio n. 2
0
    def get_derived_value(self, name, d):
        """Compute a derived value for the attribute `name` from the dictionary d of attributes provided."""
        # first, see if the attribute is already in the object:
        if hasattr(self, name):
            return
        # next, see if it was passed:
        try:
            return d[name]
        except KeyError:
            pass

        # check for provided fields:
        try:
            self.tenant = d['tenant']
        except KeyError:
            logger.error("The nonce controller did not pass tenant to the Nonce model.")
            raise errors.DAOError("Could not instantiate nonce: tenant parameter missing.")
        try:
            self.api_server = d['api_server']
        except KeyError:
            logger.error("The nonce controller did not pass api_server to the Nonce model.")
            raise errors.DAOError("Could not instantiate nonce: api_server parameter missing.")
        try:
            self.db_id = d['db_id']
        except KeyError:
            logger.error("The nonce controller did not pass db_id to the Nonce model.")
            raise errors.DAOError("Could not instantiate nonce: db_id parameter missing.")
        try:
            self.owner = d['owner']
        except KeyError:
            logger.error("The nonce controller did not pass owner to the Nonce model.")
            raise errors.DAOError("Could not instantiate nonce: owner parameter missing.")
        try:
            self.roles = d['roles']
        except KeyError:
            logger.error("The nonce controller did not pass roles to the Nonce model.")
            raise errors.DAOError("Could not instantiate nonce: roles parameter missing.")

        # generate a nonce id:
        self.id = self.get_nonce_id(self.tenant, self.get_uuid())

        # derive the actor_id from the db_id
        self.actor_id = Actor.get_display_id(self.tenant, self.db_id)

        # time fields
        time_str = get_current_utc_time()
        self.create_time = time_str
        # initially there are no uses
        self.last_use_time = None

        # apply defaults to provided fields since those aren't applied in the constructor:
        self.current_uses = 0
        self.remaining_uses = self.max_uses

        # always return the requested attribute since the __init__ expects it.
        return getattr(self, name)
Esempio n. 3
0
def display_time(t):
    """ Convert a string representation of a UTC timestamp to a display string."""
    if not t:
        return "None"
    try:
        time_f = float(t)
        dt = datetime.datetime.fromtimestamp(time_f)
    except ValueError as e:
        logger.error("Invalid time data. Could not cast {} to float. Exception: {}".format(t, e))
        raise errors.DAOError("Error retrieving time data.")
    except TypeError as e:
        logger.error("Invalid time data. Could not convert float to datetime. t: {}. Exception: {}".format(t, e))
        raise errors.DAOError("Error retrieving time data.")
    return str(dt)
Esempio n. 4
0
 def compute_summary_stats(self, dbid):
     try:
         actor = actors_store[dbid]
     except KeyError:
         raise errors.DAOError(
             "actor not found: {}'".format(dbid), 404)
     tot = {'api_server': actor['api_server'],
            'actor_id': actor['id'],
            'owner': actor['owner'],
            'total_executions': 0,
            'total_cpu': 0,
            'total_io': 0,
            'total_runtime': 0,
            'ids': []}
     try:
         executions = executions_store[dbid]
     except KeyError:
         executions = {}
     for id, val in executions.items():
         tot['ids'].append(id)
         tot['total_executions'] += 1
         tot['total_cpu'] += int(val['cpu'])
         tot['total_io'] += int(val['io'])
         tot['total_runtime'] += int(val['runtime'])
     return tot
Esempio n. 5
0
 def get_nonce(cls, actor_id, nonce_id):
     """Retrieve a nonce for an actor. Pass db_id as `actor_id` parameter."""
     try:
         nonce = nonce_store[actor_id][nonce_id]
         return Nonce(**nonce)
     except KeyError:
         raise errors.DAOError("Nonce not found.")
Esempio n. 6
0
    def get_derived_value(self, name, d):
        """Compute a derived value for the attribute `name` from the dictionary d of attributes provided."""
        # first, see if the attribute is already in the object:
        if hasattr(self, 'id'):
            return
        # next, see if it was passed:
        try:
            return d[name]
        except KeyError:
            pass
        # if not, generate an id
        try:
            actor_id, db_id = self.generate_id(d['name'], d['tenant'])
        except KeyError:
            logger.debug(
                "name or tenant missing from actor dict: {}.".format(d))
            raise errors.DAOError("Required field name or tenant missing")
        # id fields:
        self.id = actor_id
        self.db_id = db_id

        # time fields
        time_str = get_current_utc_time()
        self.create_time = time_str
        self.last_update_time = time_str
        if name == 'id':
            return actor_id
        elif name == 'create_time' or name == 'last_update_time':
            return time_str
        else:
            return db_id
Esempio n. 7
0
def set_permission(user, actor_id, level):
    """Set the permission for a user and level to an actor."""
    logger.debug("top of set_permission().")
    if not isinstance(level, PermissionLevel):
        raise errors.DAOError("level must be a PermissionLevel object.")
    try:
        permissions_store.update(actor_id, user, level.name)
    except KeyError:
        # if actor has no permissions, a KeyError will be thrown
        permissions_store[actor_id] = {user: level.name}
    logger.info("Permission set for actor: {}; user: {} at level: {}".format(actor_id, user, level))
Esempio n. 8
0
 def __init__(self, **kwargs):
     """Construct a DAO from **kwargs. Client can also create from a dictionary, d, using AbacoDAO(**d)"""
     for name, source, attr, typ, help, default in self.PARAMS:
         if source == 'required':
             try:
                 value = kwargs[name]
             except KeyError:
                 logger.debug("required missing field: {}. ".format(name))
                 raise errors.DAOError("Required field {} missing.".format(name))
         elif source == 'optional':
             value = kwargs.get(name, default)
         elif source == 'provided':
             try:
                 value = kwargs[name]
             except KeyError:
                 logger.debug("provided field missing: {}.".format(name))
                 raise errors.DAOError("Required field {} missing.".format(name))
         else:
             # derived value - check to see if already computed
             if hasattr(self, name):
                 value = getattr(self, name)
             else:
                 value = self.get_derived_value(name, kwargs)
         setattr(self, attr, value)
Esempio n. 9
0
 def compute_summary_stats(self, dbid):
     try:
         actor = actors_store[dbid]
     except KeyError:
         raise errors.DAOError("actor not found: {}'".format(dbid), 404)
     tot = {
         'api_server': actor['api_server'],
         'actor_id': actor['id'],
         'owner': actor['owner'],
         'total_executions': 0,
         'total_cpu': 0,
         'total_io': 0,
         'total_runtime': 0,
         'executions': []
     }
     try:
         executions = executions_store[dbid]
     except KeyError:
         executions = {}
     for id, val in executions.items():
         execution = {
             'id': id,
             'status': val.get('status'),
             'start_time': val.get('start_time'),
             'message_received_time': val.get('message_received_time')
         }
         if val.get('final_state'):
             execution['finish_time'] = val.get('final_state').get(
                 'FinishedAt')
             # we rely completely on docker for the final_state object which includes the FinishedAt time stamp;
             # under heavy load, we have seen docker fail to set this time correctly and instead set it to 1/1/0001.
             # in that case, we should use the total_runtime to back into it.
             if execution['finish_time'].startswith('0001-01-01'):
                 finish_time = float(val.get('start_time')) + float(
                     val['runtime'])
                 execution['finish_time'] = display_time(str(finish_time))
         tot['executions'].append(execution)
         tot['total_executions'] += 1
         tot['total_cpu'] += int(val['cpu'])
         tot['total_io'] += int(val['io'])
         tot['total_runtime'] += int(val['runtime'])
     return tot
Esempio n. 10
0
 def get_derived_value(self, name, d):
     """Compute a derived value for the attribute `name` from the dictionary d of attributes provided."""
     # first, see if the attribute is already in the object:
     if hasattr(self, 'id'):
         return
     # next, see if it was passed:
     try:
         return d[name]
     except KeyError:
         pass
     # if not, generate an id
     try:
         actor_id, db_id = self.generate_id(d['name'], d['tenant'])
     except KeyError:
         raise errors.DAOError("Required field name or tenant missing")
     self.id = actor_id
     self.db_id = db_id
     if name == 'id':
         return actor_id
     else:
         return db_id