Skip to content

Commit

Permalink
Finishing up syncing.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgliss committed Jun 6, 2016
1 parent f46c076 commit 4af6489
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
22 changes: 11 additions & 11 deletions lemur/endpoints/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
from lemur.database import db


class Policy(db.Model):
___tablename__ = 'policies'
id = Column(Integer, primary_key=True)
endpoint_id = Column(Integer, ForeignKey('endpoints.id'))
name = Column(String(32), nullable=True)
ciphers = Column(JSONType)


class Endpoint(db.Model):
__tablename__ = 'endpoints'
id = Column(Integer, primary_key=True)
owner = Column(String(128), nullable=False)
owner = Column(String(128))
name = Column(String(128))
dnsname = Column(String(256), unique=True)
dnsname = Column(String(256))
type = Column(String(128))
active = Column(Boolean, default=True)
port = Column(Integer)
date_created = Column(DateTime, PassiveDefault(func.now()), nullable=False)
policy = relationship("Policy", backref='endpoint')
policy = relationship('Policy', backref='endpoint', uselist=False)
certificate_id = Column(Integer, ForeignKey('certificates.id'))


class Policy(db.Model):
___tablename__ = 'policies'
id = Column(Integer, primary_key=True)
endpoint_id = Column(Integer, ForeignKey('endpoints.id'))
name = Column(String(32), nullable=True)
ciphers = Column(JSONType)
26 changes: 20 additions & 6 deletions lemur/endpoints/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from lemur import database
from lemur.extensions import metrics
from lemur.endpoints.models import Endpoint
from lemur.endpoints.models import Endpoint, Policy


def get_all():
Expand All @@ -36,14 +36,14 @@ def get(endpoint_id):
return database.get(Endpoint, endpoint_id)


def get_by_name(endpoint_name):
def get_by_dnsname(endpoint_dnsname):
"""
Retrieves an endpoint given it's name.
:param endpoint_name:
:param endpoint_dnsname:
:return:
"""
return database.get(Endpoint, endpoint_name, field='name')
return database.get(Endpoint, endpoint_dnsname, field='dnsname')


def create(**kwargs):
Expand All @@ -52,13 +52,27 @@ def create(**kwargs):
:param kwargs:
:return:
"""
print(kwargs)
endpoint = Endpoint(**kwargs)
database.commit()
database.create(endpoint)
metrics.send('endpoint_added', 'counter', 1)
return endpoint


def create_policy(**kwargs):
policy = Policy(**kwargs)
database.create(policy)
return policy


def update(endpoint_id, **kwargs):
endpoint = database.get(Endpoint, endpoint_id)

endpoint.policy = kwargs['policy']
endpoint.certificate = kwargs['certificate']
database.update(endpoint)
return endpoint


def render(args):
"""
Helper that helps us render the REST Api responses.
Expand Down
48 changes: 48 additions & 0 deletions lemur/migrations/versions/368320d26c6c_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""empty message
Revision ID: 368320d26c6c
Revises: 3307381f3b88
Create Date: 2016-05-27 13:41:47.413694
"""

# revision identifiers, used by Alembic.
revision = '368320d26c6c'
down_revision = '3307381f3b88'

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils


def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('endpoints',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('owner', sa.String(length=128), nullable=True),
sa.Column('name', sa.String(length=128), nullable=True),
sa.Column('dnsname', sa.String(length=256), nullable=True),
sa.Column('type', sa.String(length=128), nullable=True),
sa.Column('active', sa.Boolean(), nullable=True),
sa.Column('port', sa.Integer(), nullable=True),
sa.Column('date_created', sa.DateTime(), server_default=sa.text(u'now()'), nullable=False),
sa.Column('certificate_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['certificate_id'], ['certificates.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('policy',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('endpoint_id', sa.Integer(), nullable=True),
sa.Column('name', sa.String(length=32), nullable=True),
sa.Column('ciphers', sqlalchemy_utils.types.json.JSONType(), nullable=True),
sa.ForeignKeyConstraint(['endpoint_id'], ['endpoints.id'], ),
sa.PrimaryKeyConstraint('id')
)
### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_table('policy')
op.drop_table('endpoints')
### end Alembic commands ###
27 changes: 18 additions & 9 deletions lemur/sources/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
:license: Apache, see LICENSE for more details.
.. moduleauthor:: Kevin Glisson <[email protected]>
"""
import datetime

from flask import current_app

from lemur import database
Expand Down Expand Up @@ -79,21 +81,25 @@ def sync_endpoints(source):
return

for endpoint in endpoints:
exists = endpoint_service.get_by_name(endpoint['dnsname'])
exists = endpoint_service.get_by_dnsname(endpoint['dnsname'])

if not exists:
certificate_name = endpoint.pop('certificate_name')
cert = cert_service.get_by_name(certificate_name)
certificate_name = endpoint.pop('certificate_name')
cert = cert_service.get_by_name(certificate_name)
endpoint['certificate'] = cert

if not cert:
current_app.logger.error("Unable to find associated certificate, be sure that certificates are sync'ed before endpoints")
continue
if not cert:
current_app.logger.error("Unable to find associated certificate, be sure that certificates are sync'ed before endpoints")
continue

policy = endpoint.pop('policy')
endpoint['policy'] = endpoint_service.create_policy(**policy)

if not exists:
endpoint_service.create(**endpoint)
new += 1

elif len(exists) == 1:
endpoint_service.update(exists[0].id, **endpoint)
else:
endpoint_service.update(exists.id, **endpoint)
updated += 1


Expand Down Expand Up @@ -138,6 +144,9 @@ def sync(labels=None, type='all'):
sync_certificates(source)
sync_endpoints(source)

source.last_run = datetime.datetime.utcnow()
database.update(source)


def create(label, plugin_name, options, description=None):
"""
Expand Down

0 comments on commit 4af6489

Please sign in to comment.