def rip_expired_instances(): """Run periodically to remove expired instances""" server_mgr = admin_client_set().compute.servers for instance_data in InstanceDataDAO.expired_list(datetime.utcnow()): try: server_mgr.delete(instance_data.instance_id) AuditDAO.create_record(dict( resource=url_for('instances.delete_instance', instance_id=instance_data.instance_id), method='DELETE', response_status=200, message='Automatically deleted expired instance', )) except osc_exc.NotFound: InstanceDataDAO.delete(instance_data.instance_id) except Exception: current_app.logger.exception('Failed to delete expired instance %r' % instance_data.instance_id)
def save_audit_data(response): data = g.audit_data status = response.status_code if current_app.config['AUDIT_VERBOSITY'] < 2: # don't save certain data if data['method'] not in ('POST', 'PUT', 'DELETE'): return response if status not in _INTERESTING_STATUSES: return response if data.get('response_status') is None: data['response_status'] = status if data['message'] is None and status in (200, 201, 202, 204): data['message'] = 'OK' AuditDAO.create_record(data) return response
def rip_expired_instances(): """Run periodically to remove expired instances""" server_mgr = admin_client_set().compute.servers for instance_data in InstanceDataDAO.expired_list(datetime.utcnow()): try: server_mgr.delete(instance_data.instance_id) AuditDAO.create_record( dict( resource=url_for('instances.delete_instance', instance_id=instance_data.instance_id), method='DELETE', response_status=200, message='Automatically deleted expired instance', )) except osc_exc.NotFound: InstanceDataDAO.delete(instance_data.instance_id) except Exception: current_app.logger.exception( 'Failed to delete expired instance %r' % instance_data.instance_id)
def test_get(self): record = AuditDAO.get(self.record_id) self.assertEquals(record.message, 'OK') self.assertEquals(record.resource_id, 'test') self.assertEquals(record.user_id, 'UID') self.assertEquals(record.response_status, 200) delta = datetime.utcnow() - record.timestamp # assume tests are rather fast self.assertTrue(delta <= timedelta(seconds=5)) # but not faster then light self.assertTrue(delta >= timedelta(0))
def setUp(self): super(AuditDAOTestCase, self).setUp() data = { 'extra': {}, 'message': 'OK', 'method': 'POST', 'project_id': 'PID', 'remote_address': '127.0.0.1', 'resource': '/test', 'resource_id': 'test', 'response_status': 200, 'user_id': 'UID' } record = AuditDAO.create_record(data) self.record_id = record.record_id
def test_list_all(self): l = list(AuditDAO.list_all()) self.assertEquals(l, [AuditDAO.get(self.record_id)])
def list_all_records(): parse_collection_request(_SCHEMA) result = [record_to_view(record) for record in AuditDAO.list_all()] return make_collection_response(u'audit-log', result)
def get_log_record(record_id): result = record_to_view(AuditDAO.get(record_id)) return make_json_response(result)