Merge "convert windows line endings to unix format"

This commit is contained in:
Zuul 2018-06-07 22:12:40 +00:00 committed by Gerrit Code Review
commit 5f89e04018
2 changed files with 229 additions and 229 deletions

View File

@ -1,101 +1,101 @@
# Copyright 2018 NTT DATA, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
from taskflow.patterns import linear_flow as lf
from taskflow import task
from glance.i18n import _
CONF = cfg.CONF
inject_metadata_opts = [
cfg.ListOpt('ignore_user_roles',
default='admin',
help=_("""
Specify name of user roles to be ignored for injecting metadata
properties in the image.
Possible values:
* List containing user roles. For example: [admin,member]
""")),
cfg.DictOpt('inject',
default={},
help=_("""
Dictionary contains metadata properties to be injected in image.
Possible values:
* Dictionary containing key/value pairs. Key characters
length should be <= 255. For example: k1:v1,k2:v2
""")),
]
CONF.register_opts(inject_metadata_opts, group='inject_metadata_properties')
class _InjectMetadataProperties(task.Task):
def __init__(self, context, task_id, task_type, image_repo, image_id):
self.context = context
self.task_id = task_id
self.task_type = task_type
self.image_repo = image_repo
self.image_id = image_id
super(_InjectMetadataProperties, self).__init__(
name='%s-InjectMetadataProperties-%s' % (task_type, task_id))
def execute(self):
"""Inject custom metadata properties to image
:param image_id: Glance Image ID
"""
user_roles = self.context.roles
ignore_user_roles = CONF.inject_metadata_properties.ignore_user_roles
if not [role for role in user_roles if role in ignore_user_roles]:
properties = CONF.inject_metadata_properties.inject
if properties:
image = self.image_repo.get(self.image_id)
image.extra_properties.update(properties)
self.image_repo.save(image)
def get_flow(**kwargs):
"""Return task flow for inject_image_metadata.
:param task_id: Task ID.
:param task_type: Type of the task.
:param image_repo: Image repository used.
:param image_id: Image_ID used.
:param context: Context used.
"""
task_id = kwargs.get('task_id')
task_type = kwargs.get('task_type')
image_repo = kwargs.get('image_repo')
image_id = kwargs.get('image_id')
context = kwargs.get('context')
return lf.Flow(task_type).add(
_InjectMetadataProperties(context, task_id, task_type, image_repo,
image_id),
)
# Copyright 2018 NTT DATA, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
from taskflow.patterns import linear_flow as lf
from taskflow import task
from glance.i18n import _
CONF = cfg.CONF
inject_metadata_opts = [
cfg.ListOpt('ignore_user_roles',
default='admin',
help=_("""
Specify name of user roles to be ignored for injecting metadata
properties in the image.
Possible values:
* List containing user roles. For example: [admin,member]
""")),
cfg.DictOpt('inject',
default={},
help=_("""
Dictionary contains metadata properties to be injected in image.
Possible values:
* Dictionary containing key/value pairs. Key characters
length should be <= 255. For example: k1:v1,k2:v2
""")),
]
CONF.register_opts(inject_metadata_opts, group='inject_metadata_properties')
class _InjectMetadataProperties(task.Task):
def __init__(self, context, task_id, task_type, image_repo, image_id):
self.context = context
self.task_id = task_id
self.task_type = task_type
self.image_repo = image_repo
self.image_id = image_id
super(_InjectMetadataProperties, self).__init__(
name='%s-InjectMetadataProperties-%s' % (task_type, task_id))
def execute(self):
"""Inject custom metadata properties to image
:param image_id: Glance Image ID
"""
user_roles = self.context.roles
ignore_user_roles = CONF.inject_metadata_properties.ignore_user_roles
if not [role for role in user_roles if role in ignore_user_roles]:
properties = CONF.inject_metadata_properties.inject
if properties:
image = self.image_repo.get(self.image_id)
image.extra_properties.update(properties)
self.image_repo.save(image)
def get_flow(**kwargs):
"""Return task flow for inject_image_metadata.
:param task_id: Task ID.
:param task_type: Type of the task.
:param image_repo: Image repository used.
:param image_id: Image_ID used.
:param context: Context used.
"""
task_id = kwargs.get('task_id')
task_type = kwargs.get('task_type')
image_repo = kwargs.get('image_repo')
image_id = kwargs.get('image_id')
context = kwargs.get('context')
return lf.Flow(task_type).add(
_InjectMetadataProperties(context, task_id, task_type, image_repo,
image_id),
)

