Ejemplo n.º 1
0
    def post(self, request, content_type, object_id):
        form = self.get_form()
        if form.is_valid():
            form.instance.set_object(content_type, object_id)
            prometheus.check_rules([form.instance])

            try:
                # Set an instance of our service here so that we can pass it
                # along for promtool to render
                form.instance.set_object(content_type, object_id)
                prometheus.check_rules([form.instance])
            except Exception as e:
                form._update_errors(e)
                return self.form_invalid(form)

            form.instance.save()
            return HttpResponseRedirect(form.instance.get_absolute_url())

        if 'rules' not in request.POST:
            return self.form_invalid(form)

        importform = forms.ImportRuleForm(request.POST)
        service = get_object_or_404(models.Service, id=self.kwargs['pk'])
        if importform.is_valid():
            data = importform.clean()
            counters = prometheus.import_rules(data['rules'], service)
            messages.info(request, 'Imported %s' % counters)
            return HttpResponseRedirect(service.get_absolute_url())

        return self.form_invalid(form)
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = form_class(request.POST, request.FILES)

        if 'rules' in request.POST:
            form = forms.ImportRuleForm(request.POST)
            if form.is_valid():
                data = form.clean()
                counters = prometheus.import_rules(data['rules'])
                messages.info(request, 'Imported %s' % counters)
                return self.form_valid(form)
            else:
                return self.form_invalid(form)

        if form.is_valid():
            data = form.clean()
            if data.get('file_field'):
                messages.info(request, 'Importing config from file')
                config = data['file_field'].read().decode('utf8')
            elif data.get('url'):
                messages.info(request, 'Importing config from url')
                response = util.get(data['url'])
                response.raise_for_status()
                config = response.text
            else:
                messages.info(request, 'Importing config')
                config = data['config']

            kwargs = {}
            # This also lets us catch passing an empty string to signal using
            # the shard value from the post request
            if data.get('shard'):
                kwargs['replace_shard'] = data.get('shard')

            imported, skipped = prometheus.import_config(json.loads(config), **kwargs)

            if imported:
                counters = {key: len(imported[key]) for key in imported}
                messages.info(request, 'Imported %s' % counters)

            if skipped:
                counters = {key: len(skipped[key]) for key in skipped}
                messages.info(request, 'Skipped %s' % counters)

            # If we only have a single object in a category, automatically
            # redirect to that category to make things easier to understand
            if len(imported['Project']) == 1:
                return HttpResponseRedirect(imported['Project'][0].get_absolute_url())
            if len(imported['Service']) == 1:
                return HttpResponseRedirect(imported['Service'][0].get_absolute_url())
            if len(imported['Shard']) == 1:
                return HttpResponseRedirect(imported['Shard'][0].get_absolute_url())

            # otherwise we can just use the default behavior
            return self.form_valid(form)
        else:
            return self.form_invalid(form)
Ejemplo n.º 3
0
    def form_valid(self, form):
        data = form.clean()
        if data.get('file_field'):
            rules = data['file_field'].read().decode('utf8')
        elif data.get('rules'):
            rules = data.get('rules')
        else:
            messages.warning(self.request, 'Missing rules')
            return self.form_invalid(form)

        try:
            counters = prometheus.import_rules(rules)
            messages.info(self.request, 'Imported %s' % counters)
            return redirect('rule-import')
        except:
            messages.error(self.request, 'Error importing rules')
            return self.form_invalid(form)