def get_price_name(priceitem): billing_client = billing.PriceitemBilling() price, resultStatus = \ billing_client.get_price(str(priceitem.price_id)) if resultStatus == 200: price = json.loads(price) return PTYPE.get(price['ptype'], '') else: price = '' return PTYPE.get(price['ptype'], '')
def get_priceitem_data(self): account_id = self.tab_group.kwargs['account_id'] try: billing_client = billing.PriceDetailBilling() search_param = { "q.field": "price.account_id", "q.op": "eq", "q.value": int(account_id), "pagation.number": 1, "pagation.count": 100, "q.type": "number", "q.orderby": "price.id", "q.sort": "asc" } prices, resultStatus = billing_client.list_prices(search_param) prices = json.loads(prices) if prices['prices']: for i, price in enumerate(prices['prices']): prices['prices'][i] = json2object.get_price(price) if resultStatus == 500: return [] except Exception: redirect = reverse('horizon:admin:billing:index') exceptions.handle(self.request, _('Unable to retrieve account price.'), redirect=redirect) billing_client = billing.PriceitemBilling() items = [] for p in prices['prices']: search_param = { "q.field": "price_item.price_id", "q.op": "eq", "q.value": p.id, "pagation.number": 1, "pagation.count": 100, "q.type": "number", "q.orderby": "price_item.id", "q.sort": "asc" } priceitems, resultStatus = \ billing_client.list_priceitems(search_param) priceitems = json.loads(priceitems) if priceitems['priceitems']: for i, priceitem in enumerate(priceitems['priceitems']): priceitems['priceitems'][i] = \ json2object.get_priceItem(priceitem) items += priceitems['priceitems'] return items
def get_priceitem_rule(priceitem): billing_client = billing.PriceitemBilling() priceitem, resultStatus = \ billing_client.get_priceitem(str(priceitem.id)) if resultStatus == 200: priceitem = json.loads(priceitem) if priceitem['rule']: cpu = priceitem['rule']['cpu'] mem = priceitem['rule']['mem'] os = priceitem['rule']['os'] return 'CPU: '+str(cpu)+' Memory: '+str(mem)+' OS: '+str(os) else: return priceitem['rule'] else: priceitem = '' return priceitem
def allowed(self, request, price=None): account_id = self.table.kwargs['account_id'] billing_client = billing.PriceitemBilling() search_param = {"q.field": "price.account_id", "q.op": "eq", "q.value": int(account_id), "pagation.number": 1, "pagation.count": 100, "q.type": "number", "q.orderby": "price.id", "q.sort": "asc"} prices, resultStatus = billing_client.list_prices(search_param) prices = json.loads(prices) if prices['prices']: return True return False
def handle(self, request, data): price_id = data.pop('price_id') # unit = data.pop('unit') ptype = data.pop('ptype') fee = data.pop('fee') cpu = data.pop('cpu') memory = data.pop('memory') # os = data.pop('os') try: billing_client = billing.PriceitemBilling() if cpu and memory: rule = {"cpu": int(cpu), "mem": int(memory), "os": 'os'} body = { "rule": str(rule), "fee": float(fee), "unit": 'Hour', "ptype": str(ptype), "price_id": int(price_id) } else: body = { "rule": "", "fee": float(fee), "unit": 'Hour', "ptype": str(ptype), "price_id": int(price_id) } priceitem, resultStatus = billing_client.create_priceitem(body) if resultStatus != 500: messages.success( request, _('Price item has been created successfully.')) else: messages.error(request, _('Unable to create the price item.')) except Exception: messages.error(request, _('Unable to create the price item.')) return True
def delete(self, request, obj_id): billing_client = billing.PriceitemBilling() billing_client.delete_priceitem(obj_id)
def __init__(self, *args, **kwargs): super(CreatePriceItemForm, self).__init__(*args, **kwargs) account_id = kwargs.get('initial', {}).get('account_id') try: billing_client = billing.PriceBilling() billingitem_client = billing.PriceitemBilling() temp_prices = [] search_param = { "q.field": "price.account_id", "q.op": "eq", "q.value": account_id, "pagation.number": 1, "pagation.count": 100, "q.type": "string", "q.orderby": "price.id", "q.sort": "asc" } prices, resultStatus = billing_client.list_prices(search_param) prices = json.loads(prices) if prices['prices']: for i, price in enumerate(prices['prices']): prices['prices'][i] = json2object.get_price(price) if resultStatus == 500: prices = [] if prices['prices']: for id, price in enumerate(prices['prices']): search_param = { "q.field": "price_item.price_id", "q.op": "eq", "q.value": price.id, "pagation.number": 1, "pagation.count": 100, "q.type": "number", "q.orderby": "price_item.id", "q.sort": "asc" } priceitems, priceresultStatus = \ billingitem_client.list_priceitems(search_param) if priceresultStatus == 200: priceitems = json.loads(priceitems) if not priceitems['priceitems']: temp_prices.append(price) else: for j, priceitem in enumerate( priceitems['priceitems']): if self.get_price_name(priceitem) == str(price.ptype) \ and str(price.ptype) != 'instance': break else: if j == len(priceitems['priceitems']) - 1: temp_prices.append(price) except Exception: redirect = reverse('horizon:admin:billing:index') exceptions.handle(self.request, _('Unable to retrieve account price.'), redirect=redirect) self.fields['price_id'].choices = \ [(price.id, _(price.ptype)) for price in temp_prices]