Skip to content

Commit 2495353

Browse files
committed
extractor: new per-project base extractor
This change creates a new base extractor that is aware of the project that we are getting the accounting for. This way the code gets much simpler, as it is not needed to keep track of projects, clients, etc. within the extrating methods. As a side effect, this change breaks the old ceilometer extractor.
1 parent 57c00f5 commit 2495353

File tree

3 files changed

+276
-168
lines changed

3 files changed

+276
-168
lines changed

caso/extract/base.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
@six.add_metaclass(abc.ABCMeta)
8383
class BaseExtractor(object):
8484
def __init__(self):
85+
# FIXME(aloga): add deprecated warning here
86+
LOG.warning("Using DEPRECATED extractor!! Please update as soon as "
87+
"possible!!")
8588
try:
8689
mapping = json.loads(open(CONF.mapping_file).read())
8790
except ValueError:
@@ -134,3 +137,51 @@ def extract_for_project(self, project, extract_from):
134137
135138
This method should be overriden in a subclass.
136139
"""
140+
141+
142+
@six.add_metaclass(abc.ABCMeta)
143+
class BaseProjectExtractor(object):
144+
def __init__(self, project):
145+
self.project = project
146+
147+
# FIXME(remove this)
148+
try:
149+
mapping = json.loads(open(CONF.mapping_file).read())
150+
except ValueError:
151+
# FIXME(aloga): raise a proper exception here
152+
raise
153+
else:
154+
self.voms_map = {}
155+
for vo, vomap in six.iteritems(mapping):
156+
tenant = vomap.get("tenant", None)
157+
tenants = vomap.get("tenants", [])
158+
if tenant is not None:
159+
warnings.warn("Using deprecated 'tenant' mapping, please "
160+
"use 'projects' instead", DeprecationWarning)
161+
if tenants:
162+
warnings.warn("Using deprecated 'tenants' mapping, please "
163+
"use 'projects' instead", DeprecationWarning)
164+
tenants.append(tenant)
165+
projects = vomap.get("projects", tenants)
166+
if not projects:
167+
LOG.warning("No project mapping found for VO %s" % vo)
168+
for project in projects:
169+
self.voms_map[project] = vo
170+
171+
def vm_status(self, status):
172+
"""Return the status corresponding to the OpenStack status.
173+
174+
:param status: OpenStack status.
175+
"""
176+
return openstack_vm_statuses.get(status.lower(), 'unknown')
177+
178+
@abc.abstractmethod
179+
def extract(self, extract_from):
180+
"""Extract records for a project from given date.
181+
182+
:param extract_from: datetime.datetime object indicating the date to
183+
extract records from
184+
:returns: A dictionary of {"server_id": caso.record.Record"}
185+
186+
This method should be overriden in a subclass.
187+
"""

caso/extract/manager.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from oslo_log import log
2323
import six
2424

25+
from caso.extract import base
2526
from caso import loading
2627

2728
cli_opts = [
@@ -65,7 +66,7 @@
6566
class Manager(object):
6667
def __init__(self):
6768
extractor = loading.get_available_extractors()[CONF.extractor]
68-
self.extractor = extractor()
69+
self.extractor = extractor
6970
self.last_run_base = os.path.join(CONF.spooldir, "lastrun")
7071

7172
def get_lastrun(self, project):
@@ -114,7 +115,6 @@ def get_records(self):
114115

115116
cloud_records = {}
116117
ip_records_total = {}
117-
extr = self.extractor
118118
for project in CONF.projects:
119119
LOG.info("Extracting records for project '%s'" % project)
120120

@@ -127,9 +127,19 @@ def get_records(self):
127127
LOG.debug("Extracting records from '%s'" % extract_from)
128128
LOG.debug("Extracting records to '%s'" % extract_to)
129129
try:
130-
records, ip_records = extr.extract_for_project(project,
131-
extract_from,
132-
extract_to)
130+
if issubclass(self.extractor, base.BaseProjectExtractor):
131+
extractor = self.extractor(project)
132+
records, ip_records = extractor.extract(
133+
extract_from,
134+
extract_to
135+
)
136+
else:
137+
extractor = self.extractor
138+
records, ip_records = extractor.extract_for_project(
139+
project,
140+
extract_from,
141+
extract_to
142+
)
133143
except Exception:
134144
LOG.exception("Cannot extract records for '%s', got "
135145
"the following exception: " % project)

0 commit comments

Comments
 (0)