From 239867963e47b44609e168135750591eb7ea31dc Mon Sep 17 00:00:00 2001 From: Adam Palay Date: Tue, 20 Sep 2016 12:13:24 -0400 Subject: [PATCH] set neo4j configuration at the command line --- .../management/commands/dump_to_neo4j.py | 40 +++++++++++++++---- .../commands/tests/test_dump_to_neo4j.py | 26 +++++++----- lms/envs/aws.py | 4 -- lms/envs/common.py | 5 --- lms/envs/test.py | 10 ----- 5 files changed, 47 insertions(+), 38 deletions(-) diff --git a/lms/djangoapps/courseware/management/commands/dump_to_neo4j.py b/lms/djangoapps/courseware/management/commands/dump_to_neo4j.py index cf5fc8d3b9..3de7b7862e 100644 --- a/lms/djangoapps/courseware/management/commands/dump_to_neo4j.py +++ b/lms/djangoapps/courseware/management/commands/dump_to_neo4j.py @@ -139,7 +139,22 @@ class ModuleStoreSerializer(object): class Command(BaseCommand): """ Command to dump modulestore data to neo4j + + Takes the following named arguments: + host: the host of the neo4j server + port: the port on the server that accepts https requests + user: the username for the neo4j user + password: the user's password + + Example usage: + python manage.py lms dump_to_neo4j --host localhost --port 7473 \ + --user user --password password --settings=aws """ + def add_arguments(self, parser): + parser.add_argument('--host', type=unicode) + parser.add_argument('--port', type=int) + parser.add_argument('--user', type=unicode) + parser.add_argument('--password', type=unicode) @staticmethod def add_to_transaction(neo4j_entities, transaction): @@ -156,16 +171,25 @@ class Command(BaseCommand): Iterates through each course, serializes them into graphs, and saves those graphs to neo4j. """ - # first, make sure that there's a valid neo4j configuration - if settings.NEO4J_CONFIG is None: - raise CommandError( - "No neo4j configuration (NEO4J_CONFIG) defined in lms.auth.json." - ) + host = options['host'] + port = options['port'] + neo4j_user = options['user'] + neo4j_password = options['password'] - auth_params = ["{host}:{https_port}", "{user}", "{password}"] - authenticate(*[param.format(**settings.NEO4J_CONFIG) for param in auth_params]) + authenticate( + "{host}:{port}".format(host=host, port=port), + neo4j_user, + neo4j_password, + ) - graph = Graph(**settings.NEO4J_CONFIG) + graph = Graph( + bolt=True, + password=neo4j_password, + user=neo4j_user, + https_port=port, + host=host, + secure=True + ) mss = ModuleStoreSerializer() diff --git a/lms/djangoapps/courseware/management/commands/tests/test_dump_to_neo4j.py b/lms/djangoapps/courseware/management/commands/tests/test_dump_to_neo4j.py index 0d7aea4d59..1afb2e39a7 100644 --- a/lms/djangoapps/courseware/management/commands/tests/test_dump_to_neo4j.py +++ b/lms/djangoapps/courseware/management/commands/tests/test_dump_to_neo4j.py @@ -36,6 +36,7 @@ class TestDumpToNeo4jCommandBase(SharedModuleStoreTestCase): cls.course2 = CourseFactory.create() +@ddt.ddt class TestDumpToNeo4jCommand(TestDumpToNeo4jCommandBase): """ Tests for the dump to neo4j management command @@ -50,7 +51,13 @@ class TestDumpToNeo4jCommand(TestDumpToNeo4jCommandBase): mock_transaction = mock.Mock() mock_graph.begin.return_value = mock_transaction - call_command('dump_to_neo4j') + call_command( + 'dump_to_neo4j', + host='mock_host', + port=7473, + user='mock_user', + password='mock_password', + ) self.assertEqual(mock_graph.begin.call_count, 2) self.assertEqual(mock_transaction.commit.call_count, 2) @@ -72,21 +79,18 @@ class TestDumpToNeo4jCommand(TestDumpToNeo4jCommandBase): mock_graph.begin.return_value = mock_transaction mock_transaction.run.side_effect = ValueError('Something went wrong!') - call_command('dump_to_neo4j') + call_command( + 'dump_to_neo4j', + host='mock_host', + port=7473, + user='mock_user', + password='mock_password', + ) self.assertEqual(mock_graph.begin.call_count, 2) self.assertEqual(mock_transaction.commit.call_count, 0) self.assertEqual(mock_transaction.rollback.call_count, 2) - @mock.patch('django.conf.settings.NEO4J_CONFIG', None) - def test_dump_to_neo4j_no_config(self): - """ - Tests that the command errors out if there isn't a configuration - file found - """ - with self.assertRaises(CommandError): - call_command('dump_to_neo4j') - @ddt.ddt class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase): diff --git a/lms/envs/aws.py b/lms/envs/aws.py index bc52bb4ed6..c2c03d425e 100644 --- a/lms/envs/aws.py +++ b/lms/envs/aws.py @@ -871,10 +871,6 @@ APP_UPGRADE_CACHE_TIMEOUT = ENV_TOKENS.get('APP_UPGRADE_CACHE_TIMEOUT', APP_UPGR AFFILIATE_COOKIE_NAME = ENV_TOKENS.get('AFFILIATE_COOKIE_NAME', AFFILIATE_COOKIE_NAME) -############## Settings for Neo4j ############################ - -NEO4J_CONFIG = AUTH_TOKENS.get('NEO4J_CONFIG') - ############## Settings for LMS Context Sensitive Help ############## DOC_LINK_BASE_URL = ENV_TOKENS.get('DOC_LINK_BASE_URL', DOC_LINK_BASE_URL) diff --git a/lms/envs/common.py b/lms/envs/common.py index 47adc782f9..1a9a39e1ca 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -2979,11 +2979,6 @@ AFFILIATE_COOKIE_NAME = 'affiliate_id' REDIRECT_CACHE_TIMEOUT = None # The length of time we cache Redirect model data REDIRECT_CACHE_KEY_PREFIX = 'redirects' -############## Settings for Neo4j ############################ - -# This should be set in configuration -NEO4J_CONFIG = None - ############## Settings for LMS Context Sensitive Help ############## DOC_LINK_BASE_URL = None diff --git a/lms/envs/test.py b/lms/envs/test.py index 6d5781b91d..6c0e373ae5 100644 --- a/lms/envs/test.py +++ b/lms/envs/test.py @@ -592,13 +592,3 @@ COMPREHENSIVE_THEME_DIRS = [REPO_ROOT / "themes", REPO_ROOT / "common/test"] COMPREHENSIVE_THEME_LOCALE_PATHS = [REPO_ROOT / "themes/conf/locale", ] LMS_ROOT_URL = "http://localhost:8000" - -# Test configuration for neo4j -NEO4J_CONFIG = { - 'bolt': True, - 'password': 'password', - 'user': 'neo4j', - 'https_port': 7473, - 'host': 'localhost', - 'secure': True, -}