Skip to content

Commit 57c00f5

Browse files
committed
nova extractor: refactor code to make it simpler
This change starts separating things into different methods, so that the code is simpler to maintain.
1 parent de4de15 commit 57c00f5

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

caso/extract/nova.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,44 @@ def _get_measure_time():
173173
measure_time = datetime.now()
174174
return measure_time
175175

176+
def _get_servers(self, nova, extract_from):
177+
servers = []
178+
limit = 200
179+
marker = None
180+
# Use a marker and iter over results until we do not have more to get
181+
while True:
182+
aux = nova.servers.list(
183+
search_opts={"changes-since": extract_from},
184+
limit=limit,
185+
marker=marker
186+
)
187+
servers.extend(aux)
188+
189+
if len(aux) < limit:
190+
break
191+
marker = aux[-1].id
192+
193+
servers = sorted(servers, key=operator.attrgetter("created"))
194+
return servers
195+
196+
def _get_images(self, glance):
197+
images = {image.id: image for image in glance.images.list()}
198+
return images
199+
200+
def _get_flavors(self, nova):
201+
flavors = {}
202+
for flavor in nova.flavors.list():
203+
flavors[flavor.id] = flavor.to_dict()
204+
flavors[flavor.id]["extra"] = flavor.get_keys()
205+
return flavors
206+
207+
def _get_vo(self, project):
208+
vo = self.voms_map.get(project)
209+
if vo is None:
210+
LOG.warning("No mapping could be found for project '%s', "
211+
"please check mapping file!", project)
212+
return vo
213+
176214
def extract_for_project(self, project, extract_from, extract_to):
177215
"""Extract records for a project from given date querying nova.
178216
@@ -197,26 +235,21 @@ def extract_for_project(self, project, extract_from, extract_to):
197235
ks_client = self._get_keystone_client(project)
198236

199237
# Membership in keystone can be direct (a user belongs to a project) or
200-
# via group membership.
238+
# via group membership, therefore we cannot get a list directly. We
239+
# will build this aftewards
201240
users = {}
241+
202242
project_id = nova.client.session.get_project_id()
243+
vo = self._get_vo(project)
203244

204-
flavors = {}
205-
for flavor in nova.flavors.list():
206-
flavors[flavor.id] = flavor.to_dict()
207-
flavors[flavor.id]["extra"] = flavor.get_keys()
245+
flavors = self._get_flavors(nova)
246+
images = self._get_images(glance)
208247

209-
images = {image.id: image for image in glance.images.list()}
210248
records = {}
211249
ip_records = {}
212250
ip_counts = {}
213251
floatings = []
214252

215-
vo = self.voms_map.get(project)
216-
if vo is None:
217-
LOG.warning("No mapping could be found for project '%s', "
218-
"please check mapping file!", project_id)
219-
220253
# We cannot use just 'changes-since' in the servers.list() API query,
221254
# as it will only include servers that have changed its status after
222255
# that date. However we cannot just get all the usages and then query
@@ -242,26 +275,9 @@ def extract_for_project(self, project, extract_from, extract_to):
242275

243276
# Lets start
244277

245-
# 1.- List all the deleted servers from that period.
246-
servers = []
247-
limit = 200
248-
marker = None
249278
ip = None
250-
# Use a marker and iter over results until we do not have more to get
251-
while True:
252-
aux = nova.servers.list(
253-
search_opts={"changes-since": extract_from},
254-
limit=limit,
255-
marker=marker
256-
)
257-
servers.extend(aux)
258-
259-
if len(aux) < limit:
260-
break
261-
marker = aux[-1].id
262-
263-
servers = sorted(servers, key=operator.attrgetter("created"))
264-
279+
# 1.- List all the deleted servers from that period.
280+
servers = self._get_servers(nova, extract_from)
265281
# 2.- Build the records for the period. Drop servers outside the period
266282
# (we do this manually as we cannot limit the query to a period, only
267283
# changes after start date).

0 commit comments

Comments
 (0)