From 06de5b0c04f042bf8d5380bef16bcd11ea1e1288 Mon Sep 17 00:00:00 2001 From: bubbleupdotnet Date: Tue, 7 Jul 2020 05:16:24 -0500 Subject: [PATCH] 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 --- src/Endpoints/CustomHostnames.php | 33 +++++++++++++++++++------ tests/Endpoints/CustomHostnamesTest.php | 33 +++++++++++++++++++++---- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 4af5c9e..a7f09b0 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -30,18 +30,27 @@ class CustomHostnames implements API * @param string $hostname * @param string $sslMethod * @param string $sslType + * @param array $sslSettings + * @param string $customOriginServer + * @param bool $wildcard * @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 = [ 'hostname' => $hostname, 'ssl' => [ '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); $this->body = json_decode($zone->getBody()); return $this->body->result; @@ -118,7 +127,7 @@ class CustomHostnames implements API * @param string $sslType * @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 = []; @@ -130,9 +139,19 @@ class CustomHostnames implements API $query['type'] = $sslType; } - $options = [ - 'ssl' => $query - ]; + if (!empty($sslSettings)) { + $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); $this->body = json_decode($zone->getBody()); diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index faa247f..7298d86 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -23,15 +23,27 @@ class CustomHostnamesTest extends TestCase $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'), $this->equalTo([ 'hostname' => 'app.example.com', + 'custom_origin_server' => 'origin.example.com', 'ssl' => [ 'method' => 'http', - 'type' => 'dv' - ] + 'type' => 'dv', + 'settings' => [ + 'http2' => 'on', + 'http3' => 'on', + 'min_tls_version' => '1.2' + ] + ], + 'wildcard' => true, ]) ); $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); } @@ -101,15 +113,26 @@ class CustomHostnamesTest extends TestCase ->with( $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'), $this->equalTo([ + 'custom_origin_server' => 'origin.example.com', 'ssl' => [ 'method' => 'http', - 'type' => 'dv' + 'type' => 'dv', + 'settings' => [ + 'http2' => 'on', + 'http3' => 'on', + 'min_tls_version' => '1.2' + ] ] ]) ); $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('hostname', $result);