Add sg_id to Vip data_model

Change-Id: I3a1de2d1c09e8eda3af91008704b4876da9b9bfc
This commit is contained in:
Gregory Thiemonge 2024-04-05 10:07:47 -04:00
parent 5898d54b13
commit 97a8e5ed4c
6 changed files with 90 additions and 3 deletions

View File

@ -130,6 +130,7 @@ def lb_dict_to_provider_dict(lb_dict, vip=None, add_vips=None, db_pools=None,
new_lb_dict['vip_port_id'] = vip.port_id
new_lb_dict['vip_subnet_id'] = vip.subnet_id
new_lb_dict['vip_qos_policy_id'] = vip.qos_policy_id
new_lb_dict['vip_sg_ids'] = [vip_sg.sg_id for vip_sg in vip.sgs]
if 'flavor_id' in lb_dict and lb_dict['flavor_id']:
flavor_repo = repositories.FlavorRepository()
session = db_api.get_session()
@ -565,6 +566,8 @@ def vip_dict_to_provider_dict(vip_dict):
new_vip_dict['vip_subnet_id'] = vip_dict['subnet_id']
if 'qos_policy_id' in vip_dict:
new_vip_dict['vip_qos_policy_id'] = vip_dict['qos_policy_id']
if 'sg_id' in vip_dict:
new_vip_dict['vip_sg_id'] = vip_dict['sg_id']
if constants.OCTAVIA_OWNED in vip_dict:
new_vip_dict[constants.OCTAVIA_OWNED] = vip_dict[
constants.OCTAVIA_OWNED]
@ -596,6 +599,8 @@ def provider_vip_dict_to_vip_obj(vip_dictionary):
vip_obj.subnet_id = vip_dictionary['vip_subnet_id']
if 'vip_qos_policy_id' in vip_dictionary:
vip_obj.qos_policy_id = vip_dictionary['vip_qos_policy_id']
if 'vip_sg_id' in vip_dictionary:
vip_obj.sg_id = vip_dictionary['vip_sg_id']
if constants.OCTAVIA_OWNED in vip_dictionary:
vip_obj.octavia_owned = vip_dictionary[constants.OCTAVIA_OWNED]
return vip_obj

View File

@ -558,7 +558,7 @@ class Vip(BaseDataModel):
def __init__(self, load_balancer_id=None, ip_address=None,
subnet_id=None, network_id=None, port_id=None,
load_balancer=None, qos_policy_id=None, octavia_owned=None,
vnic_type=None):
vnic_type=None, sgs=None):
self.load_balancer_id = load_balancer_id
self.ip_address = ip_address
self.subnet_id = subnet_id
@ -568,6 +568,7 @@ class Vip(BaseDataModel):
self.qos_policy_id = qos_policy_id
self.octavia_owned = octavia_owned
self.vnic_type = vnic_type
self.sgs = sgs or []
class AdditionalVip(BaseDataModel):
@ -886,3 +887,9 @@ class ListenerCidr(BaseDataModel):
# object. Otherwise we recurse down the "ghost" listener object.
def to_dict(self, **kwargs):
return {'cidr': self.cidr, 'listener_id': self.listener_id}
class VipSecurityGroup(BaseDataModel):
def __init__(self, load_balancer_id=None, sg_id=None):
self.load_balancer_id = load_balancer_id
self.sg_id = sg_id

View File

@ -54,6 +54,8 @@ class OctaviaBase(models.ModelBase):
if obj.__class__.__name__ in ['AdditionalVip']:
return (obj.__class__.__name__ +
obj.load_balancer_id + obj.subnet_id)
if obj.__class__.__name__ in ['VipSecurityGroup']:
return obj.__class__.__name__ + obj.load_balancer_id + obj.sg_id
raise NotImplementedError
def to_data_model(self, _graph_nodes=None):

View File

@ -0,0 +1,39 @@
# 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 sg_id to vip table
Revision ID: 3097e55493ae
Revises: db2a73e82626
Create Date: 2024-04-05 10:04:32.015445
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '3097e55493ae'
down_revision = 'db2a73e82626'
def upgrade():
op.create_table(
"vip_security_group",
sa.Column("load_balancer_id", sa.String(36), nullable=False),
sa.Column("sg_id", sa.String(36), nullable=False),
sa.ForeignKeyConstraint(["load_balancer_id"],
["vip.load_balancer_id"],
name="fk_vip_sg_vip_lb_id"),
sa.PrimaryKeyConstraint("load_balancer_id", "sg_id")
)

View File

@ -508,6 +508,10 @@ class Vip(base_models.BASE):
octavia_owned = sa.Column(sa.Boolean(), nullable=True)
vnic_type = sa.Column(sa.String(64), nullable=True)
sgs = orm.relationship(
"VipSecurityGroup", cascade="all,delete-orphan",
uselist=True, backref=orm.backref("vip", uselist=False))
class AdditionalVip(base_models.BASE):
@ -976,3 +980,19 @@ class ListenerCidr(base_models.BASE):
sa.ForeignKey("listener.id", name="fk_listener_cidr_listener_id"),
nullable=False)
cidr = sa.Column(sa.String(64), nullable=False)
class VipSecurityGroup(base_models.BASE):
__data_model__ = data_models.VipSecurityGroup
__tablename__ = "vip_security_group"
__table_args__ = (
sa.PrimaryKeyConstraint('load_balancer_id', 'sg_id'),
)
load_balancer_id = sa.Column(
sa.String(36),
sa.ForeignKey("vip.load_balancer_id", name="fk_vip_sg_vip_lb_id"),
nullable=False)
sg_id = sa.Column(sa.String(64), nullable=False)

View File

@ -253,9 +253,15 @@ class Repositories:
lb_dict['id'] = uuidutils.generate_uuid()
lb = models.LoadBalancer(**lb_dict)
session.add(lb)
vip_sg_ids = vip_dict.pop("sg_ids", [])
vip_dict['load_balancer_id'] = lb_dict['id']
vip = models.Vip(**vip_dict)
session.add(vip)
for vip_sg_id in vip_sg_ids:
vip_sg = models.VipSecurityGroup(
load_balancer_id=lb_dict['id'],
sg_id=vip_sg_id)
session.add(vip_sg)
for add_vip_dict in additional_vip_dicts:
add_vip_dict['load_balancer_id'] = lb_dict['id']
add_vip_dict['network_id'] = vip_dict.get('network_id')
@ -789,8 +795,16 @@ class VipRepository(BaseRepository):
def update(self, session, load_balancer_id, **model_kwargs):
"""Updates a vip entity in the database by load_balancer_id."""
session.query(self.model_class).filter_by(
load_balancer_id=load_balancer_id).update(model_kwargs)
vip = session.query(self.model_class).filter_by(
load_balancer_id=load_balancer_id).first()
if 'sg_ids' in model_kwargs:
sg_ids = model_kwargs.pop('sg_ids', [])
vip.sgs = [
models.VipSecurityGroup(
load_balancer_id=load_balancer_id,
sg_id=sg_id)
for sg_id in sg_ids]
vip.update(model_kwargs)
class AdditionalVipRepository(BaseRepository):