Merge "Implement protectable RPC handlers"

This commit is contained in:
Jenkins 2016-03-23 15:09:49 +00:00 committed by Gerrit Code Review
commit b55f5287cb
3 changed files with 142 additions and 51 deletions

View File

@ -14,13 +14,18 @@
Protection Service
"""
import six
from oslo_config import cfg
from oslo_log import log as logging
import oslo_messaging as messaging
from oslo_utils import importutils
from smaug.i18n import _LI
from smaug.i18n import _LI, _LE
from smaug import exception
from smaug import manager
from smaug.resource import Resource
from smaug.services.protection import protectable_registry as p_reg
LOG = logging.getLogger(__name__)
@ -227,64 +232,76 @@ class ProtectionManager(manager.Manager):
return return_stub
def list_protectable_types(self, context):
# TODO(zengyingzhe)
LOG.info(_LI("Starting list protectable types."))
return_stub = [
"OS::Keystone::Project",
"OS::Nova::Server",
"OS::Glance::Image",
"OS::Cinder::Volume",
"OS::Neutron::Topology"
]
return return_stub
LOG.info(_LI("Start to list protectable types."))
return p_reg.ProtectableRegistry.list_resource_types()
def show_protectable_type(self, context, protectable_type):
# TODO(zengyingzhe)
LOG.info(_LI("Starting show protectable "
"type. tpye:%s"), protectable_type)
return_stub = {
"name": "OS::Nova::Server",
"dependent_types": [
"OS::Cinder::Volume",
"OS::Glance::Image"
]
LOG.info(_LI("Start to show protectable type %s"),
protectable_type)
plugin = p_reg.ProtectableRegistry.get_protectable_resource_plugin(
protectable_type)
if not plugin:
raise exception.ProtectableTypeNotFound(
protectable_type=protectable_type)
dependents = []
for t in p_reg.ProtectableRegistry.list_resource_types():
if t == protectable_type:
continue
p = p_reg.ProtectableRegistry.get_protectable_resource_plugin(t)
if p and protectable_type in p.get_parent_resource_types():
dependents.append(t)
return {
'name': plugin.get_resource_type(),
"dependent_types": dependents
}
return return_stub
def list_protectable_instances(self, context, protectable_type):
LOG.info(_LI("Start to list protectable instances of type: %s"),
protectable_type)
def list_protectable_instances(self, context, protectable_type,
marker=None, limit=None, sort_keys=None,
sort_dirs=None, filters=None):
# TODO(zengyingzhe)
LOG.info(_LI("Starting list protectable instances. "
"tpye:%s"), protectable_type)
registry = p_reg.ProtectableRegistry.create_instance(context)
return_stub = [
{
"id": "557d0cd2-fd8d-4279-91a5-24763ebc6cbc",
},
{
"id": "557d0cd2-fd8d-4279-91a5-24763ebc6cbc",
}
]
return return_stub
try:
resource_instances = registry.list_resources(protectable_type)
except exception.ListProtectableResourceFailed as err:
LOG.error(_LE("List resources of type %(type)s failed: %(err)s"),
{'type': protectable_type,
'err': six.text_type(err)})
raise
result = []
for resource in resource_instances:
result.append(dict(id=resource.id))
return result
def list_protectable_dependents(self, context,
protectable_id,
protectable_type):
# TODO(zengyingzhe)
LOG.info(_LI("Starting list protectable dependents."
"id:%s."), protectable_id)
LOG.info(_LI("Start to list dependents of resource "
"(type:%(type)s, id:%(id)s)"),
{'type': protectable_type,
'id': protectable_id})
return_stub = [
{
"id": "5fad94de-2926-486b-ae73-ff5d3477f80d",
"type": "OS::Cinder::Volume"
},
{
"id": "5fad94de-2926-486b-ae73-ff5d34775555",
"type": "OS::Cinder::Volume"
}
]
return return_stub
registry = p_reg.ProtectableRegistry.create_instance(context)
parent_resource = Resource(type=protectable_type, id=protectable_id)
try:
dependent_resources = registry.fetch_dependent_resources(
parent_resource)
except exception.ListProtectableResourceFailed as err:
LOG.error(_LE("List dependent resources of (%(res)s) "
"failed: %(err)s"),
{'res': parent_resource,
'err': six.text_type(err)})
raise
result = []
for resource in dependent_resources:
result.append(dict(type=resource.type, id=resource.id))
return result

View File

@ -61,3 +61,17 @@ class FakeCheckpointManager(object):
def update_protection_definition(self, checkpoint, **kwargs):
self.fake_protection_definition = 'fake_definition'
class FakeProtectablePlugin(object):
def get_resource_type(self):
pass
def get_parent_resource_types(self):
pass
def list_resources(self):
pass
def get_dependent_resources(self, parent_resource):
pass

View File

@ -10,8 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from smaug.resource import Resource
from smaug.services.protection import manager
from smaug.services.protection import protectable_registry
from smaug.tests import base
from smaug.tests.unit.protection import fakes
@ -21,3 +24,60 @@ class ProtectionServiceTest(base.TestCase):
super(ProtectionServiceTest, self).setUp()
self.pro_manager = manager.ProtectionManager()
self.protection_plan = fakes.fake_protection_plan()
@mock.patch.object(protectable_registry.ProtectableRegistry,
'list_resource_types')
def test_list_protectable_types(self, mocker):
excepted = ["OS::Nova::Server",
"OS::Cinder::Volume"]
mocker.return_value = excepted
result = self.pro_manager.list_protectable_types(None)
self.assertEqual(excepted, result)
def test_show_protectable_type(self):
server_plugin = fakes.FakeProtectablePlugin()
server_plugin.get_resource_type = mock.MagicMock(
return_value="OS::Nova::Server")
volume_plugin = fakes.FakeProtectablePlugin()
volume_plugin.get_parent_resource_types = mock.MagicMock(
return_value=["OS::Nova::Server"])
protectable_registry.ProtectableRegistry._plugin_map = {
"OS::Nova::Server": server_plugin,
"OS::Cinder::Volume": volume_plugin}
result = self.pro_manager.show_protectable_type(None,
"OS::Nova::Server")
self.assertEqual({
"name": "OS::Nova::Server",
"dependent_types": ["OS::Cinder::Volume"]},
result)
@mock.patch.object(protectable_registry.ProtectableRegistry,
'list_resources')
def test_list_protectable_instances(self, mocker):
mocker.return_value = [Resource(type='OS::Nova::Server',
id='123456'),
Resource(type='OS::Nova::Server',
id='654321')]
fake_cntx = mock.MagicMock()
result = self.pro_manager.list_protectable_instances(
fake_cntx, 'OS::Nova::Server')
self.assertEqual([{'id': '123456'},
{'id': '654321'}], result)
@mock.patch.object(protectable_registry.ProtectableRegistry,
'fetch_dependent_resources')
def test_list_protectable_dependents(self, mocker):
mocker.return_value = [Resource(type='OS::Cinder::Volume',
id='123456'),
Resource(type='OS::Cinder::Volume',
id='654321')]
fake_cntx = mock.MagicMock()
result = self.pro_manager.list_protectable_dependents(
fake_cntx, 'fake_id', 'OS::Nova::Server')
self.assertEqual([{'type': 'OS::Cinder::Volume', 'id': '123456'},
{'type': 'OS::Cinder::Volume', 'id': '654321'}],
result)