diff --git a/README.md b/README.md index 3b84403..77549c1 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,16 @@ m6web_cassandra: default_pagesize: 10000 # ~ to disable pagination contact_endpoints: # required list of ip to contact - 127.0.0.1 + contact_whitelist: # if specified cassandra will connect only to this datacenters/hosts + dc: + - "testdc" + host: + - 172.0.0.1 + contact_blacklist: # if specified cassandra will not connect to this datacenters/hosts + dc: + - "blacklisted_testdc" + host: + - 6.6.6.6 port_endpoint: 9042 # cassandra port token_aware_routing: true # Enable or disable token aware routing credentials: # cassandra authentication diff --git a/src/Cassandra/Configurator.php b/src/Cassandra/Configurator.php index a685d17..437d44c 100644 --- a/src/Cassandra/Configurator.php +++ b/src/Cassandra/Configurator.php @@ -50,6 +50,16 @@ public static function buildCluster(Client $client) $cluster->withDatacenterAwareRoundRobinLoadBalancingPolicy($dcOption['local_dc_name'], $dcOption['host_per_remote_dc'], $dcOption['remote_dc_for_local_consistency']); } + if($opt = $config['contact_whitelist']) { + !$opt['host'] ?: $cluster->withWhiteListHosts(implode(', ', $opt['host'])); + !$opt['dc'] ?: $cluster->withWhiteListDCs(implode(', ', $opt['dc'])); + } + + if($opt = $config['contact_blacklist']) { + !$opt['host'] ?: $cluster->withBlackListHosts(implode(', ', $opt['host'])); + !$opt['dc'] ?: $cluster->withBlackListDCs(implode(', ', $opt['dc'])); + } + if (array_key_exists('credentials', $config)) { $cluster->withCredentials($config['credentials']['username'], $config['credentials']['password']); } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 0fa8a29..202150e 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -70,6 +70,20 @@ public function getConfigTreeBuilder() ->arrayNode('contact_endpoints')->isRequired()->requiresAtLeastOneElement()->performNoDeepMerging() ->prototype('scalar')->end() ->end() + ->arrayNode('contact_whitelist') + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('dc')->defaultValue([])->prototype('scalar')->end()->end() + ->arrayNode('host')->defaultValue([])->prototype('scalar')->end()->end() + ->end() + ->end() + ->arrayNode('contact_blacklist') + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('dc')->defaultValue([])->prototype('scalar')->end()->end() + ->arrayNode('host')->defaultValue([])->prototype('scalar')->end()->end() + ->end() + ->end() ->integerNode('port_endpoint')->defaultValue(9042)->end() ->booleanNode('token_aware_routing')->defaultValue(true)->end() ->arrayNode('credentials') diff --git a/src/Tests/Fixtures/override-config.yml b/src/Tests/Fixtures/override-config.yml index 0f19c8a..f877878 100644 --- a/src/Tests/Fixtures/override-config.yml +++ b/src/Tests/Fixtures/override-config.yml @@ -15,6 +15,16 @@ m6web_cassandra: - '127.0.0.1' - '127.0.0.2' - '127.0.0.3' + contact_whitelist: + dc: + - "testdc" + host: + - 172.0.0.1 + contact_blacklist: + dc: + - "blacklisted_testdc" + host: + - 6.6.6.6 port_endpoint: 8906 token_aware_routing : false credentials: diff --git a/src/Tests/Units/DependencyInjection/M6WebCassandraExtension.php b/src/Tests/Units/DependencyInjection/M6WebCassandraExtension.php index 19cd50d..f8f8ecb 100644 --- a/src/Tests/Units/DependencyInjection/M6WebCassandraExtension.php +++ b/src/Tests/Units/DependencyInjection/M6WebCassandraExtension.php @@ -28,7 +28,7 @@ public function testDefaultConfig() ->boolean($container->has('m6web_cassandra.client.client_test')) ->isTrue() ->array($arguments = $container->getDefinition('m6web_cassandra.client.client_test')->getArgument(0)) - ->hasSize(12) + ->hasSize(14) ->hasKeys($this->getDefaultConfigKeys()) ->notHasKeys(['default_timeout']) ->boolean($arguments['dispatch_events']) @@ -45,6 +45,18 @@ public function testDefaultConfig() ->isEqualTo('127.0.0.2') ->string($endpoints[2]) ->isEqualTo('127.0.0.3') + ->array($list = $arguments['contact_whitelist']) + ->hasSize(2) + ->array($list['dc']) + ->hasSize(0) + ->array($list['host']) + ->hasSize(0) + ->array($list = $arguments['contact_blacklist']) + ->hasSize(2) + ->array($list['dc']) + ->hasSize(0) + ->array($list['host']) + ->hasSize(0) ->string($arguments['load_balancing']) ->isEqualTo('round-robin') ->string($arguments['default_consistency']) @@ -83,7 +95,7 @@ public function testShouldGetADefaultNullKeyspaceWhenNoKeyspaceGiven() ->boolean($container->has('m6web_cassandra.client.client_test')) ->isTrue() ->array($arguments = $container->getDefinition('m6web_cassandra.client.client_test')->getArgument(0)) - ->hasSize(12) + ->hasSize(14) ->hasKeys($this->getDefaultConfigKeys()) ->notHasKeys(['default_timeout']) ->variable($arguments['keyspace']) @@ -103,7 +115,7 @@ public function testOverrideConfig() ->boolean($container->has('m6web_cassandra.client.client_test')) ->isTrue() ->array($arguments = $container->getDefinition('m6web_cassandra.client.client_test')->getArgument(0)) - ->hasSize(15) + ->hasSize(17) ->hasKeys($this->getDefaultConfigKeys(['credentials', 'default_timeout', 'dc_options'])) ->boolean($arguments['dispatch_events']) ->isFalse() @@ -119,6 +131,26 @@ public function testOverrideConfig() ->isEqualTo('127.0.0.2') ->string($endpoints[2]) ->isEqualTo('127.0.0.3') + ->array($list = $arguments['contact_whitelist']) + ->hasSize(2) + ->array($dc = $list['dc']) + ->hasSize(1) + ->string($dc[0]) + ->isEqualTo('testdc') + ->array($host = $list['host']) + ->hasSize(1) + ->string($host[0]) + ->isEqualTo('172.0.0.1') + ->array($list = $arguments['contact_blacklist']) + ->hasSize(2) + ->array($dc = $list['dc']) + ->hasSize(1) + ->string($dc[0]) + ->isEqualTo('blacklisted_testdc') + ->array($host = $list['host']) + ->hasSize(1) + ->string($host[0]) + ->isEqualTo('6.6.6.6') ->string($arguments['load_balancing']) ->isEqualTo('dc-aware-round-robin') ->array($dcOptions = $arguments['dc_options']) @@ -183,6 +215,18 @@ public function testOverrideDefaultEndPointsConfig() ->isEqualTo('127.0.0.5') ->string($endpoints[2]) ->isEqualTo('127.0.0.6') + ->array($list = $arguments['contact_whitelist']) + ->hasSize(2) + ->array($list['dc']) + ->hasSize(0) + ->array($list['host']) + ->hasSize(0) + ->array($list = $arguments['contact_blacklist']) + ->hasSize(2) + ->array($list['dc']) + ->hasSize(0) + ->array($list['host']) + ->hasSize(0) ->variable($arguments['default_pagesize']) ->isNull() ; @@ -200,7 +244,7 @@ public function testMulticlientsConfig() ->boolean($container->has('m6web_cassandra.client.client_test')) ->isTrue() ->array($arguments = $container->getDefinition('m6web_cassandra.client.client_test')->getArgument(0)) - ->hasSize(12) + ->hasSize(14) ->hasKeys($this->getDefaultConfigKeys()) ->boolean($arguments['dispatch_events']) ->isTrue() @@ -214,10 +258,22 @@ public function testMulticlientsConfig() ->isEqualTo('127.0.0.2') ->string($endpoints[2]) ->isEqualTo('127.0.0.3') + ->array($list = $arguments['contact_whitelist']) + ->hasSize(2) + ->array($list['dc']) + ->hasSize(0) + ->array($list['host']) + ->hasSize(0) + ->array($list = $arguments['contact_blacklist']) + ->hasSize(2) + ->array($list['dc']) + ->hasSize(0) + ->array($list['host']) + ->hasSize(0) ->boolean($container->has('m6web_cassandra.client.client_test2')) ->isTrue() ->array($arguments2 = $container->getDefinition('m6web_cassandra.client.client_test2')->getArgument(0)) - ->hasSize(14) + ->hasSize(16) ->hasKeys($this->getDefaultConfigKeys(['credentials', 'dc_options'])) ->boolean($arguments['dispatch_events']) ->isTrue() @@ -229,6 +285,18 @@ public function testMulticlientsConfig() ->isEqualTo('127.0.0.4') ->string($endpoints2[1]) ->isEqualTo('127.0.0.5') + ->array($list = $arguments['contact_whitelist']) + ->hasSize(2) + ->array($list['dc']) + ->hasSize(0) + ->array($list['host']) + ->hasSize(0) + ->array($list = $arguments['contact_blacklist']) + ->hasSize(2) + ->array($list['dc']) + ->hasSize(0) + ->array($list['host']) + ->hasSize(0) ->array($credentials = $arguments2['credentials']) ->hasSize(2) ->hasKeys(['username', 'password'])