Skip to content

Commit ad5f69c

Browse files
author
Jonathan Visser
committed
Add support for creating backups (POST /v2/app/{app}/backup/)
1 parent a5b807e commit ad5f69c

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/Service/Backup.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,24 @@ public function getList(string $app, array $params = []): array
4444

4545
return $backups;
4646
}
47+
48+
/**
49+
* Create a new snapshot backup. This requires sla-standard to be enabled
50+
* on the Hypernode.
51+
*
52+
* @param string $app Name of the Hypernode
53+
* @return string Response message
54+
* @throws HypernodeApiClientException
55+
* @throws HypernodeApiServerException
56+
*/
57+
public function create(string $app): string
58+
{
59+
$url = sprintf(self::V2_APP_BACKUP_URL, $app);
60+
61+
$response = $this->client->api->post($url);
62+
63+
$this->client->maybeThrowApiExceptions($response);
64+
65+
return $this->client->getJsonFromResponse($response);
66+
}
4767
}

tests/unit/Service/BackupTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,42 @@ public function testGetListRaisesServerExceptions()
131131

132132
$this->client->backup->getList('johndoe');
133133
}
134+
135+
public function testCreate()
136+
{
137+
$this->responses->append(
138+
new Response(200, [], json_encode('Backup creation initiated')),
139+
);
140+
141+
$result = $this->client->backup->create('johndoe');
142+
143+
$request = $this->responses->getLastRequest();
144+
$this->assertEquals('POST', $request->getMethod());
145+
$this->assertEquals('/v2/app/johndoe/backup/', $request->getUri());
146+
$this->assertEquals('Backup creation initiated', $result);
147+
}
148+
149+
public function testCreateRaisesClientExceptions()
150+
{
151+
$badRequestResponse = new Response(400, [], json_encode(
152+
'Creating backups requires sla-standard to be enabled.'
153+
));
154+
$this->responses->append($badRequestResponse);
155+
156+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
157+
158+
$this->client->backup->create('johndoe');
159+
}
160+
161+
public function testCreateRaisesServerExceptions()
162+
{
163+
$badRequestResponse = new Response(500, [], json_encode([
164+
'non_field_errors' => ['Something went wrong processing your request.']
165+
]));
166+
$this->responses->append($badRequestResponse);
167+
168+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
169+
170+
$this->client->backup->create('johndoe');
171+
}
134172
}

0 commit comments

Comments
 (0)