Commit ef8d7fb9 authored by Simon Leinen's avatar Simon Leinen

Improved Nova endpoint handling

Provoke and catch token-too-big errors.
parent 5c8aa058
...@@ -32,6 +32,7 @@ from keystoneclient.v2_0 import client ...@@ -32,6 +32,7 @@ from keystoneclient.v2_0 import client
from keystoneclient.auth.identity import v2 from keystoneclient.auth.identity import v2
from keystoneclient import session from keystoneclient import session
from novaclient.client import Client from novaclient.client import Client
from novaclient.exceptions import BadRequest
import sys import sys
class UsageAnalyzer: class UsageAnalyzer:
...@@ -49,17 +50,16 @@ class UsageAnalyzer: ...@@ -49,17 +50,16 @@ class UsageAnalyzer:
self.password = password self.password = password
self.tenant_name = tenant_name self.tenant_name = tenant_name
self.auth_url = auth_url self.auth_url = auth_url
self.auth = v2.Password(auth_url=auth_url, self.keystone = client.Client(username=self.username,
username=username, password=self.password,
password=password, tenant_name=self.tenant_name,
tenant_name=tenant_name) auth_url=self.auth_url)
self.sess = session.Session(auth=self.auth)
self.keystone = client.Client(session=self.sess)
self.usage_per_tenant = dict() self.usage_per_tenant = dict()
self.tenants = None self.tenants = None
self.cost_centers = None self.cost_centers = None
self.regions = None self.regions = None
self._nova_endpoints = None
self.cc_re = re.compile(".* \[(.*)\]") self.cc_re = re.compile(".* \[(.*)\]")
...@@ -73,6 +73,8 @@ class UsageAnalyzer: ...@@ -73,6 +73,8 @@ class UsageAnalyzer:
return self.regions return self.regions
def nova_endpoints(self): def nova_endpoints(self):
if self._nova_endpoints is not None:
return self._nova_endpoints
ne = dict() ne = dict()
for region in self.get_regions(): for region in self.get_regions():
try: try:
...@@ -89,13 +91,20 @@ class UsageAnalyzer: ...@@ -89,13 +91,20 @@ class UsageAnalyzer:
## ##
auth_token=self.keystone.auth_token auth_token=self.keystone.auth_token
) )
except Error as c: ##
print(c) ## In order to check whether this, we need to actually
## try to run an arbitrary operation using the
## endpoint. If our token is too large, we'll receive
## an error that we'll catch below.
##
ne[region].flavors.list()
except BadRequest as c:
ne[region] = Client(3, ne[region] = Client(3,
self.username, self.password, self.username, self.password,
self.tenant_name, self.auth_url, self.tenant_name, self.auth_url,
service_type="compute", service_type="compute",
region_name=region) region_name=region)
self._nova_endpoints = ne
return ne return ne
def cc_name_from_tenant_1(self, tenant, cc_name, fine_grained=True): def cc_name_from_tenant_1(self, tenant, cc_name, fine_grained=True):
...@@ -219,11 +228,11 @@ class UsageAnalyzer: ...@@ -219,11 +228,11 @@ class UsageAnalyzer:
return ['ACTIVE', 'SUSPENDED', 'ERROR'] return ['ACTIVE', 'SUSPENDED', 'ERROR']
return ['ACTIVE', 'SUSPENDED', 'ERROR', 'SHUTOFF'] return ['ACTIVE', 'SUSPENDED', 'ERROR', 'SHUTOFF']
def title(per_region, active_only, undead_only): def title():
return "Status " + ", ".join(status_set(active_only, undead_only)) return "Status " + ", ".join(status_set(active_only, undead_only))
def out_title(): def out_title():
print(title(active_only, undead_only), "\n") print(title() + "\n")
## All cost centers that total less than this ## All cost centers that total less than this
## are considered "small" and thrown into one bin. ## are considered "small" and thrown into one bin.
...@@ -281,6 +290,7 @@ class UsageAnalyzer: ...@@ -281,6 +290,7 @@ class UsageAnalyzer:
sys.stdout.write(" {:s}".format(usage['total'].tostr())) sys.stdout.write(" {:s}".format(usage['total'].tostr()))
sys.stdout.write("\n") sys.stdout.write("\n")
out_title()
for cc_name in sorted(self.get_cost_centers().keys(), for cc_name in sorted(self.get_cost_centers().keys(),
reverse=True, reverse=True,
key=lambda cc_name: usage_per_cc[cc_name]['total'].ram): key=lambda cc_name: usage_per_cc[cc_name]['total'].ram):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment