def obj_create(self, bundle, **kwargs): url, clusters = bundle.data['url'], bundle.data['clusters'] purger = VarnishPurger() if not isinstance(clusters, list): clusters = [clusters] servers = ServerExtractor().extract_servers_by_clusters( LogicalCluster.objects.filter(name__in=clusters)) raise ImmediateHttpResponse( self.create_json_response(purger.purge_url(url, servers), HttpResponse))
def test_should_prepare_headers_combinations(self): excepted_result = [{ 'header2': 'val1', 'header1': 'val1' }, { 'header2': 'val1', 'header1': 'val2' }, { 'header2': 'val2', 'header1': 'val1' }, { 'header2': 'val2', 'header1': 'val2' }] headers = {"header1": ["val1", "val2"], "header2": ["val1", "val2"]} result = VarnishPurger().prepare_headers_combinations(headers) self.assertCountEqual(result, excepted_result)
def obj_create(self, bundle, **kwargs): try: if not bundle.request.user.is_staff: raise Unauthorized() except Unauthorized as e: self.unauthorized_result(e) url, clusters = bundle.data['url'], bundle.data['clusters'] purger = VarnishPurger() if not isinstance(clusters, list): clusters = [clusters] servers = ServerExtractor().extract_servers_by_clusters( LogicalCluster.objects.filter(name__in=clusters)) raise ImmediateHttpResponse( self.create_json_response(purger.purge_url(url, servers), HttpResponse))
def purge_view(request): if request.method == 'POST': form = PurgeForm(request.POST) if form.is_valid(): cluster = LogicalCluster.objects.get(pk=form.cleaned_data['cluster'].pk) servers = ServerExtractor().extract_servers_by_clusters([cluster]) result = VarnishPurger().purge_url(form.cleaned_data['url'], servers) messages.warning( request, 'Url {} purged from cluster {} - cleaned {} server(s), errors occurred for {} server(s)'.format( form.cleaned_data['url'], cluster.name, len(result['success']), len(result['error']) ) ) return HttpResponseRedirect('/') else: form = PurgeForm() return render(request, 'purge_form.html', {'form': form, 'has_permission': True})
def obj_create(self, bundle, **kwargs): try: if not bundle.request.user.is_staff: raise Unauthorized() except Unauthorized as e: self.unauthorized_result(e) self.is_valid(bundle) if bundle.errors: raise ImmediateHttpResponse(response=self.error_response(bundle.request, bundle.errors)) url, clusters, headers = bundle.data['url'], bundle.data['clusters'], bundle.data.get('headers') purger = VarnishPurger() if not isinstance(clusters, list): clusters = [clusters] servers = ServerExtractor().extract_servers_by_clusters(LogicalCluster.objects.filter(name__in=clusters)) purger_result = purger.purge_url(url, servers, headers) if len(purger_result.get("error")) > 0: raise ImmediateHttpResponse(self.create_json_response(purger_result, HttpApplicationError)) raise ImmediateHttpResponse(self.create_json_response(purger_result, HttpResponse))
def test_should_report_error_on_generic_exception(self): expected_error = "Unexpected error: Unknown error, url=http://example.com/modified_resource/" servers = [VarnishServer(ip='127.0.0.4')] response = VarnishPurger().purge_url('http://example.com/modified_resource/', servers) self.assertEqual(1, len(response['error'])) self.assertEqual(expected_error, response['error']['127.0.0.4'])
def test_should_report_error_on_bad_status_line(self): expected_error = "Bad status line from varnish server, url=http://example.com/modified_resource/" servers = [VarnishServer(ip='127.0.0.3')] response = VarnishPurger().purge_url('http://example.com/modified_resource/', servers) self.assertEqual(1, len(response['error'])) self.assertEqual(expected_error, response['error']['127.0.0.3'])
def test_should_purge_active_varnishes(self, response_mock): expected_message = "varnish http response code: 200, url=http://example.com/modified_resource/" servers = [VarnishServer(ip='127.0.0.1'), VarnishServer(ip='127.0.0.2')] response = VarnishPurger().purge_url('http://example.com/modified_resource/', servers) self.assertEqual(2, len(response['success'])) self.assertEqual(expected_message, response['success']['127.0.0.1'])