Skip to content

Commit e3712f7

Browse files
authored
Merge pull request plesk#39 from plesk/dns-template
Add DNS template manipulations as a separate operator
2 parents 78753ab + 6fbccc8 commit e3712f7

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

src/PleskX/Api/Client.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@ public function dns()
419419
return $this->_getOperator('Dns');
420420
}
421421

422+
/**
423+
* @return Operator\DnsTemplate
424+
*/
425+
public function dnsTemplate()
426+
{
427+
return $this->_getOperator('DnsTemplate');
428+
}
429+
422430
/**
423431
* @return Operator\DatabaseServer
424432
*/
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
// Copyright 1999-2018. Plesk International GmbH.
3+
namespace PleskX\Api\Operator;
4+
use PleskX\Api\Struct\Dns as Struct;
5+
6+
class DnsTemplate extends \PleskX\Api\Operator
7+
{
8+
protected $_wrapperTag = 'dns';
9+
10+
/**
11+
* @param array $properties
12+
* @return Struct\Info
13+
*/
14+
public function create(array $properties)
15+
{
16+
$packet = $this->_client->getPacket();
17+
$info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');
18+
19+
unset($properties['site-id'], $properties['site-alias-id']);
20+
foreach ($properties as $name => $value) {
21+
$info->addChild($name, $value);
22+
}
23+
24+
return new Struct\Info($this->_client->request($packet));
25+
}
26+
27+
/**
28+
* @param string $field
29+
* @param integer|string $value
30+
* @return Struct\Info|null
31+
*/
32+
public function get($field, $value)
33+
{
34+
$items = $this->getAll($field, $value);
35+
return reset($items);
36+
}
37+
38+
/**
39+
* @param string $field
40+
* @param integer|string $value
41+
* @return Struct\Info[]
42+
*/
43+
public function getAll($field = null, $value = null)
44+
{
45+
$packet = $this->_client->getPacket();
46+
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get_rec');
47+
48+
$filterTag = $getTag->addChild('filter');
49+
if (!is_null($field)) {
50+
$filterTag->addChild($field, $value);
51+
}
52+
$getTag->addChild('template');
53+
54+
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
55+
$items = [];
56+
foreach ($response->xpath('//result') as $xmlResult) {
57+
$item = new Struct\Info($xmlResult->data);
58+
$item->id = (int)$xmlResult->id;
59+
$items[] = $item;
60+
}
61+
return $items;
62+
}
63+
64+
/**
65+
* @param string $field
66+
* @param integer|string $value
67+
* @return bool
68+
*/
69+
public function delete($field, $value)
70+
{
71+
$packet = $this->_client->getPacket();
72+
$delTag = $packet->addChild($this->_wrapperTag)->addChild('del_rec');
73+
$delTag->addChild('filter')->addChild($field, $value);
74+
$delTag->addChild('template');
75+
76+
$response = $this->_client->request($packet);
77+
return 'ok' === (string)$response->status;
78+
}
79+
}

tests/DnsTemplateTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
// Copyright 1999-2018. Plesk International GmbH.
3+
4+
class DnsTemplateTest extends TestCase
5+
{
6+
/**
7+
* @var bool
8+
*/
9+
private static $_isDnsSupported;
10+
11+
public static function setUpBeforeClass()
12+
{
13+
parent::setUpBeforeClass();
14+
15+
$serviceStates = static::$_client->server()->getServiceStates();
16+
static::$_isDnsSupported = $serviceStates['dns'] && ('running' == $serviceStates['dns']['state']);
17+
}
18+
19+
protected function setUp()
20+
{
21+
parent::setUp();
22+
23+
if (!static::$_isDnsSupported) {
24+
$this->markTestSkipped('DNS system is not supported.');
25+
}
26+
}
27+
28+
public function testCreate()
29+
{
30+
$dns = static::$_client->dnsTemplate()->create([
31+
'type' => 'TXT',
32+
'host' => 'test.create',
33+
'value' => 'value',
34+
]);
35+
$this->assertInternalType('integer', $dns->id);
36+
$this->assertGreaterThan(0, $dns->id);
37+
$this->assertEquals(0, $dns->siteId);
38+
$this->assertEquals(0, $dns->siteAliasId);
39+
static::$_client->dnsTemplate()->delete('id', $dns->id);
40+
}
41+
42+
public function testGetById()
43+
{
44+
$dns = static::$_client->dnsTemplate()->create([
45+
'type' => 'TXT',
46+
'host' => 'test.get.by.id',
47+
'value' => 'value',
48+
]);
49+
50+
$dnsInfo = static::$_client->dnsTemplate()->get('id', $dns->id);
51+
$this->assertEquals('TXT', $dnsInfo->type);
52+
$this->assertEquals('value', $dnsInfo->value);
53+
54+
static::$_client->dnsTemplate()->delete('id', $dns->id);
55+
}
56+
57+
public function testGetAll()
58+
{
59+
$dns = static::$_client->dnsTemplate()->create([
60+
'type' => 'TXT',
61+
'host' => 'test.get.all',
62+
'value' => 'value',
63+
]);
64+
$dns2 = static::$_client->dnsTemplate()->create([
65+
'type' => 'TXT',
66+
'host' => 'test.get.all',
67+
'value' => 'value2',
68+
]);
69+
$dnsInfo = static::$_client->dnsTemplate()->getAll();
70+
$dsRecords = [];
71+
foreach ($dnsInfo as $dnsRec) {
72+
if ('TXT' === $dnsRec->type && 0 === strpos($dnsRec->host, 'test.get.all')) {
73+
$dsRecords[] = $dnsRec;
74+
}
75+
}
76+
$this->assertCount(2, $dsRecords);
77+
78+
static::$_client->dnsTemplate()->delete('id', $dns->id);
79+
static::$_client->dnsTemplate()->delete('id', $dns2->id);
80+
}
81+
82+
public function testDelete()
83+
{
84+
$dns = static::$_client->dnsTemplate()->create([
85+
'type' => 'TXT',
86+
'host' => 'test.delete',
87+
'value' => 'value',
88+
]);
89+
$result = static::$_client->dnsTemplate()->delete('id', $dns->id);
90+
$this->assertTrue($result);
91+
}
92+
}

0 commit comments

Comments
 (0)