def create_container(self, context, values): # ensure defaults are present for new containers if not values.get('uuid'): values['uuid'] = uuidutils.generate_uuid() if values.get('name'): self._validate_unique_container_name(context, values['name']) container = models.Container() container.update(values) try: container.save() except db_exc.DBDuplicateEntry: raise exception.ContainerAlreadyExists(field='UUID', value=values['uuid']) return container
def _validate_unique_container_name(self, context, name): if not CONF.compute.unique_container_name_scope: return lowername = name.lower() base_query = model_query(models.Container).\ filter(func.lower(models.Container.name) == lowername) if CONF.compute.unique_container_name_scope == 'project': container_with_same_name = base_query.\ filter_by(project_id=context.project_id).count() elif CONF.compute.unique_container_name_scope == 'global': container_with_same_name = base_query.count() else: return if container_with_same_name > 0: raise exception.ContainerAlreadyExists(field='name', value=lowername)
def _validate_unique_container_name(self, context, name): if not CONF.compute.unique_container_name_scope: return lowername = name.lower() filters = {'name': name} if CONF.compute.unique_container_name_scope == 'project': filters['project_id'] = context.project_id elif CONF.compute.unique_container_name_scope == 'global': pass else: return try: containers = self.list_containers(context, filters=filters) except etcd.EtcdKeyNotFound: return except Exception as e: LOG.error('Error occurred while retrieving container: %s', six.text_type(e)) raise if len(containers) > 0: raise exception.ContainerAlreadyExists(field='name', value=lowername)