summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVishvananda Ishaya <vishvananda@gmail.com>2011-08-30 12:01:18 -0700
committerVishvananda Ishaya <vishvananda@gmail.com>2011-08-30 12:01:18 -0700
commitdcf5970dd9bed27201c593d7d053970a632e5eee (patch)
tree5b3b765ac87f9f27fd73aad40369a8efadd1832b
parent30a1d84529cfc093b1652ac9bb72871f8bc3ce36 (diff)
downloadnova-dcf5970dd9bed27201c593d7d053970a632e5eee.tar.gz
nova-dcf5970dd9bed27201c593d7d053970a632e5eee.tar.xz
nova-dcf5970dd9bed27201c593d7d053970a632e5eee.zip
make two functions instead of fast flag and add compute api commands instead of hitting db directly
-rwxr-xr-xbin/instance-usage-audit5
-rw-r--r--nova/api/openstack/contrib/simple_tenant_usage.py14
-rw-r--r--nova/compute/api.py17
-rw-r--r--nova/db/api.py16
-rw-r--r--nova/db/sqlalchemy/api.py36
-rw-r--r--nova/tests/api/openstack/contrib/test_simple_tenant_usage.py10
6 files changed, 62 insertions, 36 deletions
diff --git a/bin/instance-usage-audit b/bin/instance-usage-audit
index a06c6b1b3..7ce5732e7 100755
--- a/bin/instance-usage-audit
+++ b/bin/instance-usage-audit
@@ -102,9 +102,8 @@ if __name__ == '__main__':
logging.setup()
begin, end = time_period(FLAGS.instance_usage_audit_period)
print "Creating usages for %s until %s" % (str(begin), str(end))
- instances = db.instance_get_active_by_window(context.get_admin_context(),
- begin,
- end)
+ ctxt = context.get_admin_context()
+ instances = db.instance_get_active_by_window_joined(ctxt, begin, end)
print "%s instances" % len(instances)
for instance_ref in instances:
usage_info = utils.usage_from_instance(instance_ref,
diff --git a/nova/api/openstack/contrib/simple_tenant_usage.py b/nova/api/openstack/contrib/simple_tenant_usage.py
index 16e712815..363ac1451 100644
--- a/nova/api/openstack/contrib/simple_tenant_usage.py
+++ b/nova/api/openstack/contrib/simple_tenant_usage.py
@@ -19,10 +19,9 @@ import urlparse
import webob
from datetime import datetime
-from nova import db
from nova import exception
from nova import flags
-from nova.compute import instance_types
+from nova.compute import api
from nova.api.openstack import extensions
from nova.api.openstack import views
from nova.db.sqlalchemy.session import get_session
@@ -71,11 +70,11 @@ class SimpleTenantUsageController(object):
def _tenant_usages_for_period(self, context, period_start,
period_stop, tenant_id=None, detailed=True):
- instances = db.instance_get_active_by_window(context,
+ compute_api = api.API()
+ instances = compute_api.get_active_by_window(context,
period_start,
period_stop,
- tenant_id,
- fast=True)
+ tenant_id)
from nova import log as logging
logging.info(instances)
rval = {}
@@ -90,8 +89,9 @@ class SimpleTenantUsageController(object):
if not flavors.get(flavor_type):
try:
- flavors[flavor_type] = db.instance_type_get(context,
- flavor_type)
+ it_ref = compute_api.get_instance_type(context,
+ flavor_type)
+ flavors[flavor_type] = it_ref
except exception.InstanceTypeNotFound:
# can't bill if there is no instance type
continue
diff --git a/nova/compute/api.py b/nova/compute/api.py
index 3b4bde8ea..53bab53a4 100644
--- a/nova/compute/api.py
+++ b/nova/compute/api.py
@@ -19,13 +19,11 @@
"""Handles all requests relating to instances (guest vms)."""
-import eventlet
import novaclient
import re
import time
from nova import block_device
-from nova import db
from nova import exception
from nova import flags
import nova.image
@@ -237,7 +235,7 @@ class API(base.Base):
self.ensure_default_security_group(context)
if key_data is None and key_name:
- key_pair = db.key_pair_get(context, context.user_id, key_name)
+ key_pair = self.db.key_pair_get(context, context.user_id, key_name)
key_data = key_pair['public_key']
if reservation_id is None:
@@ -802,6 +800,15 @@ class API(base.Base):
"args": {"topic": FLAGS.compute_topic,
"instance_id": instance_id}})
+ def get_active_by_window(self, context, begin, end=None, project_id=None):
+ """Get instances that were continuously active over a window."""
+ return self.db.instance_get_active_by_window(context, begin, end,
+ project_id)
+
+ def get_instance_type(self, context, instance_type_id):
+ """Get an instance type by instance type id."""
+ return self.db.instance_type_get(context, instance_type_id)
+
def get(self, context, instance_id):
"""Get a single instance with the given instance_id."""
# NOTE(sirp): id used to be exclusively integer IDs; now we're
@@ -1001,7 +1008,7 @@ class API(base.Base):
:param extra_properties: dict of extra image properties to include
"""
- instance = db.api.instance_get(context, instance_id)
+ instance = self.db.api.instance_get(context, instance_id)
properties = {'instance_uuid': instance['uuid'],
'user_id': str(context.user_id),
'image_state': 'creating',
@@ -1026,7 +1033,7 @@ class API(base.Base):
def rebuild(self, context, instance_id, image_href, admin_password,
name=None, metadata=None, files_to_inject=None):
"""Rebuild the given instance with the provided metadata."""
- instance = db.api.instance_get(context, instance_id)
+ instance = self.db.instance_get(context, instance_id)
if instance["state"] == power_state.BUILDING:
msg = _("Instance already building")
diff --git a/nova/db/api.py b/nova/db/api.py
index 3233985b6..148887635 100644
--- a/nova/db/api.py
+++ b/nova/db/api.py
@@ -496,14 +496,20 @@ def instance_get_all_by_filters(context, filters):
return IMPL.instance_get_all_by_filters(context, filters)
-def instance_get_active_by_window(context, begin, end=None,
- project_id=None, fast=False):
+def instance_get_active_by_window(context, begin, end=None, project_id=None):
"""Get instances active during a certain time window.
- Setting fast to true will stop all joinedloads.
Specifying a project_id will filter for a certain project."""
- return IMPL.instance_get_active_by_window(context, begin, end,
- project_id, fast)
+ return IMPL.instance_get_active_by_window(context, begin, end, project_id)
+
+
+def instance_get_active_by_window_joined(context, begin, end=None,
+ project_id=None):
+ """Get instances and joins active during a certain time window.
+
+ Specifying a project_id will filter for a certain project."""
+ return IMPL.instance_get_active_by_window_joined(context, begin, end,
+ project_id)
def instance_get_all_by_user(context, user_id):
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py
index c4cc199eb..d76dc22ed 100644
--- a/nova/db/sqlalchemy/api.py
+++ b/nova/db/sqlalchemy/api.py
@@ -1306,19 +1306,33 @@ def instance_get_all_by_filters(context, filters):
return instances
-@require_admin_context
-def instance_get_active_by_window(context, begin, end=None,
- project_id=None, fast=False):
- """Return instances that were continuously active over the given window"""
+@require_context
+def instance_get_active_by_window(context, begin, end=None, project_id=None):
+ """Return instances that were continuously active over window."""
session = get_session()
- query = session.query(models.Instance)
- if not fast:
- query = query.options(joinedload_all('fixed_ips.floating_ips')).\
- options(joinedload('security_groups')).\
- options(joinedload_all('fixed_ips.network')).\
- options(joinedload('instance_type'))
+ query = session.query(models.Instance).\
+ filter(models.Instance.launched_at < begin)
+ if end:
+ query = query.filter(or_(models.Instance.terminated_at == None,
+ models.Instance.terminated_at > end))
+ else:
+ query = query.filter(models.Instance.terminated_at == None)
+ if project_id:
+ query = query.filter_by(project_id=project_id)
+ return query.all()
+
- query = query.filter(models.Instance.launched_at < begin)
+@require_admin_context
+def instance_get_active_by_window_joined(context, begin, end=None,
+ project_id=None):
+ """Return instances and joins that were continuously active over window."""
+ session = get_session()
+ query = session.query(models.Instance).\
+ options(joinedload_all('fixed_ips.floating_ips')).\
+ options(joinedload('security_groups')).\
+ options(joinedload_all('fixed_ips.network')).\
+ options(joinedload('instance_type')).\
+ filter(models.Instance.launched_at < begin)
if end:
query = query.filter(or_(models.Instance.terminated_at == None,
models.Instance.terminated_at > end))
diff --git a/nova/tests/api/openstack/contrib/test_simple_tenant_usage.py b/nova/tests/api/openstack/contrib/test_simple_tenant_usage.py
index 2bd619820..de0a6d779 100644
--- a/nova/tests/api/openstack/contrib/test_simple_tenant_usage.py
+++ b/nova/tests/api/openstack/contrib/test_simple_tenant_usage.py
@@ -20,9 +20,9 @@ import json
import webob
from nova import context
-from nova import db
from nova import flags
from nova import test
+from nova.compute import api
from nova.tests.api.openstack import fakes
@@ -39,7 +39,7 @@ STOP = datetime.datetime.utcnow()
START = STOP - datetime.timedelta(hours=HOURS)
-def fake_instance_type_get(context, instance_type_id):
+def fake_instance_type_get(self, context, instance_type_id):
return {'id': 1,
'vcpus': VCPUS,
'local_gb': LOCAL_GB,
@@ -59,7 +59,7 @@ def get_fake_db_instance(start, end, instance_id, tenant_id):
'launched_at': start,
'terminated_at': end}
-def fake_instance_get_active_by_window(context, begin, end, project_id, fast):
+def fake_instance_get_active_by_window(self, context, begin, end, project_id):
return [get_fake_db_instance(START,
STOP,
x,
@@ -70,9 +70,9 @@ def fake_instance_get_active_by_window(context, begin, end, project_id, fast):
class SimpleTenantUsageTest(test.TestCase):
def setUp(self):
super(SimpleTenantUsageTest, self).setUp()
- self.stubs.Set(db, "instance_type_get",
+ self.stubs.Set(api.API, "get_instance_type",
fake_instance_type_get)
- self.stubs.Set(db, "instance_get_active_by_window",
+ self.stubs.Set(api.API, "get_active_by_window",
fake_instance_get_active_by_window)
self.admin_context = context.RequestContext('fakeadmin_0',
'faketenant_0',