Merge "LBaas v2 HealthMonitor"

This commit is contained in:
Jenkins 2016-03-02 05:50:35 +00:00 committed by Gerrit Code Review
commit 311461e6be
3 changed files with 416 additions and 0 deletions

View File

@ -0,0 +1,229 @@
#
# Copyright 2015 IBM Corp.
#
# 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 heat.common.i18n import _
from heat.engine import attributes
from heat.engine import constraints
from heat.engine import properties
from heat.engine.resources.openstack.neutron import neutron
from heat.engine import support
class HealthMonitor(neutron.NeutronResource):
"""A resource to handle load balancer health monitors.
This resource creates and manages Neutron LBaaS v2 healthmonitors,
which watches status of the load balanced servers.
"""
support_status = support.SupportStatus(version='6.0.0')
required_service_extension = 'lbaasv2'
# Properties inputs for the resources create/update.
PROPERTIES = (
ADMIN_STATE_UP, DELAY, EXPECTED_CODES, HTTP_METHOD,
MAX_RETRIES, POOL, TIMEOUT, TYPE, URL_PATH, TENANT_ID
) = (
'admin_state_up', 'delay', 'expected_codes', 'http_method',
'max_retries', 'pool', 'timeout', 'type', 'url_path', 'tenant_id'
)
# Supported HTTP methods
HTTP_METHODS = (
GET, HEAT, POST, PUT, DELETE, TRACE, OPTIONS,
CONNECT, PATCH
) = (
'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', 'OPTIONS',
'CONNECT', 'PATCH'
)
# Supported output attributes of the resources.
ATTRIBUTES = (POOLS_ATTR) = ('pools')
properties_schema = {
ADMIN_STATE_UP: properties.Schema(
properties.Schema.BOOLEAN,
_('The administrative state of the health monitor.'),
default=True,
update_allowed=True
),
DELAY: properties.Schema(
properties.Schema.INTEGER,
_('The minimum time in seconds between regular connections of '
'the member.'),
required=True,
update_allowed=True,
constraints=[constraints.Range(min=0)]
),
EXPECTED_CODES: properties.Schema(
properties.Schema.STRING,
_('The HTTP status codes expected in response from the '
'member to declare it healthy. Specify one of the following '
'values: a single value, such as 200. a list, such as 200, 202. '
'a range, such as 200-204.'),
update_allowed=True,
default='200'
),
HTTP_METHOD: properties.Schema(
properties.Schema.STRING,
_('The HTTP method used for requests by the monitor of type '
'HTTP.'),
update_allowed=True,
default=GET,
constraints=[constraints.AllowedValues(HTTP_METHODS)]
),
MAX_RETRIES: properties.Schema(
properties.Schema.INTEGER,
_('Number of permissible connection failures before changing the '
'member status to INACTIVE.'),
required=True,
update_allowed=True,
constraints=[constraints.Range(min=1, max=10)],
),
POOL: properties.Schema(
properties.Schema.STRING,
_('ID or name of the load balancing pool.'),
required=True,
constraints=[
constraints.CustomConstraint('neutron.lbaas.pool')
]
),
TIMEOUT: properties.Schema(
properties.Schema.INTEGER,
_('Maximum number of seconds for a monitor to wait for a '
'connection to be established before it times out.'),
required=True,
update_allowed=True,
constraints=[constraints.Range(min=0)]
),
TYPE: properties.Schema(
properties.Schema.STRING,
_('One of predefined health monitor types.'),
required=True,
constraints=[
constraints.AllowedValues(['PING', 'TCP', 'HTTP', 'HTTPS']),
]
),
URL_PATH: properties.Schema(
properties.Schema.STRING,
_('The HTTP path used in the HTTP request used by the monitor to '
'test a member health. A valid value is a string the begins '
'with a forward slash (/).'),
update_allowed=True,
default='/'
),
TENANT_ID: properties.Schema(
properties.Schema.STRING,
_('ID of the tenant who owns the health monitor.')
)
}
attributes_schema = {
POOLS_ATTR: attributes.Schema(
_('The list of Pools related to this monitor.'),
type=attributes.Schema.LIST
)
}
def __init__(self, name, definition, stack):
super(HealthMonitor, self).__init__(name, definition, stack)
self._lb_id = None
@property
def lb_id(self):
if self._lb_id is None:
pool_id = self.client_plugin().find_resourceid_by_name_or_id(
self.POOL,
self.properties[self.POOL],
cmd_resource='lbaas_pool')
pool = self.client().show_lbaas_pool(pool_id)['pool']
listener_id = pool['listeners'][0]['id']
listener = self.client().show_listener(listener_id)['listener']
self._lb_id = listener['loadbalancers'][0]['id']
return self._lb_id
def _check_lb_status(self):
return self.client_plugin().check_lb_status(self.lb_id)
def handle_create(self):
properties = self.prepare_properties(
self.properties,
self.physical_resource_name())
self.client_plugin().resolve_pool(
properties, self.POOL, 'pool_id')
return properties
def check_create_complete(self, properties):
if not self._check_lb_status():
return False
if self.resource_id is None:
healthmonitor = self.client().create_lbaas_healthmonitor(
{'healthmonitor': properties})['healthmonitor']
self.resource_id_set(healthmonitor['id'])
return False
return True
def _show_resource(self):
return self.client().show_lbaas_healthmonitor(
self.resource_id)['healthmonitor']
def handle_update(self, json_snippet, tmpl_diff, prop_diff):
self._update_called = False
return prop_diff
def check_update_complete(self, prop_diff):
if not prop_diff:
return True
if self._update_called:
return self._check_lb_status()
if self._check_lb_status():
self.client().update_lbaas_healthmonitor(
self.resource_id, {'healthmonitor': prop_diff})
self._update_called = True
return False
def handle_delete(self):
self._delete_called = False
def check_delete_complete(self, data):
if self.resource_id is None:
return True
if self._delete_called:
return self._check_lb_status()
if self._check_lb_status():
with self.client_plugin().ignore_not_found:
self.client().delete_lbaas_healthmonitor(self.resource_id)
self._delete_called = True
return False
def resource_mapping():
return {
'OS::Neutron::LBaaS::HealthMonitor': HealthMonitor,
}

