def _contract_get_all_authorize_filters(cdict, start_time=None, end_time=None, status=None, offer_uuid=None, project_id=None, view=None, owner=None): if status is None: status = [statuses.CREATED, statuses.ACTIVE] elif status == 'any': status = None possible_filters = { 'status': status, 'offer_uuid': offer_uuid, 'start_time': start_time, 'end_time': end_time, } if view == 'all': policy.authorize('esi_leap:contract:contract_admin', cdict, cdict) possible_filters['owner'] = owner possible_filters['project_id'] = project_id else: policy.authorize('esi_leap:contract:get', cdict, cdict) if owner: if cdict['project_id'] != owner: policy.authorize('esi_leap:contract:contract_admin', cdict, cdict) possible_filters['owner'] = owner possible_filters['project_id'] = project_id else: if project_id is None: project_id = cdict['project_id'] elif project_id != cdict['project_id']: policy.authorize('esi_leap:contract:contract_admin', cdict, cdict) possible_filters['project_id'] = project_id if (start_time and end_time is None) or \ (end_time and start_time is None): raise exception.InvalidTimeAPICommand(resource="a contract", start_time=str(start_time), end_time=str(end_time)) if start_time and end_time and\ end_time <= start_time: raise exception.InvalidTimeAPICommand(resource='a contract', start_time=str(start_time), end_time=str(end_time)) filters = {} for k, v in possible_filters.items(): if v is not None: filters[k] = v return filters
def get_all(self, project_id=None, resource_type=None, resource_uuid=None, start_time=None, end_time=None, available_start_time=None, available_end_time=None, status=None): request = pecan.request.context cdict = request.to_policy_values() policy.authorize('esi_leap:offer:get', cdict, cdict) if (start_time and end_time is None) or\ (end_time and start_time is None): raise exception.InvalidTimeAPICommand(resource='an offer', start_time=str(start_time), end_time=str(end_time)) if start_time and end_time and\ end_time <= start_time: raise exception.InvalidTimeAPICommand(resource='an offer', start_time=str(start_time), end_time=str(end_time)) if (available_start_time and available_end_time is None) or\ (available_end_time and available_start_time is None): raise exception.InvalidAvailabilityAPICommand( a_start=str(start_time), a_end=str(end_time)) if available_start_time and available_end_time and\ available_end_time <= available_start_time: raise exception.InvalidAvailabilityAPICommand( a_start=available_start_time, a_end=available_end_time) if status is None: status = statuses.AVAILABLE elif status == 'any': status = None possible_filters = { 'project_id': project_id, 'resource_type': resource_type, 'resource_uuid': resource_uuid, 'status': status, 'start_time': start_time, 'end_time': end_time, 'available_start_time': available_start_time, 'available_end_time': available_end_time, } filters = {} for k, v in possible_filters.items(): if v is not None: filters[k] = v offer_collection = OfferCollection() offers = offer.Offer.get_all(request, filters) offer_collection.offers = [ Offer(**OffersController._add_offer_availabilities(o)) for o in offers ] return offer_collection
def get_all(self, project_id=None, resource_type=None, resource_class=None, resource_uuid=None, start_time=None, end_time=None, available_start_time=None, available_end_time=None, status=None): request = pecan.request.context cdict = request.to_policy_values() utils.policy_authorize('esi_leap:offer:get_all', cdict, cdict) if project_id is not None: project_id = keystone.get_project_uuid_from_ident(project_id) if resource_uuid is not None: if resource_type is None: resource_type = CONF.api.default_resource_type resource = get_resource_object(resource_type, resource_uuid) resource_uuid = resource.get_resource_uuid() # either both are defined or both are None if bool(start_time) != bool(end_time): raise exception.InvalidTimeAPICommand(resource='an offer', start_time=str(start_time), end_time=str(end_time)) if (start_time or end_time) and (end_time <= start_time): raise exception.InvalidTimeAPICommand(resource='an offer', start_time=str(start_time), end_time=str(end_time)) if bool(available_start_time) != bool(available_end_time): raise exception.InvalidAvailabilityAPICommand( a_start=str(available_start_time), a_end=str(available_end_time)) if ((available_start_time or available_end_time) and (available_end_time <= available_start_time)): raise exception.InvalidAvailabilityAPICommand( a_start=str(available_start_time), a_end=str(available_end_time)) if status is None: status = statuses.OFFER_CAN_DELETE elif status == 'any': status = None try: utils.policy_authorize('esi_leap:offer:offer_admin', cdict, cdict) lessee_id = None except exception.HTTPForbidden: lessee_id = cdict['project_id'] filters = { 'project_id': project_id, 'lessee_id': lessee_id, 'resource_type': resource_type, 'resource_uuid': resource_uuid, 'status': status, 'start_time': start_time, 'end_time': end_time, 'available_start_time': available_start_time, 'available_end_time': available_end_time, } # unpack iterator to tuple so we can use 'del' for k, v in tuple(filters.items()): if v is None: del filters[k] offer_collection = OfferCollection() offers = offer_obj.Offer.get_all(filters, request) offer_collection.offers = [] if len(offers) > 0: project_list = keystone.get_project_list() node_list = ironic.get_node_list() offers_with_added_info = [ Offer(**utils.offer_get_dict_with_added_info( o, project_list, node_list)) for o in offers ] if resource_class: offer_collection.offers = [ o for o in offers_with_added_info if o.resource_class == resource_class ] else: offer_collection.offers = offers_with_added_info return offer_collection
def _lease_get_all_authorize_filters(cdict, start_time=None, end_time=None, status=None, offer_uuid=None, project_id=None, view=None, owner_id=None, resource_type=None, resource_uuid=None): if status is not None: status = [status] if status != 'any' else None else: status = [ statuses.CREATED, statuses.ACTIVE, statuses.ERROR, statuses.WAIT_CANCEL, statuses.WAIT_EXPIRE, statuses.WAIT_FULFILL ] filters = { 'status': status, 'offer_uuid': offer_uuid, 'start_time': start_time, 'end_time': end_time, 'resource_type': resource_type, 'resource_uuid': resource_uuid, 'time_filter_type': constants.WITHIN_TIME_FILTER, } if view == 'all': utils.policy_authorize('esi_leap:lease:lease_admin', cdict, cdict) filters['owner_id'] = owner_id filters['project_id'] = project_id else: utils.policy_authorize('esi_leap:lease:get_all', cdict, cdict) if owner_id: if cdict['project_id'] != owner_id: utils.policy_authorize('esi_leap:lease:lease_admin', cdict, cdict) filters['owner_id'] = owner_id filters['project_id'] = project_id else: if project_id is None: project_id = cdict['project_id'] filters['project_or_owner_id'] = project_id else: if project_id != cdict['project_id']: utils.policy_authorize('esi_leap:lease:lease_admin', cdict, cdict) filters['project_id'] = project_id # either both are defined or both are None if bool(start_time) != bool(end_time): raise exception.InvalidTimeAPICommand(resource='a lease', start_time=str(start_time), end_time=str(end_time)) if (start_time or end_time) and (end_time <= start_time): raise exception.InvalidTimeAPICommand(resource='a lease', start_time=str(start_time), end_time=str(end_time)) # unpack iterator to tuple so we can use 'del' for k, v in tuple(filters.items()): if v is None: del filters[k] return filters