def _list_keystone_projects(self): projects = [] domain_ids = \ [domain.id for domain in keystone.get_domain_list()] for domain_id in domain_ids: projects.extend(keystone.get_project_list(domain_id)) return projects
def get_all(self, type=None, status=None, start_time=None, end_time=None, limit=None, offset=None, resource_id=None, region_id=None, project_id=None, user_id=None, owed=None, bill_methods=None): """Get queried orders If start_time and end_time is not None, will get orders that have bills during start_time and end_time, or return all orders directly. """ if limit and limit < 0: raise exception.InvalidParameterValue(err="Invalid limit") if offset and offset < 0: raise exception.InvalidParameterValue(err="Invalid offset") limit_user_id = acl.get_limited_to_user( request.headers, 'orders_get') if limit_user_id: # normal user user_id = None projects = keystone.get_project_list(user=limit_user_id) _project_ids = [project.id for project in projects] if project_id: project_ids = ([project_id] if project_id in _project_ids else _project_ids) else: project_ids = _project_ids else: # accountant if project_id: # look up specified project project_ids = [project_id] else: # look up all projects project_ids = None if project_ids: project_ids = list(set(project_ids) - set(cfg.CONF.ignore_tenants)) conn = pecan.request.db_conn orders_db, total_count = conn.get_orders(request.context, type=type, status=status, start_time=start_time, end_time=end_time, owed=owed, limit=limit, offset=offset, with_count=True, resource_id=resource_id, bill_methods=bill_methods, region_id=region_id, user_id=user_id, project_ids=project_ids) orders = [] for order in orders_db: price = self._get_order_price(order, start_time=start_time, end_time=end_time) order.total_price = gringutils._quantize_decimal(price) orders.append(models.Order.from_db_model(order)) return models.Orders.transform(total_count=total_count, orders=orders)
def get_all(self, user_id=None, type=None, duration=None): """Get all projects.""" user_id = acl.get_limited_to_user(request.headers, 'projects_get') or user_id self.conn = pecan.request.db_conn result = [] if not type or type.lower() == 'pay': # if admin call this api, limit to admin's user_id if not user_id: user_id = request.context.user_id try: user_projects = self.conn.get_user_projects(request.context, user_id=user_id) except Exception as e: LOG.exception('Fail to get all projects') raise exception.DBError(reason=e) project_ids = [up.project_id for up in user_projects] if not project_ids: LOG.warn('User %s has no payed projects' % user_id) return [] projects = self._list_keystone_projects() for u, p in itertools.product(user_projects, projects): if u.project_id == p['id']: billing_owner = p['users']['billing_owner'] project_owner = p['users']['project_owner'] project_creator = p['users']['project_creator'] up = models.UserProject(user_id=user_id, project_id=u.project_id, project_name=p['name'], user_consumption=u.user_consumption, project_consumption=u.project_consumption, billing_owner=dict(user_id=billing_owner.get('id') if billing_owner else None, user_name=billing_owner.get('name') if billing_owner else None), project_owner=dict(user_id=project_owner.get('id') if project_owner else None, user_name=project_owner.get('name') if project_owner else None), project_creator=dict(user_id=project_creator.get('id') if project_creator else None, user_name=project_creator.get('name') if project_creator else None), is_historical=u.is_historical, created_at=timeutils.parse_isotime(p['created_at']) if p['created_at'] else None) result.append(up) elif type.lower() == 'all': # if admin call this api, limit to admin's user_id if not user_id: user_id = request.context.user_id k_projects = keystone.get_project_list(name=user_id) project_ids = [p['id'] for p in k_projects] if not project_ids: LOG.warn('User %s has no projects' % user_id) return [] try: g_projects = self.conn.get_projects_by_project_ids(request.context, project_ids) except Exception as e: LOG.exception('Fail to get all projects') raise exception.DBError(reason=e) for k, g in itertools.product(k_projects, g_projects): if k['id'] == g.project_id: billing_owner = k['users']['billing_owner'] project_owner = k['users']['project_owner'] project_creator = k['users']['project_creator'] up = models.UserProject(user_id=user_id, project_id=g.project_id, project_name=k['name'], project_consumption=g.consumption, billing_owner=dict(user_id=billing_owner.get('id') if billing_owner else None, user_name=billing_owner.get('name') if billing_owner else None), project_owner=dict(user_id=project_owner.get('id') if project_owner else None, user_name=project_owner.get('name') if project_owner else None), project_creator=dict(user_id=project_creator.get('id') if project_creator else None, user_name=project_creator.get('name') if project_creator else None), is_historical=False, created_at=timeutils.parse_isotime(k['created_at']) if k['created_at'] else None) result.append(up) elif type.lower() == 'simple': duration = gringutils.normalize_timedelta(duration) if duration: active_from = datetime.datetime.utcnow() - duration else: active_from = None g_projects = list(self.conn.get_projects(request.context, user_id=user_id, active_from=active_from)) project_ids = [p.project_id for p in g_projects] if not project_ids: LOG.warn('User %s has no payed projects' % user_id) return [] k_projects = self._list_keystone_projects() for k, g in itertools.product(k_projects, g_projects): if k.id == g.project_id: up = models.UserProject(project_id=g.project_id, project_name=k.name, domain_id=g.domain_id, billing_owner=dict(user_id=g.user_id)) result.append(up) return result
def get_all(self, type=None, status=None, start_time=None, end_time=None, limit=None, offset=None, resource_id=None, region_id=None, project_id=None, user_id=None, owed=None, bill_methods=None): """Get queried orders If start_time and end_time is not None, will get orders that have bills during start_time and end_time, or return all orders directly. """ if limit and limit < 0: raise exception.InvalidParameterValue(err="Invalid limit") if offset and offset < 0: raise exception.InvalidParameterValue(err="Invalid offset") limit_user_id = acl.get_limited_to_user(request.headers, 'orders_get') if limit_user_id: # normal user user_id = None projects = keystone.get_project_list(user=limit_user_id) _project_ids = [project.id for project in projects] if project_id: project_ids = ([project_id] if project_id in _project_ids else _project_ids) else: project_ids = _project_ids else: # accountant if project_id: # look up specified project project_ids = [project_id] else: # look up all projects project_ids = None if project_ids: project_ids = list(set(project_ids) - set(cfg.CONF.ignore_tenants)) conn = pecan.request.db_conn orders_db, total_count = conn.get_orders(request.context, type=type, status=status, start_time=start_time, end_time=end_time, owed=owed, limit=limit, offset=offset, with_count=True, resource_id=resource_id, bill_methods=bill_methods, region_id=region_id, user_id=user_id, project_ids=project_ids) orders = [] for order in orders_db: price = self._get_order_price(order, start_time=start_time, end_time=end_time) order.total_price = gringutils._quantize_decimal(price) orders.append(models.Order.from_db_model(order)) return models.Orders.transform(total_count=total_count, orders=orders)
def get_all(self, user_id=None, type=None, duration=None): """Get all projects.""" user_id = acl.get_limited_to_user(request.headers, 'projects_get') or user_id self.conn = pecan.request.db_conn result = [] if not type or type.lower() == 'pay': # if admin call this api, limit to admin's user_id if not user_id: user_id = request.context.user_id try: user_projects = self.conn.get_user_projects(request.context, user_id=user_id) except Exception as e: LOG.exception('Fail to get all projects') raise exception.DBError(reason=e) project_ids = [up.project_id for up in user_projects] if not project_ids: LOG.warn('User %s has no payed projects' % user_id) return [] projects = self._list_keystone_projects() for u, p in itertools.product(user_projects, projects): if u.project_id == p['id']: billing_owner = p['users']['billing_owner'] project_owner = p['users']['project_owner'] project_creator = p['users']['project_creator'] up = models.UserProject( user_id=user_id, project_id=u.project_id, project_name=p['name'], user_consumption=u.user_consumption, project_consumption=u.project_consumption, billing_owner=dict(user_id=billing_owner.get('id') if billing_owner else None, user_name=billing_owner.get('name') if billing_owner else None), project_owner=dict(user_id=project_owner.get('id') if project_owner else None, user_name=project_owner.get('name') if project_owner else None), project_creator=dict( user_id=project_creator.get('id') if project_creator else None, user_name=project_creator.get('name') if project_creator else None), is_historical=u.is_historical, created_at=timeutils.parse_isotime(p['created_at']) if p['created_at'] else None) result.append(up) elif type.lower() == 'all': # if admin call this api, limit to admin's user_id if not user_id: user_id = request.context.user_id k_projects = keystone.get_project_list(name=user_id) project_ids = [p['id'] for p in k_projects] if not project_ids: LOG.warn('User %s has no projects' % user_id) return [] try: g_projects = self.conn.get_projects_by_project_ids( request.context, project_ids) except Exception as e: LOG.exception('Fail to get all projects') raise exception.DBError(reason=e) for k, g in itertools.product(k_projects, g_projects): if k['id'] == g.project_id: billing_owner = k['users']['billing_owner'] project_owner = k['users']['project_owner'] project_creator = k['users']['project_creator'] up = models.UserProject( user_id=user_id, project_id=g.project_id, project_name=k['name'], project_consumption=g.consumption, billing_owner=dict(user_id=billing_owner.get('id') if billing_owner else None, user_name=billing_owner.get('name') if billing_owner else None), project_owner=dict(user_id=project_owner.get('id') if project_owner else None, user_name=project_owner.get('name') if project_owner else None), project_creator=dict( user_id=project_creator.get('id') if project_creator else None, user_name=project_creator.get('name') if project_creator else None), is_historical=False, created_at=timeutils.parse_isotime(k['created_at']) if k['created_at'] else None) result.append(up) elif type.lower() == 'simple': duration = gringutils.normalize_timedelta(duration) if duration: active_from = datetime.datetime.utcnow() - duration else: active_from = None g_projects = list( self.conn.get_projects(request.context, user_id=user_id, active_from=active_from)) project_ids = [p.project_id for p in g_projects] if not project_ids: LOG.warn('User %s has no payed projects' % user_id) return [] k_projects = self._list_keystone_projects() for k, g in itertools.product(k_projects, g_projects): if k.id == g.project_id: up = models.UserProject( project_id=g.project_id, project_name=k.name, domain_id=g.domain_id, billing_owner=dict(user_id=g.user_id)) result.append(up) return result