Resource plug-in for keystone service

Adds contrib resource plug-in for keystone service.

implments blueprint keystone-resource-service-endpoint

Change-Id: I002cc9d6a5c2156c015535c163f8a61a8441737e
This commit is contained in:
Kanagaraj Manickam 2015-04-29 12:14:58 +05:30
parent 0185ad16f4
commit b660268388
2 changed files with 392 additions and 0 deletions

View File

@ -0,0 +1,118 @@
#
# 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 heat.common.i18n import _
from heat.engine import properties
from heat.engine import resource
from heat.engine import support
class KeystoneService(resource.Resource):
"""Heat Template Resource for Keystone Service."""
support_status = support.SupportStatus(
version='2015.2',
message=_('Supported versions: keystone v3'))
PROPERTIES = (
NAME, DESCRIPTION, TYPE
) = (
'name', 'description', 'type'
)
properties_schema = {
NAME: properties.Schema(
properties.Schema.STRING,
_('Name of keystone service.'),
update_allowed=True
),
DESCRIPTION: properties.Schema(
properties.Schema.STRING,
_('Description of keystone service.'),
update_allowed=True
),
TYPE: properties.Schema(
properties.Schema.STRING,
_('Type of keystone Service.'),
update_allowed=True,
required=True
)
}
def _create_service(self,
name,
type,
description=None):
return self.keystone().client.services.create(
name=name,
description=description,
type=type)
def _delete_service(self, service_id):
return self.keystone().client.services.delete(service_id)
def _update_service(self,
service_id,
new_name=None,
new_description=None,
new_type=None):
return self.keystone().client.services.update(
service=service_id,
name=new_name,
description=new_description,
type=new_type)
def handle_create(self):
name = (self.properties.get(self.NAME) or
self.physical_resource_name())
description = self.properties.get(self.DESCRIPTION)
type = self.properties.get(self.TYPE)
service = self._create_service(
name=name,
description=description,
type=type
)
self.resource_id_set(service.id)
def handle_update(self,
json_snippet=None,
tmpl_diff=None,
prop_diff=None):
name = None
if self.NAME in prop_diff:
name = (prop_diff.get(self.NAME) or
self.physical_resource_name())
description = prop_diff.get(self.DESCRIPTION)
type = prop_diff.get(self.TYPE)
self._update_service(
service_id=self.resource_id,
new_name=name,
new_description=description,
new_type=type
)
def handle_delete(self):
if self.resource_id is not None:
try:
self._delete_service(service_id=self.resource_id)
except Exception as ex:
self.client_plugin('keystone').ignore_not_found(ex)
def resource_mapping():
return {
'OS::Keystone::Service': KeystoneService
}

View File

