diff --git a/src/Configurations/LoadBalancer.php b/src/Configurations/LoadBalancer.php new file mode 100644 index 00000000..70b2da1f --- /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 $defaultPools) + { + $this->configs['default_pools'] = $defaultPools; + } + + 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 getEnabled():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 enableProxied() + { + $this->configs['proxied'] = true; + } + + public function disableProxied() + { + $this->configs['proxied'] = false; + } + + 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/Configurations/Pool.php b/src/Configurations/Pool.php new file mode 100644 index 00000000..7b45d41f --- /dev/null +++ b/src/Configurations/Pool.php @@ -0,0 +1,121 @@ + + * 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 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) { + 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 new file mode 100644 index 00000000..7813d313 --- /dev/null +++ b/src/Endpoints/LoadBalancers.php @@ -0,0 +1,157 @@ + + * 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); + + $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)) { + $lbConfiguration->setPopPools($loadBalancer->pop_pools); + } + + if (isset($loadBalancer->ttl)) { + $lbConfiguration->setTtl($loadBalancer->ttl); + } + + if (is_array($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(); + } + + if (isset($loadBalancer->session_affinity_ttl)) { + $lbConfiguration->setSessionAffinityTtl($loadBalancer->session_affinity_ttl); + } + + return $lbConfiguration; + } + + /** + * @param string $zoneID + * @param string $loadBalancerID + * @param LoadBalancer $lbConfiguration + * @return bool + */ + public function updateLoadBalancer( + string $zoneID, + string $loadBalancerID, + LoadBalancer $lbConfiguration + ): bool { + $options = $lbConfiguration->getArray(); + + $query = $this->adapter->put('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 $lbConfiguration + * @return bool + */ + public function createLoadBalancer( + string $zoneID, + LoadBalancer $lbConfiguration + ): bool { + $options = $lbConfiguration->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/src/Endpoints/Pools.php b/src/Endpoints/Pools.php new file mode 100644 index 00000000..f6ceb599 --- /dev/null +++ b/src/Endpoints/Pools.php @@ -0,0 +1,156 @@ + + * 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); + + if (is_array($pool->check_regions)) { + $poolConfiguration->setCheckRegions($pool->check_regions); + } + + 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 new file mode 100644 index 00000000..96b3d1e7 --- /dev/null +++ b/tests/Configurations/LoadBalancerTest.php @@ -0,0 +1,56 @@ + + * 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) + { + $loadBalancer = new LoadBalancer('bogus', [], 'bogus'); + foreach ($arguments as $argument) { + if ($invalid === true) { + try { + $loadBalancer->{$setFunction}($argument); + } catch (ConfigurationsException $e) { + $this->assertNotEquals($argument, $loadBalancer->{$getFunction}()); + } + } elseif ($invalid === false) { + $loadBalancer->{$setFunction}($argument); + $this->assertEquals($argument, $loadBalancer->{$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/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 new file mode 100644 index 00000000..93418d43 --- /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('put')->willReturn($response); + + $mock->expects($this->once()) + ->method('put') + ->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/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/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/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/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/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/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/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 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/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/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 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