diff --git a/octavia/db/migration/alembic_migrations/versions/fac584114642_.py b/octavia/db/migration/alembic_migrations/versions/fac584114642_.py new file mode 100644 index 0000000000..fa5d3894c5 --- /dev/null +++ b/octavia/db/migration/alembic_migrations/versions/fac584114642_.py @@ -0,0 +1,56 @@ +# Copyright 2017 Intel Corporation +# +# 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. +# + +"""Add ID column to Healthmonitor table + +Revision ID: fac584114642 +Revises: fc5582da7d8a +Create Date: 2017-02-07 20:47:52.405865 + +""" + + +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic. +revision = 'fac584114642' +down_revision = 'fc5582da7d8a' + + +def upgrade(): + op.add_column('health_monitor', + sa.Column('id', + sa.String(length=36), + nullable=True, + )) + + op.drop_constraint('fk_health_monitor_pool_id', + 'health_monitor', + type_='foreignkey',) + + op.execute("UPDATE health_monitor SET id = pool_id") + + op.execute("ALTER TABLE health_monitor MODIFY id varchar(36) NOT NULL") + + op.execute("ALTER TABLE health_monitor DROP PRIMARY KEY," + "ADD PRIMARY KEY(id);") + + op.create_foreign_key('fk_health_monitor_pool_id', 'health_monitor', + 'pool', ['pool_id'], ['id']) + + op.create_index('uq_health_monitor_pool', + 'health_monitor', ['pool_id'], + unique=True) diff --git a/octavia/db/models.py b/octavia/db/models.py index 277ca4bd8a..3ac00b0bab 100644 --- a/octavia/db/models.py +++ b/octavia/db/models.py @@ -175,7 +175,8 @@ class Member(base_models.BASE, base_models.IdMixin, base_models.ProjectMixin, nullable=True) -class HealthMonitor(base_models.BASE, base_models.ProjectMixin, +class HealthMonitor(base_models.BASE, base_models.IdMixin, + base_models.ProjectMixin, base_models.NameMixin): __data_model__ = data_models.HealthMonitor @@ -190,7 +191,7 @@ class HealthMonitor(base_models.BASE, base_models.ProjectMixin, pool_id = sa.Column( sa.String(36), sa.ForeignKey("pool.id", name="fk_health_monitor_pool_id"), - nullable=False, primary_key=True) + nullable=False) delay = sa.Column(sa.Integer, nullable=False) timeout = sa.Column(sa.Integer, nullable=False) fall_threshold = sa.Column(sa.Integer, nullable=False) diff --git a/octavia/db/prepare.py b/octavia/db/prepare.py index 3e713f6898..ba5ae00474 100644 --- a/octavia/db/prepare.py +++ b/octavia/db/prepare.py @@ -142,5 +142,6 @@ def create_member(member_dict, pool_id): def create_health_monitor(hm_dict, pool_id): + hm_dict['id'] = pool_id hm_dict['pool_id'] = pool_id return hm_dict diff --git a/octavia/db/repositories.py b/octavia/db/repositories.py index 9bb1694d79..188c0e34e6 100644 --- a/octavia/db/repositories.py +++ b/octavia/db/repositories.py @@ -532,6 +532,7 @@ class Repositories(object): data_models.HealthMonitor, lb_dict['project_id']): raise exceptions.QuotaException + hm_dict['id'] = pool_dm.id hm_dict['pool_id'] = pool_dm.id self.health_monitor.create(lock_session, **hm_dict) for r_member_dict in member_dicts: @@ -580,6 +581,7 @@ class Repositories(object): data_models.HealthMonitor, lb_dict['project_id']): raise exceptions.QuotaException + r_hm_dict['id'] = r_pool_dm.id r_hm_dict['pool_id'] = r_pool_dm.id self.health_monitor.create(lock_session, **r_hm_dict) @@ -677,6 +679,13 @@ class VipRepository(BaseRepository): class HealthMonitorRepository(BaseRepository): model_class = models.HealthMonitor + def create(self, session, **model_kwargs): + with session.begin(subtransactions=True): + model_kwargs['id'] = model_kwargs['pool_id'] + model = self.model_class(**model_kwargs) + session.add(model) + return model.to_data_model() + def update(self, session, pool_id, **model_kwargs): """Updates a health monitor entity in the database by pool_id.""" with session.begin(subtransactions=True): diff --git a/octavia/tests/functional/db/test_models.py b/octavia/tests/functional/db/test_models.py index abc4b149ce..a56bbcf612 100644 --- a/octavia/tests/functional/db/test_models.py +++ b/octavia/tests/functional/db/test_models.py @@ -80,7 +80,8 @@ class ModelTestMixin(object): return self._insert(session, models.SessionPersistence, kwargs) def create_health_monitor(self, session, pool_id, **overrides): - kwargs = {'pool_id': pool_id, + kwargs = {'id': pool_id, + 'pool_id': pool_id, 'type': constants.HEALTH_MONITOR_HTTP, 'delay': 1, 'timeout': 1, diff --git a/octavia/tests/functional/db/test_repositories.py b/octavia/tests/functional/db/test_repositories.py index d50a3ed3b0..44b6e19acf 100644 --- a/octavia/tests/functional/db/test_repositories.py +++ b/octavia/tests/functional/db/test_repositories.py @@ -1296,8 +1296,8 @@ class AllRepositoriesTest(base.OctaviaDBTestBase): operating_status=constants.ONLINE, enabled=True, load_balancer_id=lb.id) self.repos.health_monitor.create( - self.session, project_id=project_id, name="health_mon1", - type=constants.HEALTH_MONITOR_HTTP, + self.session, project_id=project_id, + name="health_mon1", type=constants.HEALTH_MONITOR_HTTP, delay=1, timeout=1, fall_threshold=1, rise_threshold=1, provisioning_status=constants.ACTIVE, enabled=True, pool_id=pool.id) @@ -1325,8 +1325,8 @@ class AllRepositoriesTest(base.OctaviaDBTestBase): operating_status=constants.ONLINE, enabled=True, load_balancer_id=lb.id) self.repos.health_monitor.create( - self.session, project_id=project_id, name="health_mon1", - type=constants.HEALTH_MONITOR_HTTP, + self.session, project_id=project_id, + name="health_mon1", type=constants.HEALTH_MONITOR_HTTP, delay=1, timeout=1, fall_threshold=1, rise_threshold=1, provisioning_status=constants.DELETED, enabled=True, pool_id=pool.id) @@ -2433,9 +2433,10 @@ class HealthMonitorRepositoryTest(BaseRepositoryTest): def create_health_monitor(self, pool_id): health_monitor = self.hm_repo.create( - self.session, type=constants.HEALTH_MONITOR_HTTP, pool_id=pool_id, - delay=1, timeout=1, fall_threshold=1, rise_threshold=1, - http_method="POST", url_path="http://localhost:80/index.php", + self.session, type=constants.HEALTH_MONITOR_HTTP, + pool_id=pool_id, delay=1, timeout=1, fall_threshold=1, + rise_threshold=1, http_method="POST", + url_path="http://localhost:80/index.php", expected_codes="200", enabled=True) return health_monitor diff --git a/releasenotes/notes/add-id-column-to-healthmonitor-a331934ad2cede87.yaml b/releasenotes/notes/add-id-column-to-healthmonitor-a331934ad2cede87.yaml new file mode 100644 index 0000000000..ca542fffd9 --- /dev/null +++ b/releasenotes/notes/add-id-column-to-healthmonitor-a331934ad2cede87.yaml @@ -0,0 +1,6 @@ +--- +upgrade: + - Adding `ID` column to the health_monitor table in Octavia, + whose value is same as the `pool_id` column. + The database needs to be upgraded first, followed by upgrade + and restart of the API servers.