def register(req, response): q = gdata.Tenant.all() q.filter("email = ", req['email']) results = q.fetch(1000) if len(results) > 0: response.set_status(409, "Email unavailable") return {"email not available" : req['email']} attrs = None if 'attributes' in req: attrs = json.dumps(req['attributes']) x = gdata.Tenant(id=genid(), name=req["name"], email=req["email"], api_key=genid(), password=req["password"], attributes=attrs) x.put() response.headers['Location'] = "/tenants/"+x.id response.set_status(201) rv = gdata.to_dict(x) if rv['attributes'] is None: rv['attributes'] = {} else: rv['attributes'] = json.loads(rv['attributes'],parse_float=Decimal) return rv
def create_svc(): sdef = gdata.Service(id=genid(), tenant_id = req['tenant_id'], service_name = req['service_name']) sdef.put() metrics = req['metrics'] for metric in metrics: m = gdata.Metric(id=genid(), service_id = sdef.id, metric_name=metric['metric_name'], aggregator=metric['aggregator'], uom=metric['uom']) m.put()
def add_usage(req, response): tenant = get_tenant(req, response) if tenant is None: response.set_status('404 Tenant Not Found') return None sdef = get_service(req, response) if sdef is None: response.set_status('404 Service Not Found') return None attrs = None if req.has_key('attributes'): attrs = json.dumps(req['attributes']) u = gdata.Usage(id=genid(), service_id=sdef['service_id'], resource_owner = req['resource_owner'], resource_id = req['resource_id'], ext_usage_id = req['ext_usage_id'], location=req['location'], event_time=datetime.datetime.strptime(req['event_time'], '%Y-%m-%dT%H:%M:%S.%fZ'), attributes=attrs) u.put() umlist = req['usagemetrics'] for umitem in umlist: m = gdata.Metric.all() m.filter("service_id = ", sdef['service_id']) m.filter("metric_name = ", umitem['metric_name']) r = m.fetch(1000) if len(r) > 0: mu = r[0] um = gdata.UsageMetric(id=genid(), metric_id=mu.id, value=umitem['value'], usage_id=u.id) um.put() response.headers['Location'] = "/tenants/"+req['tenant_id'] +"/services/"+req['service_name'] + "/usage/" + u.id response.set_status(201) return None
def create_account(): attrs = None if "attributes" in req: attrs = json.dumps(req['attributes']) adef = gdata.Account(id=genid(), tenant_id = req['tenant_id'], account_name = req['account_name'], account_no = req['account_no'], bdom = int(req['bdom']), currency = req['currency'], payment_terms = req.get('payment_terms', gdata.PAYTERM_NET30), attributes=attrs ) adef.put()
def create_bill(req, response): j = yield get_tenant(req, response) tenant = j >> unpack tenant # just to thawrt PEP warning j = yield get_account(req, response) adef = j >> unpack end_date = datetime.datetime.utcnow() end_date = datetime.date(end_date.year, end_date.month, adef.bdom) start_date = subtract_one_month(end_date) br = gdata.bills(from_date=start_date, to_date=end_date, account_id=adef.id).fetch(1000) bill = None btot = 0 if len(br) > 0: bill = br[0] else: acct_attrs = {} if adef.attributes is not None: acct_attrs = json.loads(adef.attributes, parse_float=Decimal) bill = gdata.Bill(id=genid(), from_date=start_date, to_date=end_date, account_id=adef.id, bill_total_charge=str(btot)) balances = {} # TODO (btot, items, subbalances) = get_bill_items(bill.id, adef.account_no, adef.currency, acct_attrs, start_date, end_date, balances) for item in items: logging.info(gdata.to_dict(item)) item.put() for sb in subbalances: sb.put() bill.bill_total_charge = str(btot) bill.put() loc= "/tenants/"+req['tenant_id'] +"/accounts/"+req['account_no'] +"/bills/"+bill.id response.headers['Location'] = str(loc) response.set_status(201) yield Nothing()
def get_bill_items(bill_id, account_no, currency, acct_attrs, start_date, end_date, balances): logging.info( "Looking for usage for account: %s between %s and %s" % (account_no, str(start_date), str(end_date))) uq = gdata.usages(resource_owner=account_no) uq.filter("event_time >= ", start_date) uq.filter("event_time < ", end_date) uqr = uq.fetch(1000) logging.info("Found " + str(len(uqr)) + " entries...") billtot = 0 bisb = [] items = [] for u in uqr: all_subbalances = [] attrs = {} for aak in acct_attrs.keys(): attrs[aak] = acct_attrs[aak] if u.attributes is not None: usg_attrs = json.loads(str(u.attributes),parse_float=Decimal) for uak in usg_attrs.keys(): attrs[uak] = usg_attrs[uak] logging.info("Searching in UsageMetric for unbilled entries having usage_id = " + u.id) umql = gdata.usageMetrics(usage_id=u.id,billed=False).fetch(1000) logging.info("Found Usage Metrics:" + str(len(umql))) # Create one bill item per metric for um in umql: itemtot = 0 mr = gdata.metrics(id=um.metric_id).fetch(1000) m = mr[0] rr = gdata.metricRates(metric_id=m.id).fetch(1000) bill_item_id = genid() if len(rr) == 0: logging.error("Could not find a rate for metric: " + m.metric_name) continue # the loop else: rsel = rr[0].selector r = json.loads(rsel, parse_float=Decimal) subbalances = rate_usage(r, m.metric_name, balances, attrs, Decimal(um.value), currency) for balance_counter in subbalances.keys(): for il in subbalances[balance_counter]: for i in il: cval = None if is_currency(balance_counter): cval = currency itemtot += i['balance'] all_subbalances.append(gdata.BillItemSubBalance(id=genid(),bill_item_id=bill_item_id,currency=cval, balance_counter=balance_counter, charge_tier_name=i['tier_name'], rate=str(i['rate']),quantity=str(i['quantity']), total=str(i['balance']))) c = gdata.BillItem(id=bill_item_id, bill_id=bill_id, resource_id = u.resource_id, usage_metric_id = um.id, total_charge = str(itemtot)) items.append(c) logging.info("Using bill_item_id " + c.id + " for metric: " + m.metric_name) for s in all_subbalances: bisb.append(s) billtot += itemtot return (billtot, items, bisb)