diff --git a/src/Service/BrancherApp.php b/src/Service/BrancherApp.php index 26ae7e9..1403186 100644 --- a/src/Service/BrancherApp.php +++ b/src/Service/BrancherApp.php @@ -60,13 +60,22 @@ public function create(string $app, ?array $data = null): string * * @param string $name Name of the Brancher node * @param array $data Data to be updated + * @param bool $appendLabels Whether to append labels or to overwrite * @return array Updated data * @throws HypernodeApiClientException * @throws HypernodeApiServerException */ - public function update(string $name, array $data): array + public function update(string $name, array $data, bool $appendLabels = false): array { $url = sprintf(App::V2_BRANCHER_DETAIL_URL, $name); + + if ($appendLabels) { + $originHypernode = substr($name, 0, strrpos($name, '-')); + $existingLabels = $this->list($originHypernode)[0]['labels'] ?? []; + foreach (explode('&', http_build_query($existingLabels)) as $label) { + $data['labels'][] = $label; + } + } $response = $this->client->api->put($url, [], json_encode($data)); diff --git a/tests/unit/Service/BrancherAppTest.php b/tests/unit/Service/BrancherAppTest.php index a7d8310..309d4ca 100644 --- a/tests/unit/Service/BrancherAppTest.php +++ b/tests/unit/Service/BrancherAppTest.php @@ -183,6 +183,46 @@ public function testUpdateBrancherApp() ); } + public function testUpdateBrancherAppAppendsLabels() + { + $this->responses->append( + // List Brancher apps + new Response(200, [], json_encode([ + 'branchers' => [ + [ + 'name' => 'johndoe-eph123456', + 'labels' => ['key1' => 'value1'] + ] + ] + ])), + // Update Brancher app + new Response(200, [], json_encode([ + 'labels' => [ + 'key1' => 'value1', + 'key2' => 'value2', + ] + ])), + ); + + $result = $this->client->brancherApp->update( + 'johndoe-eph123456', ['labels' => ['key2=value2']], + true + ); + + $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' => ['key2=value2', 'key1=value1']], + json_decode((string)$request->getBody(), true) + ); + $this->assertEquals( + ['labels' => [ 'key1' => 'value1', 'key2' => 'value2']], + $result + ); + } + public function testUpdateBrancherAppRaisesClientExceptions() { $badRequestResponse = new Response(400, [], json_encode([