コード例 #1
0
    def get_content_type(self):
        """Determine content type of the request body.

        Does not do any body introspection, only checks header.
        """
        if "Content-Type" not in self.headers:
            return None

        allowed_types = SUPPORTED_CONTENT_TYPES
        content_type = self.content_type

        if content_type not in allowed_types:
            raise exception.InvalidContentType(content_type)

        return content_type
コード例 #2
0
    def metrics_config(self, req, body, id):
        """
        :param req:
        :param body:
        :param id:
        :return:
        """
        ctxt = req.environ['delfin.context']

        # check storage is registered
        db.storage_get(ctxt, id)

        metrics_config_dict = body
        metrics_config_dict.update(body)

        # get scheduler object
        schedule = config.Scheduler.getInstance()

        # The path of scheduler config file
        config_file = CONF.scheduler.config_path

        try:
            # Load the scheduler configuration file
            data = config.load_json_file(config_file)
            storage_found = False
            for storage in data.get("storages"):
                config_storage_id = storage.get('id')
                if config_storage_id == id:
                    for resource in metrics_config_dict.keys():
                        storage_dict = storage.get(resource)
                        metric_dict = metrics_config_dict.get(resource)
                        storage_dict.update(metric_dict)

                        interval = storage_dict.get('interval')
                        is_historic = storage_dict.get('is_historic')

                        job_id = id + resource

                        if schedule.get_job(job_id):
                            schedule.reschedule_job(job_id=job_id,
                                                    trigger='interval',
                                                    seconds=interval)
                        else:
                            schedule.add_job(
                                self.perf_collect,
                                'interval',
                                args=[id, interval, is_historic, resource],
                                seconds=interval,
                                next_run_time=datetime.now(),
                                id=job_id)

                        storage_found = True

            if not storage_found:
                temp_dict = {'id': id}
                temp_dict.update(metrics_config_dict)
                data.get("storages").append(temp_dict)

                for resource in metrics_config_dict.keys():
                    resource_dict = metrics_config_dict.get(resource)
                    interval = resource_dict.get('interval')
                    is_historic = resource_dict.get('is_historic')

                    job_id = id + resource

                    schedule.add_job(
                        self.perf_collect,
                        'interval',
                        args=[id, interval, is_historic, resource],
                        seconds=interval,
                        next_run_time=datetime.now(),
                        id=job_id)

            with open(config_file, "w") as jsonFile:
                json.dump(data, jsonFile)
                jsonFile.close()

        except TypeError as e:
            LOG.error("Error occurred during parsing of config file")
            raise exception.InvalidContentType(e)
        except json.decoder.JSONDecodeError as e:
            msg = ("Not able to open the config file: {0}".format(config_file))
            LOG.error(msg)
            raise exception.InvalidInput(e.msg)
        else:
            return metrics_config_dict
        finally:
            try:
                schedule.start()
            except Exception as e:
                LOG.debug("Scheduler is already running.{0}".format(e))