Remove the fake and meta collectors

This is part of a global effort to clean up CloudKitty's
unmaintained codebase.

Change-Id: I60831f714c8a904170493a13bc82108d37d2c87d
Story: 2004400
Task: 28033
This commit is contained in:
Luka Peschke 2018-12-19 11:14:11 +01:00
parent dafd27011a
commit 2d7415a3d3
4 changed files with 4 additions and 255 deletions

View File

@ -1,165 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Objectif Libre
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# @author: Stéphane Albert
#
import csv
import json
from oslo_config import cfg
from cloudkitty import collector
fake_collector_opts = [
cfg.StrOpt('file',
default='/var/lib/cloudkitty/input.csv',
help='Collector input file.')]
cfg.CONF.register_opts(fake_collector_opts, 'fake_collector')
class CSVCollector(collector.BaseCollector):
collector_name = 'csvcollector'
dependencies = ('CloudKittyFormatTransformer', )
def __init__(self, transformers, **kwargs):
super(CSVCollector, self).__init__(transformers, **kwargs)
self.t_cloudkitty = self.transformers['CloudKittyFormatTransformer']
self._file = None
self._csv = None
def _open_csv(self):
filename = cfg.CONF.fake_collector.file
csvfile = open(filename, 'rb')
reader = csv.DictReader(csvfile)
self._file = csvfile
self._csv = reader
@classmethod
def get_metadata(cls, resource_name, transformers):
res = super(CSVCollector, cls).get_metadata(resource_name,
transformers)
try:
filename = cfg.CONF.fake_collector.file
csvfile = open(filename, 'rb')
reader = csv.DictReader(csvfile)
entry = None
for row in reader:
if row['type'] == resource_name:
entry = row
break
res['metadata'] = json.loads(entry['desc']).keys() if entry else {}
except IOError:
pass
return res
def filter_rows(self,
start,
end=None,
project_id=None,
res_type=None):
rows = []
for row in self._csv:
if int(row['begin']) == start:
if res_type:
if row['type'] == res_type:
rows.append(row)
else:
rows.append(row)
return rows
def _get_data(self,
res_type,
start,
end=None,
project_id=None,
q_filter=None):
self._open_csv()
rows = self.filter_rows(start, end, project_id, res_type=res_type)
data = []
for row in rows:
data.append({
'desc': json.loads(row['desc']),
'vol': json.loads(row['vol'])})
if not data:
raise collector.NoDataCollected(self.collector_name, res_type)
return self.t_cloudkitty.format_service(res_type, data)
def get_compute(self,
start,
end=None,
project_id=None,
q_filter=None):
return self._get_data('compute',
start,
end,
project_id,
q_filter)
def get_image(self,
start,
end=None,
project_id=None,
q_filter=None):
return self._get_data('image',
start,
end,
project_id,
q_filter)
def get_volume(self,
start,
end=None,
project_id=None,
q_filter=None):
return self._get_data('volume',
start,
end,
project_id,
q_filter)
def get_network_bw_in(self,
start,
end=None,
project_id=None,
q_filter=None):
return self._get_data('network.bw.in',
start,
end,
project_id,
q_filter)
def get_network_bw_out(self,
start,
end=None,
project_id=None,
q_filter=None):
return self._get_data('network.bw.out',
start,
end,
project_id,
q_filter)
def get_network_floating(self,
start,
end=None,
project_id=None,
q_filter=None):
return self._get_data('network.floating',
start,
end,
project_id,
q_filter)

View File

@ -1,88 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright 2014 Objectif Libre
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# @author: Stéphane Albert
#
from stevedore import extension
from cloudkitty import collector
from cloudkitty.db import api as db_api
COLLECTORS_NAMESPACE = 'cloudkitty.collector.backends'
class MetaCollector(collector.BaseCollector):
def __init__(self, transformers, **kwargs):
super(MetaCollector, self).__init__(transformers, **kwargs)
self._db = db_api.get_instance().get_service_to_collector_mapping()
self._collectors = {}
self._load_collectors()
self._mappings = {}
self._load_mappings()
def _load_mappings(self):
mappings = self._db.list_services()
for mapping in mappings:
db_mapping = self._db.get_mapping(mapping.service)
self._mappings[db_mapping.service] = db_mapping.collector
def _check_enabled(self, name):
enable_state = db_api.get_instance().get_module_info()
return enable_state.get_state('collector_{}'.format(name))
def _load_collectors(self):
self._collectors = {}
collectors = extension.ExtensionManager(
COLLECTORS_NAMESPACE,
)
collectors_list = collectors.names()
collectors_list.remove('meta')
for name in collectors_list:
if self._check_enabled(name):
self._collectors[name] = collectors[name].plugin(
self.transformers,
period=self.period)
def retrieve(self,
resource,
start,
end=None,
project_id=None,
q_filter=None):
collector_list = self._collectors.values()
# Set designated collector on top of the list
try:
collector_name = self._mappings[resource]
designated_collector = self._collectors[collector_name]
collector_list.remove(designated_collector)
collector_list.insert(0, designated_collector)
except KeyError:
pass
for cur_collector in collector_list:
# Try every collector until we get a result
try:
return cur_collector.retrieve(
resource,
start,
end,
project_id,
q_filter)
except NotImplementedError:
pass
raise NotImplementedError("No collector for resource '%s'." % resource)

View File

@ -0,0 +1,4 @@
---
deprecations:
- |
The fake and meta collectors have been removed from CloudKitty's codebase.

View File

@ -45,10 +45,8 @@ oslo.config.opts.defaults =
cloudkitty.common.config = cloudkitty.common.defaults:set_cors_middleware_defaults
cloudkitty.collector.backends =
fake = cloudkitty.collector.fake:CSVCollector
gnocchi = cloudkitty.collector.gnocchi:GnocchiCollector
monasca = cloudkitty.collector.monasca:MonascaCollector
meta = cloudkitty.collector.meta:MetaCollector
prometheus = cloudkitty.collector.prometheus:PrometheusCollector
cloudkitty.fetchers =