From 9948ecb0ee5a47f6911e3d8f25d33c0fb1fbdb93 Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Tue, 4 Apr 2023 20:57:45 +0200 Subject: [PATCH 1/3] Add option to append labels instead of overwrite --- src/Service/BrancherApp.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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)); From 95015c9f2e4454908385509e0e6e1675b6800fca Mon Sep 17 00:00:00 2001 From: Timon de Groot Date: Wed, 5 Apr 2023 09:10:29 +0200 Subject: [PATCH 2/3] service/brancher: Add test for update with appendLabels option --- banaan.php | 15 ++++++++++ tests/unit/Service/BrancherAppTest.php | 40 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 banaan.php diff --git a/banaan.php b/banaan.php new file mode 100644 index 0000000..a0ed078 --- /dev/null +++ b/banaan.php @@ -0,0 +1,15 @@ +brancherApp->list('hntestgroot'); + +var_dump($active[0]['labels']); + +$result = $client->brancherApp->update('hntestgroot-ephx0u2z7', ['labels' => ['mykey='.time()]]); + +var_dump($result); \ No newline at end of file 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([ From 2c7a8210a155493ae3688deb87679672e5a8578f Mon Sep 17 00:00:00 2001 From: Timon de Groot Date: Wed, 5 Apr 2023 09:29:39 +0200 Subject: [PATCH 3/3] Remove development testing script --- banaan.php | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 banaan.php diff --git a/banaan.php b/banaan.php deleted file mode 100644 index a0ed078..0000000 --- a/banaan.php +++ /dev/null @@ -1,15 +0,0 @@ -brancherApp->list('hntestgroot'); - -var_dump($active[0]['labels']); - -$result = $client->brancherApp->update('hntestgroot-ephx0u2z7', ['labels' => ['mykey='.time()]]); - -var_dump($result); \ No newline at end of file