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)
def get_image_id(client, image_id, image_tags): """Get and check image ID.""" if image_id: try: client.images.get(image_id) except glance_exceptions.HTTPNotFound: raise exception.ImageNotFound(uuid=image_id) return image_id elif image_tags: if isinstance(image_tags, str): image_tags = image_tags.split(',') filters = {'tag': image_tags, 'status': 'active'} images = list( client.images.list(filters=filters, sort='created_at:desc', limit=1)) if not images: raise exception.ImageNotFoundByTags(tags=image_tags) image_id = images[0]['id'] return image_id
def edit(self, req, body, tenant_id, id): """Updates the attributes of a datastore version.""" context = req.environ[wsgi.CONTEXT_KEY] datastore_version = models.DatastoreVersion.load_by_uuid(id) LOG.info( "Tenant: '%(tenant)s' is updating the datastore " "version: '%(version)s' for datastore: '%(datastore)s'", { 'tenant': tenant_id, 'version': datastore_version.name, 'datastore': datastore_version.datastore_name }) manager = body.get('datastore_manager', datastore_version.manager) image_id = body.get('image', datastore_version.image_id) active = body.get('active', datastore_version.active) default = body.get('default', None) packages = body.get('packages', datastore_version.packages) if type(packages) is list: packages = ','.join(packages) client = clients.create_glance_client(context) try: client.images.get(image_id) except glance_exceptions.HTTPNotFound: raise exception.ImageNotFound(uuid=image_id) models.update_datastore_version(datastore_version.datastore_name, datastore_version.name, manager, image_id, packages, active) if default: models.update_datastore(datastore_version.datastore_name, datastore_version.name) elif (default is False and datastore_version.default is True): models.update_datastore(datastore_version.datastore_name, None) return wsgi.Result(None, 202)