Put flows in a subdirectory of each integration
This commit is contained in:
parent
6581d86cac
commit
11b35bab28
|
@ -1,4 +1,5 @@
|
||||||
import glob
|
import glob
|
||||||
|
import collections
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import io
|
import io
|
||||||
|
@ -9,6 +10,7 @@ from slugify import slugify
|
||||||
|
|
||||||
from .prompt import prompt
|
from .prompt import prompt
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
L = logging.getLogger(__name__)
|
L = logging.getLogger(__name__)
|
||||||
DEFAULT_BASE_URL = "https://api.integrator.io/v1/"
|
DEFAULT_BASE_URL = "https://api.integrator.io/v1/"
|
||||||
|
|
||||||
|
@ -76,9 +78,10 @@ class BackupCeligo(object):
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
self.session.headers.update(self.headers)
|
self.session.headers.update(self.headers)
|
||||||
|
|
||||||
def ensure_directories_exist(self):
|
def ensure_directories_exist(self, subdirs=None):
|
||||||
""" Make the directory if it doesn't exist """
|
""" Make the directory if it doesn't exist """
|
||||||
subdirs = ('imports', 'connections')
|
if not subdirs:
|
||||||
|
subdirs = ('integrations', 'connections')
|
||||||
|
|
||||||
for subdir in subdirs:
|
for subdir in subdirs:
|
||||||
_dir = os.path.join(self.data_dir, subdir)
|
_dir = os.path.join(self.data_dir, subdir)
|
||||||
|
@ -123,6 +126,17 @@ class BackupCeligo(object):
|
||||||
varable.
|
varable.
|
||||||
Once this is cached, save the imports.
|
Once this is cached, save the imports.
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
|
integrations = self._celigo_api_get("integrations/")
|
||||||
|
except requests.exceptions.RequestException:
|
||||||
|
L.info('HTTP Request failed')
|
||||||
|
raise
|
||||||
|
# Setup integrations dictionaries
|
||||||
|
for integration in integrations:
|
||||||
|
self.imports_cache[integration['_id']] = {
|
||||||
|
'name': integration['name'],
|
||||||
|
'slug': slugify(integration['name']),
|
||||||
|
'flows': []}
|
||||||
try:
|
try:
|
||||||
flows = self._celigo_api_get("flows/")
|
flows = self._celigo_api_get("flows/")
|
||||||
for flow in flows:
|
for flow in flows:
|
||||||
|
@ -131,10 +145,16 @@ class BackupCeligo(object):
|
||||||
L.info('HTTP Request failed')
|
L.info('HTTP Request failed')
|
||||||
raise
|
raise
|
||||||
L.info("Got all imports, writing now")
|
L.info("Got all imports, writing now")
|
||||||
L.info("We have imports for: %s", ", ".join(self.imports_cache.keys()))
|
|
||||||
|
|
||||||
for flow_name, (import_id, import_conf) in self.imports_cache.items():
|
for integration_id, integration in self.imports_cache.items():
|
||||||
self.save_import(flow_name, auto)
|
# Make sure the integration directories exist
|
||||||
|
base = "integrations/"+integration_id
|
||||||
|
subdirs = (base + "imports", base + "exports")
|
||||||
|
self.ensure_directories_exist(subdirs=subdirs)
|
||||||
|
|
||||||
|
# save the files
|
||||||
|
for flow in integration['flows']:
|
||||||
|
self.save_flow(integration_id, flow, auto)
|
||||||
|
|
||||||
def restore(self, auto=False):
|
def restore(self, auto=False):
|
||||||
"""
|
"""
|
||||||
|
@ -174,28 +194,35 @@ class BackupCeligo(object):
|
||||||
"""
|
"""
|
||||||
Stores the import in self.imports_cache before write.
|
Stores the import in self.imports_cache before write.
|
||||||
"""
|
"""
|
||||||
flow_name = slugify(flow['name'])
|
flow_name = flow['name']
|
||||||
import_id = flow['_importId']
|
import_id = flow['_importId']
|
||||||
|
integration_id = flow['_integrationId']
|
||||||
import_conf = self._celigo_api_get(
|
import_conf = self._celigo_api_get(
|
||||||
"imports/{id}/distributed".format(
|
"imports/{id}/distributed".format(id=import_id))
|
||||||
id=import_id))
|
|
||||||
|
|
||||||
self.imports_cache[flow_name] = (import_id, import_conf)
|
self.imports_cache[integration_id]['flows'].append({
|
||||||
|
"name": flow_name,
|
||||||
|
"id": import_id,
|
||||||
|
"configuration": import_conf
|
||||||
|
})
|
||||||
|
|
||||||
def save_import(self, flow_name, auto=False):
|
def save_flow(self, integration_id, flow, auto=False):
|
||||||
"""
|
"""
|
||||||
Write the import to a .json file with name_id.json format.
|
Write the import to a .json file with name_id.json format.
|
||||||
Prompt for overwrite.
|
Prompt for overwrite.
|
||||||
:param flow_name: the slugified name of the flow as a key
|
:param flow: dictionary of "name", "id", "configuration" for the
|
||||||
|
flow.
|
||||||
:param auto: if auto is true, don't prompt for overwrite
|
:param auto: if auto is true, don't prompt for overwrite
|
||||||
"""
|
"""
|
||||||
import_id, import_conf = self.imports_cache[flow_name]
|
flow_name = flow['name']
|
||||||
|
flow_id = flow['id']
|
||||||
filename = os.path.join(
|
flow_conf = flow['configuration']
|
||||||
self.data_dir,
|
|
||||||
"imports",
|
|
||||||
"%s_%s.json" % (flow_name, import_id))
|
|
||||||
|
|
||||||
|
filename = os.path.join(self.data_dir,
|
||||||
|
"integrations",
|
||||||
|
integration_id,
|
||||||
|
"imports",
|
||||||
|
"%s_%s.json" % (slugify(flow_name), flow_id))
|
||||||
write = True
|
write = True
|
||||||
|
|
||||||
# By default, we prompt for overwrites
|
# By default, we prompt for overwrites
|
||||||
|
@ -209,7 +236,7 @@ class BackupCeligo(object):
|
||||||
write = bool(overwrite == "Yes")
|
write = bool(overwrite == "Yes")
|
||||||
|
|
||||||
if write:
|
if write:
|
||||||
self.write_json(filename, import_conf)
|
self.write_json(filename, flow_conf)
|
||||||
else:
|
else:
|
||||||
L.info("You chose not to save this file.")
|
L.info("You chose not to save this file.")
|
||||||
|
|
||||||
|
|
8
tests.py
8
tests.py
|
@ -48,9 +48,10 @@ class CeligoTest(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
Test that the ensure_directories_exist works properly.
|
Test that the ensure_directories_exist works properly.
|
||||||
"""
|
"""
|
||||||
|
fake_id = "fake_integration_id"
|
||||||
tempdir = tempfile.mkdtemp('celigo_testing')
|
tempdir = tempfile.mkdtemp('celigo_testing')
|
||||||
bc = celigo.BackupCeligo(tempdir)
|
bc = celigo.BackupCeligo(tempdir)
|
||||||
imports_dir = os.path.join(tempdir, "imports")
|
imports_dir = os.path.join(tempdir, fake_id, "imports")
|
||||||
connections_dir = os.path.join(tempdir, "connections")
|
connections_dir = os.path.join(tempdir, "connections")
|
||||||
# Check that the directories don't exist already.
|
# Check that the directories don't exist already.
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
|
@ -59,8 +60,9 @@ class CeligoTest(unittest.TestCase):
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
os.path.exists(connections_dir),
|
os.path.exists(connections_dir),
|
||||||
"connections dir exists")
|
"connections dir exists")
|
||||||
|
|
||||||
# Make the directories.
|
# Make the directories.
|
||||||
bc.ensure_directories_exist()
|
bc.ensure_directories_exist((imports_dir, connections_dir))
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
os.path.exists(imports_dir),
|
os.path.exists(imports_dir),
|
||||||
"Did not create proper directory"
|
"Did not create proper directory"
|
||||||
|
@ -71,7 +73,7 @@ class CeligoTest(unittest.TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Make sure nothing errors if the directories exist already.
|
# Make sure nothing errors if the directories exist already.
|
||||||
bc.ensure_directories_exist()
|
bc.ensure_directories_exist((imports_dir, connections_dir))
|
||||||
|
|
||||||
# @requests_mock.Mocker()
|
# @requests_mock.Mocker()
|
||||||
# def test_fake_requests(self, rqm):
|
# def test_fake_requests(self, rqm):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user