Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #33 from metricso/master
Browse files Browse the repository at this point in the history
adds whitelist & blacklist config properties
  • Loading branch information
gbouyge authored Jul 31, 2017
2 parents f94a99c + 79a7773 commit 0b0cb28
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/Cassandra/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
14 changes: 14 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 10 additions & 0 deletions src/Tests/Fixtures/override-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
78 changes: 73 additions & 5 deletions src/Tests/Units/DependencyInjection/M6WebCassandraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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'])
Expand Down Expand Up @@ -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'])
Expand All @@ -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()
Expand All @@ -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'])
Expand Down Expand Up @@ -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()
;
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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'])
Expand Down

0 comments on commit 0b0cb28

Please sign in to comment.