From f8d6be57399212f6da837ba08ee95933e0ef05da Mon Sep 17 00:00:00 2001 From: Vladimir Selliakhov Date: Wed, 16 Dec 2020 17:18:02 +0700 Subject: [PATCH] Added method for a bulk create and delete DNS records --- src/Api/Operator/Dns.php | 53 ++++++++++++++++++++++++++++++++++++ tests/DnsTest.php | 58 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/src/Api/Operator/Dns.php b/src/Api/Operator/Dns.php index 8b7e3f04..a10449b6 100644 --- a/src/Api/Operator/Dns.php +++ b/src/Api/Operator/Dns.php @@ -24,6 +24,34 @@ public function create($properties) return new Struct\Info($this->_client->request($packet)); } + /** + * Send multiply records by one request. + * + * @param array $records + * + * @return \PleskX\Api\XmlResponse[] + */ + public function bulkCreate(array $records) + { + $packet = $this->_client->getPacket(); + + foreach ($records as $properties) { + $info = $packet->addChild($this->_wrapperTag)->addChild('add_rec'); + + foreach ($properties as $name => $value) { + $info->addChild($name, $value); + } + } + + $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL); + $items = []; + foreach ($response->xpath('//result') as $xmlResult) { + $items[] = $xmlResult; + } + + return $items; + } + /** * @param string $field * @param int|string $value @@ -74,4 +102,29 @@ public function delete($field, $value) { return $this->_delete($field, $value, 'del_rec'); } + + /** + * Delete multiply records by one request. + * + * @param array $recordIds + * + * @return \PleskX\Api\XmlResponse[] + */ + public function bulkDelete(array $recordIds) + { + $packet = $this->_client->getPacket(); + + foreach ($recordIds as $recordId) { + $packet->addChild($this->_wrapperTag)->addChild('del_rec') + ->addChild('filter')->addChild('id', $recordId); + } + + $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL); + $items = []; + foreach ($response->xpath('//result') as $xmlResult) { + $items[] = $xmlResult; + } + + return $items; + } } diff --git a/tests/DnsTest.php b/tests/DnsTest.php index 5feadcac..633f50d7 100644 --- a/tests/DnsTest.php +++ b/tests/DnsTest.php @@ -44,6 +44,64 @@ public function testCreate() static::$_client->dns()->delete('id', $dns->id); } + /** + * @return \PleskX\Api\XmlResponse[] + */ + public function testBulkCreate() + { + $response = static::$_client->dns()->bulkCreate([ + [ + 'site-id' => static::$webspace->id, + 'type' => 'TXT', + 'host' => 'host', + 'value' => 'value', + ], + [ + 'site-id' => static::$webspace->id, + 'type' => 'A', + 'host' => 'host', + 'value' => '1.1.1.1', + ], + [ + 'site-id' => static::$webspace->id, + 'type' => 'MX', + 'host' => 'custom-mail', + 'value' => '1.1.1.1', + 'opt' => '10', + ], + ]); + + $this->assertCount(3, $response); + + foreach ($response as $xml) { + $this->assertEquals('ok', (string) $xml->status); + $this->assertGreaterThan(0, (int) $xml->id); + } + + return $response; + } + + /** + * @depends testBulkCreate + * + * @param \PleskX\Api\XmlResponse[] $createdRecords + */ + public function testBulkDelete(array $createdRecords) + { + $createdRecordIds = array_map(function ($record) { + return (int) $record->id; + }, $createdRecords); + + $response = static::$_client->dns()->bulkDelete($createdRecordIds); + + $this->assertCount(3, $response); + + foreach ($response as $xml) { + $this->assertEquals('ok', (string) $xml->status); + $this->assertGreaterThan(0, (int) $xml->id); + } + } + public function testGetById() { $dns = static::$_client->dns()->create([