Пример #1
0
    def create(self, context=None):
        updates = self.obj_get_changes()

        with utils.lock(utils.get_resource_lock_name(updates['resource_type'],
                                                     updates['resource_uuid']),
                        external=True):
            LOG.info('Creating offer')
            if updates['start_time'] >= updates['end_time']:
                raise exception.InvalidTimeRange(
                    resource='offer',
                    start_time=str(updates['start_time']),
                    end_time=str(updates['end_time']))

            if updates.get('parent_lease_uuid'):
                # offer is a child of an existing lease
                parent_lease = lease_obj.Lease.get(
                    updates['parent_lease_uuid'])

                if parent_lease.status != statuses.ACTIVE:
                    raise exception.LeaseNotActive(
                        updates['parent_lease_uuid'])

                parent_lease.verify_child_availability(updates['start_time'],
                                                       updates['end_time'])
            else:
                ro = get_resource_object(updates['resource_type'],
                                         updates['resource_uuid'])
                ro.verify_availability(updates['start_time'],
                                       updates['end_time'])

            db_offer = self.dbapi.offer_create(updates)
            self._from_db_object(context, self, db_offer)
Пример #2
0
        def _create_offer():

            if updates['start_time'] >= updates['end_time']:
                raise exception.InvalidTimeRange(
                    resource='contract',
                    start_time=str(updates['start_time']),
                    end_time=str(updates['end_time']))

            self.dbapi.offer_verify_resource_availability(
                updates['resource_type'], updates['resource_uuid'],
                updates['start_time'], updates['end_time'])

            db_offer = self.dbapi.offer_create(updates)
            self._from_db_object(context, self, db_offer)
Пример #3
0
    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))
Пример #4
0
        def _create_contract():

            if updates['start_time'] >= updates['end_time']:
                raise exception.InvalidTimeRange(
                    resource='contract',
                    start_time=str(updates['start_time']),
                    end_time=str(updates['end_time']))

            related_offer = self.dbapi.offer_get_by_uuid(updates['offer_uuid'])

            if related_offer.status != statuses.AVAILABLE:
                raise exception.OfferNotAvailable(
                    offer_uuid=related_offer.uuid, status=related_offer.status)

            self.dbapi.offer_verify_contract_availability(
                related_offer, updates['start_time'], updates['end_time'])

            db_contract = self.dbapi.contract_create(updates)
            self._from_db_object(context, self, db_contract)
Пример #5
0
    def create(self, context=None):
        updates = self.obj_get_changes()

        with utils.lock(utils.get_resource_lock_name(updates['resource_type'],
                                                     updates['resource_uuid']),
                        external=True):
            if updates['start_time'] >= updates['end_time']:
                raise exception.InvalidTimeRange(
                    resource='lease',
                    start_time=str(updates['start_time']),
                    end_time=str(updates['end_time']))

            # check availability
            if 'offer_uuid' in updates:
                # lease is being created from an offer
                related_offer = offer_obj.Offer.get(updates['offer_uuid'])

                if related_offer.status != statuses.AVAILABLE:
                    raise exception.OfferNotAvailable(
                        offer_uuid=related_offer.uuid,
                        status=related_offer.status)

                related_offer.verify_availability(updates['start_time'],
                                                  updates['end_time'])
            elif 'parent_lease_uuid' in updates:
                # lease is a child of an existing lease
                parent_lease = Lease.get(updates['parent_lease_uuid'])

                if parent_lease.status != statuses.ACTIVE:
                    raise exception.LeaseNotActive(
                        updates['parent_lease_uuid'])

                parent_lease.verify_child_availability(updates['start_time'],
                                                       updates['end_time'])
            else:
                ro = get_resource_object(updates['resource_type'],
                                         updates['resource_uuid'])
                ro.verify_availability(updates['start_time'],
                                       updates['end_time'])

            db_lease = self.dbapi.lease_create(updates)
            self._from_db_object(context, self, db_lease)
Пример #6
0
def contract_update(contract_uuid, values):
    s = get_session()
    query = model_query(models.Contract, s)
    contract_ref = query.filter_by(uuid=contract_uuid).one_or_none()

    values.pop('uuid', None)
    values.pop('project_id', None)

    start = values.get('start_time', None)
    end = values.get('end_time', None)
    if start is None:
        start = contract_ref.start_time
    if end is None:
        end = contract_ref.end_time
    if start >= end:
        raise exception.InvalidTimeRange(resource="a contract",
                                         start_time=str(start),
                                         end_time=str(end))

    contract_ref.update(values)
    contract_ref.save(s)
    return contract_ref
Пример #7
0
def offer_update(offer_uuid, values):
    s = get_session()
    query = model_query(models.Offer, s)
    offer_ref = query.filter_by(uuid=offer_uuid).one_or_none()

    values.pop('uuid', None)
    values.pop('project_id', None)

    start = values.pop('start_time', None)
    end = values.pop('end_time', None)
    if start is None:
        start = offer_ref.start_time
    if end is None:
        end = offer_ref.end_time
    if start >= end:
        raise exception.InvalidTimeRange(resource="an offer",
                                         start_time=str(values['start_time']),
                                         end_time=str(values['end_time']))

    offer_ref.update(values)
    offer_ref.save(s)
    return offer_ref
Пример #8
0
def lease_update(lease_uuid, values):
    with _session_for_write() as session:
        query = model_query(models.Lease)
        lease_ref = query.filter_by(uuid=lease_uuid).one_or_none()

        values.pop('uuid', None)
        values.pop('project_id', None)

        start = values.get('start_time', None)
        end = values.get('end_time', None)
        if start is None:
            start = lease_ref.start_time
        if end is None:
            end = lease_ref.end_time
        if start >= end:
            raise exception.InvalidTimeRange(resource='a lease',
                                             start_time=str(start),
                                             end_time=str(end))

        lease_ref.update(values)
        session.flush()
        return lease_ref