Skip to content

Commit

Permalink
Merge pull request #51 from cloudflare/SOPS-161
Browse files Browse the repository at this point in the history
SOPS-161 :: Add Custom Hostname endpoints to cloudflare-php
  • Loading branch information
IcyApril authored Mar 19, 2018
2 parents 19a4f48 + d955739 commit 35ce8ea
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Each API call is provided via a similarly named function within various classes
- [x] [Page Rules](https://support.cloudflare.com/hc/en-us/articles/200168306-Is-there-a-tutorial-for-Page-Rules-)
- [x] [Web Application Firewall (WAF)](https://www.cloudflare.com/waf/)
- [ ] Virtual DNS Management
- [ ] Custom hostnames
- [x] Custom hostnames
- [x] Zone Lockdown and User-Agent Block rules
- [ ] Organization Administration
- [x] [Railgun](https://www.cloudflare.com/railgun/) administration
Expand Down
150 changes: 150 additions & 0 deletions src/Endpoints/CustomHostnames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 18/03/2018
* Time: 21:46
*/

namespace Cloudflare\API\Endpoints;

use Cloudflare\API\Adapter\Adapter;

class CustomHostnames implements API
{
private $adapter;

public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}


/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*
* @param string $zoneID
* @param string $hostname
* @param string $sslMethod
* @param string $sslType
* @return \stdClass
*/
public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv'): \stdClass
{
$options = [
'hostname' => $hostname,
'ssl' => (object)[
'method' => $sslMethod,
'type' => $sslType
]
];

$zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', [], $options);
$body = json_decode($zone->getBody());
return $body->result;
}

/**
* @param string $zoneID
* @param string $hostname
* @param string $id
* @param int $page
* @param int $perPage
* @param string $order
* @param string $direction
* @param int $ssl
* @return \stdClass
*/
public function listHostnames(
string $zoneID,
string $hostname = '',
string $hostnameID = '',
int $page = 1,
int $perPage = 20,
string $order = '',
string $direction = '',
int $ssl = 0
): \stdClass {
$query = [
'page' => $page,
'per_page' => $perPage,
'ssl' => $ssl
];

if (!empty($hostname)) {
$query['hostname'] = $hostname;
}

if (!empty($hostnameID)) {
$query['id'] = $hostnameID;
}

if (!empty($order)) {
$query['order'] = $order;
}

if (!empty($direction)) {
$query['direction'] = $direction;
}

$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query, []);
$body = json_decode($zone->getBody());

return (object)['result' => $body->result, 'result_info' => $body->result_info];
}

/**
* @param string $zoneID
* @param string $hostnameID
* @return mixed
*/
public function getHostname(string $zoneID, string $hostnameID)
{
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], []);
$body = json_decode($zone->getBody());

return $body->result;
}

/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*
* @param string $zoneID
* @param string $hostnameID
* @param string $sslMethod
* @param string $sslType
* @return \stdClass
*/
public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = ''): \stdClass
{
$query = [];

if (!empty($sslMethod)) {
$query['method'] = $sslMethod;
}

if (!empty($sslType)) {
$query['type'] = $sslType;
}

$options = [
'ssl' => (object)$query
];

$zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], $options);
$body = json_decode($zone->getBody());
return $body->result;
}

/**
* @param string $zoneID
* @param string $hostnameID
* @return \stdClass
*/
public function deleteHostname(string $zoneID, string $hostnameID): \stdClass
{
$zone = $this->adapter->delete('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], []);
$body = json_decode($zone->getBody());
return $body;
}
}
140 changes: 140 additions & 0 deletions tests/Endpoints/CustomHostnamesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* Created by PhpStorm.
* User: junade
* Date: 18/03/2018
* Time: 22:23
*/

use Cloudflare\API\Endpoints\CustomHostnames;

class CustomHostnamesTest extends TestCase
{
public function testAddHostname()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.json');

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

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
$this->equalTo([]),
$this->equalTo([
'hostname' => 'app.example.com',
'ssl' => (object)[
'method' => 'http',
'type' => 'dv'
]
])
);

$hostname = new CustomHostnames($mock);
$hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv');
}

public function testListHostnames()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listHostnames.json');

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

$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
$this->equalTo([
'hostname' => 'app.example.com',
'id' => '0d89c70d-ad9f-4843-b99f-6cc0252067e9',
'page' => 1,
'per_page' => 20,
'order' => 'ssl',
'direction' => 'desc',
'ssl' => 0
]),
$this->equalTo([])
);

$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
$result = $zones->listHostnames('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 1, 20, 'ssl', 'desc', 0);

$this->assertObjectHasAttribute('result', $result);
$this->assertObjectHasAttribute('result_info', $result);

$this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->result[0]->id);
$this->assertEquals(1, $result->result_info->page);
}

public function testGetHostname()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getHostname.json');

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

$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
$this->equalTo([]),
$this->equalTo([])
);

$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
$result = $zones->getHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', '0d89c70d-ad9f-4843-b99f-6cc0252067e9');

$this->assertObjectHasAttribute('id', $result);
$this->assertObjectHasAttribute('hostname', $result);
}

public function testUpdateHostname()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHostname.json');

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

$mock->expects($this->once())
->method('patch')
->with(
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
$this->equalTo([]),
$this->equalTo([
'ssl' => (object)[
'method' => 'http',
'type' => 'dv'
]
])
);

$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
$result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv');

$this->assertObjectHasAttribute('id', $result);
$this->assertObjectHasAttribute('hostname', $result);
}

public function testDeleteHostname()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteHostname.json');

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

$mock->expects($this->once())
->method('delete')
->with(
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
$this->equalTo([]),
$this->equalTo([])
);

$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
$result = $zones->deleteHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9');

$this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->id);
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/Endpoints/createCustomHostname.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"success": true,
"errors": [
{}
],
"messages": [
{}
],
"result": {
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
"hostname": "app.example.com",
"ssl": {
"status": "pending_validation",
"method": "http",
"type": "dv",
"cname_target": "dcv.digicert.com",
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
}
}
}
3 changes: 3 additions & 0 deletions tests/Fixtures/Endpoints/deleteHostname.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9"
}
20 changes: 20 additions & 0 deletions tests/Fixtures/Endpoints/getHostname.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"success": true,
"errors": [
{}
],
"messages": [
{}
],
"result": {
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
"hostname": "app.example.com",
"ssl": {
"status": "pending_validation",
"method": "http",
"type": "dv",
"cname_target": "dcv.digicert.com",
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
}
}
}
28 changes: 28 additions & 0 deletions tests/Fixtures/Endpoints/listHostnames.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"success": true,
"errors": [
{}
],
"messages": [
{}
],
"result": [
{
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
"hostname": "app.example.com",
"ssl": {
"status": "pending_validation",
"method": "http",
"type": "dv",
"cname_target": "dcv.digicert.com",
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
}
}
],
"result_info": {
"page": 1,
"per_page": 20,
"count": 1,
"total_count": 2000
}
}
Loading

0 comments on commit 35ce8ea

Please sign in to comment.