refactoring the sync job into a service method that we can also call when adding a new destination

This commit is contained in:
Hossein Shafagh 2019-04-11 16:37:31 -07:00
parent d1ead4b79c
commit 557fac39b5
2 changed files with 37 additions and 21 deletions

View File

@ -21,8 +21,7 @@ from lemur.pending_certificates import service as pending_certificate_service
from lemur.plugins.base import plugins, IPlugin from lemur.plugins.base import plugins, IPlugin
from lemur.sources.cli import clean, sync, validate_sources from lemur.sources.cli import clean, sync, validate_sources
from lemur.destinations import service as destinations_service from lemur.destinations import service as destinations_service
from lemur.sources import service as sources_service from lemur.sources.service import add_aws_destination_to_sources
if current_app: if current_app:
flask_app = current_app flask_app = current_app
@ -269,24 +268,10 @@ def sync_source_destination():
The destination sync_as_source_name reviels the name of the suitable source-plugin. The destination sync_as_source_name reviels the name of the suitable source-plugin.
We rely on account numbers to avoid duplicates. We rely on account numbers to avoid duplicates.
""" """
current_app.logger.debug("Syncing source and destination") current_app.logger.debug("Syncing AWWS destinations and sources")
# a set of all accounts numbers available as sources
src_accounts = set()
sources = validate_sources("all")
for src in sources:
src_accounts.add(IPlugin.get_option('accountNumber', src.options))
for dst in destinations_service.get_all(): for dst in destinations_service.get_all():
destination_plugin = plugins.get(dst.plugin_name) if add_aws_destination_to_sources(dst):
account_number = IPlugin.get_option('accountNumber', dst.options) current_app.logger.debug("Source: %s added", dst.label)
if destination_plugin.sync_as_source and (account_number not in src_accounts):
src_options = copy.deepcopy(plugins.get(destination_plugin.sync_as_source_name).options) current_app.logger.debug("Completed Syncing AWS destinations and sources")
for o in src_options:
if o.get('name') == 'accountNumber':
o.update({'value': account_number})
sources_service.create(label=dst.label,
plugin_name=destination_plugin.sync_as_source_name,
options=src_options,
description=dst.description)
current_app.logger.info("Source: %s added", dst.label)

View File

@ -6,6 +6,7 @@
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com> .. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
""" """
import arrow import arrow
import copy
from flask import current_app from flask import current_app
@ -21,6 +22,7 @@ from lemur.common.utils import find_matching_certificates_by_hash, parse_certifi
from lemur.common.defaults import serial from lemur.common.defaults import serial
from lemur.plugins.base import plugins from lemur.plugins.base import plugins
from lemur.plugins.utils import get_plugin_option, set_plugin_option
def certificate_create(certificate, source): def certificate_create(certificate, source):
@ -256,3 +258,32 @@ def render(args):
query = database.filter(query, Source, terms) query = database.filter(query, Source, terms)
return database.sort_and_page(query, Source, args) return database.sort_and_page(query, Source, args)
def add_aws_destination_to_sources(dst):
"""
Given a destination check, if it can be added as sources, and included it if not already a source
We identify qualified destinations based on the sync_as_source attributed of the plugin.
The destination sync_as_source_name reveals the name of the suitable source-plugin.
We rely on account numbers to avoid duplicates.
:return: true for success and false for not adding the destination as source
"""
# a set of all accounts numbers available as sources
src_accounts = set()
sources = get_all()
for src in sources:
src_accounts.add(get_plugin_option('accountNumber', src.options))
# check
destination_plugin = plugins.get(dst.plugin_name)
account_number = get_plugin_option('accountNumber', dst.options)
if destination_plugin.sync_as_source and (account_number not in src_accounts):
src_options = copy.deepcopy(plugins.get(destination_plugin.sync_as_source_name).options)
set_plugin_option('accountNumber', account_number, src_options)
create(label=dst.label,
plugin_name=destination_plugin.sync_as_source_name,
options=src_options,
description=dst.description)
return True
return False