Added loadbalancers class and tests
This commit is contained in:
178
src/Configurations/LoadBalancer.php
Normal file
178
src/Configurations/LoadBalancer.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Martijn Smidt <martijn@squeezely.tech>
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
149
src/Endpoints/LoadBalancers.php
Normal file
149
src/Endpoints/LoadBalancers.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?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\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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user