Decouple the config dependence on glance domain
Move the configurations which core domain objects depend on to common/config.py, and add a argument "task_time_to_live" of Task's __init__ method and TaskFactory's new_task method for delivering the CONF.task.task_time_to_live value. For convenience,the argument "task_time_to_live" is set a default value 48. Change-Id: Iffda1ecd25470824c812d66a27fa64ebbaabcf07 Closes-Bug: #1250633
This commit is contained in:
parent
1d35a88b85
commit
f035efd043
@ -57,8 +57,9 @@ SUPPORTED_FILTERS = glance.api.v1.SUPPORTED_FILTERS
|
|||||||
ACTIVE_IMMUTABLE = glance.api.v1.ACTIVE_IMMUTABLE
|
ACTIVE_IMMUTABLE = glance.api.v1.ACTIVE_IMMUTABLE
|
||||||
|
|
||||||
CONF = cfg.CONF
|
CONF = cfg.CONF
|
||||||
CONF.import_opt('disk_formats', 'glance.domain')
|
CONF.import_opt('disk_formats', 'glance.common.config', group='image_format')
|
||||||
CONF.import_opt('container_formats', 'glance.domain')
|
CONF.import_opt('container_formats', 'glance.common.config',
|
||||||
|
group='image_format')
|
||||||
CONF.import_opt('image_property_quota', 'glance.common.config')
|
CONF.import_opt('image_property_quota', 'glance.common.config')
|
||||||
|
|
||||||
|
|
||||||
@ -69,12 +70,12 @@ def validate_image_meta(req, values):
|
|||||||
container_format = values.get('container_format')
|
container_format = values.get('container_format')
|
||||||
|
|
||||||
if 'disk_format' in values:
|
if 'disk_format' in values:
|
||||||
if disk_format not in CONF.disk_formats:
|
if disk_format not in CONF.image_format.disk_formats:
|
||||||
msg = "Invalid disk format '%s' for image." % disk_format
|
msg = "Invalid disk format '%s' for image." % disk_format
|
||||||
raise HTTPBadRequest(explanation=msg, request=req)
|
raise HTTPBadRequest(explanation=msg, request=req)
|
||||||
|
|
||||||
if 'container_format' in values:
|
if 'container_format' in values:
|
||||||
if container_format not in CONF.container_formats:
|
if container_format not in CONF.image_format.container_formats:
|
||||||
msg = "Invalid container format '%s' for image." % container_format
|
msg = "Invalid container format '%s' for image." % container_format
|
||||||
raise HTTPBadRequest(explanation=msg, request=req)
|
raise HTTPBadRequest(explanation=msg, request=req)
|
||||||
|
|
||||||
|
@ -35,8 +35,9 @@ import glance.store
|
|||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF = cfg.CONF
|
CONF = cfg.CONF
|
||||||
CONF.import_opt('disk_formats', 'glance.domain')
|
CONF.import_opt('disk_formats', 'glance.common.config', group='image_format')
|
||||||
CONF.import_opt('container_formats', 'glance.domain')
|
CONF.import_opt('container_formats', 'glance.common.config',
|
||||||
|
group='image_format')
|
||||||
|
|
||||||
|
|
||||||
class ImagesController(object):
|
class ImagesController(object):
|
||||||
@ -677,13 +678,13 @@ def _get_base_properties():
|
|||||||
'type': 'string',
|
'type': 'string',
|
||||||
'description': _('Format of the container'),
|
'description': _('Format of the container'),
|
||||||
'type': 'string',
|
'type': 'string',
|
||||||
'enum': CONF.container_formats,
|
'enum': CONF.image_format.container_formats,
|
||||||
},
|
},
|
||||||
'disk_format': {
|
'disk_format': {
|
||||||
'type': 'string',
|
'type': 'string',
|
||||||
'description': _('Format of the disk'),
|
'description': _('Format of the disk'),
|
||||||
'type': 'string',
|
'type': 'string',
|
||||||
'enum': CONF.disk_formats,
|
'enum': CONF.image_format.disk_formats,
|
||||||
},
|
},
|
||||||
'created_at': {
|
'created_at': {
|
||||||
'type': 'string',
|
'type': 'string',
|
||||||
|
@ -35,6 +35,7 @@ import glance.schema
|
|||||||
import glance.store
|
import glance.store
|
||||||
|
|
||||||
CONF = cfg.CONF
|
CONF = cfg.CONF
|
||||||
|
CONF.import_opt('task_time_to_live', 'glance.common.config', group='task')
|
||||||
|
|
||||||
|
|
||||||
class TasksController(object):
|
class TasksController(object):
|
||||||
@ -52,10 +53,12 @@ class TasksController(object):
|
|||||||
def create(self, req, task):
|
def create(self, req, task):
|
||||||
task_factory = self.gateway.get_task_factory(req.context)
|
task_factory = self.gateway.get_task_factory(req.context)
|
||||||
task_repo = self.gateway.get_task_repo(req.context)
|
task_repo = self.gateway.get_task_repo(req.context)
|
||||||
|
live_time = CONF.task.task_time_to_live
|
||||||
try:
|
try:
|
||||||
new_task = task_factory.new_task(task_type=task['type'],
|
new_task = task_factory.new_task(task_type=task['type'],
|
||||||
task_input=task['input'],
|
task_input=task['input'],
|
||||||
owner=req.context.owner)
|
owner=req.context.owner,
|
||||||
|
task_time_to_live=live_time)
|
||||||
task_repo.add(new_task)
|
task_repo.add(new_task)
|
||||||
except exception.Forbidden as e:
|
except exception.Forbidden as e:
|
||||||
raise webob.exc.HTTPForbidden(explanation=unicode(e))
|
raise webob.exc.HTTPForbidden(explanation=unicode(e))
|
||||||
|
@ -40,6 +40,29 @@ paste_deploy_opts = [
|
|||||||
cfg.StrOpt('config_file',
|
cfg.StrOpt('config_file',
|
||||||
help=_('Name of the paste configuration file.')),
|
help=_('Name of the paste configuration file.')),
|
||||||
]
|
]
|
||||||
|
image_format_opts = [
|
||||||
|
cfg.ListOpt('container_formats',
|
||||||
|
default=['ami', 'ari', 'aki', 'bare', 'ovf'],
|
||||||
|
help=_("Supported values for the 'container_format' "
|
||||||
|
"image attribute"),
|
||||||
|
deprecated_opts=[cfg.DeprecatedOpt('container_formats',
|
||||||
|
group='DEFAULT')]),
|
||||||
|
cfg.ListOpt('disk_formats',
|
||||||
|
default=['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', 'qcow2',
|
||||||
|
'vdi', 'iso'],
|
||||||
|
help=_("Supported values for the 'disk_format' "
|
||||||
|
"image attribute"),
|
||||||
|
deprecated_opts=[cfg.DeprecatedOpt('disk_formats',
|
||||||
|
group='DEFAULT')]),
|
||||||
|
]
|
||||||
|
task_opts = [
|
||||||
|
cfg.IntOpt('task_time_to_live',
|
||||||
|
default=48,
|
||||||
|
help=_("Time in hours for which a task lives after, either "
|
||||||
|
"succeeding or failing"),
|
||||||
|
deprecated_opts=[cfg.DeprecatedOpt('task_time_to_live',
|
||||||
|
group='DEFAULT')]),
|
||||||
|
]
|
||||||
common_opts = [
|
common_opts = [
|
||||||
cfg.BoolOpt('allow_additional_image_properties', default=True,
|
cfg.BoolOpt('allow_additional_image_properties', default=True,
|
||||||
help=_('Whether to allow users to specify image properties '
|
help=_('Whether to allow users to specify image properties '
|
||||||
@ -97,6 +120,8 @@ common_opts = [
|
|||||||
|
|
||||||
CONF = cfg.CONF
|
CONF = cfg.CONF
|
||||||
CONF.register_opts(paste_deploy_opts, group='paste_deploy')
|
CONF.register_opts(paste_deploy_opts, group='paste_deploy')
|
||||||
|
CONF.register_opts(image_format_opts, group='image_format')
|
||||||
|
CONF.register_opts(task_opts, group='task')
|
||||||
CONF.register_opts(common_opts)
|
CONF.register_opts(common_opts)
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,8 +18,6 @@ import collections
|
|||||||
import datetime
|
import datetime
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from oslo.config import cfg
|
|
||||||
|
|
||||||
from glance.common import exception
|
from glance.common import exception
|
||||||
import glance.openstack.common.log as logging
|
import glance.openstack.common.log as logging
|
||||||
from glance.openstack.common import timeutils
|
from glance.openstack.common import timeutils
|
||||||
@ -28,27 +26,6 @@ from glance.openstack.common import timeutils
|
|||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
image_format_opts = [
|
|
||||||
cfg.ListOpt('container_formats',
|
|
||||||
default=['ami', 'ari', 'aki', 'bare', 'ovf'],
|
|
||||||
help=_("Supported values for the 'container_format' "
|
|
||||||
"image attribute")),
|
|
||||||
cfg.ListOpt('disk_formats',
|
|
||||||
default=['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', 'qcow2',
|
|
||||||
'vdi', 'iso'],
|
|
||||||
help=_("Supported values for the 'disk_format' "
|
|
||||||
"image attribute")),
|
|
||||||
cfg.IntOpt('task_time_to_live',
|
|
||||||
default=48,
|
|
||||||
help=_("Time in hours for which a task lives after, either "
|
|
||||||
"succeeding or failing")),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
CONF = cfg.CONF
|
|
||||||
CONF.register_opts(image_format_opts)
|
|
||||||
|
|
||||||
|
|
||||||
class ImageFactory(object):
|
class ImageFactory(object):
|
||||||
_readonly_properties = ['created_at', 'updated_at', 'status', 'checksum',
|
_readonly_properties = ['created_at', 'updated_at', 'status', 'checksum',
|
||||||
'size']
|
'size']
|
||||||
@ -288,7 +265,7 @@ class Task(object):
|
|||||||
_supported_task_status = ('pending', 'processing', 'success', 'failure')
|
_supported_task_status = ('pending', 'processing', 'success', 'failure')
|
||||||
|
|
||||||
def __init__(self, task_id, type, status, input, result, owner, message,
|
def __init__(self, task_id, type, status, input, result, owner, message,
|
||||||
expires_at, created_at, updated_at):
|
expires_at, created_at, updated_at, task_time_to_live=48):
|
||||||
|
|
||||||
if type not in self._supported_task_type:
|
if type not in self._supported_task_type:
|
||||||
raise exception.InvalidTaskType(type)
|
raise exception.InvalidTaskType(type)
|
||||||
@ -306,7 +283,7 @@ class Task(object):
|
|||||||
self.expires_at = expires_at
|
self.expires_at = expires_at
|
||||||
# NOTE(nikhil): We use '_time_to_live' to determine how long a
|
# NOTE(nikhil): We use '_time_to_live' to determine how long a
|
||||||
# task should live from the time it succeeds or fails.
|
# task should live from the time it succeeds or fails.
|
||||||
self._time_to_live = datetime.timedelta(hours=CONF.task_time_to_live)
|
self._time_to_live = datetime.timedelta(hours=task_time_to_live)
|
||||||
self.created_at = created_at
|
self.created_at = created_at
|
||||||
self.updated_at = updated_at
|
self.updated_at = updated_at
|
||||||
|
|
||||||
@ -366,7 +343,7 @@ class Task(object):
|
|||||||
|
|
||||||
class TaskFactory(object):
|
class TaskFactory(object):
|
||||||
|
|
||||||
def new_task(self, task_type, task_input, owner):
|
def new_task(self, task_type, task_input, owner, task_time_to_live=48):
|
||||||
task_id = str(uuid.uuid4())
|
task_id = str(uuid.uuid4())
|
||||||
status = 'pending'
|
status = 'pending'
|
||||||
result = None
|
result = None
|
||||||
@ -386,5 +363,6 @@ class TaskFactory(object):
|
|||||||
message,
|
message,
|
||||||
expires_at,
|
expires_at,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at
|
updated_at,
|
||||||
|
task_time_to_live
|
||||||
)
|
)
|
||||||
|
@ -328,8 +328,12 @@ class TestTask(test_utils.BaseTestCase):
|
|||||||
task_input = ('{"import_from": "file:///home/a.img",'
|
task_input = ('{"import_from": "file:///home/a.img",'
|
||||||
' "import_from_format": "qcow2"}')
|
' "import_from_format": "qcow2"}')
|
||||||
owner = TENANT1
|
owner = TENANT1
|
||||||
|
task_ttl = CONF.task.task_time_to_live
|
||||||
self.gateway = unittest_utils.FakeGateway()
|
self.gateway = unittest_utils.FakeGateway()
|
||||||
self.task = self.task_factory.new_task(task_type, task_input, owner)
|
self.task = self.task_factory.new_task(task_type,
|
||||||
|
task_input,
|
||||||
|
owner,
|
||||||
|
task_time_to_live=task_ttl)
|
||||||
|
|
||||||
def test_task_invalid_status(self):
|
def test_task_invalid_status(self):
|
||||||
task_id = str(uuid.uuid4())
|
task_id = str(uuid.uuid4())
|
||||||
@ -411,7 +415,7 @@ class TestTask(test_utils.BaseTestCase):
|
|||||||
self.task.succeed('{"location": "file://home"}')
|
self.task.succeed('{"location": "file://home"}')
|
||||||
self.assertEqual(self.task.status, 'success')
|
self.assertEqual(self.task.status, 'success')
|
||||||
expected = (timeutils.utcnow() +
|
expected = (timeutils.utcnow() +
|
||||||
datetime.timedelta(hours=CONF.task_time_to_live))
|
datetime.timedelta(hours=CONF.task.task_time_to_live))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.task.expires_at,
|
self.task.expires_at,
|
||||||
expected
|
expected
|
||||||
@ -424,7 +428,7 @@ class TestTask(test_utils.BaseTestCase):
|
|||||||
self.task.fail('{"message": "connection failed"}')
|
self.task.fail('{"message": "connection failed"}')
|
||||||
self.assertEqual(self.task.status, 'failure')
|
self.assertEqual(self.task.status, 'failure')
|
||||||
expected = (timeutils.utcnow() +
|
expected = (timeutils.utcnow() +
|
||||||
datetime.timedelta(hours=CONF.task_time_to_live))
|
datetime.timedelta(hours=CONF.task.task_time_to_live))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.task.expires_at,
|
self.task.expires_at,
|
||||||
expected
|
expected
|
||||||
|
@ -227,7 +227,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
|
|||||||
self.assertTrue('Invalid disk format' in res.body, res.body)
|
self.assertTrue('Invalid disk format' in res.body, res.body)
|
||||||
|
|
||||||
def test_configured_disk_format_good(self):
|
def test_configured_disk_format_good(self):
|
||||||
self.config(disk_formats=['foo'])
|
self.config(disk_formats=['foo'], group="image_format")
|
||||||
fixture_headers = {
|
fixture_headers = {
|
||||||
'x-image-meta-store': 'bad',
|
'x-image-meta-store': 'bad',
|
||||||
'x-image-meta-name': 'bogus',
|
'x-image-meta-name': 'bogus',
|
||||||
@ -245,7 +245,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
|
|||||||
self.assertEqual(res.status_int, 201)
|
self.assertEqual(res.status_int, 201)
|
||||||
|
|
||||||
def test_configured_disk_format_bad(self):
|
def test_configured_disk_format_bad(self):
|
||||||
self.config(disk_formats=['foo'])
|
self.config(disk_formats=['foo'], group="image_format")
|
||||||
fixture_headers = {
|
fixture_headers = {
|
||||||
'x-image-meta-store': 'bad',
|
'x-image-meta-store': 'bad',
|
||||||
'x-image-meta-name': 'bogus',
|
'x-image-meta-name': 'bogus',
|
||||||
@ -264,7 +264,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
|
|||||||
self.assertTrue('Invalid disk format' in res.body, res.body)
|
self.assertTrue('Invalid disk format' in res.body, res.body)
|
||||||
|
|
||||||
def test_configured_container_format_good(self):
|
def test_configured_container_format_good(self):
|
||||||
self.config(container_formats=['foo'])
|
self.config(container_formats=['foo'], group="image_format")
|
||||||
fixture_headers = {
|
fixture_headers = {
|
||||||
'x-image-meta-store': 'bad',
|
'x-image-meta-store': 'bad',
|
||||||
'x-image-meta-name': 'bogus',
|
'x-image-meta-name': 'bogus',
|
||||||
@ -282,7 +282,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
|
|||||||
self.assertEqual(res.status_int, 201)
|
self.assertEqual(res.status_int, 201)
|
||||||
|
|
||||||
def test_configured_container_format_bad(self):
|
def test_configured_container_format_bad(self):
|
||||||
self.config(container_formats=['foo'])
|
self.config(container_formats=['foo'], group="image_format")
|
||||||
fixture_headers = {
|
fixture_headers = {
|
||||||
'x-image-meta-store': 'bad',
|
'x-image-meta-store': 'bad',
|
||||||
'x-image-meta-name': 'bogus',
|
'x-image-meta-name': 'bogus',
|
||||||
|
@ -3104,7 +3104,7 @@ class TestImageSchemaFormatConfiguration(test_utils.BaseTestCase):
|
|||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
def test_custom_disk_formats(self):
|
def test_custom_disk_formats(self):
|
||||||
self.config(disk_formats=['gabe'])
|
self.config(disk_formats=['gabe'], group="image_format")
|
||||||
schema = glance.api.v2.images.get_schema()
|
schema = glance.api.v2.images.get_schema()
|
||||||
expected = ['gabe']
|
expected = ['gabe']
|
||||||
actual = schema.properties['disk_format']['enum']
|
actual = schema.properties['disk_format']['enum']
|
||||||
@ -3117,7 +3117,7 @@ class TestImageSchemaFormatConfiguration(test_utils.BaseTestCase):
|
|||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
def test_custom_container_formats(self):
|
def test_custom_container_formats(self):
|
||||||
self.config(container_formats=['mark'])
|
self.config(container_formats=['mark'], group="image_format")
|
||||||
schema = glance.api.v2.images.get_schema()
|
schema = glance.api.v2.images.get_schema()
|
||||||
expected = ['mark']
|
expected = ['mark']
|
||||||
actual = schema.properties['container_format']['enum']
|
actual = schema.properties['container_format']['enum']
|
||||||
|
Loading…
x
Reference in New Issue
Block a user