diff --git a/artifice/api/web.py b/artifice/api/web.py index dc38124..8ea6315 100644 --- a/artifice/api/web.py +++ b/artifice/api/web.py @@ -102,11 +102,11 @@ def keystone(func): def collect_usage(tenant, db, session, resp, end): timestamp = datetime.now() - db.insert_tenant(tenant.conn['id'], tenant.conn['name'], - tenant.conn['description'], timestamp) + db.insert_tenant(tenant.id, tenant.name, + tenant.description, timestamp) session.begin(subtransactions=True) start = session.query(func.max(UsageEntry.end).label('end')).\ - filter(UsageEntry.tenant_id == tenant.conn['id']).first().end + filter(UsageEntry.tenant_id == tenant.id).first().end if not start: start = datetime.strptime(dawn_of_time, iso_date) @@ -118,7 +118,7 @@ def collect_usage(tenant, db, session, resp, end): try: session.commit() resp["tenants"].append( - {"id": tenant.conn['id'], + {"id": tenant.id, "updated": True, "start": start.strftime(iso_time), "end": end.strftime(iso_time) @@ -127,7 +127,7 @@ def collect_usage(tenant, db, session, resp, end): except sqlalchemy.exc.IntegrityError: # this is fine. resp["tenants"].append( - {"id": tenant.conn['id'], + {"id": tenant.id, "updated": False, "error": "Integrity error", "start": start.strftime(iso_time), @@ -146,24 +146,30 @@ def run_usage_collection(): The volume will be parsed from JSON as a Decimal object. """ + try: - session = Session() - - artifice = interface.Artifice() - db = database.Database(session) + session = Session() + + artifice = interface.Artifice() + db = database.Database(session) - tenants = artifice.tenants + tenants = artifice.tenants - end = datetime.now().\ - replace(minute=0, second=0, microsecond=0) + end = datetime.now().\ + replace(minute=0, second=0, microsecond=0) - resp = {"tenants": [], "errors": 0} + resp = {"tenants": [], "errors": 0} - for tenant in tenants: - collect_usage(tenant, db, session, resp, end) + for tenant in tenants: + collect_usage(tenant, db, session, resp, end) - session.close() - return json.dumps(resp) + session.close() + return json.dumps(resp) + + except Exception as e: + print 'Exception escaped!', type(e), e + import traceback + traceback.print_exc() def generate_sales_order(tenant, session, end, rates): diff --git a/artifice/database.py b/artifice/database.py index 04358cf..b3220c5 100644 --- a/artifice/database.py +++ b/artifice/database.py @@ -53,6 +53,7 @@ class Database(object): end=end, created=timestamp ) + print entry self.session.add(entry) self.session.flush() except TransformerValidationError: diff --git a/artifice/helpers.py b/artifice/helpers.py index 61bf2a2..40068fe 100644 --- a/artifice/helpers.py +++ b/artifice/helpers.py @@ -1,12 +1,19 @@ from novaclient.v1_1 import client import config +cache = {} def flavor_name(f_id): - nova = client.Client( - config.auth['username'], - config.auth['password'], - config.auth['default_tenant'], - config.auth['end_point'], - service_type="compute") - return nova.flavors.get(f_id).name + f_id = int(f_id) + + if f_id not in cache: + nova = client.Client( + config.auth['username'], + config.auth['password'], + config.auth['default_tenant'], + config.auth['end_point'], + service_type="compute", + insecure=config.auth['insecure']) + + cache[f_id] = nova.flavors.get(f_id).name + return cache[f_id] diff --git a/artifice/interface.py b/artifice/interface.py index dc0d5ef..7d39c4a 100644 --- a/artifice/interface.py +++ b/artifice/interface.py @@ -76,20 +76,15 @@ class Tenant(object): self._meters = set() self._resources = None - def __getitem__(self, item): - - try: - return getattr(self.tenant, item) - except AttributeError: - try: - return self.tenant[item] - except KeyError: - raise KeyError("No such key '%s' in tenant" % item) - - def __getattr__(self, attr): - if attr not in self.tenant: - return object.__getattribute__(self, attr) - return self.tenant[attr] + @property + def id(self): + return self.tenant.id + @property + def name(self): + return self.tenant.name + @property + def description(self): + return self.tenant.description def resources(self, start, end): if not self._resources: diff --git a/client/client.py b/client/client.py index 189bfc2..4a8eeae 100644 --- a/client/client.py +++ b/client/client.py @@ -1,5 +1,6 @@ import requests from requests.exceptions import ConnectionError +import json class Client(object): @@ -9,20 +10,20 @@ class Client(object): self.auth_token = kwargs.get('token') def usage(self, tenants): - url = self.endpoint + "usage" + url = self.endpoint + "collect_usage" data = {"tenants": tenants} try: response = requests.post(url, headers={"Content-Type": "application/json", "token": self.auth_token}, - data=data) + data=json.dumps(data)) if response.status_code != 200: raise AttributeError("Usage cycle failed: " + response.text + " code: " + str(response.status_code)) - except ConnectionError: - pass + except ConnectionError as e: + print e def sales_order(self, tenants): url = self.endpoint + "sales_order" @@ -37,5 +38,5 @@ class Client(object): raise AttributeError("Sales order cycle failed: " + response.text + " code: " + str(response.status_code)) - except ConnectionError: - pass + except ConnectionError as e: + print e diff --git a/client/shell.py b/client/shell.py index 52870f7..1db14e2 100644 --- a/client/shell.py +++ b/client/shell.py @@ -62,7 +62,7 @@ if __name__ == '__main__': args = parser.parse_args() - conf = {'api': {'endpoint': 'http://0.0.0.0/', + conf = {'api': {'endpoint': 'http://0.0.0.0:8000/', 'token': 'sah324sdf5wad4dh839uhjuUH'}} # try: diff --git a/scripts/initdb.py b/scripts/initdb.py deleted file mode 100644 index cd806c9..0000000 --- a/scripts/initdb.py +++ /dev/null @@ -1,25 +0,0 @@ -import sys, os - -loc = None -try: - loc, fn = os.path.split(__file__) -except NameError: - loc = os.getcwd() - - -sys.path.insert(0, os.path.abspath(os.path.join(loc +"/../artifice"))) - -from models import usage, resources, tenants, Session, Base -# string = 'postgresql://%(username)s:%(password)s@%(host)s:%(port)s/%(database)s' -# conn_string = string % {'username':'aurynn', 'host':'localhost', 'port':5433, 'password':'aurynn', 'database':'artifice'} - -from sqlalchemy import MetaData, create_engine - -import os - -engine = create_engine( os.environ["DATABASE_URL"] ) -Session.configure(bind=engine) - -s = Session() - -Base.metadata.create_all(engine) \ No newline at end of file diff --git a/tests/test_api.py b/tests/test_api.py index 3b065d9..cd6a099 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -30,7 +30,11 @@ class TestApi(test_interface.TestInterface): for tenant in constants.TENANTS: t = mock.Mock(spec=interface.Tenant) t.usage.return_value = usage - t.conn = tenant + t.conn = mock.Mock() + t.tenant = tenant + t.id = tenant['id'] + t.name = tenant['name'] + t.description = tenant['description'] tenants.append(t) artifice = mock.Mock(spec=interface.Artifice)