Skip to content

Instantly share code, notes, and snippets.

@kahou82
Created November 19, 2014 22:17
Show Gist options
  • Save kahou82/1bdb4a2c7fbbe44c9fdf to your computer and use it in GitHub Desktop.
Save kahou82/1bdb4a2c7fbbe44c9fdf to your computer and use it in GitHub Desktop.
Patch for getting usage from nova
class LimitsController(object):
"""Controller for accessing limits in the OpenStack API."""
@wsgi.serializers(xml=LimitsTemplate)
def index(self, req):
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)
"""Return all global and rate limit information."""
context = req.environ['nova.context']
if 'QUERY_STRING' in req.environ:
qry = req.environ['QUERY_STRING']
qry = qry.split("=")
tenant_id = qry[1]
LOG.error(qry)
else:
tenant_id = context.project_id
#LOG.error(type(req))
#LOG.error(req.environ)
#LOG.error("get Project quotas")
#LOG.error(type(QUOTAS))
#LOG.error(tenant_id)
quotas = QUOTAS.get_project_quotas(context, tenant_id,
usages=False)
#LOG.error("Hi I am here----------------------------------")
#LOG.error(quotas)
#LOG.error(context.project_id)
#print "Hi I am here"
abs_limits = dict((k, v['limit']) for k, v in quotas.items())
rate_limits = req.environ.get("nova.limits", [])
builder = self._get_view_builder(req)
LOG.error("done!")
return builder.build(rate_limits, abs_limits)
def create(self, req, body):
"""Create a new limit."""
raise webob.exc.HTTPNotImplemented()
def delete(self, req, id):
"""Delete the limit."""
raise webob.exc.HTTPNotImplemented()
def detail(self, req):
"""Return limit details."""
raise webob.exc.HTTPNotImplemented()
def show(self, req, id):
"""Show limit information."""
raise webob.exc.HTTPNotImplemented()
def update(self, req, id, body):
"""Update existing limit."""
raise webob.exc.HTTPNotImplemented()
def _get_view_builder(self, req):
return limits_views.ViewBuilder()
class UsedLimitsController(wsgi.Controller):
@staticmethod
def _reserved(req):
try:
return int(req.GET['reserved'])
except (ValueError, KeyError):
return False
@wsgi.extends
def index(self, req, resp_obj):
resp_obj.attach(xml=UsedLimitsTemplate())
context = req.environ['nova.context']
LOG.error("here")
if 'QUERY_STRING' in req.environ:
qry = req.environ['QUERY_STRING']
qry = qry.split("=")
tenant_id = qry[1]
LOG.error(qry)
else:
tenant_id = context.project_id
quotas = QUOTAS.get_project_quotas(context, tenant_id,
usages=True)
quota_map = {
'totalRAMUsed': 'ram',
'totalCoresUsed': 'cores',
'totalInstancesUsed': 'instances',
'totalFloatingIpsUsed': 'floating_ips',
'totalSecurityGroupsUsed': 'security_groups',
}
used_limits = {}
for display_name, quota in quota_map.iteritems():
if quota in quotas:
reserved = (quotas[quota]['reserved']
if self._reserved(req) else 0)
used_limits[display_name] = quotas[quota]['in_use'] + reserved
resp_obj.obj['limits']['absolute'].update(used_limits)
f = open("/vagrant/abc.txt", "w")
f.write(str(req.environ))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment