Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/Endpoints/DNS.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;
use Psr\Http\Message\StreamInterface;

class DNS implements API
{
Expand Down Expand Up @@ -46,8 +47,8 @@ public function addRecord(
array $data = []
): bool {
$options = [
'type' => $type,
'name' => $name,
'type' => $type,
'name' => $name,
'content' => $content,
'proxied' => $proxied
];
Expand All @@ -59,7 +60,7 @@ public function addRecord(
if (is_numeric($priority)) {
$options['priority'] = (int)$priority;
}

if (!empty($data)) {
$options['data'] = $data;
}
Expand Down Expand Up @@ -87,9 +88,9 @@ public function listRecords(
string $match = 'all'
): \stdClass {
$query = [
'page' => $page,
'page' => $page,
'per_page' => $perPage,
'match' => $match
'match' => $match
];

if (!empty($type)) {
Expand Down Expand Up @@ -153,4 +154,33 @@ public function deleteRecord(string $zoneID, string $recordID): bool

return false;
}

/**
* @param string $cloudflare_zone_id
* @param StreamInterface $bind_file_stream
* @return \stdClass
*/
public function importRecordsFromBindFile(
string $cloudflare_zone_id,
StreamInterface $bind_file_stream
): \stdClass {
$options = [
'multipart' => [
[
'name' => 'file',
'contents' => $bind_file_stream->getContents(),
'filename' => '@bind_config.txt',
]
]
];

$response = $this->adapter->post('zones/' . $cloudflare_zone_id . '/dns_records/import', $options);

$this->body = json_decode($response->getBody());

return (object)[
'result' => $this->body->result,
'messages' => $this->body->messages,
];
}
}
32 changes: 32 additions & 0 deletions tests/Endpoints/DNSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,36 @@ public function testUpdateDNSRecord()
$this->assertEquals($result->result->{ $property }, $value);
}
}

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

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

$stream = \GuzzleHttp\Psr7\stream_for('string data');
$options = [
'multipart' => [
[
'name' => 'file',
'contents' => 'string data',
'filename' => '@bind_config.txt',
]
]
];
$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/import'),
$this->equalTo($options)
);

$dns = new \Cloudflare\API\Endpoints\DNS($mock);
$result = $dns->importRecordsFromBindFile('023e105f4ecef8ad9ca31a8372d0c353', $stream);

$this->assertEquals(5, $result->result->recs_added);
$this->assertEquals(5, $result->result->total_records_parsed);
$this->assertEquals([], $result->messages);
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/Endpoints/importRecordsFromBindFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"recs_added": 5,
"total_records_parsed": 5
},
"timing": {
"start_time": "2014-03-01T12:20:00Z",
"end_time": "2014-03-01T12:20:01Z",
"process_time": 1
}
}