Skip to content

Commit dd83464

Browse files
author
Vladimir Selliakhov
committed
Added method for a bulk create and delete DNS records
1 parent 3285435 commit dd83464

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/Api/Operator/Dns.php

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

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

tests/DnsTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,63 @@ 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+
* @param \PleskX\Api\XmlResponse[] $createdRecords
87+
*/
88+
public function testBulkDelete(array $createdRecords)
89+
{
90+
$createdRecordIds = array_map(function ($record) {
91+
return (int) $record->id;
92+
}, $createdRecords);
93+
94+
$response = static::$_client->dns()->bulkDelete($createdRecordIds);
95+
96+
$this->assertCount(3, $response);
97+
98+
foreach ($response as $xml) {
99+
$this->assertEquals('ok', (string) $xml->status);
100+
$this->assertGreaterThan(0, (int) $xml->id);
101+
}
102+
}
103+
47104
public function testGetById()
48105
{
49106
$dns = static::$_client->dns()->create([

0 commit comments

Comments
 (0)