From 6be6467f5920cf9a4aa9b4f524ddfc7b9f7057bb Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 19 Oct 2018 17:51:25 -0500 Subject: [PATCH] Add appliance table, and probe "location" in that context --- .../versions/796084026e5b_add_appliance.py | 49 +++++++++++++++++++ rattail_tempmon/db/model.py | 48 +++++++++++++++++- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 rattail_tempmon/db/alembic/versions/796084026e5b_add_appliance.py diff --git a/rattail_tempmon/db/alembic/versions/796084026e5b_add_appliance.py b/rattail_tempmon/db/alembic/versions/796084026e5b_add_appliance.py new file mode 100644 index 0000000..280c051 --- /dev/null +++ b/rattail_tempmon/db/alembic/versions/796084026e5b_add_appliance.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8; -*- +"""add appliance + +Revision ID: 796084026e5b +Revises: b02c531caca5 +Create Date: 2018-10-19 17:28:34.146307 + +""" + +from __future__ import unicode_literals, absolute_import + +# revision identifiers, used by Alembic. +revision = '796084026e5b' +down_revision = u'b02c531caca5' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa +import rattail.db.types + + + +def upgrade(): + + # appliance + op.create_table('appliance', + sa.Column('uuid', sa.String(length=32), nullable=False), + sa.Column('name', sa.String(length=255), nullable=False), + sa.Column('location', sa.String(length=255), nullable=True), + sa.PrimaryKeyConstraint('uuid'), + sa.UniqueConstraint('name', name=u'appliance_uq_name') + ) + + # probe + op.add_column(u'probe', sa.Column('appliance_uuid', sa.String(length=32), nullable=True)) + op.add_column(u'probe', sa.Column('location', sa.String(length=255), nullable=True)) + op.create_foreign_key(u'probe_fk_appliance', 'probe', 'appliance', ['appliance_uuid'], ['uuid']) + + +def downgrade(): + + # probe + op.drop_constraint(u'probe_fk_appliance', 'probe', type_='foreignkey') + op.drop_column(u'probe', 'location') + op.drop_column(u'probe', 'appliance_uuid') + + # appliance + op.drop_table('appliance') diff --git a/rattail_tempmon/db/model.py b/rattail_tempmon/db/model.py index bf56f50..01633ee 100644 --- a/rattail_tempmon/db/model.py +++ b/rattail_tempmon/db/model.py @@ -41,6 +41,30 @@ from rattail.db.model.core import ModelBase Base = declarative_base(cls=ModelBase) +@six.python_2_unicode_compatible +class Appliance(Base): + """ + Represents an appliance which is monitored by tempmon. + """ + __tablename__ = 'appliance' + __table_args__ = ( + sa.UniqueConstraint('name', name='appliance_uq_name'), + ) + + uuid = uuid_column() + + name = sa.Column(sa.String(length=255), nullable=False, doc=""" + Human-friendly (and unique) name for the appliance. + """) + + location = sa.Column(sa.String(length=255), nullable=True, doc=""" + Description of the appliance's physical location. + """) + + def __str__(self): + return self.name + + @six.python_2_unicode_compatible class Client(Base): """ @@ -107,6 +131,7 @@ class Probe(Base): __tablename__ = 'probe' __table_args__ = ( sa.ForeignKeyConstraint(['client_uuid'], ['client.uuid'], name='probe_fk_client'), + sa.ForeignKeyConstraint(['appliance_uuid'], ['appliance.uuid'], name='probe_fk_appliance'), sa.UniqueConstraint('config_key', name='probe_uq_config_key'), ) @@ -126,8 +151,29 @@ class Probe(Base): """)) config_key = sa.Column(sa.String(length=50), nullable=False) + appliance_type = sa.Column(sa.Integer(), nullable=False) - description = sa.Column(sa.String(length=255), nullable=False) + + appliance_uuid = sa.Column(sa.String(length=32), nullable=True) + appliance = orm.relationship( + Appliance, + doc=""" + Reference to the appliance which this probe monitors. + """, + backref=orm.backref( + 'probes', + doc=""" + List of probes which monitor this appliance. + """)) + + description = sa.Column(sa.String(length=255), nullable=False, doc=""" + General human-friendly description for the probe. + """) + + location = sa.Column(sa.String(length=255), nullable=True, doc=""" + Description of the probe's physical location, relative to the appliance. + """) + device_path = sa.Column(sa.String(length=255), nullable=True) enabled = sa.Column(sa.Boolean(), nullable=False, default=True)