class Bid(base.FLOCXMarketObject): fields = { 'marketplace_bid_id': fields.StringField(), 'creator_bid_id': fields.StringField(), 'server_quantity': fields.IntegerField(), 'start_time': fields.DateTimeField(nullable=True), 'end_time': fields.DateTimeField(nullable=True), 'duration': fields.IntegerField(), 'status': fields.StringField(), 'server_config_query': fields.FlexibleDictField(nullable=True), 'cost': fields.FloatField(), 'project_id': fields.StringField() } @classmethod def create(cls, data, context): b = db.bid_create(data, context) return cls._from_db_object(cls(), b) @classmethod def get(cls, bid_id, context): if bid_id is None: return None else: b = db.bid_get(bid_id, context) if b is None: return None else: return cls._from_db_object(cls(), b) def destroy(self, context): db.bid_destroy(self.marketplace_bid_id, context) return True @classmethod def get_all(cls, context): all_bids = db.bid_get_all(context) return cls._from_db_object_list(all_bids) def save(self, context): updates = self.obj_get_changes() db_bid = db.bid_update(self.marketplace_bid_id, updates, context) return self._from_db_object(self, db_bid) @classmethod def get_all_unexpired(cls, context): unexpired = db.bid_get_all_unexpired(context) return cls._from_db_object_list(unexpired) @classmethod def get_all_by_project_id(cls, context): by_project_id = db.bid_get_all_by_project_id(context) return cls._from_db_object_list(by_project_id) def expire(self, context): self.status = 'expired' self.save(context)
class Offer(base.FLOCXMarketObject): fields = { 'marketplace_offer_id': fields.StringField(), 'provider_offer_id': fields.StringField(), 'project_id': fields.StringField(), 'status': fields.StringField(), 'server_id': fields.StringField(), 'start_time': fields.DateTimeField(nullable=True), 'end_time': fields.DateTimeField(nullable=True), 'server_config': fields.FlexibleDictField(nullable=True), 'cost': fields.FloatField(), } @classmethod def create(cls, data, context): o = db.offer_create(data, context) return cls._from_db_object(cls(), o) @classmethod def get(cls, offer_id, context): if offer_id is None: return None else: o = db.offer_get(offer_id, context) if o is None: return None else: return cls._from_db_object(cls(), o) def destroy(self, context): db.offer_destroy(self.marketplace_offer_id, context) return True @classmethod def get_all(cls, context): all_offers = db.offer_get_all(context) return cls._from_db_object_list(all_offers) def save(self, context): updates = self.obj_get_changes() db_offer = db.offer_update(self.marketplace_offer_id, updates, context) return self._from_db_object(self, db_offer) @classmethod def get_all_unexpired(cls, context): unexpired = db.offer_get_all_unexpired(context) return cls._from_db_object_list(unexpired) @classmethod def get_all_by_project_id(cls, context): by_project_id = db.offer_get_all_by_project_id(context) return cls._from_db_object_list(by_project_id) def expire(self, context): self.status = 'expired' self.save(context)
class OfferContractRelationship(base.FLOCXMarketObject): fields = { 'offer_contract_relationship_id': fields.StringField(), 'marketplace_offer_id': fields.StringField(), 'contract_id': fields.StringField(), 'status': fields.StringField(), } @classmethod def get(cls, context, contract_id, marketplace_offer_id): if (contract_id is None) and (marketplace_offer_id is None): return None else: o = db.offer_contract_relationship_get( contract_id=contract_id, marketplace_offer_id=marketplace_offer_id, context=context) if o is None: return None else: return cls._from_db_object(cls(), o) def destroy(self, context): db.offer_contract_relationship_destroy( contract_id=self.contract_id, marketplace_offer_id=self.marketplace_offer_id, context=context) return True @classmethod def get_all(cls, context): all_ocrs = db.offer_contract_relationship_get_all(context) return cls._from_db_object_list(all_ocrs) def save(self, context): updates = self.obj_get_changes() db_offer_contract_relationship = db.offer_contract_relationship_update( context, self.contract_id, self.marketplace_offer_id, updates, ) return self._from_db_object(self, db_offer_contract_relationship) @classmethod def get_all_unexpired(cls, context): unexpired = db.offer_contract_relationship_get_all_unexpired(context) return cls._from_db_object_list(unexpired) def expire(self, context): self.status = 'expired' self.save(context)
class Offer(base.FLOCXMarketObject): fields = { 'marketplace_offer_id': fields.StringField(), 'provider_id': fields.StringField(), 'creator_id': fields.StringField(), 'marketplace_date_created': fields.DateTimeField(nullable=True), 'status': fields.StringField(), 'server_id': fields.StringField(), 'start_time': fields.DateTimeField(nullable=True), 'end_time': fields.DateTimeField(nullable=True), 'server_config': fields.FlexibleDictField(nullable=True), 'cost': fields.FloatField() } @classmethod def create(cls, data): o = db.offer_create(data) return cls._from_db_object(cls(), o) @classmethod def get(cls, offer_id): if offer_id is None: return None else: o = db.offer_get(offer_id) if o is None: return None else: return cls._from_db_object(cls(), o) def destroy(self): db.offer_destroy(self.marketplace_offer_id) return True @classmethod def get_all(cls): all_offers = db.offer_get_all() return cls._from_db_object_list(all_offers) def save(self): updates = self.obj_get_changes() db_offer = db.offer_update(self.marketplace_offer_id, updates) return self._from_db_object(self, db_offer)
class Contract(base.FLOCXMarketObject): fields = { 'contract_id': fields.StringField(), 'time_created': fields.DateTimeField(nullable=True), 'status': fields.StringField(), 'start_time': fields.DateTimeField(nullable=True), 'end_time': fields.DateTimeField(nullable=True), 'cost': fields.FloatField(), 'bid_id': fields.StringField(), 'project_id': fields.StringField() } def to_dict(self): ret = dict() for k in self.fields: val = getattr(self, k) if type(val) == datetime.datetime: ret[k] = val.isoformat() elif k == 'bid' and val is None: continue else: ret[k] = val return ret @classmethod def get(cls, contract_id, context): if contract_id is None: return None else: c = db.contract_get(contract_id, context) if c is None: return None else: return cls._from_db_object(cls(), c) @classmethod def get_all(cls, context): all_contracts = db.contract_get_all(context) return cls._from_db_object_list(all_contracts) @classmethod def create(cls, data, context): c = db.contract_create(data, context) return cls._from_db_object(cls(), c) def destroy(self, context): db.contract_destroy(self.contract_id, context) return True def save(self, context): updates = self.obj_get_changes() db_contract = db.contract_update(self.contract_id, updates, context) return self._from_db_object(self, db_contract) @classmethod def get_all_unexpired(cls, context): unexpired = db.contract_get_all_unexpired(context) return cls._from_db_object_list(unexpired) def expire(self, context): self.status = 'expired' self.save(context)
class Offer(base.FLOCXMarketObject): fields = { 'offer_id': fields.StringField(), 'project_id': fields.StringField(), 'status': fields.StringField(), 'resource_id': fields.StringField(), 'resource_type': fields.StringField(), 'start_time': fields.DateTimeField(nullable=True), 'end_time': fields.DateTimeField(nullable=True), 'config': fields.FlexibleDictField(nullable=True), 'cost': fields.FloatField(), } @classmethod def create(cls, data, context): if 'config' not in data: ro = ro_factory.ResourceObjectFactory.get_resource_object( data['resource_type'], data['resource_id']) data['config'] = ro.get_node_config() o = db.offer_create(data, context) return cls._from_db_object(cls(), o) @classmethod def get(cls, offer_id, context): if offer_id is None: return None else: o = db.offer_get(offer_id, context) if o is None: return None else: return cls._from_db_object(cls(), o) def destroy(self, context): db.offer_destroy(self.offer_id, context) return True @classmethod def get_all(cls, context): all_offers = db.offer_get_all(context) return cls._from_db_object_list(all_offers) def save(self, context): updates = self.obj_get_changes() db_offer = db.offer_update(self.offer_id, updates, context) return self._from_db_object(self, db_offer) @classmethod def get_all_unexpired(cls, context): unexpired = db.offer_get_all_unexpired(context) return cls._from_db_object_list(unexpired) @classmethod def get_all_by_project_id(cls, context): by_project_id = db.offer_get_all_by_project_id(context) return cls._from_db_object_list(by_project_id) def related_contracts(self, context): related_contracts = [] ocrs = oc_relationship.OfferContractRelationship.get_all( context, {'offer_id': self.offer_id}) for ocr in ocrs: related_contracts.append(ocr.contract(context)) return related_contracts def expire(self, context): # make sure all related contracts are expired related_contracts = self.related_contracts(context) for c in related_contracts: if c.status != statuses.EXPIRED: c.expire(context) self.status = statuses.EXPIRED self.save(context) def resource_object(self): return ro_factory.ResourceObjectFactory.get_resource_object( self.resource_type, self.resource_id) @classmethod def get_available_status_contract(cls, context, start_time, end_time): def check_contracts_time(context, prev_contracts, start_time, end_time): for con in prev_contracts: c = contract.Contract.get(con.contract_id, context) if not (end_time <= c.start_time or start_time >= c.end_time): return False return True offers_by_status = db.offer_get_all_by_status(statuses.AVAILABLE, context) if start_time is None and end_time is None: return cls._from_db_object_list(offers_by_status) else: valid_offers = [] for o in offers_by_status: prev_con = oc_relationship.OfferContractRelationship. \ get_all(context, filters={'offer_id': o.offer_id} ) if o.start_time < start_time and o.end_time > end_time \ and check_contracts_time(context, prev_con, start_time, end_time): valid_offers.append(o) return cls._from_db_object_list(valid_offers) @classmethod def get_all_by_status(cls, status, context): available = db.offer_get_all_by_status(status, context) return cls._from_db_object_list(available)
class Offer(base.FLOCXMarketObject): fields = { 'marketplace_offer_id': fields.StringField(), 'provider_offer_id': fields.StringField(), 'project_id': fields.StringField(), 'status': fields.StringField(), 'server_id': fields.StringField(), 'start_time': fields.DateTimeField(nullable=True), 'end_time': fields.DateTimeField(nullable=True), 'server_config': fields.FlexibleDictField(nullable=True), 'cost': fields.FloatField(), } @classmethod def create(cls, data, context): o = db.offer_create(data, context) return cls._from_db_object(cls(), o) @classmethod def get(cls, offer_id, context): if offer_id is None: return None else: o = db.offer_get(offer_id, context) if o is None: return None else: return cls._from_db_object(cls(), o) def destroy(self, context): db.offer_destroy(self.marketplace_offer_id, context) return True @classmethod def get_all(cls, context): all_offers = db.offer_get_all(context) return cls._from_db_object_list(all_offers) def save(self, context): updates = self.obj_get_changes() db_offer = db.offer_update( self.marketplace_offer_id, updates, context) return self._from_db_object(self, db_offer) @classmethod def get_all_unexpired(cls, context): unexpired = db.offer_get_all_unexpired(context) return cls._from_db_object_list(unexpired) @classmethod def get_all_by_project_id(cls, context): by_project_id = db.offer_get_all_by_project_id(context) return cls._from_db_object_list(by_project_id) def expire(self, context): self.status = 'expired' self.save(context) @classmethod def get_available_status_contract(cls, context, start_time, end_time): def check_contracts_time(context, prev_contracts, start_time, end_time): for con in prev_contracts: c = contract.Contract.get(con.contract_id, context) if not (end_time <= c.start_time or start_time >= c.end_time): return False return True offers_by_status = db.offer_get_all_by_status('available', context) if start_time is None and end_time is None: return cls._from_db_object_list(offers_by_status) else: valid_offers = [] for o in offers_by_status: prev_con = ocr.OfferContractRelationship. \ get_all(context, filters={'marketplace_offer_id': o.marketplace_offer_id} ) if o.start_time < start_time and o.end_time > end_time \ and check_contracts_time(context, prev_con, start_time, end_time): valid_offers.append(o) return cls._from_db_object_list(valid_offers) @classmethod def get_all_by_status(cls, status, context): available = db.offer_get_all_by_status(status, context) return cls._from_db_object_list(available)
class OfferContractRelationship(base.FLOCXMarketObject): fields = { 'offer_contract_relationship_id': fields.StringField(), 'offer_id': fields.StringField(), 'contract_id': fields.StringField(), 'status': fields.StringField(), } @classmethod def get(cls, context, ocr_id): o = db.offer_contract_relationship_get(context, ocr_id) if o is None: return None else: return cls._from_db_object(cls(), o) def destroy(self, context): db.offer_contract_relationship_destroy( context, self.offer_contract_relationship_id) return True @classmethod def get_all(cls, context, filters=None): o = db.offer_contract_relationship_get_all(context, filters) return cls._from_db_object_list(o) def save(self, context): updates = self.obj_get_changes() db_offer_contract_relationship = db.offer_contract_relationship_update( context, self.offer_contract_relationship_id, updates) return self._from_db_object(self, db_offer_contract_relationship) def contract(self, context): return contract.Contract.get(self.contract_id, context) def offer(self, context): return offer.Offer.get(self.offer_id, context) @classmethod def get_all_unexpired(cls, context): unexpired = db.offer_contract_relationship_get_all_unexpired(context) return cls._from_db_object_list(unexpired) def fulfill(self, context): ro = self.offer(context).resource_object() ro.set_contract(self.contract(context)) self.status = statuses.FULFILLED self.save(context) def expire(self, context): ro = self.offer(context).resource_object() c = self.contract(context) if ro.get_contract_uuid() == c.contract_id: ro.set_contract(None) self.status = statuses.EXPIRED self.save(context)