@ -0,0 +1,274 @@
#
# 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
from heat.engine import properties
from heat.engine import resource
from heat.engine import stack
from heat.engine import template
from heat.tests import common
from heat.tests import utils
from ..resources import service # noqa
keystone_service_template = {
'heat_template_version': '2015-04-30',
'resources': {
'test_service': {
'type': 'OS::Keystone::Service',
'properties': {
'name': 'test_service_1',
'description': 'Test service',
'type': 'orchestration'
}
}
}
}
RESOURCE_TYPE = 'OS::Keystone::Service'
class KeystoneServiceTest(common.HeatTestCase):
def setUp(self):
super(KeystoneServiceTest, self).setUp()
self.ctx = utils.dummy_context()
# For unit testing purpose. Register resource provider explicitly.
resource._register_class(RESOURCE_TYPE,
service.KeystoneService)
self.stack = stack.Stack(
self.ctx, 'test_stack_keystone',
template.Template(keystone_service_template)
)
self.test_service = self.stack['test_service']
# Mock client
self.keystoneclient = mock.MagicMock()
self.test_service.keystone = mock.MagicMock()
self.test_service.keystone.return_value = self.keystoneclient
self.services = self.keystoneclient.client.services
# Mock client plugin
keystone_client_plugin = mock.MagicMock()
self.test_service.client_plugin = mock.MagicMock()
self.test_service.client_plugin.return_value = keystone_client_plugin
def _get_mock_service(self):
value = mock.MagicMock()
value.id = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
return value
def test_service_handle_create(self):
mock_service = self._get_mock_service()
self.services.create.return_value = mock_service
# validate the properties
self.assertEqual(
'test_service_1',
self.test_service.properties.get(service.KeystoneService.NAME))
self.assertEqual(
'Test service',
self.test_service.properties.get(
service.KeystoneService.DESCRIPTION))
self.assertEqual(
'orchestration',
self.test_service.properties.get(service.KeystoneService.TYPE))
self.test_service.handle_create()
# validate service creation
self.services.create.assert_called_once_with(
name='test_service_1',
description='Test service',
type='orchestration')
# validate physical resource id
self.assertEqual(mock_service.id, self.test_service.resource_id)
def test_service_handle_create_default(self):
values = {
service.KeystoneService.NAME: None,
service.KeystoneService.DESCRIPTION: None,
service.KeystoneService.TYPE: 'orchestration'
}
def _side_effect(key):
return values[key]
mock_service = self._get_mock_service()
self.services.create.return_value = mock_service
self.test_service.properties = mock.MagicMock()
self.test_service.properties.get.side_effect = _side_effect
self.test_service.physical_resource_name = mock.MagicMock()
self.test_service.physical_resource_name.return_value = 'foo'
# validate the properties
self.assertIsNone(
self.test_service.properties.get(service.KeystoneService.NAME))
self.assertIsNone(
self.test_service.properties.get(
service.KeystoneService.DESCRIPTION))
self.assertEqual(
'orchestration',
self.test_service.properties.get(service.KeystoneService.TYPE))
self.test_service.handle_create()
# validate service creation with physical resource name
self.services.create.assert_called_once_with(
name='foo',
description=None,
type='orchestration')
def test_service_handle_update(self):
self.test_service.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
prop_diff = {service.KeystoneService.NAME: 'test_service_1_updated',
service.KeystoneService.DESCRIPTION:
'Test Service updated',
service.KeystoneService.TYPE: 'heat_updated'}
self.test_service.handle_update(json_snippet=None,
tmpl_diff=None,
prop_diff=prop_diff)
self.services.update.assert_called_once_with(
service=self.test_service.resource_id,
name=prop_diff[service.KeystoneService.NAME],
description=prop_diff[service.KeystoneService.DESCRIPTION],
type=prop_diff[service.KeystoneService.TYPE]
)
def test_service_handle_update_default(self):
self.test_service.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
self.test_service.physical_resource_name = mock.MagicMock()
self.test_service.physical_resource_name.return_value = 'foo'
# Name is reset to None, so default to physical resource name
prop_diff = {service.KeystoneService.NAME: None}
self.test_service.handle_update(json_snippet=None,
tmpl_diff=None,
prop_diff=prop_diff)
# validate default name to physical resource name
self.services.update.assert_called_once_with(
service=self.test_service.resource_id,
name='foo',
type=None,
description=None
)
def test_service_handle_delete(self):
self.test_service.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
self.services.delete.return_value = None
self.assertIsNone(self.test_service.handle_delete())
self.services.delete.assert_called_once_with(
self.test_service.resource_id
)
def test_service_handle_delete_resource_id_is_none(self):
self.test_service.resource_id = None
self.assertIsNone(self.test_service.handle_delete())
def test_service_handle_delete_not_found(self):
exc = self.keystoneclient.NotFound
self.services.delete.side_effect = exc
self.assertIsNone(self.test_service.handle_delete())
def test_resource_mapping(self):
mapping = service.resource_mapping()
self.assertEqual(1, len(mapping))
self.assertEqual(service.KeystoneService, mapping[RESOURCE_TYPE])
self.assertIsInstance(self.test_service, service.KeystoneService)
def test_properties_title(self):
property_title_map = {
service.KeystoneService.NAME: 'name',
service.KeystoneService.DESCRIPTION: 'description',
service.KeystoneService.TYPE: 'type'
}
for actual_title, expected_title in property_title_map.items():
self.assertEqual(
expected_title,
actual_title,
'KeystoneService PROPERTIES(%s) title modified.' %
actual_title)
def test_property_name_validate_schema(self):
schema = service.KeystoneService.properties_schema[
service.KeystoneService.NAME]
self.assertTrue(
schema.update_allowed,
'update_allowed for property %s is modified' %
service.KeystoneService.NAME)
self.assertEqual(properties.Schema.STRING,
schema.type,
'type for property %s is modified' %
service.KeystoneService.NAME)
self.assertEqual('Name of keystone service.',
schema.description,
'description for property %s is modified' %
service.KeystoneService.NAME)
def test_property_description_validate_schema(self):
schema = service.KeystoneService.properties_schema[
service.KeystoneService.DESCRIPTION]
self.assertTrue(
schema.update_allowed,
'update_allowed for property %s is modified' %
service.KeystoneService.DESCRIPTION)
self.assertEqual(properties.Schema.STRING,
schema.type,
'type for property %s is modified' %
service.KeystoneService.DESCRIPTION)
self.assertEqual('Description of keystone service.',
schema.description,
'description for property %s is modified' %
service.KeystoneService.DESCRIPTION)
def test_property_type_validate_schema(self):
schema = service.KeystoneService.properties_schema[
service.KeystoneService.TYPE]
self.assertTrue(
schema.update_allowed,
'update_allowed for property %s is modified' %
service.KeystoneService.TYPE)
self.assertTrue(
schema.required,
'required for property %s is modified' %
service.KeystoneService.TYPE)
self.assertEqual(properties.Schema.STRING,
schema.type,
'type for property %s is modified' %
service.KeystoneService.TYPE)
self.assertEqual('Type of keystone Service.',
schema.description,
'description for property %s is modified' %
service.KeystoneService.TYPE)