Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Missing Support For Custom Hostname Properties #178

37 changes: 30 additions & 7 deletions src/Endpoints/CustomHostnames.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class CustomHostnames implements API
{
use BodyAccessorTrait;

private $adapter;

public function __construct(Adapter $adapter)
Expand All @@ -30,27 +30,50 @@ public function __construct(Adapter $adapter)
* @param string $hostname
* @param string $sslMethod
* @param string $sslType
* @param array $sslSettings
* @param array $sslSettings
* @param string $customOriginServer
* @param bool $wildcard
* @param bool $wildcard
* @param string $bundleMethod
* @param array $customSsl
* @return \stdClass
*/
public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv', array $sslSettings = [], string $customOriginServer = '', bool $wildcard = false): \stdClass
{
public function addHostname(
string $zoneID,
string $hostname,
string $sslMethod = 'http',
string $sslType = 'dv',
array $sslSettings = [],
string $customOriginServer = '',
bool $wildcard = false,
string $bundleMethod = '',
array $customSsl = []
): \stdClass {
$options = [
'hostname' => $hostname,
'ssl' => [
'method' => $sslMethod,
'type' => $sslType,
'settings' => $sslSettings
'settings' => $sslSettings,
'wildcard' => $wildcard,
],
'wildcard' => $wildcard,
];

if (!empty($customOriginServer)) {
$options['custom_origin_server'] = $customOriginServer;
}

if (!empty($bundleMethod)) {
$options['ssl']['bundle_method'] = $bundleMethod;
}

if (!empty($customSsl['key'])) {
$options['ssl']['custom_key'] = $customSsl['key'];
}

if (!empty($customSsl['certificate'])) {
$options['ssl']['custom_certificate'] = $customSsl['certificate'];
}

$zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options);
$this->body = json_decode($zone->getBody());
return $this->body->result;
Expand Down
88 changes: 84 additions & 4 deletions tests/Endpoints/CustomHostnamesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function testAddHostname()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.json');

$customSsl = $this->getCustomSsl();

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

Expand All @@ -30,10 +32,13 @@ public function testAddHostname()
'settings' => [
'http2' => 'on',
'http3' => 'on',
'min_tls_version' => '1.2'
]
'min_tls_version' => '1.2',
],
'bundle_method' => 'optimal',
'custom_key' => $customSsl['key'],
'custom_certificate' => $customSsl['certificate'],
'wildcard' => true,
],
'wildcard' => true,
])
);

Expand All @@ -43,7 +48,18 @@ public function testAddHostname()
'http3' => 'on',
'min_tls_version' => '1.2'
];
$hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv', $sslSettings, 'origin.example.com', true);

$hostname->addHostname(
'023e105f4ecef8ad9ca31a8372d0c353',
'app.example.com',
'http',
'dv',
$sslSettings,
'origin.example.com',
true,
'optimal',
$customSsl
);
$this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id);
}

Expand Down Expand Up @@ -178,4 +194,68 @@ public function testGetHostnameFallbackOrigin()
$this->assertObjectHasAttribute('origin', $result);
$this->assertObjectHasAttribute('status', $result);
}

private function getCustomSsl(): array
{
$customKey = <<<KEY
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDZfoCUkzkZLCzo
OFTtlXU9OYqNFx06J/GOKCwDCyfkY5RY1x6BVrVpTqf/JaU42DZmCjIiEugBg4bu
eu9/w21prIWgRKEe8mjrw83+3QSIyQrs+78rqwDptUfL+IyhYln6SBjqPQ569Y0w
x6A896PDMYPHgnWtclGwsxDNKJ2eWsH+C4UkLUeVM4BILEJ00YUjayowL/0sflTJ
yY58c9fVV27aGBJ4znreYkBojPQ0fzVZ3HJfYD+DgYUUkuzN/WohOLTNTxvzt/i2
GNxP8tZzi0E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Q4E
FgQUbAfyBm0wpM7FqUb1yqeaF4voY/gwHwYDVR0jBBgwFoAUbAfyBm0wpM7FqUb1
yqeaF4voY/gwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAO2Dd
k/seFjp83caYE/NVdDy5B7l5JeVtruaUdlGbb0xtVhiIdoY43ukhHFw8zuWMW9RX
SUbrzwacfKLDBikcefk9go6cMimqYIRF8Hntph1gjjqB0papUm2WVYbsBRv2okys
ej0dGSeUEsWjKRTSMkJsbbiEv6oveeSki069zl+tln0UhbHedkIY3rJsFIyoddSu
g96r5HPHksnObm1JCym0xd09+msliDkBmq87mxok9m5aEqWX4XvdGfYERV/eD5vC
KcW4DoM1KZd8E6tlniglc1jC0pzKfho7Uoe6UtObgHZGNwRYwYy+BHvHYY46ctSI
NdZ7G/lUyrBFhsRrhw==
-----END CERTIFICATE-----
CERTIFICATE;

return [
'key' => $customKey,
'certificate' => $customCertificate,
];
}
}