def get_all(self, project_id=None, start_time=None, end_time=None, status=None, offer_uuid=None, view=None, owner_id=None, resource_type=None, resource_uuid=None, resource_class=None): request = pecan.request.context cdict = request.to_policy_values() if project_id is not None: project_id = keystone.get_project_uuid_from_ident(project_id) if owner_id is not None: owner_id = keystone.get_project_uuid_from_ident(owner_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() filters = LeasesController._lease_get_all_authorize_filters( cdict, project_id=project_id, owner_id=owner_id, start_time=start_time, end_time=end_time, status=status, offer_uuid=offer_uuid, view=view, resource_type=resource_type, resource_uuid=resource_uuid) lease_collection = LeaseCollection() leases = lease_obj.Lease.get_all(filters, request) lease_collection.leases = [] if len(leases) > 0: project_list = keystone.get_project_list() node_list = ironic.get_node_list() leases_with_added_info = [ Lease(**utils.lease_get_dict_with_added_info( l, project_list, node_list)) for l in leases ] if resource_class: lease_collection.leases = [ l for l in leases_with_added_info if l.resource_class == resource_class ] else: lease_collection.leases = leases_with_added_info return lease_collection
def test_get_project_uuid_from_ident_uuid(self, mock_iul): mock_iul.return_value = True project_uuid = keystone.get_project_uuid_from_ident('uuid') mock_iul.assert_called_once_with('uuid') self.assertEqual('uuid', project_uuid)
def test_get_project_uuid_from_ident_name(self, mock_iul, mock_keystone): mock_iul.return_value = False mock_keystone.return_value.projects.list.return_value = [FakeProject()] project_uuid = keystone.get_project_uuid_from_ident('name') mock_iul.assert_called_once_with('name') self.assertEqual('uuid', project_uuid) mock_keystone.return_value.projects.list.assert_called_once_with( name='name')
def post(self, new_offer): request = pecan.request.context cdict = request.to_policy_values() utils.policy_authorize('esi_leap:offer:create', cdict, cdict) offer_dict = new_offer.to_dict() offer_dict['project_id'] = request.project_id offer_dict['uuid'] = uuidutils.generate_uuid() if 'resource_type' not in offer_dict: offer_dict['resource_type'] = CONF.api.default_resource_type resource = get_resource_object(offer_dict['resource_type'], offer_dict['resource_uuid']) offer_dict['resource_uuid'] = resource.get_resource_uuid() if 'lessee_id' in offer_dict: offer_dict['lessee_id'] = keystone.get_project_uuid_from_ident( offer_dict['lessee_id']) if 'start_time' not in offer_dict: offer_dict['start_time'] = datetime.datetime.now() if 'end_time' not in offer_dict: offer_dict['end_time'] = datetime.datetime.max if offer_dict['start_time'] >= offer_dict['end_time']: raise exception.InvalidTimeRange( resource='an offer', start_time=str(offer_dict['start_time']), end_time=str(offer_dict['end_time'])) try: utils.check_resource_admin(cdict, resource, request.project_id) except exception.HTTPResourceForbidden: parent_lease_uuid = utils.check_resource_lease_admin( cdict, resource, request.project_id, offer_dict.get('start_time'), offer_dict.get('end_time')) if parent_lease_uuid is None: raise offer_dict['parent_lease_uuid'] = parent_lease_uuid o = offer_obj.Offer(**offer_dict) o.create() return Offer(**utils.offer_get_dict_with_added_info(o))
def post(self, new_lease): request = pecan.request.context cdict = request.to_policy_values() utils.policy_authorize('esi_leap:lease:create', cdict, cdict) lease_dict = new_lease.to_dict() lease_dict['owner_id'] = request.project_id lease_dict['uuid'] = uuidutils.generate_uuid() if 'resource_type' not in lease_dict: lease_dict['resource_type'] = CONF.api.default_resource_type resource = get_resource_object(lease_dict['resource_type'], lease_dict['resource_uuid']) lease_dict['resource_uuid'] = resource.get_resource_uuid() if 'project_id' in lease_dict: lease_dict['project_id'] = keystone.get_project_uuid_from_ident( lease_dict['project_id']) if 'start_time' not in lease_dict: lease_dict['start_time'] = datetime.datetime.now() if 'end_time' not in lease_dict: lease_dict['end_time'] = datetime.datetime.max try: utils.check_resource_admin(cdict, resource, request.project_id) except exception.HTTPResourceForbidden: parent_lease_uuid = utils.check_resource_lease_admin( cdict, resource, request.project_id, lease_dict.get('start_time'), lease_dict.get('end_time')) if parent_lease_uuid is None: raise lease_dict['parent_lease_uuid'] = parent_lease_uuid lease = lease_obj.Lease(**lease_dict) lease.create(request) return Lease(**utils.lease_get_dict_with_added_info(lease))
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