Added pools class and tests
Small changes in loadbalancer configuration
This commit is contained in:
@@ -28,9 +28,9 @@ class LoadBalancer implements Configurations
|
||||
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 @@ class LoadBalancer implements Configurations
|
||||
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 @@ class LoadBalancer implements Configurations
|
||||
return $this->configs['description'] ?? '';
|
||||
}
|
||||
|
||||
public function setProxied(bool $proxied = true)
|
||||
public function enableProxied()
|
||||
{
|
||||
$this->configs['proxied'] = $proxied;
|
||||
$this->configs['proxied'] = true;
|
||||
}
|
||||
|
||||
public function disableProxied()
|
||||
{
|
||||
$this->configs['proxied'] = false;
|
||||
}
|
||||
|
||||
public function isProxied():bool
|
||||
|
||||
111
src/Configurations/Pool.php
Normal file
111
src/Configurations/Pool.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Martijn Smidt <martijn@squeezely.tech>
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -57,46 +57,54 @@ class LoadBalancers implements API
|
||||
{
|
||||
$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 @@ class LoadBalancers implements API
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
|
||||
152
src/Endpoints/Pools.php
Normal file
152
src/Endpoints/Pools.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Martijn Smidt <martijn@squeezely.tech>
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user