Skip to content

Commit 824e885

Browse files
Merge pull request #86 from guyasyou/master
Added method for a bulk create and delete DNS records
2 parents 3285435 + f8d6be5 commit 824e885

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/Api/Operator/Dns.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ public function create($properties)
2424
return new Struct\Info($this->_client->request($packet));
2525
}
2626

27+
/**
28+
* Send multiply records by one request.
29+
*
30+
* @param array $records
31+
*
32+
* @return \PleskX\Api\XmlResponse[]
33+
*/
34+
public function bulkCreate(array $records)
35+
{
36+
$packet = $this->_client->getPacket();
37+
38+
foreach ($records as $properties) {
39+
$info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');
40+
41+
foreach ($properties as $name => $value) {
42+
$info->addChild($name, $value);
43+
}
44+
}
45+
46+
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
47+
$items = [];
48+
foreach ($response->xpath('//result') as $xmlResult) {
49+
$items[] = $xmlResult;
50+
}
51+
52+
return $items;
53+
}
54+
2755
/**
2856
* @param string $field
2957
* @param int|string $value
@@ -74,4 +102,29 @@ public function delete($field, $value)
74102
{
75103
return $this->_delete($field, $value, 'del_rec');
76104
}
105+
106+
/**
107+
* Delete multiply records by one request.
108+
*
109+
* @param array $recordIds
110+
*
111+
* @return \PleskX\Api\XmlResponse[]
112+
*/
113+
public function bulkDelete(array $recordIds)
114+
{
115+
$packet = $this->_client->getPacket();
116+
117+
foreach ($recordIds as $recordId) {
118+
$packet->addChild($this->_wrapperTag)->addChild('del_rec')
119+
->addChild('filter')->addChild('id', $recordId);
120+
}
121+
122+
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
123+
$items = [];
124+
foreach ($response->xpath('//result') as $xmlResult) {
125+
$items[] = $xmlResult;
126+
}
127+
128+
return $items;
129+
}
77130
}

tests/DnsTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,64 @@ public function testCreate()
4444
static::$_client->dns()->delete('id', $dns->id);
4545
}
4646

47+
/**
48+
* @return \PleskX\Api\XmlResponse[]
49+
*/
50+
public function testBulkCreate()
51+
{
52+
$response = static::$_client->dns()->bulkCreate([
53+
[
54+
'site-id' => static::$webspace->id,
55+
'type' => 'TXT',
56+
'host' => 'host',
57+
'value' => 'value',
58+
],
59+
[
60+
'site-id' => static::$webspace->id,
61+
'type' => 'A',
62+
'host' => 'host',
63+
'value' => '1.1.1.1',
64+
],
65+
[
66+
'site-id' => static::$webspace->id,
67+
'type' => 'MX',
68+
'host' => 'custom-mail',
69+
'value' => '1.1.1.1',
70+
'opt' => '10',
71+
],
72+
]);
73+
74+
$this->assertCount(3, $response);
75+
76+
foreach ($response as $xml) {
77+
$this->assertEquals('ok', (string) $xml->status);
78+
$this->assertGreaterThan(0, (int) $xml->id);
79+
}
80+
81+
return $response;
82+
}
83+
84+
/**
85+
* @depends testBulkCreate
86+
*
87+
* @param \PleskX\Api\XmlResponse[] $createdRecords
88+
*/
89+
public function testBulkDelete(array $createdRecords)
90+
{
91+
$createdRecordIds = array_map(function ($record) {
92+
return (int) $record->id;
93+
}, $createdRecords);
94+
95+
$response = static::$_client->dns()->bulkDelete($createdRecordIds);
96+
97+
$this->assertCount(3, $response);
98+
99+
foreach ($response as $xml) {
100+
$this->assertEquals('ok', (string) $xml->status);
101+
$this->assertGreaterThan(0, (int) $xml->id);
102+
}
103+
}
104+
47105
public function testGetById()
48106
{
49107
$dns = static::$_client->dns()->create([

0 commit comments

Comments
 (0)