class Lease(base._Base): id = types.UuidType() "The UUID of the lease" name = wtypes.text "The name of the lease" start_date = types.Datetime(service.LEASE_DATE_FORMAT) "Datetime when the lease should start" end_date = types.Datetime(service.LEASE_DATE_FORMAT) "Datetime when the lease should end" user_id = types.UuidType(without_dashes=True) "The ID of the user who creates the lease" project_id = types.UuidType(without_dashes=True) "The ID of the project or tenant the lease belongs to" trust_id = types.UuidType(without_dashes=True) "The ID of the trust created for delegating the rights of the user" reservations = wtypes.ArrayType(wtypes.DictType(wtypes.text, wtypes.text)) "The list of reservations belonging to the lease" events = wtypes.ArrayType(wtypes.DictType(wtypes.text, wtypes.text)) "The list of events attached to the lease" before_end_date = types.Datetime(service.LEASE_DATE_FORMAT) "Datetime when some actions will be taken before lease ending" status = wtypes.text "The status of the lease" @classmethod def sample(cls): return cls( id=u'2bb8720a-0873-4d97-babf-0d906851a1eb', name=u'lease_test', start_date=u'2014-01-01 01:23', end_date=u'2014-02-01 13:37', user_id=u'efd8780712d24b389c705f5c2ac427ff', project_id=u'bd9431c18d694ad3803a8d4a6b89fd36', trust_id=u'35b17138b3644e6aa1318f3099c5be68', reservations=[{ u'resource_id': u'1234', u'resource_type': u'physical:host' }], events=[], before_end_date=u'2014-02-01 10:37', status=u'ACTIVE', )
class LeasesController(extensions.BaseController): """Manages operations on leases.""" name = 'leases' @policy.authorize('leases', 'get') @wsme_pecan.wsexpose(Lease, types.UuidType()) def get_one(self, id): """Returns the lease having this specific uuid :param id: ID of lease """ lease = pecan.request.rpcapi.get_lease(id) if lease is None: raise exceptions.NotFound(object={'lease_id': id}) return Lease.convert(lease) @policy.authorize('leases', 'get') @wsme_pecan.wsexpose([Lease], q=[]) def get_all(self): """Returns all leases.""" return [ Lease.convert(lease) for lease in pecan.request.rpcapi.list_leases() ] @policy.authorize('leases', 'post') @wsme_pecan.wsexpose(Lease, body=Lease, status_code=201) @trusts.use_trust_auth() def post(self, lease): """Creates a new lease. :param lease: a lease within the request body. """ # FIXME(sbauza): DB exceptions are currently catched and return a lease # equal to None instead of being sent to the API lease_dct = lease.as_dict() lease = pecan.request.rpcapi.create_lease(lease_dct) if lease is not None: return Lease.convert(lease) else: raise exceptions.BlazarException(_("Lease can't be created")) @policy.authorize('leases', 'put') @wsme_pecan.wsexpose(Lease, types.UuidType(), body=Lease) def put(self, id, sublease): """Update an existing lease. :param id: UUID of a lease. :param lease: a subset of a Lease containing values to update. """ sublease_dct = sublease.as_dict() new_name = sublease_dct.pop('name', None) end_date = sublease_dct.pop('end_date', None) start_date = sublease_dct.pop('start_date', None) before_end_date = sublease_dct.pop('before_end_date', None) if sublease_dct != {}: raise exceptions.BlazarException('Only name changing, ' 'dates and before end ' 'notifications may be ' 'proceeded.') if new_name: sublease_dct['name'] = new_name if end_date: sublease_dct['end_date'] = end_date if start_date: sublease_dct['start_date'] = start_date if before_end_date: sublease_dct['before_end_date'] = before_end_date lease = pecan.request.rpcapi.update_lease(id, sublease_dct) if lease is None: raise exceptions.NotFound(object={'lease_id': id}) return Lease.convert(lease) @policy.authorize('leases', 'delete') @wsme_pecan.wsexpose(None, types.UuidType(), status_code=204) def delete(self, id): """Delete an existing lease. :param id: UUID of a lease. """ try: pecan.request.rpcapi.delete_lease(id) except TypeError: # The lease was not existing when asking to delete it raise exceptions.NotFound(object={'lease_id': id})
class Host(base._Base): id = types.IntegerType() "The ID of the host" hypervisor_hostname = wtypes.text "The hostname of the host" # FIXME(sbauza): API V1 provides 'name', so mapping is necessary until we # patch the client name = hypervisor_hostname hypervisor_type = wtypes.text "The type of the hypervisor" vcpus = types.IntegerType() "The number of VCPUs of the host" hypervisor_version = types.IntegerType() "The version of the hypervisor" memory_mb = types.IntegerType() "The memory size (in Mb) of the host" local_gb = types.IntegerType() "The disk size (in Gb) of the host" cpu_info = types.CPUInfo() "The CPU info JSON data given by the hypervisor" trust_id = types.UuidType() "The ID of the trust created for delegating the rights of the user" extra_capas = wtypes.DictType(wtypes.text, types.TextOrInteger()) "Extra capabilities for the host" @classmethod def convert(cls, rpc_obj): extra_keys = [ key for key in rpc_obj if key not in [i.key for i in wtypes.list_attributes(Host)] ] extra_capas = dict((capa, rpc_obj[capa]) for capa in extra_keys if capa not in ['status']) rpc_obj['extra_capas'] = extra_capas obj = cls(**rpc_obj) return obj def as_dict(self): dct = super(Host, self).as_dict() extra_capas = dct.pop('extra_capas', None) if extra_capas is not None: dct.update(extra_capas) return dct @classmethod def sample(cls): return cls( id=u'1', hypervisor_hostname=u'host01', hypervisor_type=u'QEMU', vcpus=1, hypervisor_version=1000000, memory_mb=8192, local_gb=50, cpu_info="{\"vendor\": \"Intel\", \"model\": \"qemu32\", " "\"arch\": \"x86_64\", \"features\": []," " \"topology\": {\"cores\": 1}}", extra_capas={ u'vgpus': 2, u'fruits': u'bananas' }, )