diff --git a/src/PleskX/Api/Operator/Dns.php b/src/PleskX/Api/Operator/Dns.php index 7bcce32e..d8380b13 100644 --- a/src/PleskX/Api/Operator/Dns.php +++ b/src/PleskX/Api/Operator/Dns.php @@ -1,9 +1,69 @@ _client->getPacket(); + $info = $packet->addChild($this->_wrapperTag)->addChild('add_rec'); + + foreach ($properties as $name => $value) { + $info->addChild($name, $value); + } + + return new Struct\Info($this->_client->request($packet)); + } + + /** + * @param string $field + * @param integer|string $value + * @return Struct\Info + */ + public function get($field, $value) + { + $items = $this->getAll($field, $value); + return reset($items); + } + + /** + * @param string $field + * @param integer|string $value + * @return Struct\Info[] + */ + public function getAll($field, $value) + { + $packet = $this->_client->getPacket(); + $getTag = $packet->addChild($this->_wrapperTag)->addChild('get_rec'); + + $filterTag = $getTag->addChild('filter'); + if (!is_null($field)) { + $filterTag->addChild($field, $value); + } + + $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL); + $items = []; + foreach ($response->xpath('//result') as $xmlResult) { + $item = new Struct\Info($xmlResult->data); + $item->id = (int)$xmlResult->id; + $items[] = $item; + } + return $items; + } + /** + * @param string $field + * @param integer|string $value + * @return bool + */ + public function delete($field, $value) + { + return $this->_delete($field, $value, 'del_rec'); + } } diff --git a/src/PleskX/Api/Struct/Dns/Info.php b/src/PleskX/Api/Struct/Dns/Info.php new file mode 100644 index 00000000..e5021668 --- /dev/null +++ b/src/PleskX/Api/Struct/Dns/Info.php @@ -0,0 +1,41 @@ +_initScalarProperties($apiResponse, [ + 'id', + 'site-id', + 'site-alias-id', + 'type', + 'host', + 'value', + 'opt', + ]); + } +} diff --git a/tests/DnsTest.php b/tests/DnsTest.php new file mode 100644 index 00000000..44d82149 --- /dev/null +++ b/tests/DnsTest.php @@ -0,0 +1,94 @@ +webspace()->delete('id', static::$_webspace->id); + } + + public function testCreate() + { + $dns = static::$_client->dns()->create([ + 'site-id' => static::$_webspace->id, + 'type' => 'TXT', + 'host' => 'host', + 'value' => 'value' + ]); + $this->assertInternalType('integer', $dns->id); + $this->assertGreaterThan(0, $dns->id); + static::$_client->dns()->delete('id', $dns->id); + } + + public function testGetById() + { + $dns = static::$_client->dns()->create([ + 'site-id' => static::$_webspace->id, + 'type' => 'TXT', + 'host' => '', + 'value' => 'value' + ]); + + $dnsInfo = static::$_client->dns()->get('id', $dns->id); + $this->assertEquals('TXT', $dnsInfo->type); + $this->assertEquals(static::$_webspace->id, $dnsInfo->siteId); + $this->assertEquals('value', $dnsInfo->value); + + static::$_client->dns()->delete('id', $dns->id); + } + + public function testGetAllByWebspaceId() + { + $dns = static::$_client->dns()->create([ + 'site-id' => static::$_webspace->id, + 'type' => 'DS', + 'host' => '', + 'value' => '60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292118' + ]); + $dns2 = static::$_client->dns()->create([ + 'site-id' => static::$_webspace->id, + 'type' => 'DS', + 'host' => '', + 'value' => '60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292119' + ]); + $dnsInfo = static::$_client->dns()->getAll('site-id', static::$_webspace->id); + $dsRecords = []; + foreach ($dnsInfo as $dnsRec) { + if ('DS' == $dnsRec->type ) { + $dsRecords[] = $dnsRec; + } + } + $this->assertEquals(2, count($dsRecords)); + foreach ($dsRecords as $dsRecord) { + $this->assertEquals(static::$_webspace->id, $dsRecord->siteId); + } + + static::$_client->dns()->delete('id', $dns->id); + static::$_client->dns()->delete('id', $dns2->id); + } + + public function testDelete() + { + $dns = static::$_client->dns()->create([ + 'site-id' => static::$_webspace->id, + 'type' => 'TXT', + 'host' => 'host', + 'value' => 'value' + ]); + $result = static::$_client->dns()->delete('id', $dns->id); + $this->assertTrue($result); + } +}