def setUp(self): super(TestFloat, self).setUp() self.field = fields.FloatField() self.coerce_good_values = [(1.1, 1.1), ('1.1', 1.1)] self.coerce_bad_values = ['foo', None] self.to_primitive_values = self.coerce_good_values[0:1] self.from_primitive_values = self.coerce_good_values[0:1]
class ContainerBase(base.ZunPersistentObject, base.ZunObject): fields = { 'id': fields.IntegerField(), 'container_id': fields.StringField(nullable=True), 'uuid': fields.UUIDField(nullable=True), 'name': fields.StringField(nullable=True), 'project_id': fields.StringField(nullable=True), 'user_id': fields.StringField(nullable=True), 'image': fields.StringField(nullable=True), 'cpu': fields.FloatField(nullable=True), 'cpu_policy': fields.StringField(nullable=True), 'cpuset': fields.ObjectField("Cpuset", nullable=True), 'memory': fields.StringField(nullable=True), 'command': fields.ListOfStringsField(nullable=True), 'status': z_fields.ContainerStatusField(nullable=True), 'status_reason': fields.StringField(nullable=True), 'task_state': z_fields.TaskStateField(nullable=True), 'environment': fields.DictOfStringsField(nullable=True), 'workdir': fields.StringField(nullable=True), 'auto_remove': fields.BooleanField(nullable=True), 'ports': z_fields.ListOfIntegersField(nullable=True), 'hostname': fields.StringField(nullable=True), 'labels': fields.DictOfStringsField(nullable=True), 'addresses': z_fields.JsonField(nullable=True), 'image_pull_policy': fields.StringField(nullable=True), 'host': fields.StringField(nullable=True), 'restart_policy': fields.DictOfStringsField(nullable=True), 'status_detail': fields.StringField(nullable=True), 'interactive': fields.BooleanField(nullable=True), 'tty': fields.BooleanField(nullable=True), 'image_driver': fields.StringField(nullable=True), 'websocket_url': fields.StringField(nullable=True), 'websocket_token': fields.StringField(nullable=True), 'security_groups': fields.ListOfStringsField(nullable=True), 'runtime': fields.StringField(nullable=True), 'pci_devices': fields.ListOfObjectsField('PciDevice', nullable=True), 'disk': fields.IntegerField(nullable=True), 'auto_heal': fields.BooleanField(nullable=True), 'started_at': fields.DateTimeField(tzinfo_aware=False, nullable=True), 'exposed_ports': z_fields.JsonField(nullable=True), 'exec_instances': fields.ListOfObjectsField('ExecInstance', nullable=True), 'privileged': fields.BooleanField(nullable=True), 'healthcheck': z_fields.JsonField(nullable=True), 'registry_id': fields.IntegerField(nullable=True), 'registry': fields.ObjectField("Registry", nullable=True), 'annotations': z_fields.JsonField(nullable=True), 'cni_metadata': z_fields.JsonField(nullable=True), 'entrypoint': fields.ListOfStringsField(nullable=True), } # should be redefined in subclasses container_type = None @staticmethod def _from_db_object(container, db_container): """Converts a database entity to a formal object.""" for field in container.fields: if field in [ 'pci_devices', 'exec_instances', 'registry', 'containers', 'init_containers' ]: continue if field == 'cpuset': container.cpuset = Cpuset._from_dict(db_container['cpuset']) continue setattr(container, field, db_container[field]) container.obj_reset_changes() return container @staticmethod def _from_db_object_list(db_objects, cls, context): """Converts a list of database entities to a list of formal objects.""" return [cls._from_db_object(cls(context), obj) for obj in db_objects] @base.remotable_classmethod def get_by_uuid(cls, context, uuid): """Find a container based on uuid and return a :class:`Container` object. :param uuid: the uuid of a container. :param context: Security context :returns: a :class:`Container` object. """ db_container = dbapi.get_container_by_uuid(context, cls.container_type, uuid) container = cls._from_db_object(cls(context), db_container) return container @base.remotable_classmethod def get_by_name(cls, context, name): """Find a container based on name and return a Container object. :param name: the logical name of a container. :param context: Security context :returns: a :class:`Container` object. """ db_container = dbapi.get_container_by_name(context, cls.container_type, name) container = cls._from_db_object(cls(context), db_container) return container @staticmethod def get_container_any_type(context, uuid): """Find a container of any type based on uuid. :param uuid: the uuid of a container. :param context: Security context :returns: a :class:`ContainerBase` object. """ db_container = dbapi.get_container_by_uuid(context, consts.TYPE_ANY, uuid) type = db_container['container_type'] if type == consts.TYPE_CONTAINER: container_cls = Container elif type == consts.TYPE_CAPSULE: container_cls = Capsule elif type == consts.TYPE_CAPSULE_CONTAINER: container_cls = CapsuleContainer elif type == consts.TYPE_CAPSULE_INIT_CONTAINER: container_cls = CapsuleInitContainer else: raise exception.ZunException(_('Unknown container type: %s'), type) obj = container_cls(context) container = container_cls._from_db_object(obj, db_container) return container @base.remotable_classmethod def list(cls, context, limit=None, marker=None, sort_key=None, sort_dir=None, filters=None): """Return a list of Container objects. :param context: Security context. :param limit: maximum number of resources to return in a single result. :param marker: pagination marker for large data sets. :param sort_key: column to sort results by. :param sort_dir: direction to sort. "asc" or "desc". :param filters: filters when list containers, the filter name could be 'name', 'image', 'project_id', 'user_id', 'memory'. For example, filters={'image': 'nginx'} :returns: a list of :class:`Container` object. """ db_containers = dbapi.list_containers(context, cls.container_type, limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir, filters=filters) return cls._from_db_object_list(db_containers, cls, context) @base.remotable_classmethod def list_by_host(cls, context, host): """Return a list of Container objects by host. :param context: Security context. :param host: A compute host. :returns: a list of :class:`Container` object. """ db_containers = dbapi.list_containers(context, cls.container_type, filters={'host': host}) return cls._from_db_object_list(db_containers, cls, context) @base.remotable def create(self, context): """Create a Container record in the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ values = self.obj_get_changes() cpuset_obj = values.pop('cpuset', None) if cpuset_obj is not None: values['cpuset'] = cpuset_obj._to_dict() annotations = values.pop('annotations', None) if annotations is not None: values['annotations'] = self.fields['annotations'].to_primitive( self, 'annotations', self.annotations) cni_metadata = values.pop('cni_metadata', None) if cni_metadata is not None: values['cni_metadata'] = self.fields['cni_metadata'].to_primitive( self, 'cni_metadata', self.cni_metadata) values['container_type'] = self.container_type db_container = dbapi.create_container(context, values) self._from_db_object(self, db_container) @base.remotable def destroy(self, context=None): """Delete the Container from the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ dbapi.destroy_container(context, self.container_type, self.uuid) self.obj_reset_changes() @base.remotable def save(self, context=None): """Save updates to this Container. Updates will be made column by column based on the result of self.what_changed(). :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ updates = self.obj_get_changes() cpuset_obj = updates.pop('cpuset', None) if cpuset_obj is not None: updates['cpuset'] = cpuset_obj._to_dict() annotations = updates.pop('annotations', None) if annotations is not None: updates['annotations'] = self.fields['annotations'].to_primitive( self, 'annotations', self.annotations) cni_metadata = updates.pop('cni_metadata', None) if cni_metadata is not None: updates['cni_metadata'] = self.fields['cni_metadata'].to_primitive( self, 'cni_metadata', self.cni_metadata) dbapi.update_container(context, self.container_type, self.uuid, updates) self.obj_reset_changes() @base.remotable def refresh(self, context=None): """Loads updates for this Container. Loads a container with the same uuid from the database and checks for updated attributes. Updates are applied from the loaded container column by column, if there are any updates. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ current = self.__class__.get_by_uuid(self._context, uuid=self.uuid) for field in self.fields: if self.obj_attr_is_set(field) and \ getattr(self, field) != getattr(current, field): setattr(self, field, getattr(current, field)) def obj_load_attr(self, attrname): if attrname not in CONTAINER_OPTIONAL_ATTRS: raise exception.ObjectActionError( action='obj_load_attr', reason=_('attribute %s not lazy-loadable') % attrname) if not self._context: raise exception.OrphanedObjectError(method='obj_load_attr', objtype=self.obj_name()) LOG.debug("Lazy-loading '%(attr)s' on %(name)s uuid %(uuid)s", { 'attr': attrname, 'name': self.obj_name(), 'uuid': self.uuid, }) # NOTE(danms): We handle some fields differently here so that we # can be more efficient if attrname == 'pci_devices': self._load_pci_devices() if attrname == 'exec_instances': self._load_exec_instances() if attrname == 'registry': self._load_registry() self.obj_reset_changes([attrname]) def _load_pci_devices(self): self.pci_devices = pci_device.PciDevice.list_by_container_uuid( self._context, self.uuid) def _load_exec_instances(self): self.exec_instances = exec_inst.ExecInstance.list_by_container_id( self._context, self.id) def _load_registry(self): self.registry = None if self.registry_id: self.registry = registry.Registry.get_by_id( self._context, self.registry_id) @base.remotable_classmethod def get_count(cls, context, project_id, flag): """Get the counts of Container objects in the database. :param context: The request context for database access. :param project_id: The project_id to count across. :param flag: The name of resource, one of the following options: - containers: Count the number of containers owned by the project. - memory: The sum of containers's memory. - cpu: The sum of container's cpu. - disk: The sum of container's disk size. """ usage = dbapi.count_usage(context, cls.container_type, project_id, flag)[0] or 0.0 return usage
class Capsule(base.ZunPersistentObject, base.ZunObject): # Version 1.0: Initial version # Version 1.1: Add host to capsule # Version 1.2: Change the properties of meta_labels # Version 1.3: Add 'Deleting' to ContainerStatus # Version 1.4: Add addresses and volumes_info # Version 1.5: Change the properties of restort_policy # Version 1.6: Change the type of status VERSION = '1.6' fields = { 'capsule_version': fields.StringField(nullable=True), 'kind': fields.StringField(nullable=True), 'restart_policy': fields.StringField(nullable=True), 'host_selector': fields.StringField(nullable=True), 'id': fields.IntegerField(), 'uuid': fields.UUIDField(nullable=True), 'project_id': fields.StringField(nullable=True), 'user_id': fields.StringField(nullable=True), 'status': z_fields.CapsuleStatusField(nullable=True), 'status_reason': fields.StringField(nullable=True), 'cpu': fields.FloatField(nullable=True), 'memory': fields.StringField(nullable=True), 'addresses': z_fields.JsonField(nullable=True), # conclude the readable message # 'key': 'value'--> 'time':'message' # wait until zun notify is finished # 'message': fields.DictOfStringsField(nullable=True), 'spec': z_fields.JsonField(nullable=True), 'meta_name': fields.StringField(nullable=True), 'meta_labels': fields.DictOfStringsField(nullable=True), 'containers': fields.ListOfObjectsField('Container', nullable=True), # The list of containers uuids inside the capsule 'containers_uuids': fields.ListOfStringsField(nullable=True), 'host': fields.StringField(nullable=True), # volumes_info records the volume and container attached # relationship: # {'<volume-uuid1>': ['<container-uuid1>', '<container-uuid2>'], # '<volume-uuid2>': ['<container-uuid2>', '<container-uuid3>']}, # one container can attach at least one volume, also will support # one volume multiple in the future. 'volumes_info': z_fields.JsonField(nullable=True), } @staticmethod def _from_db_object(capsule, db_capsule): """Converts a database entity to a formal object.""" for field in capsule.fields: if field in CAPSULE_OPTIONAL_ATTRS: continue setattr(capsule, field, db_capsule[field]) capsule.obj_reset_changes() return capsule @staticmethod def _from_db_object_list(db_objects, cls, context): """Converts a list of database entities to a list of formal objects.""" return [ Capsule._from_db_object(cls(context), obj) for obj in db_objects ] @base.remotable_classmethod def get_by_uuid(cls, context, uuid): """Find a capsule based on uuid and return a :class:`Capsule` object. :param uuid: the uuid of a capsule. :param context: Security context :returns: a :class:`Capsule` object. """ db_capsule = dbapi.get_capsule_by_uuid(context, uuid) capsule = Capsule._from_db_object(cls(context), db_capsule) return capsule @base.remotable_classmethod def get_by_name(cls, context, name): """Find a capsule based on name and return a :class:`Capsule` object. :param name: the meta_name of a capsule. :param context: Security context :returns: a :class:`Capsule` object. """ db_capsule = dbapi.get_capsule_by_meta_name(context, name) capsule = Capsule._from_db_object(cls(context), db_capsule) return capsule @base.remotable_classmethod def list(cls, context, limit=None, marker=None, sort_key=None, sort_dir=None, filters=None): """Return a list of Capsule objects. :param context: Security context. :param limit: maximum number of resources to return in a single result. :param marker: pagination marker for large data sets. :param sort_key: column to sort results by. :param sort_dir: direction to sort. "asc" or "desc". :param filters: filters when list capsules, the filter name could be 'name', 'image', 'project_id', 'user_id', 'memory'. For example, filters={'image': 'nginx'} :returns: a list of :class:`Capsule` object. """ db_capsules = dbapi.list_capsules(context, limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir, filters=filters) return Capsule._from_db_object_list(db_capsules, cls, context) @base.remotable def create(self, context): """Create a Container record in the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Capsule(context) """ values = self.obj_get_changes() if 'containers' in values: raise exception.ObjectActionError(action='create', reason='containers assigned') db_capsule = dbapi.create_capsule(context, values) self._from_db_object(self, db_capsule) @base.remotable def destroy(self, context=None): """Delete the Container from the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Capsule(context) """ dbapi.destroy_capsule(context, self.uuid) self.obj_reset_changes() @base.remotable def save(self, context=None): """Save updates to this Capsule. Updates will be made column by column based on the result of self.what_changed(). :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Capsule(context) """ updates = self.obj_get_changes() if 'containers' in updates: raise exception.ObjectActionError(action='save', reason='containers changed') dbapi.update_capsule(context, self.uuid, updates) self.obj_reset_changes() def as_dict(self): capsule_dict = super(Capsule, self).as_dict() capsule_dict['containers'] = [c.as_dict() for c in self.containers] return capsule_dict def obj_load_attr(self, attrname): if attrname not in CAPSULE_OPTIONAL_ATTRS: raise exception.ObjectActionError( action='obj_load_attr', reason='attribute %s not lazy-loadable' % attrname) if not self._context: raise exception.OrphanedObjectError(method='obj_load_attr', objtype=self.obj_name()) LOG.debug("Lazy-loading '%(attr)s' on %(name)s uuid %(uuid)s", { 'attr': attrname, 'name': self.obj_name(), 'uuid': self.uuid, }) if attrname == 'containers': self.containers = container.Container.list_by_capsule_id( self._context, self.id) self.obj_reset_changes(fields=[attrname])
class ComputeNode(base.ZunPersistentObject, base.ZunObject): # Version 1.0: Initial version # Version 1.1: Add mem_total, mem_free, mem_available columns # Version 1.2: Add total, running, pasued, stopped containers columns # Version 1.3: Add cpus, cpu_used # Version 1.4: Add host operating system info # Version 1.5: Add host labels info # Version 1.6: Add mem_used to compute node # Version 1.7: Change get_by_hostname to get_by_name # Version 1.8: Add pci_device_pools to compute node # Version 1.9: Change PciDevicePoolList to ObjectField # Version 1.10: Add disk_total, disk_used columns # Version 1.11: Add disk_quota_supported field # Version 1.12: Add runtimes field VERSION = '1.12' fields = { 'uuid': fields.UUIDField(read_only=True, nullable=False), 'numa_topology': fields.ObjectField('NUMATopology', nullable=True), 'hostname': fields.StringField(nullable=False), 'mem_total': fields.IntegerField(nullable=False), 'mem_free': fields.IntegerField(nullable=False), 'mem_available': fields.IntegerField(nullable=False), 'mem_used': fields.IntegerField(nullable=False), 'total_containers': fields.IntegerField(nullable=False), 'running_containers': fields.IntegerField(nullable=False), 'paused_containers': fields.IntegerField(nullable=False), 'stopped_containers': fields.IntegerField(nullable=False), 'cpus': fields.IntegerField(nullable=False), 'cpu_used': fields.FloatField(nullable=False), 'architecture': fields.StringField(nullable=True), 'os_type': fields.StringField(nullable=True), 'os': fields.StringField(nullable=True), 'kernel_version': fields.StringField(nullable=True), 'labels': fields.DictOfStringsField(nullable=True), # NOTE(pmurray): the pci_device_pools field maps to the # pci_stats field in the database 'pci_device_pools': fields.ObjectField('PciDevicePoolList', nullable=True), 'disk_total': fields.IntegerField(nullable=False), 'disk_used': fields.IntegerField(nullable=False), 'disk_quota_supported': fields.BooleanField(nullable=False), 'runtimes': fields.ListOfStringsField(nullable=True), } @staticmethod def _from_db_object(context, compute_node, db_compute_node): """Converts a database entity to a formal object.""" special_cases = set(['pci_device_pools']) fields = set(compute_node.fields) - special_cases for field in fields: if field == 'numa_topology': numa_obj = NUMATopology._from_dict( db_compute_node['numa_topology']) compute_node.numa_topology = numa_obj else: setattr(compute_node, field, db_compute_node[field]) pci_stats = db_compute_node.get('pci_stats') if pci_stats is not None: pci_stats = pci_device_pool.from_pci_stats(pci_stats) compute_node.pci_device_pools = pci_stats compute_node.obj_reset_changes(recursive=True) return compute_node @staticmethod def _from_db_object_list(db_objects, cls, context): """Converts a list of database entities to a list of formal objects.""" return [ComputeNode._from_db_object(context, cls(context), obj) for obj in db_objects] @staticmethod def _convert_pci_stats_to_db_format(updates): if 'pci_device_pools' in updates: pools = updates.pop('pci_device_pools') if pools is not None: pools = jsonutils.dumps(pools.obj_to_primitive()) updates['pci_stats'] = pools @base.remotable def create(self, context): """Create a compute node record in the DB. :param context: Security context. """ values = self.obj_get_changes() numa_obj = values.pop('numa_topology', None) if numa_obj is not None: values['numa_topology'] = numa_obj._to_dict() self._convert_pci_stats_to_db_format(values) db_compute_node = dbapi.create_compute_node(context, values) self._from_db_object(context, self, db_compute_node) @base.remotable_classmethod def get_by_uuid(cls, context, uuid): """Find a compute node based on uuid. :param uuid: the uuid of a compute node. :param context: Security context :returns: a :class:`ComputeNode` object. """ db_compute_node = dbapi.get_compute_node(context, uuid) compute_node = ComputeNode._from_db_object( context, cls(context), db_compute_node) return compute_node @base.remotable_classmethod def get_by_name(cls, context, hostname): db_compute_node = dbapi.get_compute_node_by_hostname( context, hostname) return cls._from_db_object(context, cls(), db_compute_node) @base.remotable_classmethod def list(cls, context, limit=None, marker=None, sort_key=None, sort_dir=None, filters=None): """Return a list of ComputeNode objects. :param context: Security context. :param limit: maximum number of resources to return in a single result. :param marker: pagination marker for large data sets. :param sort_key: column to sort results by. :param sort_dir: direction to sort. "asc" or "desc". :param filters: filters when list resource providers. :returns: a list of :class:`ComputeNode` object. """ db_compute_nodes = dbapi.list_compute_nodes( context, limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir, filters=filters) return ComputeNode._from_db_object_list( db_compute_nodes, cls, context) @base.remotable def destroy(self, context=None): """Delete the ComputeNode from the DB. :param context: Security context. """ dbapi.destroy_compute_node(context, self.uuid) self.obj_reset_changes(recursive=True) @base.remotable def save(self, context=None): """Save updates to this ComputeNode. Updates will be made column by column based on the result of self.what_changed(). :param context: Security context. """ updates = self.obj_get_changes() numa_obj = updates.pop('numa_topology', None) if numa_obj is not None: updates['numa_topology'] = numa_obj._to_dict() self._convert_pci_stats_to_db_format(updates) dbapi.update_compute_node(context, self.uuid, updates) self.obj_reset_changes(recursive=True) @base.remotable def refresh(self, context=None): """Loads updates for this ComputeNode. Loads a compute node with the same uuid from the database and checks for updated attributes. Updates are applied from the loaded compute node column by column, if there are any updates. :param context: Security context. """ current = self.__class__.get_by_uuid(self._context, uuid=self.uuid) for field in self.fields: if self.obj_attr_is_set(field) and \ getattr(self, field) != getattr(current, field): setattr(self, field, getattr(current, field))
class Container(base.ZunPersistentObject, base.ZunObject): # Version 1.0: Initial version # Version 1.1: Add container_id column # Version 1.2: Add memory column # Version 1.3: Add task_state column # Version 1.4: Add cpu, workdir, ports, hostname and labels columns # Version 1.5: Add meta column # Version 1.6: Add addresses column # Version 1.7: Add host column # Version 1.8: Add restart_policy # Version 1.9: Add status_detail column # Version 1.10: Add tty, stdin_open # Version 1.11: Add image_driver # Version 1.12: Add 'Created' to ContainerStatus # Version 1.13: Add more task states for container # Version 1.14: Add method 'list_by_host' # Version 1.15: Combine tty and stdin_open # Version 1.16: Add websocket_url and token # Version 1.17: Add security_groups # Version 1.18: Add auto_remove # Version 1.19: Add runtime column # Version 1.20: Change runtime to String type # Version 1.21: Add pci_device attribute # Version 1.22: Add 'Deleting' to ContainerStatus # Version 1.23: Add the missing 'pci_devices' attribute # Version 1.24: Add the storage_opt attribute # Version 1.25: Change TaskStateField definition # Version 1.26: Add auto_heal # Version 1.27: Make auto_heal field nullable # Version 1.28: Add 'Dead' to ContainerStatus # Version 1.29: Add 'Restarting' to ContainerStatus # Version 1.30: Add capsule_id attribute # Version 1.31: Add 'started_at' attribute # Version 1.32: Add 'exec_instances' attribute VERSION = '1.33' fields = { 'id': fields.IntegerField(), 'container_id': fields.StringField(nullable=True), 'uuid': fields.UUIDField(nullable=True), 'name': fields.StringField(nullable=True), 'project_id': fields.StringField(nullable=True), 'user_id': fields.StringField(nullable=True), 'image': fields.StringField(nullable=True), 'cpu': fields.FloatField(nullable=True), 'memory': fields.StringField(nullable=True), 'command': fields.ListOfStringsField(nullable=True), 'status': z_fields.ContainerStatusField(nullable=True), 'status_reason': fields.StringField(nullable=True), 'task_state': z_fields.TaskStateField(nullable=True), 'environment': fields.DictOfStringsField(nullable=True), 'workdir': fields.StringField(nullable=True), 'auto_remove': fields.BooleanField(nullable=True), 'ports': z_fields.ListOfIntegersField(nullable=True), 'hostname': fields.StringField(nullable=True), 'labels': fields.DictOfStringsField(nullable=True), 'meta': fields.DictOfStringsField(nullable=True), 'addresses': z_fields.JsonField(nullable=True), 'image_pull_policy': fields.StringField(nullable=True), 'host': fields.StringField(nullable=True), 'restart_policy': fields.DictOfStringsField(nullable=True), 'status_detail': fields.StringField(nullable=True), 'interactive': fields.BooleanField(nullable=True), 'image_driver': fields.StringField(nullable=True), 'websocket_url': fields.StringField(nullable=True), 'websocket_token': fields.StringField(nullable=True), 'security_groups': fields.ListOfStringsField(nullable=True), 'runtime': fields.StringField(nullable=True), 'pci_devices': fields.ListOfObjectsField('PciDevice', nullable=True), 'disk': fields.IntegerField(nullable=True), 'auto_heal': fields.BooleanField(nullable=True), 'capsule_id': fields.IntegerField(nullable=True), 'started_at': fields.DateTimeField(tzinfo_aware=False, nullable=True), 'exec_instances': fields.ListOfObjectsField('ExecInstance', nullable=True), } @staticmethod def _from_db_object(container, db_container): """Converts a database entity to a formal object.""" for field in container.fields: if field in ['pci_devices', 'exec_instances']: continue setattr(container, field, db_container[field]) container.obj_reset_changes() return container @staticmethod def _from_db_object_list(db_objects, cls, context): """Converts a list of database entities to a list of formal objects.""" return [ Container._from_db_object(cls(context), obj) for obj in db_objects ] @base.remotable_classmethod def get_by_uuid(cls, context, uuid): """Find a container based on uuid and return a :class:`Container` object. :param uuid: the uuid of a container. :param context: Security context :returns: a :class:`Container` object. """ db_container = dbapi.get_container_by_uuid(context, uuid) container = Container._from_db_object(cls(context), db_container) return container @base.remotable_classmethod def get_by_name(cls, context, name): """Find a container based on name and return a Container object. :param name: the logical name of a container. :param context: Security context :returns: a :class:`Container` object. """ db_container = dbapi.get_container_by_name(context, name) container = Container._from_db_object(cls(context), db_container) return container @base.remotable_classmethod def list(cls, context, limit=None, marker=None, sort_key=None, sort_dir=None, filters=None): """Return a list of Container objects. :param context: Security context. :param limit: maximum number of resources to return in a single result. :param marker: pagination marker for large data sets. :param sort_key: column to sort results by. :param sort_dir: direction to sort. "asc" or "desc". :param filters: filters when list containers, the filter name could be 'name', 'image', 'project_id', 'user_id', 'memory'. For example, filters={'image': 'nginx'} :returns: a list of :class:`Container` object. """ db_containers = dbapi.list_containers(context, limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir, filters=filters) return Container._from_db_object_list(db_containers, cls, context) @base.remotable_classmethod def list_by_host(cls, context, host): """Return a list of Container objects by host. :param context: Security context. :param host: A compute host. :returns: a list of :class:`Container` object. """ db_containers = dbapi.list_containers(context, filters={'host': host}) return Container._from_db_object_list(db_containers, cls, context) @base.remotable_classmethod def list_by_capsule_id(cls, context, capsule_id): """Return a list of Container objects by capsule_id. :param context: Security context. :param host: A capsule id. :returns: a list of :class:`Container` object. """ db_containers = dbapi.list_containers( context, filters={'capsule_id': capsule_id}) return Container._from_db_object_list(db_containers, cls, context) @base.remotable def create(self, context): """Create a Container record in the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ values = self.obj_get_changes() db_container = dbapi.create_container(context, values) self._from_db_object(self, db_container) @base.remotable def destroy(self, context=None): """Delete the Container from the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ dbapi.destroy_container(context, self.uuid) self.obj_reset_changes() @base.remotable def save(self, context=None): """Save updates to this Container. Updates will be made column by column based on the result of self.what_changed(). :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ updates = self.obj_get_changes() dbapi.update_container(context, self.uuid, updates) self.obj_reset_changes() @base.remotable def refresh(self, context=None): """Loads updates for this Container. Loads a container with the same uuid from the database and checks for updated attributes. Updates are applied from the loaded container column by column, if there are any updates. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ current = self.__class__.get_by_uuid(self._context, uuid=self.uuid) for field in self.fields: if self.obj_attr_is_set(field) and \ getattr(self, field) != getattr(current, field): setattr(self, field, getattr(current, field)) def get_sandbox_id(self): if self.meta: return self.meta.get('sandbox_id', None) else: return None def set_sandbox_id(self, sandbox_id): if self.meta is None: self.meta = {'sandbox_id': sandbox_id} else: self.meta['sandbox_id'] = sandbox_id self._changed_fields.add('meta') def obj_load_attr(self, attrname): if attrname not in CONTAINER_OPTIONAL_ATTRS: raise exception.ObjectActionError( action='obj_load_attr', reason=_('attribute %s not lazy-loadable') % attrname) if not self._context: raise exception.OrphanedObjectError(method='obj_load_attr', objtype=self.obj_name()) LOG.debug("Lazy-loading '%(attr)s' on %(name)s uuid %(uuid)s", { 'attr': attrname, 'name': self.obj_name(), 'uuid': self.uuid, }) # NOTE(danms): We handle some fields differently here so that we # can be more efficient if attrname == 'pci_devices': self._load_pci_devices() if attrname == 'exec_instances': self._load_exec_instances() self.obj_reset_changes([attrname]) def _load_pci_devices(self): self.pci_devices = pci_device.PciDevice.list_by_container_uuid( self._context, self.uuid) def _load_exec_instances(self): self.exec_instances = exec_inst.ExecInstance.list_by_container_id( self._context, self.id)
class Boar(base.BaseObject): VERSION = '1.0' dbapi = dbapi.get_instance() fields = { 'id': fields.IntegerField(nullable=False), 'ear_tag': fields.IntegerField(nullable=True), 'ear_lack': fields.IntegerField(nullable=True), #'birthday': fields.DateTimeField(nullable=True), #'entryday': fields.DateTimeField(nullable=True), 'birthday': fields.StringField(nullable=True), 'entryday': fields.StringField(nullable=True), 'dormitory_id': fields.IntegerField(nullable=True), 'category_id': fields.IntegerField(nullable=True), 'breed_num': fields.IntegerField(nullable=True), 'breed_acceptability': fields.FloatField(nullable=True), #'state_id': fields.IntegerField(nullable=True), #'state_day': fields.IntegerField(nullable=True), 'source_id': fields.IntegerField(nullable=True), 'category': fields.StringField(nullable=True), 'dormitory': fields.StringField(nullable=True), 'source': fields.StringField(nullable=True), } @staticmethod def _from_db_object(boar, db_boar): """Converts a database entity to a formal object.""" foreign_key = ['category', 'dormitory', 'source'] for field in boar.fields: if field in ['birthday', 'entryday']: boar[field] = db_boar[field].strftime("%Y-%m-%d") elif field not in foreign_key: boar[field] = db_boar[field] elif field == 'category' and db_boar.category: boar[field] = db_boar.category.name elif field == 'dormitory' and db_boar.dormitory: boar[field] = db_boar.dormitory.name elif field == 'source' and db_boar.source: boar[field] = db_boar.source.name boar.obj_reset_changes() return boar @staticmethod def _from_db_object_list(db_objects, cls, context): """Converts a list of database entities to a list of formal objects.""" return [Boar._from_db_object(cls(context), obj) for obj in db_objects] @base.remotable_classmethod def list(cls, context, filters=None, limit=3000, marker=1, sort_key='id', sort_dir='asc'): """Return a list of Boar objects. :param context: Security context. :param limit: maximum number of resources to return in a single result. :param marker: pagination marker for large data sets. :param sort_key: column to sort results by. :param sort_dir: direction to sort. "asc" or "desc". :returns: a list of :class:`Boar` object. """ db_boars = cls.dbapi.get_boar_list(context, limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir, filters=filters) #import pdb; pdb.set_trace() return [Boar._from_db_object(cls(context), obj) for obj in db_boars] @base.remotable_classmethod def get_by_id(cls, context, id): db_boar = cls.dbapi.get_boar_by_id(context, id) return Boar._from_db_object(cls(context), db_boar) @base.remotable def create(self, context=None): values = self.obj_get_changes() db_boar = self.dbapi.create_boar(context, values) return self._from_db_object(self, db_boar) @base.remotable def save(self, context=None): """Save updates to this boar. Updates will be made column by column based on the result of self.what_changed(). :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Boar(context) """ updates = self.obj_get_changes() self.dbapi.update_boar(context, self.id, updates) self.obj_reset_changes() @base.remotable def delete(self, context=None): self.dbapi.delete_boar(context, self.id)
class Container(base.ZunPersistentObject, base.ZunObject): # Version 1.0: Initial version # Version 1.1: Add container_id column # Version 1.2: Add memory column # Version 1.3: Add task_state column # Version 1.4: Add cpu, workdir, ports, hostname and labels columns # Version 1.5: Add meta column # Version 1.6: Add addresses column # Version 1.7: Add host column # Version 1.8: Add restart_policy # Version 1.9: Add status_detail column # Version 1.10: Add tty, stdin_open # Version 1.11: Add image_driver # Version 1.12: Add 'Created' to ContainerStatus # Version 1.13: Add more task states for container # Version 1.14: Add method 'list_by_host' # Version 1.15: Combine tty and stdin_open # Version 1.16: Add websocket_url and token # Version 1.17: Add security_groups # Version 1.18: Add auto_remove # Version 1.19: Add runtime column VERSION = '1.19' fields = { 'id': fields.IntegerField(), 'container_id': fields.StringField(nullable=True), 'uuid': fields.UUIDField(nullable=True), 'name': fields.StringField(nullable=True), 'project_id': fields.StringField(nullable=True), 'user_id': fields.StringField(nullable=True), 'image': fields.StringField(nullable=True), 'cpu': fields.FloatField(nullable=True), 'memory': fields.StringField(nullable=True), 'command': fields.StringField(nullable=True), 'status': z_fields.ContainerStatusField(nullable=True), 'status_reason': fields.StringField(nullable=True), 'task_state': z_fields.TaskStateField(nullable=True), 'environment': fields.DictOfStringsField(nullable=True), 'workdir': fields.StringField(nullable=True), 'auto_remove': fields.BooleanField(nullable=True), 'ports': z_fields.ListOfIntegersField(nullable=True), 'hostname': fields.StringField(nullable=True), 'labels': fields.DictOfStringsField(nullable=True), 'meta': fields.DictOfStringsField(nullable=True), 'addresses': z_fields.JsonField(nullable=True), 'image_pull_policy': fields.StringField(nullable=True), 'host': fields.StringField(nullable=True), 'restart_policy': fields.DictOfStringsField(nullable=True), 'status_detail': fields.StringField(nullable=True), 'interactive': fields.BooleanField(nullable=True), 'image_driver': fields.StringField(nullable=True), 'websocket_url': fields.StringField(nullable=True), 'websocket_token': fields.StringField(nullable=True), 'security_groups': fields.ListOfStringsField(nullable=True), 'runtime': z_fields.ContainerRuntimeField(nullable=True) } @staticmethod def _from_db_object(container, db_container): """Converts a database entity to a formal object.""" for field in container.fields: setattr(container, field, db_container[field]) container.obj_reset_changes() return container @staticmethod def _from_db_object_list(db_objects, cls, context): """Converts a list of database entities to a list of formal objects.""" return [Container._from_db_object(cls(context), obj) for obj in db_objects] @base.remotable_classmethod def get_by_uuid(cls, context, uuid): """Find a container based on uuid and return a :class:`Container` object. :param uuid: the uuid of a container. :param context: Security context :returns: a :class:`Container` object. """ db_container = dbapi.get_container_by_uuid(context, uuid) container = Container._from_db_object(cls(context), db_container) return container @base.remotable_classmethod def get_by_name(cls, context, name): """Find a container based on name and return a Container object. :param name: the logical name of a container. :param context: Security context :returns: a :class:`Container` object. """ db_container = dbapi.get_container_by_name(context, name) container = Container._from_db_object(cls(context), db_container) return container @base.remotable_classmethod def list(cls, context, limit=None, marker=None, sort_key=None, sort_dir=None, filters=None): """Return a list of Container objects. :param context: Security context. :param limit: maximum number of resources to return in a single result. :param marker: pagination marker for large data sets. :param sort_key: column to sort results by. :param sort_dir: direction to sort. "asc" or "desc". :param filters: filters when list containers, the filter name could be 'name', 'image', 'project_id', 'user_id', 'memory'. For example, filters={'image': 'nginx'} :returns: a list of :class:`Container` object. """ LOG.debug('get_all xxxxxx LIST Container XXXXXXXXXX.') db_containers = dbapi.list_containers( context, limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir, filters=filters) return Container._from_db_object_list(db_containers, cls, context) @base.remotable_classmethod def list_by_host(cls, context, host): """Return a list of Container objects by host. :param context: Security context. :param host: A compute host. :returns: a list of :class:`Container` object. """ db_containers = dbapi.list_containers(context, filters={'host': host}) return Container._from_db_object_list(db_containers, cls, context) @base.remotable def create(self, context): """Create a Container record in the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ values = self.obj_get_changes() db_container = dbapi.create_container(context, values) self._from_db_object(self, db_container) @base.remotable def destroy(self, context=None): """Delete the Container from the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ dbapi.destroy_container(context, self.uuid) self.obj_reset_changes() @base.remotable def save(self, context=None): """Save updates to this Container. Updates will be made column by column based on the result of self.what_changed(). :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ LOG.debug('Save Container xxxxxx uuid =%s.', self.uuid) updates = self.obj_get_changes() dbapi.update_container(context, self.uuid, updates) self.obj_reset_changes() @base.remotable def refresh(self, context=None): """Loads updates for this Container. Loads a container with the same uuid from the database and checks for updated attributes. Updates are applied from the loaded container column by column, if there are any updates. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Container(context) """ current = self.__class__.get_by_uuid(self._context, uuid=self.uuid) for field in self.fields: if self.obj_attr_is_set(field) and \ getattr(self, field) != getattr(current, field): setattr(self, field, getattr(current, field)) def get_sandbox_id(self): if self.meta: return self.meta.get('sandbox_id', None) else: return None def set_sandbox_id(self, sandbox_id): if self.meta is None: self.meta = {'sandbox_id': sandbox_id} else: self.meta['sandbox_id'] = sandbox_id
class Payment(base.ZunPersistentObject, base.ZunObject): # Version 1.0: Initial version VERSION = '1.0' fields = { 'id': fields.IntegerField(), 'statement_id': fields.StringField(nullable=True), 'amount': fields.FloatField(nullable=True), 'payment_date': fields.DateTimeField(nullable=True), 'payment_method_id': fields.StringField(nullable=True), 'status': fields.IntegerField(nullable=True), 'uuid': fields.UUIDField(nullable=True), 'displayname': fields.StringField(nullable=True), 'displayname2': fields.StringField(nullable=True) } @staticmethod def _from_db_object(payment, db_payment): """Converts a database entity to a formal object.""" for field in payment.fields: setattr(payment, field, db_payment[field]) payment.obj_reset_changes() return payment @staticmethod def _from_db_object_list(db_objects, cls, context): """Converts a list of database entities to a list of formal objects.""" return [ Payment._from_db_object(cls(context), obj) for obj in db_objects ] @base.remotable_classmethod def get_by_uuid(cls, context, uuid): """Find a payment based on uuid and return a :class:`Payment` object. :param uuid: the uuid of a payment. :param context: Security context :returns: a :class:`Payment` object. """ db_payment = dbapi.get_payment_by_uuid(context, uuid) payment = Payment._from_db_object(cls(context), db_payment) return payment @base.remotable_classmethod def get_by_name(cls, context, name): """Find a payment based on name and return a Payment object. :param name: the logical name of a payment. :param context: Security context :returns: a :class:`Payment` object. """ db_payment = dbapi.get_payment_by_name(context, name) payment = Payment._from_db_object(cls(context), db_payment) return payment @base.remotable_classmethod def list(cls, context, limit=None, marker=None, sort_key=None, sort_dir=None, filters=None): """Return a list of Payment objects. :param context: Security context. :param limit: maximum number of resources to return in a single result. :param marker: pagination marker for large data sets. :param sort_key: column to sort results by. :param sort_dir: direction to sort. "asc" or "desc". :param filters: filters when list payments, the filter name could be 'name', 'image', 'project_id', 'user_id', 'memory'. For example, filters={'image': 'nginx'} :returns: a list of :class:`Payment` object. """ db_payments = dbapi.list_payments(context, limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir, filters=filters) LOG.debug('get_all Payment LISt Payment xxxxxx db_payments =%s.', db_payments) return Payment._from_db_object_list(db_payments, cls, context) @base.remotable_classmethod def list_by_host(cls, context, host): """Return a list of Payment objects by host. :param context: Security context. :param host: A compute host. :returns: a list of :class:`Payment` object. """ db_payments = dbapi.list_payments(context, filters={'host': host}) return Payment._from_db_object_list(db_payments, cls, context) @base.remotable def create(self, context): """Create a Payment record in the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Payment(context) """ values = self.obj_get_changes() db_payment = dbapi.create_payment(context, values) self._from_db_object(self, db_payment) @base.remotable def destroy(self, context=None): """Delete the Payment from the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Payment(context) """ dbapi.destroy_payment(context, self.uuid) self.obj_reset_changes() @base.remotable def save(self, context=None): """Save updates to this Payment. Updates will be made column by column based on the result of self.what_changed(). :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Payment(context) """ updates = self.obj_get_changes() LOG.debug('Save Payment xxxxxx uuid =%s, updates=%s', self.uuid, updates) dbapi.update_payment(context, self.uuid, updates) self.obj_reset_changes() @base.remotable def refresh(self, context=None): """Loads updates for this Payment. Loads a payment with the same uuid from the database and checks for updated attributes. Updates are applied from the loaded payment column by column, if there are any updates. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Payment(context) """ current = self.__class__.get_by_uuid(self._context, uuid=self.uuid) for field in self.fields: if self.obj_attr_is_set(field) and \ getattr(self, field) != getattr(current, field): setattr(self, field, getattr(current, field)) def get_sandbox_id(self): if self.meta: return self.meta.get('sandbox_id', None) else: return None def set_sandbox_id(self, sandbox_id): if self.meta is None: self.meta = {'sandbox_id': sandbox_id} else: self.meta['sandbox_id'] = sandbox_id
class Action(senlin_base.SenlinObject, base.VersionedObjectDictCompat): """Senlin action object.""" fields = { 'id': fields.UUIDField(), 'created_at': fields.DateTimeField(), 'updated_at': fields.DateTimeField(nullable=True), 'name': fields.StringField(), 'context': fields.DictOfStringsField(), 'target': fields.UUIDField(), 'action': fields.StringField(), 'cause': fields.StringField(), 'owner': fields.UUIDField(nullable=True), 'interval': fields.IntegerField(nullable=True), 'start_time': fields.FloatField(nullable=True), 'end_time': fields.FloatField(nullable=True), 'timeout': fields.IntegerField(nullable=True), 'status': fields.StringField(), 'status_reason': fields.StringField(), 'control': fields.StringField(nullable=True), 'inputs': fields.DictOfStringsField(nullable=True), 'outputs': fields.DictOfStringsField(nullable=True), 'data': fields.DictOfStringsField(nullable=True), 'user': fields.StringField(), 'project': fields.StringField(), 'domain': fields.StringField(), } @staticmethod def _from_db_object(context, action, db_obj): if db_obj is None: return None for field in action.fields: action[field] = db_obj[field] action._context = context action.obj_reset_changes() return action @classmethod def create(cls, context, values): obj = db_api.action_create(context, values) return cls._from_db_object(context, cls(context), obj) @classmethod def get(cls, context, action_id, **kwargs): obj = db_api.action_get(context, action_id, **kwargs) return cls._from_db_object(context, cls(), obj) @classmethod def get_by_name(cls, context, name, **kwargs): obj = db_api.action_get_by_name(context, name, **kwargs) return cls._from_db_object(context, cls(), obj) @classmethod def get_by_short_id(cls, context, short_id, **kwargs): obj = db_api.action_get_by_short_id(context, short_id, **kwargs) return cls._from_db_object(context, cls(), obj) @classmethod def get_all(cls, context, **kwargs): objs = db_api.action_get_all(context, **kwargs) return [cls._from_db_object(context, cls(), obj) for obj in objs] @classmethod def get_all_by_owner(cls, context, owner): objs = db_api.action_get_all_by_owner(context, owner) return [cls._from_db_object(context, cls(), obj) for obj in objs] @classmethod def check_status(cls, context, action_id, timestamp): return db_api.action_check_status(context, action_id, timestamp) @classmethod def mark_succeeded(cls, context, action_id, timestamp): return db_api.action_mark_succeeded(context, action_id, timestamp) @classmethod def mark_failed(cls, context, action_id, timestamp, reason=None): return db_api.action_mark_failed(context, action_id, timestamp, reason) @classmethod def mark_cancelled(cls, context, action_id, timestamp): return db_api.action_mark_cancelled(context, action_id, timestamp) @classmethod def acquire(cls, context, action_id, owner, timestamp): return db_api.action_acquire(context, action_id, owner, timestamp) @classmethod def acquire_1st_ready(cls, context, owner, timestamp): return db_api.action_acquire_1st_ready(context, owner, timestamp) @classmethod def abandon(cls, context, action_id): return db_api.action_abandon(context, action_id) @classmethod def signal(cls, context, action_id, value): return db_api.action_signal(context, action_id, value) @classmethod def signal_query(cls, context, action_id): return db_api.action_signal_query(context, action_id) @classmethod def lock_check(cls, context, action_id, owner=None): return db_api.action_lock_check(context, action_id, owner) @classmethod def update(cls, context, action_id, values): return db_api.action_update(context, action_id, values) @classmethod def delete(cls, context, action_id): db_api.action_delete(context, action_id)
class Capsule(base.ZunPersistentObject, base.ZunObject): # Version 1.0: Initial version # Version 1.1: Add host to capsule # Version 1.2: Change the properties of meta_labels # Version 1.3: Add 'Deleting' to ContainerStatus VERSION = '1.3' fields = { 'capsule_version': fields.StringField(nullable=True), 'kind': fields.StringField(nullable=True), 'restart_policy': fields.DictOfStringsField(nullable=True), 'host_selector': fields.StringField(nullable=True), # uuid is the infra-container id 'id': fields.IntegerField(), 'uuid': fields.UUIDField(nullable=True), 'project_id': fields.StringField(nullable=True), 'user_id': fields.StringField(nullable=True), 'status': z_fields.ContainerStatusField(nullable=True), 'status_reason': fields.StringField(nullable=True), 'cpu': fields.FloatField(nullable=True), 'memory': fields.StringField(nullable=True), # conclude the readable message # 'key': 'value'--> 'time':'message' # wait until zun notify is finished # 'message': fields.DictOfStringsField(nullable=True), 'spec': z_fields.JsonField(nullable=True), 'meta_name': fields.StringField(nullable=True), 'meta_labels': fields.DictOfStringsField(nullable=True), 'containers': fields.ListOfObjectsField('Container', nullable=True), 'containers_uuids': fields.ListOfStringsField(nullable=True), 'host': fields.StringField(nullable=True), # add volumes after Cinder integration is ready # 'volumes': fields.ListOfObjectsField(nullable=True), } @staticmethod def _from_db_object(capsule, db_capsule): """Converts a database entity to a formal object.""" for field in capsule.fields: if field != 'containers': setattr(capsule, field, db_capsule[field]) capsule.obj_reset_changes() return capsule @staticmethod def _from_db_object_list(db_objects, cls, context): """Converts a list of database entities to a list of formal objects.""" return [Capsule._from_db_object(cls(context), obj) for obj in db_objects] @base.remotable_classmethod def get_by_uuid(cls, context, uuid): """Find a capsule based on uuid and return a :class:`Capsule` object. :param uuid: the uuid of a capsule. :param context: Security context :returns: a :class:`Capsule` object. """ db_capsule = dbapi.get_capsule_by_uuid(context, uuid) capsule = Capsule._from_db_object(cls(context), db_capsule) return capsule @base.remotable_classmethod def get_by_name(cls, context, name): """Find a capsule based on name and return a :class:`Capsule` object. :param name: the meta_name of a capsule. :param context: Security context :returns: a :class:`Capsule` object. """ db_capsule = dbapi.get_capsule_by_meta_name(context, name) capsule = Capsule._from_db_object(cls(context), db_capsule) return capsule @base.remotable_classmethod def list(cls, context, limit=None, marker=None, sort_key=None, sort_dir=None, filters=None): """Return a list of Capsule objects. :param context: Security context. :param limit: maximum number of resources to return in a single result. :param marker: pagination marker for large data sets. :param sort_key: column to sort results by. :param sort_dir: direction to sort. "asc" or "desc". :param filters: filters when list containers, the filter name could be 'name', 'image', 'project_id', 'user_id', 'memory'. For example, filters={'image': 'nginx'} :returns: a list of :class:`Capsule` object. """ db_capsules = dbapi.list_capsules( context, limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir, filters=filters) return Capsule._from_db_object_list(db_capsules, cls, context) @base.remotable def create(self, context): """Create a Container record in the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Capsule(context) """ values = self.obj_get_changes() db_capsule = dbapi.create_capsule(context, values) self._from_db_object(self, db_capsule) @base.remotable def destroy(self, context=None): """Delete the Container from the DB. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Capsule(context) """ dbapi.destroy_capsule(context, self.uuid) self.obj_reset_changes() @base.remotable def save(self, context=None): """Save updates to this Capsule. Updates will be made column by column based on the result of self.what_changed(). :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Capsule(context) """ updates = self.obj_get_changes() dbapi.update_capsule(context, self.uuid, updates) self.obj_reset_changes()