From 3cfeedbe4dbaaea8efd38ac4e4d740a44fc977a7 Mon Sep 17 00:00:00 2001 From: Martijn Smidt Date: Mon, 13 May 2019 15:22:20 +0200 Subject: [PATCH 1/3] Added loadbalancers class and tests --- src/Configurations/LoadBalancer.php | 178 ++++++++++++++++++ src/Endpoints/LoadBalancers.php | 149 +++++++++++++++ tests/Configurations/LoadBalancerTest.php | 57 ++++++ tests/Endpoints/LoadBalancersTest.php | 128 +++++++++++++ .../Endpoints/createLoadBalancer.json | 46 +++++ .../Endpoints/deleteLoadBalancer.json | 8 + .../Endpoints/getLoadBalancerDetails.json | 46 +++++ .../Fixtures/Endpoints/listLoadbalancers.json | 54 ++++++ .../Endpoints/updateLoadBalancer.json | 46 +++++ 9 files changed, 712 insertions(+) create mode 100644 src/Configurations/LoadBalancer.php create mode 100644 src/Endpoints/LoadBalancers.php create mode 100644 tests/Configurations/LoadBalancerTest.php create mode 100644 tests/Endpoints/LoadBalancersTest.php create mode 100644 tests/Fixtures/Endpoints/createLoadBalancer.json create mode 100644 tests/Fixtures/Endpoints/deleteLoadBalancer.json create mode 100644 tests/Fixtures/Endpoints/getLoadBalancerDetails.json create mode 100644 tests/Fixtures/Endpoints/listLoadbalancers.json create mode 100644 tests/Fixtures/Endpoints/updateLoadBalancer.json diff --git a/src/Configurations/LoadBalancer.php b/src/Configurations/LoadBalancer.php new file mode 100644 index 00000000..150a88d8 --- /dev/null +++ b/src/Configurations/LoadBalancer.php @@ -0,0 +1,178 @@ + + * User: HemeraOne + * Date: 13/05/2019 + */ + +namespace Cloudflare\API\Configurations; + +class LoadBalancer implements Configurations +{ + private $configs = []; + + public function __construct(string $name, array $defaultPools, string $fallbackPool) + { + $this->setName($name); + $this->setDefaultPools($defaultPools); + $this->setFallbackPool($fallbackPool); + } + + public function setName(string $name) + { + $this->configs['name'] = $name; + } + + public function getName():string + { + return $this->configs['name'] ?? ''; + } + + public function setDefaultPools(array $default_pools) + { + $this->configs['default_pools'] = $default_pools; + } + + public function getDefaultPools():array + { + return $this->configs['default_pools'] ?? []; + } + + public function setFallbackPool(string $fallbackPool) + { + $this->configs['fallback_pools'] = $fallbackPool; + } + + public function getFallbackPool():string + { + return $this->configs['fallback_pools'] ?? ''; + } + + public function setSteeringPolicy(string $steeringPolicy = '') + { + $allowedOptions = ['off', 'geo', 'random', 'dynamic_latency', '']; + if (!in_array($steeringPolicy, $allowedOptions)) { + throw new ConfigurationsException('Given steering policy value is not a valid option, valid options are: ' . implode(', ', $allowedOptions)); + } + + $this->configs['steering_policy'] = $steeringPolicy; + } + + public function getSteeringPolicy():string + { + return $this->configs['steering_policy'] ?? ''; + } + + public function enable() + { + $this->configs['enabled'] = true; + } + + public function isEnabled():bool + { + return $this->configs['enabled'] ?? true; + } + + public function disable() + { + $this->configs['enabled'] = false; + } + + public function isDisabled():bool + { + return !$this->configs['enabled'] ?? false; + } + + public function setEnabled(bool $enabled = true) + { + $this->configs['enabled'] = $enabled; + } + + public function getEnabled(bool $enabled = true):bool + { + return $this->configs['enabled'] ?? true; + } + + public function setPopPools(array $popPools) + { + $this->configs['pop_pools'] = $popPools; + } + + public function getPopPools():array + { + return $this->configs['pop_pools'] ?? []; + } + + public function setTtl(int $ttl) + { + $this->configs['ttl'] = $ttl; + } + + public function getTtl():int + { + return $this->configs['ttl'] ?? 30; + } + + public function setRegionPools(array $regionPools) + { + $this->configs['region_pools'] = $regionPools; + } + + public function getRegionPools():array + { + return $this->configs['region_pools'] ?? []; + } + + public function setSessionAffinity(string $sessionAffinity = '') + { + $allowedOptions = ['none', 'cookie', 'ip_cookie', '']; + if (!in_array($sessionAffinity, $allowedOptions)) { + throw new ConfigurationsException('Given session affinity value is not a valid option, valid options are: ' . implode(', ', $allowedOptions)); + } + $this->configs['session_affinity'] = $sessionAffinity; + } + + public function getSessionAffinity():string + { + return $this->configs['session_affinity'] ?? ''; + } + + public function setDescription(string $description = '') + { + $this->configs['description'] = $description; + } + + public function getDescription():string + { + return $this->configs['description'] ?? ''; + } + + public function setProxied(bool $proxied = true) + { + $this->configs['proxied'] = $proxied; + } + + public function isProxied():bool + { + return $this->configs['proxied'] ?? true; + } + + public function setSessionAffinityTtl(int $sessionAffinityTtl = 82800) + { + if ($sessionAffinityTtl > 604800 || $sessionAffinityTtl < 1800) { + throw new ConfigurationsException('The value of session affinity ttl must be between 1800 and 604800'); + } + + $this->configs['session_affinity_ttl'] = $sessionAffinityTtl; + } + + public function getSessionAffinityTtl():int + { + return $this->configs['session_affinity_ttl'] ?? 82800; + } + + public function getArray(): array + { + return $this->configs; + } +} diff --git a/src/Endpoints/LoadBalancers.php b/src/Endpoints/LoadBalancers.php new file mode 100644 index 00000000..0ed43ab6 --- /dev/null +++ b/src/Endpoints/LoadBalancers.php @@ -0,0 +1,149 @@ + + * User: HemeraOne + * Date: 13/05/2019 + */ + +namespace Cloudflare\API\Endpoints; + +use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Configurations\ConfigurationsException; +use Cloudflare\API\Configurations\LoadBalancer; +use Cloudflare\API\Traits\BodyAccessorTrait; + +class LoadBalancers implements API +{ + use BodyAccessorTrait; + + private $adapter; + + public function __construct(Adapter $adapter) + { + $this->adapter = $adapter; + } + + /** + * @param string $zoneID + * @return mixed + */ + public function listLoadBalancers(string $zoneID) + { + $loadBalancers = $this->adapter->get('zones/' . $zoneID . '/load_balancers'); + $this->body = json_decode($loadBalancers->getBody()); + + return $this->body->result; + } + + /** + * @param string $zoneID + * @param string $loadBalancerID + * @return mixed + */ + public function getLoadBalancerDetails(string $zoneID, string $loadBalancerID) + { + $loadBalancer = $this->adapter->get('zones/' . $zoneID . '/load_balancers/' . $loadBalancerID); + $this->body = json_decode($loadBalancer->getBody()); + return $this->body->result; + } + + /** + * @param string $zoneID + * @param string $loadBalancerID + * @return LoadBalancer + * @throws ConfigurationsException + */ + public function getLoadBalancerConfiguration(string $zoneID, string $loadBalancerID) + { + $loadBalancer = $this->getLoadBalancerDetails($zoneID, $loadBalancerID); + + $loadBalancerConfiguration = new LoadBalancer($loadBalancer->name, $loadBalancer->default_pools, $loadBalancer->fallback_pool); + $loadBalancerConfiguration->setSteeringPolicy($loadBalancer->steering_policy); + $loadBalancerConfiguration->setEnabled($loadBalancer->enabled); + + if (is_array($loadBalancer->pop_pools)) { + $loadBalancerConfiguration->setPopPools($loadBalancer->pop_pools); + } + + if (isset($loadBalancer->ttl)) { + $loadBalancerConfiguration->setTtl($loadBalancer->ttl); + } + + if (is_array($loadBalancer->region_pools)) { + $loadBalancerConfiguration->setRegionPools($loadBalancer->region_pools); + } + $loadBalancerConfiguration->setSessionAffinity($loadBalancer->session_affinity); + $loadBalancerConfiguration->setDescription($loadBalancer->description); + $loadBalancerConfiguration->setProxied($loadBalancer->proxied); + + if (isset($loadBalancer->session_affinity_ttl)) { + $loadBalancerConfiguration->setSessionAffinityTtl($loadBalancer->session_affinity_ttl); + } + + return $loadBalancerConfiguration; + } + + /** + * @param string $zoneID + * @param string $loadBalancerID + * @param LoadBalancer $loadBalancerConfiguration + * @return bool + */ + public function updateLoadBalancer( + string $zoneID, + string $loadBalancerID, + LoadBalancer $loadBalancerConfiguration + ): bool { + $options = $loadBalancerConfiguration->getArray(); + + $query = $this->adapter->patch('zones/' . $zoneID . '/load_balancers/' . $loadBalancerID, $options); + + $this->body = json_decode($query->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } + + /** + * @param string $zoneID + * @param LoadBalancer $loadBalancerConfiguration + * @return bool + */ + public function createLoadBalancer( + string $zoneID, + LoadBalancer $loadBalancerConfiguration + ): bool { + $options = $loadBalancerConfiguration->getArray(); + + $query = $this->adapter->post('zones/' . $zoneID . '/load_balancers', $options); + + $this->body = json_decode($query->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } + + /** + * @param string $zoneID + * @param string $loadBalancerID + * @return bool + */ + public function deleteLoadBalancer(string $zoneID, string $loadBalancerID): bool + { + $loadBalancer = $this->adapter->delete('zones/' . $zoneID . '/load_balancers/' . $loadBalancerID); + + $this->body = json_decode($loadBalancer->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } +} diff --git a/tests/Configurations/LoadBalancerTest.php b/tests/Configurations/LoadBalancerTest.php new file mode 100644 index 00000000..e726cd87 --- /dev/null +++ b/tests/Configurations/LoadBalancerTest.php @@ -0,0 +1,57 @@ + + * User: HemeraOne + * Date: 13/05/2019 + */ + +use Cloudflare\API\Configurations\ConfigurationsException; +use Cloudflare\API\Configurations\LoadBalancer; + +class LoadBalancerTest extends TestCase +{ + /** + * @dataProvider testArgumentsDataProvider + */ + public function testArguments($setFunction, $arguments, $getFunction, $invalid) + { + $lb = new LoadBalancer('bous', [], 'bogus'); + foreach ($arguments as $argument) { + if ($invalid) { + try { + $lb->{$setFunction}($argument); + } catch (ConfigurationsException $e) { + $this->assertNotEquals($argument, $lb->{$getFunction}()); + } + } else { + $lb->{$setFunction}($argument); + $this->assertEquals($argument, $lb->{$getFunction}()); + } + + } + } + + public function testArgumentsDataProvider() + { + return [ + 'steeringPolicy arguments valid' => [ + 'setSteeringPolicy', ['off', 'geo', 'random', 'dynamic_latency', ''], 'getSteeringPolicy', false + ], + 'sessionAffinity arguments valid' => [ + 'setSessionAffinity', ['none', 'cookie', 'ip_cookie', ''], 'getSessionAffinity', false + ], + 'sessionAffinityTtl arguments valid' => [ + 'setSessionAffinityTtl', [3600], 'getSessionAffinityTtl', false + ], + 'steeringPolicy arguments invalid' => [ + 'setSteeringPolicy', ['invalid'], 'getSteeringPolicy', true + ], + 'sessionAffinity arguments invalid' => [ + 'setSessionAffinity', ['invalid'], 'getSessionAffinity', true + ], + 'sessionAffinityTtl arguments invalid' => [ + 'setSessionAffinityTtl', [1337], 'getSessionAffinityTtl', true + ], + ]; + } +} diff --git a/tests/Endpoints/LoadBalancersTest.php b/tests/Endpoints/LoadBalancersTest.php new file mode 100644 index 00000000..a0a87b26 --- /dev/null +++ b/tests/Endpoints/LoadBalancersTest.php @@ -0,0 +1,128 @@ + + * User: HemeraOne + * Date: 13/05/2019 + */ + +class LoadBalancersTest extends TestCase +{ + public function testCreateLoadBalancer() + { + $pools = [ + '17b5962d775c646f3f9725cbc7a53df4', + '9290f38c5d07c2e2f4df57b1f61d4196', + '00920f38ce07c2e2f4df50b1f61d4194' + ]; + + $lbConfiguration = new LoadBalancer('www.example.com', $pools, '17b5962d775c646f3f9725cbc7a53df4'); + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/createLoadBalancer.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers'), + $lbConfiguration->getArray() + ); + + $loadBalancers = new LoadBalancers($mock); + $result = $loadBalancers->createLoadBalancer('699d98642c564d2e855e9661899b7252', $lbConfiguration); + + $this->assertTrue($result); + $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id); + } + + public function testListLoadBalancer() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/listLoadBalancers.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers') + ); + + $loadBalancers = new LoadBalancers($mock); + $loadBalancers->listLoadBalancers('699d98642c564d2e855e9661899b7252'); + $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result[0]->id); + } + + public function testGetLoadBalancerDetails() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getLoadBalancerDetails.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252') + ); + + $loadBalancers = new LoadBalancers($mock); + $loadBalancers->getLoadBalancerDetails('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252'); + $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id); + } + + public function testUpdateLoadBalancer() + { + $pools = [ + '17b5962d775c646f3f9725cbc7a53df4', + '9290f38c5d07c2e2f4df57b1f61d4196', + '00920f38ce07c2e2f4df50b1f61d4194' + ]; + + $lbConfiguration = new LoadBalancer('www.example.com', $pools, '17b5962d775c646f3f9725cbc7a53df4'); + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateLoadBalancer.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252'), + $this->equalTo($lbConfiguration->getArray()) + ); + + $loadBalancers = new LoadBalancers($mock); + $result = $loadBalancers->updateLoadBalancer('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252', $lbConfiguration); + + $this->assertTrue($result); + $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id); + } + + public function testDeleteLoadBalancer() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteLoadBalancer.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252') + ); + + $loadBalancers = new LoadBalancers($mock); + $result = $loadBalancers->deleteLoadBalancer('699d98642c564d2e855e9661899b7252', '699d98642c564d2e855e9661899b7252'); + + $this->assertTrue($result); + $this->assertEquals('699d98642c564d2e855e9661899b7252', $loadBalancers->getBody()->result->id); + } +} diff --git a/tests/Fixtures/Endpoints/createLoadBalancer.json b/tests/Fixtures/Endpoints/createLoadBalancer.json new file mode 100644 index 00000000..3316cdd5 --- /dev/null +++ b/tests/Fixtures/Endpoints/createLoadBalancer.json @@ -0,0 +1,46 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "699d98642c564d2e855e9661899b7252", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "description": "Load Balancer for www.example.com", + "name": "www.example.com", + "enabled": true, + "ttl": 30, + "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4", + "default_pools": [ + "17b5962d775c646f3f9725cbc7a53df4", + "9290f38c5d07c2e2f4df57b1f61d4196", + "00920f38ce07c2e2f4df50b1f61d4194" + ], + "region_pools": { + "WNAM": [ + "de90f38ced07c2e2f4df50b1f61d4194", + "9290f38c5d07c2e2f4df57b1f61d4196" + ], + "ENAM": [ + "00920f38ce07c2e2f4df50b1f61d4194" + ] + }, + "pop_pools": { + "LAX": [ + "de90f38ced07c2e2f4df50b1f61d4194", + "9290f38c5d07c2e2f4df57b1f61d4196" + ], + "LHR": [ + "abd90f38ced07c2e2f4df50b1f61d4194", + "f9138c5d07c2e2f4df57b1f61d4196" + ], + "SJC": [ + "00920f38ce07c2e2f4df50b1f61d4194" + ] + }, + "proxied": true, + "steering_policy": "dynamic_latency", + "session_affinity": "cookie", + "session_affinity_ttl": 5000 + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/deleteLoadBalancer.json b/tests/Fixtures/Endpoints/deleteLoadBalancer.json new file mode 100644 index 00000000..77440449 --- /dev/null +++ b/tests/Fixtures/Endpoints/deleteLoadBalancer.json @@ -0,0 +1,8 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "699d98642c564d2e855e9661899b7252" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getLoadBalancerDetails.json b/tests/Fixtures/Endpoints/getLoadBalancerDetails.json new file mode 100644 index 00000000..3316cdd5 --- /dev/null +++ b/tests/Fixtures/Endpoints/getLoadBalancerDetails.json @@ -0,0 +1,46 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "699d98642c564d2e855e9661899b7252", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "description": "Load Balancer for www.example.com", + "name": "www.example.com", + "enabled": true, + "ttl": 30, + "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4", + "default_pools": [ + "17b5962d775c646f3f9725cbc7a53df4", + "9290f38c5d07c2e2f4df57b1f61d4196", + "00920f38ce07c2e2f4df50b1f61d4194" + ], + "region_pools": { + "WNAM": [ + "de90f38ced07c2e2f4df50b1f61d4194", + "9290f38c5d07c2e2f4df57b1f61d4196" + ], + "ENAM": [ + "00920f38ce07c2e2f4df50b1f61d4194" + ] + }, + "pop_pools": { + "LAX": [ + "de90f38ced07c2e2f4df50b1f61d4194", + "9290f38c5d07c2e2f4df57b1f61d4196" + ], + "LHR": [ + "abd90f38ced07c2e2f4df50b1f61d4194", + "f9138c5d07c2e2f4df57b1f61d4196" + ], + "SJC": [ + "00920f38ce07c2e2f4df50b1f61d4194" + ] + }, + "proxied": true, + "steering_policy": "dynamic_latency", + "session_affinity": "cookie", + "session_affinity_ttl": 5000 + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/listLoadbalancers.json b/tests/Fixtures/Endpoints/listLoadbalancers.json new file mode 100644 index 00000000..47a3b12c --- /dev/null +++ b/tests/Fixtures/Endpoints/listLoadbalancers.json @@ -0,0 +1,54 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "699d98642c564d2e855e9661899b7252", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "description": "Load Balancer for www.example.com", + "name": "www.example.com", + "enabled": true, + "ttl": 30, + "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4", + "default_pools": [ + "17b5962d775c646f3f9725cbc7a53df4", + "9290f38c5d07c2e2f4df57b1f61d4196", + "00920f38ce07c2e2f4df50b1f61d4194" + ], + "region_pools": { + "WNAM": [ + "de90f38ced07c2e2f4df50b1f61d4194", + "9290f38c5d07c2e2f4df57b1f61d4196" + ], + "ENAM": [ + "00920f38ce07c2e2f4df50b1f61d4194" + ] + }, + "pop_pools": { + "LAX": [ + "de90f38ced07c2e2f4df50b1f61d4194", + "9290f38c5d07c2e2f4df57b1f61d4196" + ], + "LHR": [ + "abd90f38ced07c2e2f4df50b1f61d4194", + "f9138c5d07c2e2f4df57b1f61d4196" + ], + "SJC": [ + "00920f38ce07c2e2f4df50b1f61d4194" + ] + }, + "proxied": true, + "steering_policy": "dynamic_latency", + "session_affinity": "cookie", + "session_affinity_ttl": 5000 + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 2000 + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateLoadBalancer.json b/tests/Fixtures/Endpoints/updateLoadBalancer.json new file mode 100644 index 00000000..3316cdd5 --- /dev/null +++ b/tests/Fixtures/Endpoints/updateLoadBalancer.json @@ -0,0 +1,46 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "699d98642c564d2e855e9661899b7252", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "description": "Load Balancer for www.example.com", + "name": "www.example.com", + "enabled": true, + "ttl": 30, + "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4", + "default_pools": [ + "17b5962d775c646f3f9725cbc7a53df4", + "9290f38c5d07c2e2f4df57b1f61d4196", + "00920f38ce07c2e2f4df50b1f61d4194" + ], + "region_pools": { + "WNAM": [ + "de90f38ced07c2e2f4df50b1f61d4194", + "9290f38c5d07c2e2f4df57b1f61d4196" + ], + "ENAM": [ + "00920f38ce07c2e2f4df50b1f61d4194" + ] + }, + "pop_pools": { + "LAX": [ + "de90f38ced07c2e2f4df50b1f61d4194", + "9290f38c5d07c2e2f4df57b1f61d4196" + ], + "LHR": [ + "abd90f38ced07c2e2f4df50b1f61d4194", + "f9138c5d07c2e2f4df57b1f61d4196" + ], + "SJC": [ + "00920f38ce07c2e2f4df50b1f61d4194" + ] + }, + "proxied": true, + "steering_policy": "dynamic_latency", + "session_affinity": "cookie", + "session_affinity_ttl": 5000 + } +} \ No newline at end of file From 99c174bcb3b3d24f59a03d9bb5135dccee40c8a4 Mon Sep 17 00:00:00 2001 From: Martijn Smidt Date: Mon, 13 May 2019 16:49:32 +0200 Subject: [PATCH 2/3] Added pools class and tests Small changes in loadbalancer configuration --- src/Configurations/LoadBalancer.php | 20 +-- src/Configurations/Pool.php | 111 +++++++++++++ src/Endpoints/LoadBalancers.php | 44 ++--- src/Endpoints/Pools.php | 152 ++++++++++++++++++ tests/Configurations/LoadBalancerTest.php | 15 +- tests/Configurations/PoolTest.php | 53 ++++++ tests/Endpoints/LoadBalancersTest.php | 4 +- tests/Endpoints/PoolsTest.php | 133 +++++++++++++++ tests/Fixtures/Endpoints/createPool.json | 28 ++++ tests/Fixtures/Endpoints/deletePool.json | 8 + tests/Fixtures/Endpoints/getPoolDetails.json | 28 ++++ ...dbalancers.json => listLoadBalancers.json} | 0 tests/Fixtures/Endpoints/listPools.json | 36 +++++ tests/Fixtures/Endpoints/updatePool.json | 28 ++++ 14 files changed, 622 insertions(+), 38 deletions(-) create mode 100644 src/Configurations/Pool.php create mode 100644 src/Endpoints/Pools.php create mode 100644 tests/Configurations/PoolTest.php create mode 100644 tests/Endpoints/PoolsTest.php create mode 100644 tests/Fixtures/Endpoints/createPool.json create mode 100644 tests/Fixtures/Endpoints/deletePool.json create mode 100644 tests/Fixtures/Endpoints/getPoolDetails.json rename tests/Fixtures/Endpoints/{listLoadbalancers.json => listLoadBalancers.json} (100%) create mode 100644 tests/Fixtures/Endpoints/listPools.json create mode 100644 tests/Fixtures/Endpoints/updatePool.json diff --git a/src/Configurations/LoadBalancer.php b/src/Configurations/LoadBalancer.php index 150a88d8..70b2da1f 100644 --- a/src/Configurations/LoadBalancer.php +++ b/src/Configurations/LoadBalancer.php @@ -28,9 +28,9 @@ public function getName():string return $this->configs['name'] ?? ''; } - public function setDefaultPools(array $default_pools) + public function setDefaultPools(array $defaultPools) { - $this->configs['default_pools'] = $default_pools; + $this->configs['default_pools'] = $defaultPools; } public function getDefaultPools():array @@ -83,12 +83,7 @@ public function isDisabled():bool return !$this->configs['enabled'] ?? false; } - public function setEnabled(bool $enabled = true) - { - $this->configs['enabled'] = $enabled; - } - - public function getEnabled(bool $enabled = true):bool + public function getEnabled():bool { return $this->configs['enabled'] ?? true; } @@ -147,9 +142,14 @@ public function getDescription():string return $this->configs['description'] ?? ''; } - public function setProxied(bool $proxied = true) + public function enableProxied() + { + $this->configs['proxied'] = true; + } + + public function disableProxied() { - $this->configs['proxied'] = $proxied; + $this->configs['proxied'] = false; } public function isProxied():bool diff --git a/src/Configurations/Pool.php b/src/Configurations/Pool.php new file mode 100644 index 00000000..f28347b3 --- /dev/null +++ b/src/Configurations/Pool.php @@ -0,0 +1,111 @@ + + * User: HemeraOne + * Date: 13/05/2019 + */ + +namespace Cloudflare\API\Configurations; + +class Pool implements Configurations +{ + private $configs = []; + + public function __construct(string $name, array $origins) + { + $this->setName($name); + $this->setOrigins($origins); + } + + public function setName(string $name) + { + $this->configs['name'] = $name; + } + + public function getName():string + { + return $this->configs['name'] ?? ''; + } + + public function setOrigins(array $origins) + { + foreach ($origins as $origin) { + if (!isset($origin['name'])) { + throw new ConfigurationsException('name is required for origin'); + } + if (!isset($origin['address'])) { + throw new ConfigurationsException('address is required for origin'); + } + } + $this->configs['origins'] = $origins; + } + + public function getOrigins():array + { + return $this->configs['origins'] ?? []; + } + + public function setDescription(string $description = '') + { + $this->configs['description'] = $description; + } + + public function getDescription():string + { + return $this->configs['description'] ?? ''; + } + + public function enable() + { + $this->configs['enabled'] = true; + } + + public function isEnabled():bool + { + return $this->configs['enabled'] ?? true; + } + + public function disable() + { + $this->configs['enabled'] = false; + } + + public function isDisabled():bool + { + return !$this->configs['enabled'] ?? false; + } + + public function getEnabled():bool + { + return $this->configs['enabled'] ?? true; + } + + public function setMonitor(string $monitor) + { + $this->configs['monitor'] = $monitor; + } + + public function getMonitor():string + { + return $this->configs['monitor'] ?? ''; + } + + public function setNotificationEmail(string $email) + { + if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { + throw new ConfigurationsException('Invalid notification email given'); + } + + $this->configs['notification_email'] = $email; + } + + public function getNotificationEmail():string + { + return $this->configs['notification_email'] ?? ''; + } + + public function getArray(): array + { + return $this->configs; + } +} diff --git a/src/Endpoints/LoadBalancers.php b/src/Endpoints/LoadBalancers.php index 0ed43ab6..7813d313 100644 --- a/src/Endpoints/LoadBalancers.php +++ b/src/Endpoints/LoadBalancers.php @@ -57,46 +57,54 @@ public function getLoadBalancerConfiguration(string $zoneID, string $loadBalance { $loadBalancer = $this->getLoadBalancerDetails($zoneID, $loadBalancerID); - $loadBalancerConfiguration = new LoadBalancer($loadBalancer->name, $loadBalancer->default_pools, $loadBalancer->fallback_pool); - $loadBalancerConfiguration->setSteeringPolicy($loadBalancer->steering_policy); - $loadBalancerConfiguration->setEnabled($loadBalancer->enabled); + $lbConfiguration = new LoadBalancer($loadBalancer->name, $loadBalancer->default_pools, $loadBalancer->fallback_pool); + $lbConfiguration->setSteeringPolicy($loadBalancer->steering_policy); + if ($loadBalancer->enabled === true) { + $lbConfiguration->enable(); + } elseif ($loadBalancer->enabled === false) { + $lbConfiguration->disable(); + } if (is_array($loadBalancer->pop_pools)) { - $loadBalancerConfiguration->setPopPools($loadBalancer->pop_pools); + $lbConfiguration->setPopPools($loadBalancer->pop_pools); } if (isset($loadBalancer->ttl)) { - $loadBalancerConfiguration->setTtl($loadBalancer->ttl); + $lbConfiguration->setTtl($loadBalancer->ttl); } if (is_array($loadBalancer->region_pools)) { - $loadBalancerConfiguration->setRegionPools($loadBalancer->region_pools); + $lbConfiguration->setRegionPools($loadBalancer->region_pools); + } + $lbConfiguration->setSessionAffinity($loadBalancer->session_affinity); + $lbConfiguration->setDescription($loadBalancer->description); + if ($loadBalancer->proxied === true) { + $lbConfiguration->enableProxied(); + } elseif ($loadBalancer->proxied === false) { + $lbConfiguration->disableProxied(); } - $loadBalancerConfiguration->setSessionAffinity($loadBalancer->session_affinity); - $loadBalancerConfiguration->setDescription($loadBalancer->description); - $loadBalancerConfiguration->setProxied($loadBalancer->proxied); if (isset($loadBalancer->session_affinity_ttl)) { - $loadBalancerConfiguration->setSessionAffinityTtl($loadBalancer->session_affinity_ttl); + $lbConfiguration->setSessionAffinityTtl($loadBalancer->session_affinity_ttl); } - return $loadBalancerConfiguration; + return $lbConfiguration; } /** * @param string $zoneID * @param string $loadBalancerID - * @param LoadBalancer $loadBalancerConfiguration + * @param LoadBalancer $lbConfiguration * @return bool */ public function updateLoadBalancer( string $zoneID, string $loadBalancerID, - LoadBalancer $loadBalancerConfiguration + LoadBalancer $lbConfiguration ): bool { - $options = $loadBalancerConfiguration->getArray(); + $options = $lbConfiguration->getArray(); - $query = $this->adapter->patch('zones/' . $zoneID . '/load_balancers/' . $loadBalancerID, $options); + $query = $this->adapter->put('zones/' . $zoneID . '/load_balancers/' . $loadBalancerID, $options); $this->body = json_decode($query->getBody()); @@ -109,14 +117,14 @@ public function updateLoadBalancer( /** * @param string $zoneID - * @param LoadBalancer $loadBalancerConfiguration + * @param LoadBalancer $lbConfiguration * @return bool */ public function createLoadBalancer( string $zoneID, - LoadBalancer $loadBalancerConfiguration + LoadBalancer $lbConfiguration ): bool { - $options = $loadBalancerConfiguration->getArray(); + $options = $lbConfiguration->getArray(); $query = $this->adapter->post('zones/' . $zoneID . '/load_balancers', $options); diff --git a/src/Endpoints/Pools.php b/src/Endpoints/Pools.php new file mode 100644 index 00000000..4a4f1938 --- /dev/null +++ b/src/Endpoints/Pools.php @@ -0,0 +1,152 @@ + + * User: HemeraOne + * Date: 13/05/2019 + */ + +namespace Cloudflare\API\Endpoints; + +use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Configurations\ConfigurationsException; +use Cloudflare\API\Configurations\Pool; +use Cloudflare\API\Traits\BodyAccessorTrait; + +class Pools implements API +{ + use BodyAccessorTrait; + + private $adapter; + + public function __construct(Adapter $adapter) + { + $this->adapter = $adapter; + } + + /** + * @param string $accountID + * @return mixed + */ + public function listPools(string $accountID) + { + $pools = $this->adapter->get('accounts/' . $accountID . '/load_balancers/pools'); + $this->body = json_decode($pools->getBody()); + + return $this->body->result; + } + + /** + * @param string $accountID + * @param string $poolID + * @return mixed + */ + public function getPoolDetails(string $accountID, string $poolID) + { + $pool = $this->adapter->get('accounts/' . $accountID . '/load_balancers/pools/' . $poolID); + $this->body = json_decode($pool->getBody()); + return $this->body->result; + } + + /** + * @param string $accountID + * @param string $poolID + * @return mixed + */ + public function getPoolHealthDetails(string $accountID, string $poolID) + { + $pool = $this->adapter->get('accounts/' . $accountID . '/load_balancers/pools/' . $poolID . '/health'); + $this->body = json_decode($pool->getBody()); + return $this->body->result; + } + + /** + * @param string $accountID + * @param string $poolID + * @return Pool + * @throws ConfigurationsException + */ + public function getPoolConfiguration(string $accountID, string $poolID) + { + $pool = $this->getPoolDetails($accountID, $poolID); + + $origins = []; + foreach ($pool->origins as $origin) { + $origins[] = (array)$origin; + } + $poolConfiguration = new Pool($pool->name, $origins); + $poolConfiguration->setDescription($pool->description); + if ($pool->enabled === true) { + $poolConfiguration->enable(); + } elseif ($pool->enabled === false) { + $poolConfiguration->disable(); + } + $poolConfiguration->setMonitor($pool->monitor); + $poolConfiguration->setNotificationEmail($pool->notification_email); + + return $poolConfiguration; + } + + /** + * @param string $accountID + * @param string $poolID + * @param Pool $poolConfiguration + * @return bool + */ + public function updatePool( + string $accountID, + string $poolID, + Pool $poolConfiguration + ): bool { + $options = $poolConfiguration->getArray(); + + $query = $this->adapter->put('accounts/' . $accountID . '/load_balancers/pools/' . $poolID, $options); + + $this->body = json_decode($query->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } + + /** + * @param string $accountID + * @param Pool $poolConfiguration + * @return bool + */ + public function createPool( + string $accountID, + Pool $poolConfiguration + ): bool { + $options = $poolConfiguration->getArray(); + + $query = $this->adapter->post('accounts/' . $accountID . '/load_balancers/pools', $options); + + $this->body = json_decode($query->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } + + /** + * @param string $accountID + * @param string $poolID + * @return bool + */ + public function deletePool(string $accountID, string $poolID): bool + { + $pool = $this->adapter->delete('accounts/' . $accountID . '/load_balancers/pools/' . $poolID); + + $this->body = json_decode($pool->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } +} diff --git a/tests/Configurations/LoadBalancerTest.php b/tests/Configurations/LoadBalancerTest.php index e726cd87..96b3d1e7 100644 --- a/tests/Configurations/LoadBalancerTest.php +++ b/tests/Configurations/LoadBalancerTest.php @@ -15,19 +15,18 @@ class LoadBalancerTest extends TestCase */ public function testArguments($setFunction, $arguments, $getFunction, $invalid) { - $lb = new LoadBalancer('bous', [], 'bogus'); + $loadBalancer = new LoadBalancer('bogus', [], 'bogus'); foreach ($arguments as $argument) { - if ($invalid) { + if ($invalid === true) { try { - $lb->{$setFunction}($argument); + $loadBalancer->{$setFunction}($argument); } catch (ConfigurationsException $e) { - $this->assertNotEquals($argument, $lb->{$getFunction}()); + $this->assertNotEquals($argument, $loadBalancer->{$getFunction}()); } - } else { - $lb->{$setFunction}($argument); - $this->assertEquals($argument, $lb->{$getFunction}()); + } elseif ($invalid === false) { + $loadBalancer->{$setFunction}($argument); + $this->assertEquals($argument, $loadBalancer->{$getFunction}()); } - } } diff --git a/tests/Configurations/PoolTest.php b/tests/Configurations/PoolTest.php new file mode 100644 index 00000000..204b392c --- /dev/null +++ b/tests/Configurations/PoolTest.php @@ -0,0 +1,53 @@ + + * User: HemeraOne + * Date: 13/05/2019 + */ + +use Cloudflare\API\Configurations\ConfigurationsException; +use Cloudflare\API\Configurations\Pool; + +class PoolTest extends TestCase +{ + /** + * @dataProvider testArgumentsDataProvider + */ + public function testArguments($setFunction, $arguments, $getFunction, $invalid) + { + $pool = new Pool('bogus', []); + foreach ($arguments as $argument) { + if ($invalid) { + try { + $pool->{$setFunction}($argument); + } catch (ConfigurationsException $e) { + $this->assertNotEquals($argument, $pool->{$getFunction}()); + } + } elseif ($invalid === false) { + $pool->{$setFunction}($argument); + $this->assertEquals($argument, $pool->{$getFunction}()); + } + } + } + + public function testArgumentsDataProvider() + { + return [ + 'origins arguments valid' => [ + 'setOrigins', [[['name' => 'test', 'address' => 'server1.example.com']]], 'getOrigins', false + ], + 'setNotificationEmail arguments valid' => [ + 'setNotificationEmail', ['user@example.com'], 'getNotificationEmail', false + ], + 'origins arguments invalid no address' => [ + 'setOrigins', [['name' => 'test']], 'getOrigins', true + ], + 'origins arguments invalid no name' => [ + 'setOrigins', [['address' => 'server1.example.com']], 'getOrigins', true + ], + 'setNotificationEmail arguments invalid' => [ + 'setNotificationEmail', ['userexample.com'], 'getNotificationEmail', true + ] + ]; + } +} diff --git a/tests/Endpoints/LoadBalancersTest.php b/tests/Endpoints/LoadBalancersTest.php index a0a87b26..93418d43 100644 --- a/tests/Endpoints/LoadBalancersTest.php +++ b/tests/Endpoints/LoadBalancersTest.php @@ -90,10 +90,10 @@ public function testUpdateLoadBalancer() $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateLoadBalancer.json'); $mock = $this->getMockBuilder(Adapter::class)->getMock(); - $mock->method('patch')->willReturn($response); + $mock->method('put')->willReturn($response); $mock->expects($this->once()) - ->method('patch') + ->method('put') ->with( $this->equalTo('zones/699d98642c564d2e855e9661899b7252/load_balancers/699d98642c564d2e855e9661899b7252'), $this->equalTo($lbConfiguration->getArray()) diff --git a/tests/Endpoints/PoolsTest.php b/tests/Endpoints/PoolsTest.php new file mode 100644 index 00000000..9846f48a --- /dev/null +++ b/tests/Endpoints/PoolsTest.php @@ -0,0 +1,133 @@ + + * User: HemeraOne + * Date: 13/05/2019 + */ + +class PoolsTest extends TestCase +{ + public function testCreatePool() + { + $origins = [ + [ + 'name' => 'app-server-1', + 'address' => '0.0.0.0', + 'enabled' => true, + 'weight' => 0.56 + ] + ]; + + $poolConfiguration = new \Cloudflare\API\Configurations\Pool('primary-dc-1', $origins); + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/createPool.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools'), + $poolConfiguration->getArray() + ); + + $pools = new Pools($mock); + $result = $pools->createPool('01a7362d577a6c3019a474fd6f485823', $poolConfiguration); + + $this->assertTrue($result); + $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result->id); + } + + public function testListPools() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/listPools.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools') + ); + + $pools = new Pools($mock); + $pools->listPools('01a7362d577a6c3019a474fd6f485823'); + $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result[0]->id); + } + + public function testGetPoolDetails() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getPoolDetails.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools/17b5962d775c646f3f9725cbc7a53df4') + ); + + $pools = new Pools($mock); + $pools->getPoolDetails('01a7362d577a6c3019a474fd6f485823', '17b5962d775c646f3f9725cbc7a53df4'); + $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result->id); + } + + public function testUpdatePool() + { + $origins = [ + [ + 'name' => 'app-server-1', + 'address' => '0.0.0.0', + 'enabled' => true, + 'weight' => 0.56 + ] + ]; + + $poolConfiguration = new \Cloudflare\API\Configurations\Pool('primary-dc-1', $origins); + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updatePool.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('put')->willReturn($response); + + $mock->expects($this->once()) + ->method('put') + ->with( + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools/17b5962d775c646f3f9725cbc7a53df4'), + $this->equalTo($poolConfiguration->getArray()) + ); + + $pools = new Pools($mock); + $result = $pools->updatePool('01a7362d577a6c3019a474fd6f485823', '17b5962d775c646f3f9725cbc7a53df4', $poolConfiguration); + + $this->assertTrue($result); + $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result->id); + } + + public function testDeletePool() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/deletePool.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/load_balancers/pools/17b5962d775c646f3f9725cbc7a53df4') + ); + + $pools = new Pools($mock); + $result = $pools->deletePool('01a7362d577a6c3019a474fd6f485823', '17b5962d775c646f3f9725cbc7a53df4'); + + $this->assertTrue($result); + $this->assertEquals('17b5962d775c646f3f9725cbc7a53df4', $pools->getBody()->result->id); + } +} diff --git a/tests/Fixtures/Endpoints/createPool.json b/tests/Fixtures/Endpoints/createPool.json new file mode 100644 index 00000000..e1397a17 --- /dev/null +++ b/tests/Fixtures/Endpoints/createPool.json @@ -0,0 +1,28 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "17b5962d775c646f3f9725cbc7a53df4", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "description": "Primary data center - Provider XYZ", + "name": "primary-dc-1", + "enabled": true, + "minimum_origins": 2, + "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", + "check_regions": [ + "WEU", + "ENAM" + ], + "origins": [ + { + "name": "app-server-1", + "address": "0.0.0.0", + "enabled": true, + "weight": 0.56 + } + ], + "notification_email": "someone@example.com" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/deletePool.json b/tests/Fixtures/Endpoints/deletePool.json new file mode 100644 index 00000000..96c4395f --- /dev/null +++ b/tests/Fixtures/Endpoints/deletePool.json @@ -0,0 +1,8 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "17b5962d775c646f3f9725cbc7a53df4" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getPoolDetails.json b/tests/Fixtures/Endpoints/getPoolDetails.json new file mode 100644 index 00000000..e1397a17 --- /dev/null +++ b/tests/Fixtures/Endpoints/getPoolDetails.json @@ -0,0 +1,28 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "17b5962d775c646f3f9725cbc7a53df4", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "description": "Primary data center - Provider XYZ", + "name": "primary-dc-1", + "enabled": true, + "minimum_origins": 2, + "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", + "check_regions": [ + "WEU", + "ENAM" + ], + "origins": [ + { + "name": "app-server-1", + "address": "0.0.0.0", + "enabled": true, + "weight": 0.56 + } + ], + "notification_email": "someone@example.com" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/listLoadbalancers.json b/tests/Fixtures/Endpoints/listLoadBalancers.json similarity index 100% rename from tests/Fixtures/Endpoints/listLoadbalancers.json rename to tests/Fixtures/Endpoints/listLoadBalancers.json diff --git a/tests/Fixtures/Endpoints/listPools.json b/tests/Fixtures/Endpoints/listPools.json new file mode 100644 index 00000000..2c7a4713 --- /dev/null +++ b/tests/Fixtures/Endpoints/listPools.json @@ -0,0 +1,36 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "17b5962d775c646f3f9725cbc7a53df4", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "description": "Primary data center - Provider XYZ", + "name": "primary-dc-1", + "enabled": true, + "minimum_origins": 2, + "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", + "check_regions": [ + "WEU", + "ENAM" + ], + "origins": [ + { + "name": "app-server-1", + "address": "0.0.0.0", + "enabled": true, + "weight": 0.56 + } + ], + "notification_email": "someone@example.com" + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 2000 + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updatePool.json b/tests/Fixtures/Endpoints/updatePool.json new file mode 100644 index 00000000..e1397a17 --- /dev/null +++ b/tests/Fixtures/Endpoints/updatePool.json @@ -0,0 +1,28 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "17b5962d775c646f3f9725cbc7a53df4", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "description": "Primary data center - Provider XYZ", + "name": "primary-dc-1", + "enabled": true, + "minimum_origins": 2, + "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", + "check_regions": [ + "WEU", + "ENAM" + ], + "origins": [ + { + "name": "app-server-1", + "address": "0.0.0.0", + "enabled": true, + "weight": 0.56 + } + ], + "notification_email": "someone@example.com" + } +} \ No newline at end of file From 1724b66147066b3d8016ff0917c80f9a75b16f63 Mon Sep 17 00:00:00 2001 From: Martijn Smidt Date: Mon, 3 Jun 2019 11:23:27 +0200 Subject: [PATCH 3/3] Added check_regions for pool configuration --- src/Configurations/Pool.php | 10 ++++++++++ src/Endpoints/Pools.php | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/Configurations/Pool.php b/src/Configurations/Pool.php index f28347b3..7b45d41f 100644 --- a/src/Configurations/Pool.php +++ b/src/Configurations/Pool.php @@ -90,6 +90,16 @@ public function getMonitor():string return $this->configs['monitor'] ?? ''; } + public function setCheckRegions(array $checkRegions) + { + $this->configs['check_regions'] = $checkRegions; + } + + public function getCheckRegions():array + { + return $this->configs['check_regions'] ?? []; + } + public function setNotificationEmail(string $email) { if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { diff --git a/src/Endpoints/Pools.php b/src/Endpoints/Pools.php index 4a4f1938..f6ceb599 100644 --- a/src/Endpoints/Pools.php +++ b/src/Endpoints/Pools.php @@ -83,6 +83,10 @@ public function getPoolConfiguration(string $accountID, string $poolID) $poolConfiguration->setMonitor($pool->monitor); $poolConfiguration->setNotificationEmail($pool->notification_email); + if (is_array($pool->check_regions)) { + $poolConfiguration->setCheckRegions($pool->check_regions); + } + return $poolConfiguration; }