def get_api_key(apik_id): """Retrieves an api key.""" apik_id = str(apik_id) tmp = red.hgetall(_KEY_APIK.format(apik_id)) if tmp is not None: tmp['id'] = apik_id return tmp
def delete_network(user_id, net_id): """Deletes a network.""" pip = red.pipeline() user_id = str(user_id) net_id = str(net_id) pip.delete(_KEY_NET.format(net_id)) pip.srem(_KEY_NETS_USER.format(user_id), net_id) # Delete associated events events = get_user_events_network(user_id, net_id) for event in events: pip.lrem(_KEY_EVENTS_USER_DATE.format(user_id, event['date']), 0, event['id']) pip.delete(_KEY_EVENT_USER.format(event['id'], user_id)) pip.delete(_KEY_EVENTS_USER_NET.format(user_id, net_id)) # user-network # Delete associated entries on wb lists for entry in get_entries_network(net_id): # delete network from the entry, if it has one network delete entry if red.scard(_KEY_ENTRY_SET_NETS.format(entry['id'])) == 1: pip.delete(_KEY_ENTRY.format(entry['id'])) # delete from w or b list of entries if entry['type'] == 'B': pip.srem(_KEY_ENTRY_BLACK_USER.format(user_id), entry['id']) else: pip.srem(_KEY_ENTRY_WHITE_USER.format(user_id), entry['id']) pip.srem(_KEY_ENTRY_SET_NETS.format(entry['id']), 0, net_id) pip.execute() # Search for associated api keys to this network apik_ids = red.smembers(_KEY_APIKS_USER.format(user_id)) for apik in apik_ids: tmp = red.hgetall(_KEY_APIK.format(apik)) if tmp and tmp['network'] == net_id: _delete_api_key(user_id, apik)
def get_host(host_id): """Returs a host.""" host_id = str(host_id) temp = red.hgetall(_KEY_HOST.format(host_id)) if temp is not None: temp['id'] = host_id servs = red.smembers(_KEY_HOST_SET_SERVS.format(host_id)) temp['services'] = servs return temp
def get_event(event_id, user_id): """Retrieves an event.""" temp = red.hgetall(_KEY_EVENT_USER.format(str(event_id), str(user_id))) if temp != None: temp['id'] = event_id net = get_network(temp[_ATTR_EVENT_NET]) if net is not None: temp['net'] = (net['id'], net[_ATTR_NET_NAME]) return temp
def clean_api_key(key_id): """Cleans an api key.""" key_id = str(key_id) tmp = red.hgetall(_KEY_APIK.format(key_id)) red.delete(_KEY_BUOYACTION.format(key_id)) red.delete(_KEY_UUID_APIK.format(tmp['key'])) pip = red.pipeline() pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_KEY, '') pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_GENERATED, '') pip.hset(_KEY_APIK.format(key_id), _ATTR_APIK_STATUS, const.BUOY_NOTDEP) pip.execute()
def get_entry(entry_id): """Gets an entry.""" entry_id = str(entry_id) temp = red.hgetall(_KEY_ENTRY.format(entry_id)) if temp is not None: temp['id'] = entry_id nets = red.smembers(_KEY_ENTRY_SET_NETS.format(entry_id)) tempnets = [] if nets is not None: for net in nets: tempnet = get_network(net) tempnets.append((net, tempnet['name'])) temp['nets'] = tempnets return temp
def _delete_api_key(user_id, apik_id): """Deletes an api key.""" user_id = str(user_id) apik_id = str(apik_id) # Delete uuid to key tmp = red.hgetall(_KEY_APIK.format(apik_id)) red.delete(_KEY_UUID_APIK.format(tmp['key'])) pip = red.pipeline() if pip.scard(_KEY_APIKS_USER.format(user_id)) == 1: pip.delete(_KEY_APIKS_USER.format(user_id)) else: pip.srem(_KEY_APIKS_USER.format(user_id), apik_id) pip.delete(_KEY_APIK.format(apik_id)) pip.execute() # delete also the orders a buoy must perform red.delete(_KEY_BUOYACTION.format(apik_id))
def get_action_buoy(buoy_id): """Gets the action a buoy must complete""" temp = red.hgetall(_KEY_BUOYACTION.format(str(buoy_id))) if temp: temp['id'] = str(buoy_id) return temp
def get_network(network_id): """Retrieves a network.""" tmp = red.hgetall(_KEY_NET.format(str(network_id))) if tmp is not None: tmp['id'] = network_id return tmp