Add octavia L7Policy Resource

Change-Id: I05a5d97633ebbe8473c0f582061fa6b1ca6a1358
Partial-Bug: #1737567
This commit is contained in:
rabi 2018-01-03 11:51:22 +05:30
parent a56e1aaf4e
commit cab4258cc8
3 changed files with 484 additions and 0 deletions

View File

@ -0,0 +1,205 @@
#
# 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 import exception
from heat.common.i18n import _
from heat.engine import attributes
from heat.engine import constraints
from heat.engine import properties
from heat.engine.resources.openstack.octavia import octavia_base
from heat.engine import translation
class L7Policy(octavia_base.OctaviaBase):
"""A resource for managing octavia L7Policies.
This resource manages L7Policies, which represent a collection of L7Rules.
L7Policy holds the action that should be performed when the rules are
matched (Redirect to Pool, Redirect to URL, Reject). L7Policy holds a
Listener id, so a Listener can evaluate a collection of L7Policies.
L7Policy will return True when all of the L7Rules that belong
to this L7Policy are matched. L7Policies under a specific Listener are
ordered and the first l7Policy that returns a match will be executed.
When none of the policies match the request gets forwarded to
listener.default_pool_id.
"""
PROPERTIES = (
NAME, DESCRIPTION, ADMIN_STATE_UP, ACTION,
REDIRECT_POOL, REDIRECT_URL, POSITION, LISTENER
) = (
'name', 'description', 'admin_state_up', 'action',
'redirect_pool', 'redirect_url', 'position', 'listener'
)
L7ACTIONS = (
REJECT, REDIRECT_TO_POOL, REDIRECT_TO_URL
) = (
'REJECT', 'REDIRECT_TO_POOL', 'REDIRECT_TO_URL'
)
ATTRIBUTES = (RULES_ATTR) = ('rules')
properties_schema = {
NAME: properties.Schema(
properties.Schema.STRING,
_('Name of the policy.'),
update_allowed=True
),
DESCRIPTION: properties.Schema(
properties.Schema.STRING,
_('Description of the policy.'),
update_allowed=True
),
ADMIN_STATE_UP: properties.Schema(
properties.Schema.BOOLEAN,
_('The administrative state of the policy.'),
default=True,
update_allowed=True
),
ACTION: properties.Schema(
properties.Schema.STRING,
_('Action type of the policy.'),
required=True,
constraints=[constraints.AllowedValues(L7ACTIONS)],
update_allowed=True
),
REDIRECT_POOL: properties.Schema(
properties.Schema.STRING,
_('ID or name of the pool for REDIRECT_TO_POOL action type.'),
constraints=[
constraints.CustomConstraint('octavia.pool')
],
update_allowed=True
),
REDIRECT_URL: properties.Schema(
properties.Schema.STRING,
_('URL for REDIRECT_TO_URL action type. '
'This should be a valid URL string.'),
update_allowed=True
),
POSITION: properties.Schema(
properties.Schema.NUMBER,
_('L7 policy position in ordered policies list. This must be '
'an integer starting from 1. If not specified, policy will be '
'placed at the tail of existing policies list.'),
constraints=[constraints.Range(min=1)],
update_allowed=True
),
LISTENER: properties.Schema(
properties.Schema.STRING,
_('ID or name of the listener this policy belongs to.'),
required=True,
constraints=[
constraints.CustomConstraint('octavia.listener')
]
),
}
attributes_schema = {
RULES_ATTR: attributes.Schema(
_('L7Rules associated with this policy.'),
type=attributes.Schema.LIST
),
}
def translation_rules(self, props):
return [
translation.TranslationRule(
props,
translation.TranslationRule.RESOLVE,
[self.LISTENER],
client_plugin=self.client_plugin(),
finder='get_listener',
),
translation.TranslationRule(
props,
translation.TranslationRule.RESOLVE,
[self.REDIRECT_POOL],
client_plugin=self.client_plugin(),
finder='get_pool',
),
]
def validate(self):
super(L7Policy, self).validate()
if (self.properties[self.ACTION] == self.REJECT and
(self.properties[self.REDIRECT_POOL] is not None or
self.properties[self.REDIRECT_URL] is not None)):
msg = (_('Properties %(pool)s and %(url)s are not required when '
'%(action)s type is set to %(action_type)s.') %
{'pool': self.REDIRECT_POOL,
'url': self.REDIRECT_URL,
'action': self.ACTION,
'action_type': self.REJECT})
raise exception.StackValidationFailed(message=msg)
if self.properties[self.ACTION] == self.REDIRECT_TO_POOL:
if self.properties[self.REDIRECT_URL] is not None:
raise exception.ResourcePropertyValueDependency(
prop1=self.REDIRECT_URL,
prop2=self.ACTION,
value=self.REDIRECT_TO_URL)
if self.properties[self.REDIRECT_POOL] is None:
msg = (_('Property %(pool)s is required when %(action)s '
'type is set to %(action_type)s.') %
{'pool': self.REDIRECT_POOL,
'action': self.ACTION,
'action_type': self.REDIRECT_TO_POOL})
raise exception.StackValidationFailed(message=msg)
if self.properties[self.ACTION] == self.REDIRECT_TO_URL:
if self.properties[self.REDIRECT_POOL] is not None:
raise exception.ResourcePropertyValueDependency(
prop1=self.REDIRECT_POOL,
prop2=self.ACTION,
value=self.REDIRECT_TO_POOL)
if self.properties[self.REDIRECT_URL] is None:
msg = (_('Property %(url)s is required when %(action)s '
'type is set to %(action_type)s.') %
{'url': self.REDIRECT_URL,
'action': self.ACTION,
'action_type': self.REDIRECT_TO_URL})
raise exception.StackValidationFailed(message=msg)
def _prepare_args(self, properties):
props = dict((k, v) for k, v in properties.items()
if v is not None)
if self.NAME not in props:
props[self.NAME] = self.physical_resource_name()
props['listener_id'] = props.pop(self.LISTENER)
if self.REDIRECT_POOL in props:
props['redirect_pool_id'] = props.pop(self.REDIRECT_POOL)
return props
def _resource_create(self, properties):
return self.client().l7policy_create(
json={'l7policy': properties})['l7policy']
def _resource_update(self, prop_diff):
if self.REDIRECT_POOL in prop_diff:
prop_diff['redirect_pool_id'] = prop_diff.pop(self.REDIRECT_POOL)
self.client().l7policy_set(
self.resource_id, json={'l7policy': prop_diff})
def _resource_delete(self):
self.client().l7policy_delete(self.resource_id)
def _show_resource(self):
return self.client().l7policy_show(self.resource_id)
def resource_mapping():
return {
'OS::Octavia::L7Policy': L7Policy
}