View File

@ -1,128 +1,128 @@
# Copyright 2018 NTT DATA, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import os
import glance_store
from oslo_config import cfg
import glance.async.flows.plugins.inject_image_metadata as inject_metadata
from glance.common import utils
from glance import domain
from glance import gateway
from glance.tests.unit import utils as test_unit_utils
import glance.tests.utils as test_utils
CONF = cfg.CONF
UUID1 = 'c80a1a6c-bd1f-41c5-90ee-81afedb1d58d'
TENANT1 = '6838eb7b-6ded-434a-882c-b344c77fe8df'
class TestInjectImageMetadataTask(test_utils.BaseTestCase):
def setUp(self):
super(TestInjectImageMetadataTask, self).setUp()
glance_store.register_opts(CONF)
self.config(default_store='file',
stores=['file', 'http'],
filesystem_store_datadir=self.test_dir,
group="glance_store")
glance_store.create_stores(CONF)
self.work_dir = os.path.join(self.test_dir, 'work_dir')
utils.safe_mkdirs(self.work_dir)
self.config(work_dir=self.work_dir, group='task')
self.context = mock.MagicMock()
self.img_repo = mock.MagicMock()
self.task_repo = mock.MagicMock()
self.image_id = mock.MagicMock()
self.gateway = gateway.Gateway()
self.task_factory = domain.TaskFactory()
self.img_factory = self.gateway.get_image_factory(self.context)
self.image = self.img_factory.new_image(image_id=UUID1,
disk_format='qcow2',
container_format='bare')
task_input = {
"import_from": "http://cloud.foo/image.qcow2",
"import_from_format": "qcow2",
"image_properties": {'disk_format': 'qcow2',
'container_format': 'bare'}
}
task_ttl = CONF.task.task_time_to_live
self.task_type = 'import'
self.task = self.task_factory.new_task(self.task_type, TENANT1,
task_time_to_live=task_ttl,
task_input=task_input)
def test_inject_image_metadata_using_non_admin_user(self):
context = test_unit_utils.get_fake_context(roles='member')
inject_image_metadata = inject_metadata._InjectMetadataProperties(
context, self.task.task_id, self.task_type, self.img_repo,
self.image_id)
self.config(inject={"test": "abc"},
group='inject_metadata_properties')
with mock.patch.object(self.img_repo, 'get') as get_mock:
image = mock.MagicMock(image_id=self.image_id,
extra_properties={"test": "abc"})
get_mock.return_value = image
with mock.patch.object(self.img_repo, 'save') as save_mock:
inject_image_metadata.execute()
get_mock.assert_called_once_with(self.image_id)
save_mock.assert_called_once_with(image)
self.assertEqual({"test": "abc"}, image.extra_properties)
def test_inject_image_metadata_using_admin_user(self):
context = test_unit_utils.get_fake_context(roles='admin')
inject_image_metadata = inject_metadata._InjectMetadataProperties(
context, self.task.task_id, self.task_type, self.img_repo,
self.image_id)
self.config(inject={"test": "abc"},
group='inject_metadata_properties')
inject_image_metadata.execute()
with mock.patch.object(self.img_repo, 'get') as get_mock:
get_mock.assert_not_called()
with mock.patch.object(self.img_repo, 'save') as save_mock:
save_mock.assert_not_called()
def test_inject_image_metadata_empty(self):
context = test_unit_utils.get_fake_context(roles='member')
inject_image_metadata = inject_metadata._InjectMetadataProperties(
context, self.task.task_id, self.task_type, self.img_repo,
self.image_id)
self.config(inject={}, group='inject_metadata_properties')
inject_image_metadata.execute()
with mock.patch.object(self.img_repo, 'get') as get_mock:
get_mock.assert_not_called()
with mock.patch.object(self.img_repo, 'save') as save_mock:
save_mock.assert_not_called()
# Copyright 2018 NTT DATA, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import os
import glance_store
from oslo_config import cfg
import glance.async.flows.plugins.inject_image_metadata as inject_metadata
from glance.common import utils
from glance import domain
from glance import gateway
from glance.tests.unit import utils as test_unit_utils
import glance.tests.utils as test_utils
CONF = cfg.CONF
UUID1 = 'c80a1a6c-bd1f-41c5-90ee-81afedb1d58d'
TENANT1 = '6838eb7b-6ded-434a-882c-b344c77fe8df'
class TestInjectImageMetadataTask(test_utils.BaseTestCase):
def setUp(self):
super(TestInjectImageMetadataTask, self).setUp()
glance_store.register_opts(CONF)
self.config(default_store='file',
stores=['file', 'http'],
filesystem_store_datadir=self.test_dir,
group="glance_store")
glance_store.create_stores(CONF)
self.work_dir = os.path.join(self.test_dir, 'work_dir')
utils.safe_mkdirs(self.work_dir)
self.config(work_dir=self.work_dir, group='task')
self.context = mock.MagicMock()
self.img_repo = mock.MagicMock()
self.task_repo = mock.MagicMock()
self.image_id = mock.MagicMock()
self.gateway = gateway.Gateway()
self.task_factory = domain.TaskFactory()
self.img_factory = self.gateway.get_image_factory(self.context)
self.image = self.img_factory.new_image(image_id=UUID1,
disk_format='qcow2',
container_format='bare')
task_input = {
"import_from": "http://cloud.foo/image.qcow2",
"import_from_format": "qcow2",
"image_properties": {'disk_format': 'qcow2',
'container_format': 'bare'}
}
task_ttl = CONF.task.task_time_to_live
self.task_type = 'import'
self.task = self.task_factory.new_task(self.task_type, TENANT1,
task_time_to_live=task_ttl,
task_input=task_input)
def test_inject_image_metadata_using_non_admin_user(self):
context = test_unit_utils.get_fake_context(roles='member')
inject_image_metadata = inject_metadata._InjectMetadataProperties(
context, self.task.task_id, self.task_type, self.img_repo,
self.image_id)
self.config(inject={"test": "abc"},
group='inject_metadata_properties')
with mock.patch.object(self.img_repo, 'get') as get_mock:
image = mock.MagicMock(image_id=self.image_id,
extra_properties={"test": "abc"})
get_mock.return_value = image
with mock.patch.object(self.img_repo, 'save') as save_mock:
inject_image_metadata.execute()
get_mock.assert_called_once_with(self.image_id)
save_mock.assert_called_once_with(image)
self.assertEqual({"test": "abc"}, image.extra_properties)
def test_inject_image_metadata_using_admin_user(self):
context = test_unit_utils.get_fake_context(roles='admin')
inject_image_metadata = inject_metadata._InjectMetadataProperties(
context, self.task.task_id, self.task_type, self.img_repo,
self.image_id)
self.config(inject={"test": "abc"},
group='inject_metadata_properties')
inject_image_metadata.execute()
with mock.patch.object(self.img_repo, 'get') as get_mock:
get_mock.assert_not_called()
with mock.patch.object(self.img_repo, 'save') as save_mock:
save_mock.assert_not_called()
def test_inject_image_metadata_empty(self):
context = test_unit_utils.get_fake_context(roles='member')
inject_image_metadata = inject_metadata._InjectMetadataProperties(
context, self.task.task_id, self.task_type, self.img_repo,
self.image_id)
self.config(inject={}, group='inject_metadata_properties')
inject_image_metadata.execute()
with mock.patch.object(self.img_repo, 'get') as get_mock:
get_mock.assert_not_called()
with mock.patch.object(self.img_repo, 'save') as save_mock:
save_mock.assert_not_called()