Exemple #1
0
def get_current_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
  resp = None

  if len(br) > 0:
    bill = br[0]
   
  if bill is not None:
    bi = { "bill" : gdata.to_dict(bill), "billitems" : [] }
    iq = gdata.BillItem.all()
    iq.filter("bill_id = " , bill.id)
    ir = iq.fetch(1000)
    for item in ir:
      bi['billitems'].append(gdata.to_dict(item))
    resp = bi
    response.set_status('200 OK')
  else:
    response.set_status('404 A bill Not Found')

  yield Just(resp)
Exemple #2
0
def get_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

  r = gdata.bills(account_id=adef.id,id=req['bill_id']).fetch(1000)
  resp = None

  if len(r) == 1:
    bill = r[0]
    bi = { "bill" : gdata.to_dict(bill), "billitems" : [] }
    iq = gdata.BillItem.all()
    iq.filter("bill_id = " , bill.id)
    ir = iq.fetch(1000)
    for item in ir:
      bi['billitems'].append(gdata.to_dict(item))
    resp = bi
    response.set_status('200 OK')
  else:
    response.set_status('404 A bill Not Found')

  yield Just(resp)
Exemple #3
0
def get_aggregated_usage(req, response):

  tenant = get_tenant(req, response)
  if tenant is None:
    response.set_status('404 Tenant Not Found')
    return None

  logging.info("Got tenant...")
  svcmap = {}
 
  s = gdata.Service.all()
  s.filter("tenant_id = ", req['tenant_id'])
  sr = s.fetch(1000)

  for si in sr:
    svcmap[si.id] = si.service_name
 
  if len(svcmap.keys()) < 1:
    logging.info("Got NO services...")
    response.set_status('404 A service Not Found')
    return None

  resp = { 'tenant_id' : req['tenant_id'], 'usage' : [] }

  au = gdata.AggregatedUsage.all()
  au.filter("service_id  IN ", svcmap.keys())
  aur = au.fetch(1000)

  for auitem in  au:
    x = gdata.to_dict(auitem)
    x['service_name'] = svcmap[auitem.service_id]
    resp['usage'].append( x)

  return resp
Exemple #4
0
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 
Exemple #5
0
def list_accounts(req, response):

  qr = gdata.accounts(tenant_id=req['tenant_id']).fetch(100)

  rv = {"accounts" : [] }

  for item in qr:
    a =  gdata.to_dict(item)
    a['attributes'] = json.loads(a['attributes'],parse_float=Decimal)
    rv['accounts'].append(a)

  return rv
Exemple #6
0
def get_service(req, response):

  tenant = get_tenant(req, response) 
  if tenant is None:
    response.set_status('404 Tenant Not Found')
    return None

  q  = gdata.Service.all()
  q.filter("tenant_id = ", req['tenant_id'])
  q.filter("service_name = ", req['service_name'])
  results = q.fetch(1000)

  if len(results) != 1:
    response.set_status('404 Service Not Found')
    return None

  service = results[0]

  resp = {"service_name" : service.service_name, "metrics" : []}
  resp['service_id'] = service.id 

  q = gdata.Metric.all()
  q.filter(" service_id = ", service.id)
  results = q.fetch(1000)

  for metric in results:
    mrq = gdata.MetricRate.all()
    mrq.filter("metric_id = " , metric.id)

    mrqr = mrq.fetch(1000)

    if len(mrqr) > 0:
      mr = gdata.to_dict(mrqr[0])
      if mr['selector'] is not None:
        mr['selector'] = json.loads(mr['selector'], parse_float=Decimal)
      resp["metrics"].append({"metric:" : gdata.to_dict(metric), "rate" : mr})
    else:
      resp["metrics"].append({"metric:" : gdata.to_dict(metric) })

  return resp 
Exemple #7
0
def get_account_json(req, response):

  logging.info("Searching account = %s for tenant = %s" % (req['account_no'], req['tenant_id']) )
  qr = gdata.accounts(tenant_id=req['tenant_id'], account_no=req['account_no']).fetch(100)

  rv = None

  if len(qr) == 1:
    a = gdata.to_dict(qr[0])
    a['attributes'] = json.loads(a['attributes'], parse_float=Decimal)
    rv = a

  return rv
Exemple #8
0
def list_bills(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

  r = gdata.bills(account_id=adef.id).fetch(1000)
  
  resp = { 'account_no' : adef.account_no, 'bills' : [] }

  for bill in r:
    bi = { "bill" : gdata.to_dict(bill), "billitems" : [] }
    ir = gdata.billItems(bill_id=bill.id).fetch(1000)
    for item in ir:
      bi['billitems'].append(gdata.to_dict(item))
    resp['bills'].append(bi)

  response.set_status('200 OK')
  logging.info(resp)
  yield Just(resp)
Exemple #9
0
def list_tenants(req, response):

  tenants  = []

  q = gdata.Tenant.all()
  results = q.fetch(1000)
  for p in results:
    item = gdata.to_dict(p)
    if item['attributes'] is None:
      item['attributes'] = {}
    else:
      item['attributes'] = json.loads(item['attributes'], parse_float=Decimal)
    tenants.append(item)
  
  return {"tenants" : tenants }
Exemple #10
0
def get_tenant(req, response):

  q = gdata.Tenant.all()
  if req is not None:
    q.filter(" id = " , req['tenant_id'])
  results = q.fetch(1000)

  rv = None
  if len(results) == 1:
    rv =  gdata.to_dict(results[0])
    if rv['attributes'] is None:
      rv['attributes'] = {}
    else:
      rv['attributes'] = json.loads(rv['attributes'], parse_float=Decimal)

  return rv
Exemple #11
0
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()