Skip to content

Commit 35ce8ea

Browse files
authored
Merge pull request #51 from cloudflare/SOPS-161
SOPS-161 :: Add Custom Hostname endpoints to cloudflare-php
2 parents 19a4f48 + d955739 commit 35ce8ea

File tree

8 files changed

+382
-1
lines changed

8 files changed

+382
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Each API call is provided via a similarly named function within various classes
1818
- [x] [Page Rules](https://support.cloudflare.com/hc/en-us/articles/200168306-Is-there-a-tutorial-for-Page-Rules-)
1919
- [x] [Web Application Firewall (WAF)](https://www.cloudflare.com/waf/)
2020
- [ ] Virtual DNS Management
21-
- [ ] Custom hostnames
21+
- [x] Custom hostnames
2222
- [x] Zone Lockdown and User-Agent Block rules
2323
- [ ] Organization Administration
2424
- [x] [Railgun](https://www.cloudflare.com/railgun/) administration

src/Endpoints/CustomHostnames.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: junade
5+
* Date: 18/03/2018
6+
* Time: 21:46
7+
*/
8+
9+
namespace Cloudflare\API\Endpoints;
10+
11+
use Cloudflare\API\Adapter\Adapter;
12+
13+
class CustomHostnames implements API
14+
{
15+
private $adapter;
16+
17+
public function __construct(Adapter $adapter)
18+
{
19+
$this->adapter = $adapter;
20+
}
21+
22+
23+
/**
24+
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
25+
*
26+
* @param string $zoneID
27+
* @param string $hostname
28+
* @param string $sslMethod
29+
* @param string $sslType
30+
* @return \stdClass
31+
*/
32+
public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv'): \stdClass
33+
{
34+
$options = [
35+
'hostname' => $hostname,
36+
'ssl' => (object)[
37+
'method' => $sslMethod,
38+
'type' => $sslType
39+
]
40+
];
41+
42+
$zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', [], $options);
43+
$body = json_decode($zone->getBody());
44+
return $body->result;
45+
}
46+
47+
/**
48+
* @param string $zoneID
49+
* @param string $hostname
50+
* @param string $id
51+
* @param int $page
52+
* @param int $perPage
53+
* @param string $order
54+
* @param string $direction
55+
* @param int $ssl
56+
* @return \stdClass
57+
*/
58+
public function listHostnames(
59+
string $zoneID,
60+
string $hostname = '',
61+
string $hostnameID = '',
62+
int $page = 1,
63+
int $perPage = 20,
64+
string $order = '',
65+
string $direction = '',
66+
int $ssl = 0
67+
): \stdClass {
68+
$query = [
69+
'page' => $page,
70+
'per_page' => $perPage,
71+
'ssl' => $ssl
72+
];
73+
74+
if (!empty($hostname)) {
75+
$query['hostname'] = $hostname;
76+
}
77+
78+
if (!empty($hostnameID)) {
79+
$query['id'] = $hostnameID;
80+
}
81+
82+
if (!empty($order)) {
83+
$query['order'] = $order;
84+
}
85+
86+
if (!empty($direction)) {
87+
$query['direction'] = $direction;
88+
}
89+
90+
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query, []);
91+
$body = json_decode($zone->getBody());
92+
93+
return (object)['result' => $body->result, 'result_info' => $body->result_info];
94+
}
95+
96+
/**
97+
* @param string $zoneID
98+
* @param string $hostnameID
99+
* @return mixed
100+
*/
101+
public function getHostname(string $zoneID, string $hostnameID)
102+
{
103+
$zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], []);
104+
$body = json_decode($zone->getBody());
105+
106+
return $body->result;
107+
}
108+
109+
/**
110+
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
111+
*
112+
* @param string $zoneID
113+
* @param string $hostnameID
114+
* @param string $sslMethod
115+
* @param string $sslType
116+
* @return \stdClass
117+
*/
118+
public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = ''): \stdClass
119+
{
120+
$query = [];
121+
122+
if (!empty($sslMethod)) {
123+
$query['method'] = $sslMethod;
124+
}
125+
126+
if (!empty($sslType)) {
127+
$query['type'] = $sslType;
128+
}
129+
130+
$options = [
131+
'ssl' => (object)$query
132+
];
133+
134+
$zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], $options);
135+
$body = json_decode($zone->getBody());
136+
return $body->result;
137+
}
138+
139+
/**
140+
* @param string $zoneID
141+
* @param string $hostnameID
142+
* @return \stdClass
143+
*/
144+
public function deleteHostname(string $zoneID, string $hostnameID): \stdClass
145+
{
146+
$zone = $this->adapter->delete('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, [], []);
147+
$body = json_decode($zone->getBody());
148+
return $body;
149+
}
150+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: junade
5+
* Date: 18/03/2018
6+
* Time: 22:23
7+
*/
8+
9+
use Cloudflare\API\Endpoints\CustomHostnames;
10+
11+
class CustomHostnamesTest extends TestCase
12+
{
13+
public function testAddHostname()
14+
{
15+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.json');
16+
17+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
18+
$mock->method('post')->willReturn($response);
19+
20+
$mock->expects($this->once())
21+
->method('post')
22+
->with(
23+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
24+
$this->equalTo([]),
25+
$this->equalTo([
26+
'hostname' => 'app.example.com',
27+
'ssl' => (object)[
28+
'method' => 'http',
29+
'type' => 'dv'
30+
]
31+
])
32+
);
33+
34+
$hostname = new CustomHostnames($mock);
35+
$hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv');
36+
}
37+
38+
public function testListHostnames()
39+
{
40+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listHostnames.json');
41+
42+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
43+
$mock->method('get')->willReturn($response);
44+
45+
$mock->expects($this->once())
46+
->method('get')
47+
->with(
48+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
49+
$this->equalTo([
50+
'hostname' => 'app.example.com',
51+
'id' => '0d89c70d-ad9f-4843-b99f-6cc0252067e9',
52+
'page' => 1,
53+
'per_page' => 20,
54+
'order' => 'ssl',
55+
'direction' => 'desc',
56+
'ssl' => 0
57+
]),
58+
$this->equalTo([])
59+
);
60+
61+
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
62+
$result = $zones->listHostnames('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 1, 20, 'ssl', 'desc', 0);
63+
64+
$this->assertObjectHasAttribute('result', $result);
65+
$this->assertObjectHasAttribute('result_info', $result);
66+
67+
$this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->result[0]->id);
68+
$this->assertEquals(1, $result->result_info->page);
69+
}
70+
71+
public function testGetHostname()
72+
{
73+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getHostname.json');
74+
75+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
76+
$mock->method('get')->willReturn($response);
77+
78+
$mock->expects($this->once())
79+
->method('get')
80+
->with(
81+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
82+
$this->equalTo([]),
83+
$this->equalTo([])
84+
);
85+
86+
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
87+
$result = $zones->getHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', '0d89c70d-ad9f-4843-b99f-6cc0252067e9');
88+
89+
$this->assertObjectHasAttribute('id', $result);
90+
$this->assertObjectHasAttribute('hostname', $result);
91+
}
92+
93+
public function testUpdateHostname()
94+
{
95+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHostname.json');
96+
97+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
98+
$mock->method('patch')->willReturn($response);
99+
100+
$mock->expects($this->once())
101+
->method('patch')
102+
->with(
103+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
104+
$this->equalTo([]),
105+
$this->equalTo([
106+
'ssl' => (object)[
107+
'method' => 'http',
108+
'type' => 'dv'
109+
]
110+
])
111+
);
112+
113+
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
114+
$result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv');
115+
116+
$this->assertObjectHasAttribute('id', $result);
117+
$this->assertObjectHasAttribute('hostname', $result);
118+
}
119+
120+
public function testDeleteHostname()
121+
{
122+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteHostname.json');
123+
124+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
125+
$mock->method('delete')->willReturn($response);
126+
127+
$mock->expects($this->once())
128+
->method('delete')
129+
->with(
130+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
131+
$this->equalTo([]),
132+
$this->equalTo([])
133+
);
134+
135+
$zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
136+
$result = $zones->deleteHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9');
137+
138+
$this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->id);
139+
}
140+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"success": true,
3+
"errors": [
4+
{}
5+
],
6+
"messages": [
7+
{}
8+
],
9+
"result": {
10+
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
11+
"hostname": "app.example.com",
12+
"ssl": {
13+
"status": "pending_validation",
14+
"method": "http",
15+
"type": "dv",
16+
"cname_target": "dcv.digicert.com",
17+
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
18+
}
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9"
3+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"success": true,
3+
"errors": [
4+
{}
5+
],
6+
"messages": [
7+
{}
8+
],
9+
"result": {
10+
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
11+
"hostname": "app.example.com",
12+
"ssl": {
13+
"status": "pending_validation",
14+
"method": "http",
15+
"type": "dv",
16+
"cname_target": "dcv.digicert.com",
17+
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
18+
}
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"success": true,
3+
"errors": [
4+
{}
5+
],
6+
"messages": [
7+
{}
8+
],
9+
"result": [
10+
{
11+
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
12+
"hostname": "app.example.com",
13+
"ssl": {
14+
"status": "pending_validation",
15+
"method": "http",
16+
"type": "dv",
17+
"cname_target": "dcv.digicert.com",
18+
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com"
19+
}
20+
}
21+
],
22+
"result_info": {
23+
"page": 1,
24+
"per_page": 20,
25+
"count": 1,
26+
"total_count": 2000
27+
}
28+
}

0 commit comments

Comments
 (0)