Skip to content

Commit 17409be

Browse files
Merge pull request plesk#16 from vbaranovskiy-plesk/master
Add methods to "dns" object (create,get, delete)
2 parents a081fe5 + aa8ef68 commit 17409be

File tree

3 files changed

+196
-1
lines changed

3 files changed

+196
-1
lines changed

src/PleskX/Api/Operator/Dns.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,69 @@
11
<?php
22
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3-
43
namespace PleskX\Api\Operator;
4+
use PleskX\Api\Struct\Dns as Struct;
55

66
class Dns extends \PleskX\Api\Operator
77
{
8+
/**
9+
* @param array $properties
10+
* @return Struct\Info
11+
*/
12+
public function create($properties)
13+
{
14+
$packet = $this->_client->getPacket();
15+
$info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');
16+
17+
foreach ($properties as $name => $value) {
18+
$info->addChild($name, $value);
19+
}
20+
21+
return new Struct\Info($this->_client->request($packet));
22+
}
23+
24+
/**
25+
* @param string $field
26+
* @param integer|string $value
27+
* @return Struct\Info
28+
*/
29+
public function get($field, $value)
30+
{
31+
$items = $this->getAll($field, $value);
32+
return reset($items);
33+
}
34+
35+
/**
36+
* @param string $field
37+
* @param integer|string $value
38+
* @return Struct\Info[]
39+
*/
40+
public function getAll($field, $value)
41+
{
42+
$packet = $this->_client->getPacket();
43+
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get_rec');
44+
45+
$filterTag = $getTag->addChild('filter');
46+
if (!is_null($field)) {
47+
$filterTag->addChild($field, $value);
48+
}
49+
50+
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
51+
$items = [];
52+
foreach ($response->xpath('//result') as $xmlResult) {
53+
$item = new Struct\Info($xmlResult->data);
54+
$item->id = (int)$xmlResult->id;
55+
$items[] = $item;
56+
}
57+
return $items;
58+
}
859

60+
/**
61+
* @param string $field
62+
* @param integer|string $value
63+
* @return bool
64+
*/
65+
public function delete($field, $value)
66+
{
67+
return $this->_delete($field, $value, 'del_rec');
68+
}
969
}

src/PleskX/Api/Struct/Dns/Info.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3+
4+
namespace PleskX\Api\Struct\Dns;
5+
6+
class Info extends \PleskX\Api\Struct
7+
{
8+
/** @var integer */
9+
public $id;
10+
11+
/** @var integer */
12+
public $siteId;
13+
14+
/** @var integer */
15+
public $siteAliasId;
16+
17+
/** @var string */
18+
public $type;
19+
20+
/** @var string */
21+
public $host;
22+
23+
/** @var string */
24+
public $value;
25+
26+
/** @var string */
27+
public $opt;
28+
29+
public function __construct($apiResponse)
30+
{
31+
$this->_initScalarProperties($apiResponse, [
32+
'id',
33+
'site-id',
34+
'site-alias-id',
35+
'type',
36+
'host',
37+
'value',
38+
'opt',
39+
]);
40+
}
41+
}

tests/DnsTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3+
4+
class DnsTest extends TestCase
5+
{
6+
/**
7+
* @var \PleskX\Api\Struct\Webspace\Info
8+
*/
9+
private static $_webspace;
10+
11+
public static function setUpBeforeClass()
12+
{
13+
parent::setUpBeforeClass();
14+
static::$_webspace = static::_createWebspace('example.dom');
15+
}
16+
17+
public static function tearDownAfterClass()
18+
{
19+
parent::tearDownAfterClass();
20+
static::$_client->webspace()->delete('id', static::$_webspace->id);
21+
}
22+
23+
public function testCreate()
24+
{
25+
$dns = static::$_client->dns()->create([
26+
'site-id' => static::$_webspace->id,
27+
'type' => 'TXT',
28+
'host' => 'host',
29+
'value' => 'value'
30+
]);
31+
$this->assertInternalType('integer', $dns->id);
32+
$this->assertGreaterThan(0, $dns->id);
33+
static::$_client->dns()->delete('id', $dns->id);
34+
}
35+
36+
public function testGetById()
37+
{
38+
$dns = static::$_client->dns()->create([
39+
'site-id' => static::$_webspace->id,
40+
'type' => 'TXT',
41+
'host' => '',
42+
'value' => 'value'
43+
]);
44+
45+
$dnsInfo = static::$_client->dns()->get('id', $dns->id);
46+
$this->assertEquals('TXT', $dnsInfo->type);
47+
$this->assertEquals(static::$_webspace->id, $dnsInfo->siteId);
48+
$this->assertEquals('value', $dnsInfo->value);
49+
50+
static::$_client->dns()->delete('id', $dns->id);
51+
}
52+
53+
public function testGetAllByWebspaceId()
54+
{
55+
$dns = static::$_client->dns()->create([
56+
'site-id' => static::$_webspace->id,
57+
'type' => 'DS',
58+
'host' => '',
59+
'value' => '60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292118'
60+
]);
61+
$dns2 = static::$_client->dns()->create([
62+
'site-id' => static::$_webspace->id,
63+
'type' => 'DS',
64+
'host' => '',
65+
'value' => '60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292119'
66+
]);
67+
$dnsInfo = static::$_client->dns()->getAll('site-id', static::$_webspace->id);
68+
$dsRecords = [];
69+
foreach ($dnsInfo as $dnsRec) {
70+
if ('DS' == $dnsRec->type ) {
71+
$dsRecords[] = $dnsRec;
72+
}
73+
}
74+
$this->assertEquals(2, count($dsRecords));
75+
foreach ($dsRecords as $dsRecord) {
76+
$this->assertEquals(static::$_webspace->id, $dsRecord->siteId);
77+
}
78+
79+
static::$_client->dns()->delete('id', $dns->id);
80+
static::$_client->dns()->delete('id', $dns2->id);
81+
}
82+
83+
public function testDelete()
84+
{
85+
$dns = static::$_client->dns()->create([
86+
'site-id' => static::$_webspace->id,
87+
'type' => 'TXT',
88+
'host' => 'host',
89+
'value' => 'value'
90+
]);
91+
$result = static::$_client->dns()->delete('id', $dns->id);
92+
$this->assertTrue($result);
93+
}
94+
}

0 commit comments

Comments
 (0)