Add custom hostname options (#131)

* Add ability to set additional SSL settings

* Add ability to specify a custom origin server

* Add ability to set wildcard

* Fix json parsing error with empty 'ssl' key

Co-authored-by: Craig Menning <craig@bubbleup.net>
This commit is contained in:
bubbleupdotnet
2020-07-07 05:16:24 -05:00
committed by GitHub
parent 8879ba4c0a
commit 06de5b0c04
2 changed files with 54 additions and 12 deletions

View File

@@ -30,18 +30,27 @@ class CustomHostnames implements API
* @param string $hostname * @param string $hostname
* @param string $sslMethod * @param string $sslMethod
* @param string $sslType * @param string $sslType
* @param array $sslSettings
* @param string $customOriginServer
* @param bool $wildcard
* @return \stdClass * @return \stdClass
*/ */
public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv'): \stdClass public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv', array $sslSettings = [], string $customOriginServer = '', bool $wildcard = false): \stdClass
{ {
$options = [ $options = [
'hostname' => $hostname, 'hostname' => $hostname,
'ssl' => [ 'ssl' => [
'method' => $sslMethod, 'method' => $sslMethod,
'type' => $sslType 'type' => $sslType,
] 'settings' => $sslSettings
],
'wildcard' => $wildcard,
]; ];
if (!empty($customOriginServer)) {
$options['custom_origin_server'] = $customOriginServer;
}
$zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options); $zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options);
$this->body = json_decode($zone->getBody()); $this->body = json_decode($zone->getBody());
return $this->body->result; return $this->body->result;
@@ -118,7 +127,7 @@ class CustomHostnames implements API
* @param string $sslType * @param string $sslType
* @return \stdClass * @return \stdClass
*/ */
public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = ''): \stdClass public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = '', array $sslSettings = [], string $customOriginServer = ''): \stdClass
{ {
$query = []; $query = [];
@@ -130,9 +139,19 @@ class CustomHostnames implements API
$query['type'] = $sslType; $query['type'] = $sslType;
} }
$options = [ if (!empty($sslSettings)) {
'ssl' => $query $query['settings'] = $sslSettings;
]; }
if (!empty($query)) {
$options = [
'ssl' => $query
];
}
if (!empty($customOriginServer)) {
$options['custom_origin_server'] = $customOriginServer;
}
$zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, $options); $zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, $options);
$this->body = json_decode($zone->getBody()); $this->body = json_decode($zone->getBody());

View File

@@ -23,15 +23,27 @@ class CustomHostnamesTest extends TestCase
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'), $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
$this->equalTo([ $this->equalTo([
'hostname' => 'app.example.com', 'hostname' => 'app.example.com',
'custom_origin_server' => 'origin.example.com',
'ssl' => [ 'ssl' => [
'method' => 'http', 'method' => 'http',
'type' => 'dv' 'type' => 'dv',
] 'settings' => [
'http2' => 'on',
'http3' => 'on',
'min_tls_version' => '1.2'
]
],
'wildcard' => true,
]) ])
); );
$hostname = new CustomHostnames($mock); $hostname = new CustomHostnames($mock);
$hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv'); $sslSettings = [
'http2' => 'on',
'http3' => 'on',
'min_tls_version' => '1.2'
];
$hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv', $sslSettings, 'origin.example.com', true);
$this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id); $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id);
} }
@@ -101,15 +113,26 @@ class CustomHostnamesTest extends TestCase
->with( ->with(
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'), $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
$this->equalTo([ $this->equalTo([
'custom_origin_server' => 'origin.example.com',
'ssl' => [ 'ssl' => [
'method' => 'http', 'method' => 'http',
'type' => 'dv' 'type' => 'dv',
'settings' => [
'http2' => 'on',
'http3' => 'on',
'min_tls_version' => '1.2'
]
] ]
]) ])
); );
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
$result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv'); $sslSettings = [
'http2' => 'on',
'http3' => 'on',
'min_tls_version' => '1.2'
];
$result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv', $sslSettings, 'origin.example.com');
$this->assertObjectHasAttribute('id', $result); $this->assertObjectHasAttribute('id', $result);
$this->assertObjectHasAttribute('hostname', $result); $this->assertObjectHasAttribute('hostname', $result);