def resources_cost(self, billing_month=None, tenant_id=None, service=None, offset=None, limit=None): """Return the amount to pay for each resource within a special service type. """ policy.enforce(pecan.request.context, 'billing:show_service_rate', {}) storage = pecan.request.storage_backend access_monthly_billing_table = ck_utils.access_monthly_billing_table(cfg.CONF.collect.billing_month_start, billing_month) if access_monthly_billing_table: kwargs={'billing_month': ck_utils.reformat_month_style(billing_month)} else: begin, end = ck_utils.get_billing_month_period(cfg.CONF.collect.billing_month_start, billing_month) kwargs={'begin': ck_utils.local2utc(begin), 'end': ck_utils.local2utc(end)} if offset>=0 and limit>=0: kwargs['offset'] = offset kwargs['limit'] = limit rated_children_tenants = [] self.fetcher.get_rated_subordinate_tenants(tenant_id, rated_children_tenants) rated_children_tenants = rated_children_tenants if rated_children_tenants else [tenant_id] resources_rate = storage.list_resource_rate(rated_children_tenants, service, access_monthly_billing_table, **kwargs) resources_rate_list = [] for resource_rate in resources_rate: try: raw_resource = self._conn.resources.get(resource_rate['resource_id']) except Exception: #should to be fix resource_rate['resource_name'] = resource_rate['resource_id'] resource_rate['status'] = 'deleted&cleared' resource_rate['unit_price'] = 0 resources_rate_list.append(billing_models.CloudkittyResourceRate(**resource_rate)) continue resource_info= self.t_ceilometer.strip_resource_data(resource_rate['res_type'], raw_resource) resource_rate['resource_name'] = resource_info['name'] #print resource_rate['resource_name'] resource_rate['status'] = resource_info['status'] if resource_info['created_time']: resource_rate['created_time'] = self._str2dt(resource_info['created_time']) if resource_rate['res_type'] == 'network.bw.total': raw_resource = self._conn.resources.get(resource_info['attached_instance_id']) attached_instance_info= self.t_ceilometer.strip_resource_data('compute', raw_resource) resource_rate['resource_name'] = '%s(%s)'%(resource_rate['resource_name'],attached_instance_info['name']) resource_rate['status'] = attached_instance_info['status'] resource_rate['created_time'] = self._str2dt(attached_instance_info['created_time']) resource_rate['unit_price'] = self._get_unit_price(resource_info, resource_rate['res_type']) resources_rate_list.append(billing_models.CloudkittyResourceRate(**resource_rate)) #return billing_models.CloudkittyResourceRateCollection.sample() return billing_models.CloudkittyResourceRateCollection(resourcesRate=resources_rate_list)
def resources_cost_count(self, billing_month=None, tenant_id=None, service=None): """Return the amount to pay for each resource within a special service type. """ policy.enforce(pecan.request.context, 'billing:resource_rate_count', {}) storage = pecan.request.storage_backend access_monthly_billing_table = ck_utils.access_monthly_billing_table(cfg.CONF.collect.billing_month_start, billing_month) if access_monthly_billing_table: kwargs={'billing_month': ck_utils.reformat_month_style(billing_month)} else: begin, end = ck_utils.get_billing_month_period(cfg.CONF.collect.billing_month_start, billing_month) kwargs={'begin': ck_utils.local2utc(begin), 'end': ck_utils.local2utc(end)} rated_children_tenants = [] self.fetcher.get_rated_subordinate_tenants(tenant_id, rated_children_tenants) rated_children_tenants = rated_children_tenants if rated_children_tenants else [tenant_id] resources_rate_count = storage.count_resource_rate_list(rated_children_tenants, service, access_monthly_billing_table, **kwargs) return resources_rate_count
def total(self, billing_month=None, tenant_id=None): """Return the amount to pay for a given billing month. """ policy.enforce(pecan.request.context, 'billing:get_total', {}) storage = pecan.request.storage_backend # FIXME(sheeprine): We should filter on user id. # Use keystone token information by default but make it overridable and # enforce it by policy engine access_monthly_billing_table = ck_utils.access_monthly_billing_table(cfg.CONF.collect.billing_month_start, billing_month) if access_monthly_billing_table: kwargs={'billing_month': ck_utils.reformat_month_style(billing_month)} else: begin, end = ck_utils.get_billing_month_period(cfg.CONF.collect.billing_month_start, billing_month) kwargs={'begin': ck_utils.local2utc(begin), 'end': ck_utils.local2utc(end)} subordinate_tenants = self.fetcher.get_subordinate_tenants(tenant_id) total_list=[] for subordinate_tenant in subordinate_tenants: total = storage.get_total(subordinate_tenant['rated_children_tenants'], access_monthly_billing_table, **kwargs) total_list.append(billing_models.CloudkittyTotal(tenant_id=subordinate_tenant['tenant_id'], tenant_name=subordinate_tenant['tenant_name'], org_name=subordinate_tenant['org_name'], cost=total)) return billing_models.CloudkittyTotalCollection(totals=total_list)
def services_cost(self, billing_month=None, tenant_id=None): """Return the amount to pay for a given period, group by service. """ policy.enforce(pecan.request.context, 'billing:get_services_rate', {}) storage = pecan.request.storage_backend access_monthly_billing_table = ck_utils.access_monthly_billing_table(cfg.CONF.collect.billing_month_start, billing_month) if access_monthly_billing_table: kwargs={'billing_month': ck_utils.reformat_month_style(billing_month)} else: begin, end = ck_utils.get_billing_month_period(cfg.CONF.collect.billing_month_start, billing_month) kwargs={'begin': ck_utils.local2utc(begin), 'end': ck_utils.local2utc(end)} rated_children_tenants = [] self.fetcher.get_rated_subordinate_tenants(tenant_id, rated_children_tenants) rated_children_tenants = rated_children_tenants if rated_children_tenants else [tenant_id] services_rate = storage.list_service_rate(rated_children_tenants, access_monthly_billing_table, **kwargs) services_rate_list = [billing_models.CloudkittyServiceRate(**service_rate) for service_rate in services_rate] #return billing_models.CloudkittyServiceRateCollection.sample() return billing_models.CloudkittyServiceRateCollection(servicesRate=services_rate_list)