def ack(self, **kwargs): k, i = pa.parse_function_params(kwargs, 'ki', 'SS') lvar = pa.get_item(f'lvar:alarmer/{i}') if not lvar: raise pa.ResourceNotFound if not pa.key_check(k, lvar): raise pa.AccessDenied lv = pa.api_call("state", i=f'lvar:alarmer/{i}', full=True) pa.api_call("clear", i=f'lvar:alarmer/{i}') db = get_db() u = pa.get_aci('u') if not u: u = '' utp = pa.get_aci('utp') key_id = pa.get_aci('key_id') if not utp: utp = '' try: db.execute(sql( 'insert into alarmer_log' '(u, utp, key_id, alarm_id, description, action, t, level)' 'values (:u, :utp, :key_id, :alarm_id, :d, :action, :t, :level)' ), u=u, utp=utp, key_id=key_id, alarm_id=i, d=lv['description'], action='A', t=time.time(), level=0) except: logger.error(f'Unable to insert log record for alarm: {i}') pa.log_traceback() return True
def subscribe(self, **kwargs): k, i, l = pa.parse_function_params(kwargs, 'kil', 'SSI') lvar = pa.get_item(f'lvar:alarmer/{i}') if l < 1 or l > 2: raise pa.InvalidParameter('param "l" should be 1 or 2') if not lvar: raise pa.ResourceNotFound if not pa.key_check(k, lvar, ro_op=True): raise pa.AccessDenied db = get_db() u = pa.get_aci('u') if not u: raise pa.FunctionFailed('user is not logged in') utp = pa.get_aci('utp') if not utp: utp = '' kw = {'u': u, 'utp': utp, 'alarm_id': i, 'level': l} if db.execute( sql('select alarm_id from alarmer_sub where u=:u ' 'and utp=:utp and alarm_id=:alarm_id'), **kw).fetchone(): db.execute( sql('update alarmer_sub set level=:level ' 'where u=:u and utp=:utp and alarm_id=:alarm_id'), **kw) else: db.execute( sql('insert into alarmer_sub(u, utp, alarm_id, level) ' 'values (:u, :utp, :alarm_id, :level)'), **kw) return True
def list_subscriptions(self, **kwargs): u = pa.get_aci('u') if not u: raise pa.FunctionFailed('user is not logged in') utp = pa.get_aci('utp') if not utp: utp = '' kw = {'u': u, 'utp': utp} db = get_db() return [ dict(x) for x in db.execute( sql('select alarm_id, level ' 'from alarmer_sub where u=:u and utp=:utp'), **kw) ]
def set(self, **kwargs): k, u, utp, name, value = pa.parse_function_params( kwargs, 'kupnv', 'S..Ss') if u is not None: if not pa.key_check(k, master=True): raise pa.AccessDenied( 'setting info for the specified user requires master key') else: u = pa.get_aci('u') utp = pa.get_aci('utp') if u is None: raise pa.AccessDenied('user not logged in') if utp is None: utp = '' if name in ro_fields: if not pa.key_check(k, master=True): raise pa.AccessDenied( f'field {name} is read-only, master is required to set') elif name not in rw_fields: raise pa.ResourceNotFound(f'field {name}') dbconn = pa.get_userdb() dbt = dbconn.begin() value = msgpack.dumps(value) d = dbconn.execute(sql('select value from plugin_userinfo where ' 'u=:u and utp=:utp and name=:name'), u=u, utp=utp, name=name).fetchone() if d is None: dbconn.execute(sql('insert into plugin_userinfo' '(u, utp, name, value) ' 'values (:u, :utp, :name, :value)'), u=u, utp=utp, name=name, value=value) else: dbconn.execute(sql('update plugin_userinfo set value=:value where ' 'u=:u and utp=:utp and name=:name'), u=u, utp=utp, name=name, value=value) dbt.commit() return True
def unsubscribe(self, **kwargs): k, i = pa.parse_function_params(kwargs, 'ki', 'SS') lvar = pa.get_item(f'lvar:alarmer/{i}') if not lvar: raise pa.ResourceNotFound if not pa.key_check(k, lvar, ro_op=True): raise pa.AccessDenied db = get_db() u = pa.get_aci('u') if not u: raise pa.FunctionFailed('user is not logged in') utp = pa.get_aci('utp') if not utp: utp = '' kw = {'u': u, 'utp': utp, 'alarm_id': i} db.execute( sql('delete from alarmer_sub where u=:u ' 'and utp=:utp and alarm_id=:alarm_id'), **kw) return True
def my_square(self, **kwargs): # parse incoming params x = pa.parse_api_params(kwargs, 'x', 'N') if x < 0: raise pa.InvalidParameter('x < 0') # return some result # if API method produces no result, it SHOULD return True return { 'result': x * x, 'you': pa.get_aci('key_id'), 'me': [pa.get_directory('eva'), pa.get_product().build] }
def get(self, **kwargs): k, u, utp, name = pa.parse_function_params(kwargs, 'kupn', 'S..S') if u is not None: if not pa.key_check(k, master=True): raise pa.AccessDenied( 'getting info for the specified user requires master key') else: u = pa.get_aci('u') utp = pa.get_aci('utp') if name not in ro_fields and name not in rw_fields: raise pa.ResourceNotFound(f'field {name}') if u is None: raise pa.AccessDenied('user not logged in') if utp is None: utp = '' dbconn = pa.get_userdb() r = dbconn.execute(sql('select value from plugin_userinfo where ' 'u=:u and utp=:utp and name=:name'), u=u, utp=utp, name=name) d = r.fetchone() return {name: msgpack.loads(d.value, raw=False) if d else None}