View File

@ -99,3 +99,19 @@ resources:
type: HTTP
url_path: /health
'''
L7POLICY_TEMPLATE = '''
heat_template_version: 2016-04-08
description: Template to test L7Policy Neutron resource
resources:
l7policy:
type: OS::Octavia::L7Policy
properties:
admin_state_up: True
name: test_l7policy
description: test l7policy resource
action: REDIRECT_TO_URL
redirect_url: http://www.mirantis.com
listener: 123
position: 1
'''

View File

@ -0,0 +1,263 @@
#
# 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 yaml
from osc_lib import exceptions
from heat.common import exception
from heat.common.i18n import _
from heat.common import template_format
from heat.engine.resources.openstack.octavia import l7policy
from heat.tests import common
from heat.tests.openstack.octavia import inline_templates
from heat.tests import utils
class L7PolicyTest(common.HeatTestCase):
def test_resource_mapping(self):
mapping = l7policy.resource_mapping()
self.assertEqual(mapping['OS::Octavia::L7Policy'],
l7policy.L7Policy)
def _create_stack(self, tmpl=inline_templates.L7POLICY_TEMPLATE):
self.t = template_format.parse(tmpl)
self.stack = utils.parse_stack(self.t)
self.l7policy = self.stack['l7policy']
self.octavia_client = mock.MagicMock()
self.l7policy.client = mock.MagicMock(
return_value=self.octavia_client)
self.l7policy.client_plugin().client = mock.MagicMock(
return_value=self.octavia_client)
def test_validate_reject_action_with_conflicting_props(self):
tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
props = tmpl['resources']['l7policy']['properties']
props['action'] = 'REJECT'
self._create_stack(tmpl=yaml.safe_dump(tmpl))
msg = _('Properties redirect_pool and redirect_url are not '
'required when action type is set to REJECT.')
with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
'has_extension', return_value=True):
self.assertRaisesRegex(exception.StackValidationFailed,
msg, self.l7policy.validate)
def test_validate_redirect_pool_action_with_url(self):
tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
props = tmpl['resources']['l7policy']['properties']
props['action'] = 'REDIRECT_TO_POOL'
props['redirect_pool'] = '123'
self._create_stack(tmpl=yaml.safe_dump(tmpl))
msg = _('redirect_url property should only be specified '
'for action with value REDIRECT_TO_URL.')
with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
'has_extension', return_value=True):
self.assertRaisesRegex(exception.ResourcePropertyValueDependency,
msg, self.l7policy.validate)
def test_validate_redirect_pool_action_without_pool(self):
tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
props = tmpl['resources']['l7policy']['properties']
props['action'] = 'REDIRECT_TO_POOL'
del props['redirect_url']
self._create_stack(tmpl=yaml.safe_dump(tmpl))
msg = _('Property redirect_pool is required when action type '
'is set to REDIRECT_TO_POOL.')
with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
'has_extension', return_value=True):
self.assertRaisesRegex(exception.StackValidationFailed,
msg, self.l7policy.validate)
def test_validate_redirect_url_action_with_pool(self):
tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
props = tmpl['resources']['l7policy']['properties']
props['redirect_pool'] = '123'
self._create_stack(tmpl=yaml.safe_dump(tmpl))
msg = _('redirect_pool property should only be specified '
'for action with value REDIRECT_TO_POOL.')
with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
'has_extension', return_value=True):
self.assertRaisesRegex(exception.ResourcePropertyValueDependency,
msg, self.l7policy.validate)
def test_validate_redirect_url_action_without_url(self):
tmpl = yaml.safe_load(inline_templates.L7POLICY_TEMPLATE)
props = tmpl['resources']['l7policy']['properties']
del props['redirect_url']
self._create_stack(tmpl=yaml.safe_dump(tmpl))
msg = _('Property redirect_url is required when action type '
'is set to REDIRECT_TO_URL.')
with mock.patch('heat.engine.clients.os.neutron.NeutronClientPlugin.'
'has_extension', return_value=True):
self.assertRaisesRegex(exception.StackValidationFailed,
msg, self.l7policy.validate)
def test_create(self):
self._create_stack()
self.octavia_client.l7policy_show.side_effect = [
{'provisioning_status': 'PENDING_CREATE'},
{'provisioning_status': 'PENDING_CREATE'},
{'provisioning_status': 'ACTIVE'},
]
self.octavia_client.l7policy_create.side_effect = [
exceptions.Conflict(409),
{'l7policy': {'id': '1234'}}
]
expected = {
'l7policy': {
'name': u'test_l7policy',
'description': u'test l7policy resource',
'action': u'REDIRECT_TO_URL',
'listener_id': u'123',
'redirect_url': u'http://www.mirantis.com',
'position': 1,
'admin_state_up': True
}
}
props = self.l7policy.handle_create()
self.assertFalse(self.l7policy.check_create_complete(props))
self.octavia_client.l7policy_create.assert_called_with(json=expected)
self.assertFalse(self.l7policy.check_create_complete(props))
self.octavia_client.l7policy_create.assert_called_with(json=expected)
self.assertFalse(self.l7policy.check_create_complete(props))
self.assertTrue(self.l7policy.check_create_complete(props))
def test_create_missing_properties(self):
for prop in ('action', 'listener'):
tmpl = yaml.load(inline_templates.L7POLICY_TEMPLATE)
del tmpl['resources']['l7policy']['properties'][prop]
self._create_stack(tmpl=yaml.dump(tmpl))
self.assertRaises(exception.StackValidationFailed,
self.l7policy.validate)
def test_show_resource(self):
self._create_stack()
self.l7policy.resource_id_set('1234')
self.octavia_client.l7policy_show.return_value = {'id': '1234'}
self.assertEqual({'id': '1234'}, self.l7policy._show_resource())
self.octavia_client.l7policy_show.assert_called_with('1234')
def test_update(self):
self._create_stack()
self.l7policy.resource_id_set('1234')
self.octavia_client.l7policy_show.side_effect = [
{'provisioning_status': 'PENDING_UPDATE'},
{'provisioning_status': 'PENDING_UPDATE'},
{'provisioning_status': 'ACTIVE'},
]
self.octavia_client.l7policy_set.side_effect = [
exceptions.Conflict(409), None]
prop_diff = {
'admin_state_up': False,
'name': 'your_l7policy',
'redirect_url': 'http://www.google.com'
}
prop_diff = self.l7policy.handle_update(None, None, prop_diff)
self.assertFalse(self.l7policy.check_update_complete(prop_diff))
self.assertFalse(self.l7policy._update_called)
self.octavia_client.l7policy_set.assert_called_with(
'1234', json={'l7policy': prop_diff})
self.assertFalse(self.l7policy.check_update_complete(prop_diff))
self.assertTrue(self.l7policy._update_called)
self.octavia_client.l7policy_set.assert_called_with(
'1234', json={'l7policy': prop_diff})
self.assertFalse(self.l7policy.check_update_complete(prop_diff))
self.assertTrue(self.l7policy.check_update_complete(prop_diff))
def test_update_redirect_pool_prop_name(self):
self._create_stack()
self.l7policy.resource_id_set('1234')
self.octavia_client.l7policy_show.side_effect = [
{'provisioning_status': 'PENDING_UPDATE'},
{'provisioning_status': 'PENDING_UPDATE'},
{'provisioning_status': 'ACTIVE'},
]
self.octavia_client.l7policy_set.side_effect = [
exceptions.Conflict(409), None]
unresolved_diff = {
'redirect_url': None,
'action': 'REDIRECT_TO_POOL',
'redirect_pool': 'UNRESOLVED_POOL'
}
resolved_diff = {
'redirect_url': None,
'action': 'REDIRECT_TO_POOL',
'redirect_pool_id': '123'
}
self.l7policy.handle_update(None, None, unresolved_diff)
self.assertFalse(self.l7policy.check_update_complete(resolved_diff))
self.assertFalse(self.l7policy._update_called)
self.octavia_client.l7policy_set.assert_called_with(
'1234', json={'l7policy': resolved_diff})
self.assertFalse(self.l7policy.check_update_complete(resolved_diff))
self.assertTrue(self.l7policy._update_called)
self.octavia_client.l7policy_set.assert_called_with(
'1234', json={'l7policy': resolved_diff})
self.assertFalse(self.l7policy.check_update_complete(resolved_diff))
self.assertTrue(self.l7policy.check_update_complete(resolved_diff))
def test_delete(self):
self._create_stack()
self.l7policy.resource_id_set('1234')
self.octavia_client.l7policy_show.side_effect = [
{'provisioning_status': 'PENDING_DELETE'},
{'provisioning_status': 'PENDING_DELETE'},
{'provisioning_status': 'DELETED'},
]
self.octavia_client.l7policy_delete.side_effect = [
exceptions.Conflict(409),
None]
self.l7policy.handle_delete()
self.assertFalse(self.l7policy.check_delete_complete(None))
self.assertFalse(self.l7policy._delete_called)
self.octavia_client.l7policy_delete.assert_called_with(
'1234')
self.assertFalse(self.l7policy.check_delete_complete(None))
self.assertTrue(self.l7policy._delete_called)
self.octavia_client.l7policy_delete.assert_called_with(
'1234')
self.assertTrue(self.l7policy.check_delete_complete(None))
def test_delete_failed(self):
self._create_stack()
self.l7policy.resource_id_set('1234')
self.octavia_client.l7policy_delete.side_effect = (
exceptions.Unauthorized(401))
self.l7policy.handle_delete()
self.assertRaises(exceptions.Unauthorized,
self.l7policy.check_delete_complete, None)
self.octavia_client.l7policy_delete.assert_called_with(
'1234')