diff --git a/src/Endpoints/DNS.php b/src/Endpoints/DNS.php index a9f74749..ae6aad93 100644 --- a/src/Endpoints/DNS.php +++ b/src/Endpoints/DNS.php @@ -10,6 +10,7 @@ use Cloudflare\API\Adapter\Adapter; use Cloudflare\API\Traits\BodyAccessorTrait; +use Psr\Http\Message\StreamInterface; class DNS implements API { @@ -46,8 +47,8 @@ public function addRecord( array $data = [] ): bool { $options = [ - 'type' => $type, - 'name' => $name, + 'type' => $type, + 'name' => $name, 'content' => $content, 'proxied' => $proxied ]; @@ -59,7 +60,7 @@ public function addRecord( if (is_numeric($priority)) { $options['priority'] = (int)$priority; } - + if (!empty($data)) { $options['data'] = $data; } @@ -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)) { @@ -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, + ]; + } } diff --git a/tests/Endpoints/DNSTest.php b/tests/Endpoints/DNSTest.php index 2eec5536..505d4bda 100644 --- a/tests/Endpoints/DNSTest.php +++ b/tests/Endpoints/DNSTest.php @@ -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); + } } diff --git a/tests/Fixtures/Endpoints/importRecordsFromBindFile.json b/tests/Fixtures/Endpoints/importRecordsFromBindFile.json new file mode 100644 index 00000000..93179670 --- /dev/null +++ b/tests/Fixtures/Endpoints/importRecordsFromBindFile.json @@ -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 + } +}