Exemple #1
0
    def create(self, req, body, tenant_id):
        """Adds a new datastore version."""
        context = req.environ[wsgi.CONTEXT_KEY]
        datastore_name = body['version']['datastore_name']
        version_name = body['version']['name']
        manager = body['version']['datastore_manager']
        image_id = body['version'].get('image')
        image_tags = body['version'].get('image_tags')
        packages = body['version'].get('packages')
        if type(packages) is list:
            packages = ','.join(packages)
        active = body['version']['active']
        default = body['version'].get('default', False)
        # For backward compatibility, use name as default value for version if
        # not specified
        version_str = body['version'].get('version', version_name)

        LOG.info("Tenant: '%(tenant)s' is adding the datastore "
                 "version: '%(version)s' to datastore: '%(datastore)s'",
                 {'tenant': tenant_id, 'version': version_name,
                  'datastore': datastore_name})

        if not image_id and not image_tags:
            raise exception.BadRequest("Image must be specified.")

        client = clients.create_glance_client(context)
        common_glance.get_image_id(client, image_id, image_tags)

        try:
            datastore = models.Datastore.load(datastore_name)
        except exception.DatastoreNotFound:
            # Create the datastore if datastore_name does not exists.
            LOG.info("Creating datastore %s", datastore_name)
            datastore = models.DBDatastore()
            datastore.id = utils.generate_uuid()
            datastore.name = datastore_name
            datastore.save()

        try:
            models.DatastoreVersion.load(datastore, version_name,
                                         version=version_str)
            raise exception.DatastoreVersionAlreadyExists(
                name=version_name, version=version_str)
        except exception.DatastoreVersionNotFound:
            models.update_datastore_version(datastore.name, version_name,
                                            manager, image_id, image_tags,
                                            packages, active,
                                            version=version_str)

        if default:
            models.update_datastore(datastore.name, version_name)

        return wsgi.Result(None, 202)
Exemple #2
0
    def create(self, req, body, tenant_id):
        """Adds a new datastore version."""
        context = req.environ[wsgi.CONTEXT_KEY]
        datastore_name = body['version']['datastore_name']
        version_name = body['version']['name']
        manager = body['version']['datastore_manager']
        image_id = body['version']['image']
        packages = body['version']['packages']
        if type(packages) is list:
            packages = ','.join(packages)
        active = body['version']['active']
        default = body['version']['default']

        LOG.info(
            "Tenant: '%(tenant)s' is adding the datastore "
            "version: '%(version)s' to datastore: '%(datastore)s'", {
                'tenant': tenant_id,
                'version': version_name,
                'datastore': datastore_name
            })

        client = clients.create_glance_client(context)
        try:
            client.images.get(image_id)
        except glance_exceptions.HTTPNotFound:
            raise exception.ImageNotFound(uuid=image_id)

        try:
            datastore = models.Datastore.load(datastore_name)
        except exception.DatastoreNotFound:
            # Create the datastore if datastore_name does not exists.
            LOG.info("Creating datastore %s", datastore_name)
            datastore = models.DBDatastore()
            datastore.id = utils.generate_uuid()
            datastore.name = datastore_name
            datastore.save()

        try:
            models.DatastoreVersion.load(datastore, version_name)
            raise exception.DatastoreVersionAlreadyExists(name=version_name)
        except exception.DatastoreVersionNotFound:
            models.update_datastore_version(datastore.name, version_name,
                                            manager, image_id, packages,
                                            active)

        if default:
            models.update_datastore(datastore.name, version_name)

        return wsgi.Result(None, 202)