def delete_asset_type(): """Delete asset info Deletes asset type from database. NOTE: All assets of that type must be removed from the db before deleting type. Args: takes type from arguments EX: DELETE .../v1/asset_type?type=server Returns: Response dict: {'code': int, 'payload': dict, 'details': dict} """ response_details = {} validate_response = validate_data('asset_info', request.args, ['type']) if not validate_response.get('success'): return validate_response.get('error') asset_type = request.args.get('type') abell_asset_type = AT.get_asset_type(asset_type) if not abell_asset_type: return abell_error(404, '%s asset type not found' % asset_type) r = abell_asset_type.remove_type() if r.get('success'): response_details.update({'info': 'Asset type %s deleted' % asset_type}) return abell_success(**response_details) return abell_error(400, r.get('message', 'Asset type delete error'))
def __init__(self, asset_type, abell_id, property_dict=None, asset_type_object=None): if not asset_type_object: asset_type_object = AT.get_asset_type(asset_type) self.asset_type_object = asset_type_object self.asset_type = asset_type_object.asset_type self.abell_id = str(abell_id) self.fields = {} self.__create_fields() if property_dict: self.update_keys(property_dict)
def update_asset_type(): """Update asset info Removes or adds keys to asset types. Also handles the mass update for new or removed keys for all assets of that type. Args: Takes a json formatted dict from the post which must contain the new data type. EX:{'type': 'server', 'remove_keys': ['key5'] 'managed_keys': ['key1','key2'], 'unmanaged_keys':['key3','key4']} Returns: Response dict: {'code': int, 'payload': null, 'details': {'new keys': [], 'removed_keys': [], 'info': str}} """ data = dict(request.get_json()) response_details = {} valid_data = validate_data('update', data, ['type']) if not valid_data.get('success'): return valid_data.get('error') asset_type = data.get('type') abell_asset_type = AT.get_asset_type(asset_type) if not abell_asset_type: return abell_error(404, '%s asset type not found' % asset_type) remove_keys = data.get('remove_keys', []) managed_keys = data.get('managed_keys', []) unmanaged_keys = data.get('unmanaged_keys', []) if (type(managed_keys) is not list or type(unmanaged_keys) is not list or type(remove_keys) is not list): return abell_error(400, 'managed_keys, unmanaged_keys and remove_keys ' 'must by lists') r = abell_asset_type.update_keys(remove_keys, managed_keys, unmanaged_keys) if r.get('success'): response_details.update({'info': 'Asset %s updated' % asset_type, 'removed_keys': r.get('removed_keys'), 'new_keys': r.get('new_keys')}) return abell_success(**response_details) return abell_error(500, r.get('message', 'Asset update error'))
def create_one_asset(): # Check if new keys are to be added to data type data = dict(request.get_json()) # flags = dict(request.args) response_details = {} # Ensure Data has required options validate_response = validate_data('create', data, ['owner', 'cloud', 'type', 'abell_id']) if not validate_response.get('success'): return validate_response.get('error') # Ensure asset type exists given_asset_type = data.get('type') abell_asset_type = AT.get_asset_type(given_asset_type) if not abell_asset_type: response_details.update({'submitted_data': data}) return abell_error(400, '%s asset type does not exist. Check submitted ' 'type or have an admin create it' % given_asset_type, **response_details) # Create new keys if create_keys flag is set if request.args.get('create_keys', 'false').lower() == 'true': r = abell_asset_type.add_new_keys(data.keys()) if not r.get('success'): return abell_error(r.get('error', 500), r.get('message', 'Error in asset create')) new_keys = r.get('new_keys') response_details.update({'new_keys_added': list(new_keys)}) # Create new asset new_asset = asset.AbellAsset(given_asset_type, data.get('abell_id'), data, abell_asset_type) r = new_asset.insert_asset() if r.get('success'): response_details.update({'info': r.get('message')}) return abell_success(**response_details) return abell_error(r.get('error', 500), r.get('message', 'Unknown create error'), **response_details)
def return_asset_info(): """Get asset info Returns asset type information Args: takes type from arguments EX: GET .../v1/asset_type?type=server Returns: Response dict: {'code': int, 'payload': dict, 'details': dict} """ validate_response = validate_data('asset_info', request.args, ['type']) if not validate_response.get('success'): return validate_response.get('error') asset_type = request.args.get('type') abell_asset_type = AT.get_asset_type(asset_type) if abell_asset_type: return abell_success(payload=abell_asset_type.key_dict()) return abell_error(404, '%s asset type not found' % asset_type)
def update_asset_values(asset_type, asset_filter, user_update_dict, auth_level='user', multi=False, upsert=False): response_dict = { 'success': False, 'error': None, 'message': None, 'updated_keys': {}, 'updated_asset_ids': [] } ato = AT.get_asset_type(asset_type) if not ato: response_dict.update({ 'error': 404, 'message': 'Type %s not found.' % asset_type }) return response_dict # TEMP Auth check, this will Change) if auth_level == 'admin': valid_keys = ato.managed_keys.union(ato.unmanaged_keys) for k, v in user_update_dict.items(): if k.split('.')[0] in valid_keys: response_dict['updated_keys'][k] = v else: valid_keys = ato.unmanaged_keys for k, v in user_update_dict.items(): if k.split('.')[0] in valid_keys: response_dict['updated_keys'][k] = v payload = model_tools.item_stringify(response_dict['updated_keys']) # Get abell ids of matching assets assets_to_update = asset_find(asset_type, asset_filter, specified_keys={ '_id': 0, 'abell_id': 1 }) assets_to_update = assets_to_update.get('result') if not multi: # check for UPSERT if len is 0 if upsert and len(assets_to_update) is 0: print('creating new asset') new_asset = AbellAsset(ato.asset_type, user_update_dict.get('abell_id'), user_update_dict, ato) r = new_asset.insert_asset() if r.get('success'): response_dict.update({ 'success': True, 'new_assets': r.get('message') }) return response_dict else: response_dict.update({ 'error': 500, 'message': 'Error creating asset.' }) return response_dict if len(assets_to_update) is not 1: response_dict.update({ 'error': 400, 'message': 'The filter did not return ' 'a single asset' }) return response_dict db_response = ABELLDB.update_asset(asset_type, asset_filter, payload) if db_response.get('success'): for a in assets_to_update: asset_id = a.get('abell_id') response_dict['updated_asset_ids'].append(asset_id) response_dict.update({ 'success': True, 'message': db_response.get('result') }) # TODO Probably convert to a bulk find and update call # TODO Log updated documents return response_dict