diff --git a/README.md b/README.md index cc7b8a7..a08477d 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Here's a list of Hypernode API features implemented in the client. - Listing Hypernodes related to your API key - Updating one or multiple Hypernode settings at once. - Querying/polling the logbook for the status of a job. -- Listing, creating and cancelling Brancher Hypernode instances. +- Listing, creating, updating and cancelling Brancher Hypernode instances. ## Related projects diff --git a/src/Service/App.php b/src/Service/App.php index 29dedfe..c2eac05 100644 --- a/src/Service/App.php +++ b/src/Service/App.php @@ -10,6 +10,7 @@ class App extends AbstractService public const V2_APP_DETAIL_URL = "/v2/app/%s/"; public const V2_APP_CANCEL_URL = "/v2/app/%s/cancel/"; public const V2_BRANCHER_APP_URL = "/v2/brancher/app/%s/"; + public const V2_BRANCHER_DETAIL_URL = "/v2/brancher/%s/"; public const V1_APP_FLOWS_URL = "/logbook/v1/logbooks/%s/flows/"; /** diff --git a/src/Service/BrancherApp.php b/src/Service/BrancherApp.php index aca7489..26ae7e9 100644 --- a/src/Service/BrancherApp.php +++ b/src/Service/BrancherApp.php @@ -53,6 +53,31 @@ public function create(string $app, ?array $data = null): string return $data['name']; } + /** + * Update a Brancher app. + * + * Currently, only the `labels` field is supported. + * + * @param string $name Name of the Brancher node + * @param array $data Data to be updated + * @return array Updated data + * @throws HypernodeApiClientException + * @throws HypernodeApiServerException + */ + public function update(string $name, array $data): array + { + $url = sprintf(App::V2_BRANCHER_DETAIL_URL, $name); + + $response = $this->client->api->put($url, [], json_encode($data)); + + $this->client->maybeThrowApiExceptions($response); + + /** @var array $data */ + $data = $this->client->getJsonFromResponse($response); + + return $data; + } + /** * Cancel an brancher app. * diff --git a/tests/unit/Service/BrancherAppTest.php b/tests/unit/Service/BrancherAppTest.php index 80f55c8..a7d8310 100644 --- a/tests/unit/Service/BrancherAppTest.php +++ b/tests/unit/Service/BrancherAppTest.php @@ -158,4 +158,52 @@ public function testCancelBrancherAppRaisesServerExceptions() $this->client->brancherApp->cancel('johndoe'); } + + public function testUpdateBrancherApp() + { + $this->responses->append( + new Response(200, [], json_encode([ + 'labels' => ['somekey' => 'somevalue'] + ])), + ); + + $result = $this->client->brancherApp->update('johndoe-eph123456', ['labels' => ['somekey=somevalue']]); + + $request = $this->responses->getLastRequest(); + $this->assertEquals('PUT', $request->getMethod()); + $this->assertEquals('/v2/brancher/johndoe-eph123456/', $request->getUri()); + $this->assertJson((string)$request->getBody()); + $this->assertEquals( + ['labels' => ['somekey=somevalue']], + json_decode((string)$request->getBody(), true) + ); + $this->assertEquals( + ['labels' => ['somekey' => 'somevalue']], + $result + ); + } + + public function testUpdateBrancherAppRaisesClientExceptions() + { + $badRequestResponse = new Response(400, [], json_encode([ + 'non_field_errors' => ['Your request was invalid.'] + ])); + $this->responses->append($badRequestResponse); + + $this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse)); + + $this->client->brancherApp->update('johndoe', ['labels' => []]); + } + + public function testUpdateBrancherAppRaisesServerExceptions() + { + $badRequestResponse = new Response(500, [], json_encode([ + 'non_field_errors' => ['Something went wrong processing your request.'] + ])); + $this->responses->append($badRequestResponse); + + $this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse)); + + $this->client->brancherApp->update('johndoe', ['labels' => []]); + } }