Keystone resource plugin for Project
Adds resource plugin for Keystone project Change-Id: I1dc19fcb962e6d4f6d155582a13279bd54668438 Implements: blueprint keystone-resources
This commit is contained in:
parent
5a89374dc4
commit
957af1cf84
179
contrib/heat_keystone/heat_keystone/resources/project.py
Normal file
179
contrib/heat_keystone/heat_keystone/resources/project.py
Normal file
@ -0,0 +1,179 @@
|
||||
#
|
||||
# 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 constraints
|
||||
from heat.engine import properties
|
||||
from heat.engine import resource
|
||||
from heat.engine import support
|
||||
|
||||
|
||||
class KeystoneProject(resource.Resource):
|
||||
'''
|
||||
Heat Template Resource for Keystone Project.
|
||||
|
||||
heat_template_version: 2013-05-23
|
||||
|
||||
description: Sample Keystone Project template
|
||||
|
||||
parameters:
|
||||
project_name:
|
||||
type: string
|
||||
description: Keystone project name
|
||||
project_description:
|
||||
type: string
|
||||
description: Keystone project description
|
||||
project_enabled:
|
||||
type: boolean
|
||||
description: Keystone project is enabled or disabled
|
||||
project_domain:
|
||||
type: string
|
||||
description: Keystone project domain name or id
|
||||
|
||||
resources:
|
||||
admin_project:
|
||||
type: OS::Keystone::Project
|
||||
properties:
|
||||
name: {get_param: project_name}
|
||||
domain: {get_param: project_domain}
|
||||
description: {get_param: project_description}
|
||||
enabled: {get_param: project_enabled}
|
||||
|
||||
'''
|
||||
|
||||
support_status = support.SupportStatus(
|
||||
version='2015.1',
|
||||
message=_('Supported versions: keystone v3'))
|
||||
|
||||
PROPERTIES = (
|
||||
NAME, DOMAIN, DESCRIPTION, ENABLED
|
||||
) = (
|
||||
'name', 'domain', 'description', 'enabled'
|
||||
)
|
||||
|
||||
properties_schema = {
|
||||
NAME: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('Name of keystone project.'),
|
||||
update_allowed=True
|
||||
),
|
||||
DOMAIN: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('Name or id of keystone domain.'),
|
||||
default='default',
|
||||
update_allowed=True,
|
||||
constraints=[constraints.CustomConstraint('keystone.domain')]
|
||||
),
|
||||
DESCRIPTION: properties.Schema(
|
||||
properties.Schema.STRING,
|
||||
_('Description of keystone project.'),
|
||||
default='',
|
||||
update_allowed=True
|
||||
),
|
||||
ENABLED: properties.Schema(
|
||||
properties.Schema.BOOLEAN,
|
||||
_('This project is enabled or disabled.'),
|
||||
default=True,
|
||||
update_allowed=True
|
||||
)
|
||||
}
|
||||
|
||||
def _create_project(self,
|
||||
project_name,
|
||||
description,
|
||||
domain,
|
||||
enabled):
|
||||
domain = (self.client_plugin('keystone').
|
||||
get_domain_id(domain))
|
||||
|
||||
return self.keystone().client.projects.create(
|
||||
name=project_name,
|
||||
domain=domain,
|
||||
description=description,
|
||||
enabled=enabled)
|
||||
|
||||
def _delete_project(self, project_id):
|
||||
return self.keystone().client.projects.delete(project_id)
|
||||
|
||||
def _update_project(self,
|
||||
project_id,
|
||||
domain,
|
||||
new_name=None,
|
||||
new_description=None,
|
||||
enabled=None):
|
||||
values = dict()
|
||||
|
||||
if new_name is not None:
|
||||
values['name'] = new_name
|
||||
if new_description is not None:
|
||||
values['description'] = new_description
|
||||
if enabled is not None:
|
||||
values['enabled'] = enabled
|
||||
|
||||
if len(values) == 0:
|
||||
return
|
||||
|
||||
values['project'] = project_id
|
||||
domain = (self.client_plugin('keystone').
|
||||
get_domain_id(domain))
|
||||
|
||||
values['domain'] = domain
|
||||
|
||||
return self.keystone().client.projects.update(**values)
|
||||
|
||||
def handle_create(self):
|
||||
project_name = (self.properties.get(self.NAME) or
|
||||
self.physical_resource_name())
|
||||
description = self.properties.get(self.DESCRIPTION)
|
||||
domain = self.properties.get(self.DOMAIN)
|
||||
enabled = self.properties.get(self.ENABLED)
|
||||
|
||||
project = self._create_project(
|
||||
project_name=project_name,
|
||||
description=description,
|
||||
domain=domain,
|
||||
enabled=enabled
|
||||
)
|
||||
|
||||
self.resource_id_set(project.id)
|
||||
|
||||
def handle_update(self,
|
||||
json_snippet=None,
|
||||
tmpl_diff=None,
|
||||
prop_diff=None):
|
||||
name = prop_diff.get(self.NAME) or self.physical_resource_name()
|
||||
description = prop_diff.get(self.DESCRIPTION)
|
||||
enabled = prop_diff.get(self.ENABLED)
|
||||
domain = (prop_diff.get(self.DOMAIN) or
|
||||
self._stored_properties_data.get(self.DOMAIN))
|
||||
|
||||
self._update_project(
|
||||
project_id=self.resource_id,
|
||||
domain=domain,
|
||||
new_name=name,
|
||||
new_description=description,
|
||||
enabled=enabled
|
||||
)
|
||||
|
||||
def handle_delete(self):
|
||||
if self.resource_id is not None:
|
||||
try:
|
||||
self._delete_project(project_id=self.resource_id)
|
||||
except Exception as ex:
|
||||
self.client_plugin('keystone').ignore_not_found(ex)
|
||||
|
||||
|
||||
def resource_mapping():
|
||||
return {
|
||||
'OS::Keystone::Project': KeystoneProject
|
||||
}
|
342
contrib/heat_keystone/heat_keystone/tests/test_project.py
Normal file
342
contrib/heat_keystone/heat_keystone/tests/test_project.py
Normal file
@ -0,0 +1,342 @@
|
||||
#
|
||||
# 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 constraints
|
||||
from heat.engine import parser
|
||||
from heat.engine import properties
|
||||
from heat.engine import resource
|
||||
from heat.engine import template
|
||||
from heat.tests import common
|
||||
from heat.tests import utils
|
||||
|
||||
from ..resources.project import KeystoneProject # noqa
|
||||
from ..resources.project import resource_mapping # noqa
|
||||
|
||||
keystone_project_template = {
|
||||
'heat_template_version': '2013-05-23',
|
||||
'resources': {
|
||||
'test_project': {
|
||||
'type': 'OS::Keystone::Project',
|
||||
'properties': {
|
||||
'name': 'test_project_1',
|
||||
'description': 'Test project',
|
||||
'domain': 'default',
|
||||
'enabled': 'True'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RESOURCE_TYPE = 'OS::Keystone::Project'
|
||||
|
||||
|
||||
class KeystoneProjectTest(common.HeatTestCase):
|
||||
def setUp(self):
|
||||
super(KeystoneProjectTest, self).setUp()
|
||||
|
||||
self.ctx = utils.dummy_context()
|
||||
|
||||
# For unit testing purpose. Register resource provider explicitly.
|
||||
resource._register_class(RESOURCE_TYPE, KeystoneProject)
|
||||
|
||||
self.stack = parser.Stack(
|
||||
self.ctx, 'test_stack_keystone',
|
||||
template.Template(keystone_project_template)
|
||||
)
|
||||
|
||||
self.test_project = self.stack['test_project']
|
||||
|
||||
# Mock client
|
||||
self.keystoneclient = mock.MagicMock()
|
||||
self.test_project.keystone = mock.MagicMock()
|
||||
self.test_project.keystone.return_value = self.keystoneclient
|
||||
self.projects = self.keystoneclient.client.projects
|
||||
|
||||
# Mock client plugin
|
||||
def _domain_side_effect(value):
|
||||
return value
|
||||
|
||||
keystone_client_plugin = mock.MagicMock()
|
||||
keystone_client_plugin.get_domain_id.side_effect = _domain_side_effect
|
||||
self.test_project.client_plugin = mock.MagicMock()
|
||||
self.test_project.client_plugin.return_value = keystone_client_plugin
|
||||
|
||||
def _get_mock_project(self):
|
||||
value = mock.MagicMock()
|
||||
project_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
|
||||
value.id = project_id
|
||||
|
||||
return value
|
||||
|
||||
def test_resource_mapping(self):
|
||||
mapping = resource_mapping()
|
||||
self.assertEqual(1, len(mapping))
|
||||
self.assertEqual(KeystoneProject, mapping[RESOURCE_TYPE])
|
||||
self.assertIsInstance(self.test_project, KeystoneProject)
|
||||
|
||||
def test_project_handle_create(self):
|
||||
mock_project = self._get_mock_project()
|
||||
self.projects.create.return_value = mock_project
|
||||
|
||||
# validate the properties
|
||||
self.assertEqual(
|
||||
'test_project_1',
|
||||
self.test_project.properties.get(KeystoneProject.NAME))
|
||||
self.assertEqual(
|
||||
'Test project',
|
||||
self.test_project.properties.get(KeystoneProject.DESCRIPTION))
|
||||
self.assertEqual(
|
||||
'default',
|
||||
self.test_project.properties.get(KeystoneProject.DOMAIN))
|
||||
self.assertEqual(
|
||||
True,
|
||||
self.test_project.properties.get(KeystoneProject.ENABLED))
|
||||
|
||||
self.test_project.handle_create()
|
||||
|
||||
# validate project creation
|
||||
self.projects.create.assert_called_once_with(
|
||||
name='test_project_1',
|
||||
description='Test project',
|
||||
domain='default',
|
||||
enabled=True)
|
||||
|
||||
# validate physical resource id
|
||||
self.assertEqual(mock_project.id, self.test_project.resource_id)
|
||||
|
||||
def test_properties_title(self):
|
||||
property_title_map = {
|
||||
KeystoneProject.NAME: 'name',
|
||||
KeystoneProject.DESCRIPTION: 'description',
|
||||
KeystoneProject.DOMAIN: 'domain',
|
||||
KeystoneProject.ENABLED: 'enabled'
|
||||
}
|
||||
|
||||
for actual_title, expected_title in property_title_map.items():
|
||||
self.assertEqual(
|
||||
expected_title,
|
||||
actual_title,
|
||||
'KeystoneProject PROPERTIES(%s) title modified.' %
|
||||
actual_title)
|
||||
|
||||
def test_property_name_validate_schema(self):
|
||||
schema = KeystoneProject.properties_schema[KeystoneProject.NAME]
|
||||
self.assertEqual(
|
||||
True,
|
||||
schema.update_allowed,
|
||||
'update_allowed for property %s is modified' %
|
||||
KeystoneProject.NAME)
|
||||
|
||||
self.assertEqual(properties.Schema.STRING,
|
||||
schema.type,
|
||||
'type for property %s is modified' %
|
||||
KeystoneProject.NAME)
|
||||
|
||||
self.assertEqual('Name of keystone project.',
|
||||
schema.description,
|
||||
'description for property %s is modified' %
|
||||
KeystoneProject.NAME)
|
||||
|
||||
def test_property_description_validate_schema(self):
|
||||
schema = KeystoneProject.properties_schema[KeystoneProject.DESCRIPTION]
|
||||
self.assertEqual(
|
||||
True,
|
||||
schema.update_allowed,
|
||||
'update_allowed for property %s is modified' %
|
||||
KeystoneProject.DESCRIPTION)
|
||||
|
||||
self.assertEqual(properties.Schema.STRING,
|
||||
schema.type,
|
||||
'type for property %s is modified' %
|
||||
KeystoneProject.DESCRIPTION)
|
||||
|
||||
self.assertEqual('Description of keystone project.',
|
||||
schema.description,
|
||||
'description for property %s is modified' %
|
||||
KeystoneProject.DESCRIPTION)
|
||||
|
||||
self.assertEqual(
|
||||
'',
|
||||
schema.default,
|
||||
'default for property %s is modified' %
|
||||
KeystoneProject.DESCRIPTION)
|
||||
|
||||
def test_property_domain_validate_schema(self):
|
||||
schema = KeystoneProject.properties_schema[KeystoneProject.DOMAIN]
|
||||
self.assertEqual(
|
||||
True,
|
||||
schema.update_allowed,
|
||||
'update_allowed for property %s is modified' %
|
||||
KeystoneProject.DOMAIN)
|
||||
|
||||
self.assertEqual(properties.Schema.STRING,
|
||||
schema.type,
|
||||
'type for property %s is modified' %
|
||||
KeystoneProject.DOMAIN)
|
||||
|
||||
self.assertEqual('Name or id of keystone domain.',
|
||||
schema.description,
|
||||
'description for property %s is modified' %
|
||||
KeystoneProject.DOMAIN)
|
||||
|
||||
self.assertEqual(
|
||||
[constraints.CustomConstraint('keystone.domain')],
|
||||
schema.constraints,
|
||||
'constrains for property %s is modified' %
|
||||
KeystoneProject.DOMAIN)
|
||||
|
||||
self.assertEqual(
|
||||
'default',
|
||||
schema.default,
|
||||
'default for property %s is modified' %
|
||||
KeystoneProject.DOMAIN)
|
||||
|
||||
def test_property_enabled_validate_schema(self):
|
||||
schema = KeystoneProject.properties_schema[KeystoneProject.ENABLED]
|
||||
self.assertEqual(
|
||||
True,
|
||||
schema.update_allowed,
|
||||
'update_allowed for property %s is modified' %
|
||||
KeystoneProject.DOMAIN)
|
||||
|
||||
self.assertEqual(properties.Schema.BOOLEAN,
|
||||
schema.type,
|
||||
'type for property %s is modified' %
|
||||
KeystoneProject.ENABLED)
|
||||
|
||||
self.assertEqual('This project is enabled or disabled.',
|
||||
schema.description,
|
||||
'description for property %s is modified' %
|
||||
KeystoneProject.ENABLED)
|
||||
|
||||
self.assertEqual(
|
||||
True,
|
||||
schema.default,
|
||||
'default for property %s is modified' %
|
||||
KeystoneProject.ENABLED)
|
||||
|
||||
def _get_property_schema_value_default(self, name):
|
||||
schema = KeystoneProject.properties_schema[name]
|
||||
return schema.default
|
||||
|
||||
def test_project_handle_create_default(self):
|
||||
values = {
|
||||
KeystoneProject.NAME: None,
|
||||
KeystoneProject.DESCRIPTION:
|
||||
(self._get_property_schema_value_default(
|
||||
KeystoneProject.DESCRIPTION)),
|
||||
KeystoneProject.DOMAIN:
|
||||
(self._get_property_schema_value_default(
|
||||
KeystoneProject.DOMAIN)),
|
||||
KeystoneProject.ENABLED:
|
||||
(self._get_property_schema_value_default(
|
||||
KeystoneProject.ENABLED))
|
||||
}
|
||||
|
||||
def _side_effect(key):
|
||||
return values[key]
|
||||
|
||||
mock_project = self._get_mock_project()
|
||||
self.projects.create.return_value = mock_project
|
||||
self.test_project.properties = mock.MagicMock()
|
||||
self.test_project.properties.get.side_effect = _side_effect
|
||||
|
||||
self.test_project.physical_resource_name = mock.MagicMock()
|
||||
self.test_project.physical_resource_name.return_value = 'foo'
|
||||
|
||||
# validate the properties
|
||||
self.assertEqual(
|
||||
None,
|
||||
self.test_project.properties.get(KeystoneProject.NAME))
|
||||
self.assertEqual(
|
||||
'',
|
||||
self.test_project.properties.get(KeystoneProject.DESCRIPTION))
|
||||
self.assertEqual(
|
||||
'default',
|
||||
self.test_project.properties.get(KeystoneProject.DOMAIN))
|
||||
self.assertEqual(
|
||||
True,
|
||||
self.test_project.properties.get(KeystoneProject.ENABLED))
|
||||
|
||||
self.test_project.handle_create()
|
||||
|
||||
# validate project creation
|
||||
self.projects.create.assert_called_once_with(
|
||||
name='foo',
|
||||
description='',
|
||||
domain='default',
|
||||
enabled=True)
|
||||
|
||||
def test_project_handle_update(self):
|
||||
self.test_project.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
|
||||
|
||||
prop_diff = {KeystoneProject.NAME: 'test_project_1_updated',
|
||||
KeystoneProject.DESCRIPTION: 'Test Project updated',
|
||||
KeystoneProject.ENABLED: False,
|
||||
KeystoneProject.DOMAIN: 'test_domain'}
|
||||
|
||||
self.test_project.handle_update(json_snippet=None,
|
||||
tmpl_diff=None,
|
||||
prop_diff=prop_diff)
|
||||
|
||||
self.projects.update.assert_called_once_with(
|
||||
project=self.test_project.resource_id,
|
||||
name=prop_diff[KeystoneProject.NAME],
|
||||
description=prop_diff[KeystoneProject.DESCRIPTION],
|
||||
enabled=prop_diff[KeystoneProject.ENABLED],
|
||||
domain='test_domain'
|
||||
)
|
||||
|
||||
def test_project_handle_update_default(self):
|
||||
self.test_project.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
|
||||
self.test_project._stored_properties_data = dict(domain='default')
|
||||
self.test_project.physical_resource_name = mock.MagicMock()
|
||||
self.test_project.physical_resource_name.return_value = 'foo'
|
||||
|
||||
prop_diff = {KeystoneProject.DESCRIPTION: 'Test Project updated',
|
||||
KeystoneProject.ENABLED: False}
|
||||
|
||||
self.test_project.handle_update(json_snippet=None,
|
||||
tmpl_diff=None,
|
||||
prop_diff=prop_diff)
|
||||
|
||||
# validate default name to physical resource name and
|
||||
# domain is set from stored properties used during creation.
|
||||
self.projects.update.assert_called_once_with(
|
||||
project=self.test_project.resource_id,
|
||||
name='foo',
|
||||
description=prop_diff[KeystoneProject.DESCRIPTION],
|
||||
enabled=prop_diff[KeystoneProject.ENABLED],
|
||||
domain='default'
|
||||
)
|
||||
|
||||
def test_project_handle_delete(self):
|
||||
self.test_project.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
|
||||
self.projects.delete.return_value = None
|
||||
|
||||
self.assertIsNone(self.test_project.handle_delete())
|
||||
self.projects.delete.assert_called_once_with(
|
||||
self.test_project.resource_id
|
||||
)
|
||||
|
||||
def test_project_handle_delete_resource_id_is_none(self):
|
||||
self.resource_id = None
|
||||
self.assertIsNone(self.test_project.handle_delete())
|
||||
|
||||
def test_project_handle_delete_not_found(self):
|
||||
exc = self.keystoneclient.NotFound
|
||||
self.projects.delete.side_effect = exc
|
||||
|
||||
self.assertIsNone(self.test_project.handle_delete())
|
Loading…
x
Reference in New Issue
Block a user