View File

@ -124,3 +124,21 @@ resources:
subnet: sub123
admin_state_up: True
'''
MONITOR_TEMPLATE = '''
heat_template_version: 2016-04-08
description: Create a health monitor
resources:
monitor:
type: OS::Neutron::LBaaS::HealthMonitor
properties:
admin_state_up: True
delay: 3
expected_codes: 200-202
http_method: HEAD
max_retries: 5
pool: 123
timeout: 10
type: HTTP
url_path: /health
'''

View File

@ -0,0 +1,169 @@
#
# Copyright 2015 IBM Corp.
#
# 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 neutronclient.common import exceptions
from heat.common import template_format
from heat.engine.resources.openstack.neutron.lbaas import health_monitor
from heat.tests import common
from heat.tests.openstack.neutron import inline_templates
from heat.tests import utils
class HealthMonitorTest(common.HeatTestCase):
def setUp(self):
super(HealthMonitorTest, self).setUp()
@mock.patch('heat.engine.clients.os.neutron.'
'NeutronClientPlugin.has_extension', return_value=True)
def _create_stack(self, ext_func, tmpl=inline_templates.MONITOR_TEMPLATE):
self.t = template_format.parse(tmpl)
self.stack = utils.parse_stack(self.t)
self.healthmonitor = self.stack['monitor']
self.neutron_client = mock.MagicMock()
self.healthmonitor.client = mock.MagicMock(
return_value=self.neutron_client)
self.healthmonitor.client_plugin().find_resourceid_by_name_or_id = (
mock.MagicMock(return_value='123'))
self.healthmonitor.client_plugin().client = mock.MagicMock(
return_value=self.neutron_client)
def test_resource_mapping(self):
mapping = health_monitor.resource_mapping()
self.assertEqual(health_monitor.HealthMonitor,
mapping['OS::Neutron::LBaaS::HealthMonitor'])
def test_create(self):
self._create_stack()
self.neutron_client.show_loadbalancer.side_effect = [
{'loadbalancer': {'provisioning_status': 'PENDING_UPDATE'}},
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
{'loadbalancer': {'provisioning_status': 'PENDING_UPDATE'}},
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
]
expected = {
'healthmonitor': {
'admin_state_up': True,
'delay': 3,
'expected_codes': '200-202',
'http_method': 'HEAD',
'max_retries': 5,
'pool_id': '123',
'timeout': 10,
'type': 'HTTP',
'url_path': '/health'
}
}
props = self.healthmonitor.handle_create()
self.assertFalse(self.healthmonitor.check_create_complete(props))
self.assertFalse(self.healthmonitor.check_create_complete(props))
self.neutron_client.create_lbaas_healthmonitor.assert_called_with(
expected)
self.assertFalse(self.healthmonitor.check_create_complete(props))
self.assertTrue(self.healthmonitor.check_create_complete(props))
def test_show_resource(self):
self._create_stack()
self.healthmonitor.resource_id_set('1234')
self.assertTrue(self.healthmonitor._show_resource())
self.neutron_client.show_lbaas_healthmonitor.assert_called_with(
'1234')
def test_update(self):
self._create_stack()
self.healthmonitor.resource_id_set('1234')
self.neutron_client.show_loadbalancer.side_effect = [
{'loadbalancer': {'provisioning_status': 'PENDING_UPDATE'}},
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
{'loadbalancer': {'provisioning_status': 'PENDING_UPDATE'}},
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
]
prop_diff = {
'admin_state_up': False,
}
prop_diff = self.healthmonitor.handle_update(None, None, prop_diff)
self.assertFalse(self.healthmonitor.check_update_complete(prop_diff))
self.assertFalse(self.healthmonitor._update_called)
self.assertFalse(self.healthmonitor.check_update_complete(prop_diff))
self.assertTrue(self.healthmonitor._update_called)
self.neutron_client.update_lbaas_healthmonitor.assert_called_with(
'1234', {'healthmonitor': prop_diff})
self.assertFalse(self.healthmonitor.check_update_complete(prop_diff))
self.assertTrue(self.healthmonitor.check_update_complete(prop_diff))
def test_delete(self):
self._create_stack()
self.healthmonitor.resource_id_set('1234')
self.neutron_client.show_loadbalancer.side_effect = [
{'loadbalancer': {'provisioning_status': 'PENDING_UPDATE'}},
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
{'loadbalancer': {'provisioning_status': 'PENDING_UPDATE'}},
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
]
self.healthmonitor.handle_delete()
self.assertFalse(self.healthmonitor.check_delete_complete(None))
self.assertFalse(self.healthmonitor._delete_called)
self.assertFalse(self.healthmonitor.check_delete_complete(None))
self.assertTrue(self.healthmonitor._delete_called)
self.neutron_client.delete_lbaas_healthmonitor.assert_called_with(
'1234')
self.assertFalse(self.healthmonitor.check_delete_complete(None))
self.assertTrue(self.healthmonitor.check_delete_complete(None))
def test_delete_already_gone(self):
self._create_stack()
self.healthmonitor.resource_id_set('1234')
self.neutron_client.show_loadbalancer.side_effect = [
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
]
self.neutron_client.delete_lbaas_healthmonitor.side_effect = (
exceptions.NotFound)
self.healthmonitor.handle_delete()
self.assertFalse(self.healthmonitor.check_delete_complete(None))
self.assertTrue(self.healthmonitor.check_delete_complete(None))
self.neutron_client.delete_lbaas_healthmonitor.assert_called_with(
'1234')
def test_delete_failed(self):
self._create_stack()
self.healthmonitor.resource_id_set('1234')
self.neutron_client.show_loadbalancer.side_effect = [
{'loadbalancer': {'provisioning_status': 'ACTIVE'}},
]
self.neutron_client.delete_lbaas_healthmonitor.side_effect = (
exceptions.Unauthorized)
self.healthmonitor.handle_delete()
self.assertRaises(exceptions.Unauthorized,
self.healthmonitor.check_delete_complete, None)
self.neutron_client.delete_lbaas_healthmonitor.assert_called_with(
'1234')