Skip to content

Commit 1726fea

Browse files
authored
Merge pull request #17 from ByteInternet/brancher_update
2 parents 93d8d97 + 8a6e0d0 commit 1726fea

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Here's a list of Hypernode API features implemented in the client.
5050
- Listing Hypernodes related to your API key
5151
- Updating one or multiple Hypernode settings at once.
5252
- Querying/polling the logbook for the status of a job.
53-
- Listing, creating and cancelling Brancher Hypernode instances.
53+
- Listing, creating, updating and cancelling Brancher Hypernode instances.
5454

5555
## Related projects
5656

src/Service/App.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class App extends AbstractService
1010
public const V2_APP_DETAIL_URL = "/v2/app/%s/";
1111
public const V2_APP_CANCEL_URL = "/v2/app/%s/cancel/";
1212
public const V2_BRANCHER_APP_URL = "/v2/brancher/app/%s/";
13+
public const V2_BRANCHER_DETAIL_URL = "/v2/brancher/%s/";
1314
public const V1_APP_FLOWS_URL = "/logbook/v1/logbooks/%s/flows/";
1415

1516
/**

src/Service/BrancherApp.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ public function create(string $app, ?array $data = null): string
5353
return $data['name'];
5454
}
5555

56+
/**
57+
* Update a Brancher app.
58+
*
59+
* Currently, only the `labels` field is supported.
60+
*
61+
* @param string $name Name of the Brancher node
62+
* @param array $data Data to be updated
63+
* @return array Updated data
64+
* @throws HypernodeApiClientException
65+
* @throws HypernodeApiServerException
66+
*/
67+
public function update(string $name, array $data): array
68+
{
69+
$url = sprintf(App::V2_BRANCHER_DETAIL_URL, $name);
70+
71+
$response = $this->client->api->put($url, [], json_encode($data));
72+
73+
$this->client->maybeThrowApiExceptions($response);
74+
75+
/** @var array $data */
76+
$data = $this->client->getJsonFromResponse($response);
77+
78+
return $data;
79+
}
80+
5681
/**
5782
* Cancel an brancher app.
5883
*

tests/unit/Service/BrancherAppTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,52 @@ public function testCancelBrancherAppRaisesServerExceptions()
158158

159159
$this->client->brancherApp->cancel('johndoe');
160160
}
161+
162+
public function testUpdateBrancherApp()
163+
{
164+
$this->responses->append(
165+
new Response(200, [], json_encode([
166+
'labels' => ['somekey' => 'somevalue']
167+
])),
168+
);
169+
170+
$result = $this->client->brancherApp->update('johndoe-eph123456', ['labels' => ['somekey=somevalue']]);
171+
172+
$request = $this->responses->getLastRequest();
173+
$this->assertEquals('PUT', $request->getMethod());
174+
$this->assertEquals('/v2/brancher/johndoe-eph123456/', $request->getUri());
175+
$this->assertJson((string)$request->getBody());
176+
$this->assertEquals(
177+
['labels' => ['somekey=somevalue']],
178+
json_decode((string)$request->getBody(), true)
179+
);
180+
$this->assertEquals(
181+
['labels' => ['somekey' => 'somevalue']],
182+
$result
183+
);
184+
}
185+
186+
public function testUpdateBrancherAppRaisesClientExceptions()
187+
{
188+
$badRequestResponse = new Response(400, [], json_encode([
189+
'non_field_errors' => ['Your request was invalid.']
190+
]));
191+
$this->responses->append($badRequestResponse);
192+
193+
$this->expectExceptionObject(new HypernodeApiClientException($badRequestResponse));
194+
195+
$this->client->brancherApp->update('johndoe', ['labels' => []]);
196+
}
197+
198+
public function testUpdateBrancherAppRaisesServerExceptions()
199+
{
200+
$badRequestResponse = new Response(500, [], json_encode([
201+
'non_field_errors' => ['Something went wrong processing your request.']
202+
]));
203+
$this->responses->append($badRequestResponse);
204+
205+
$this->expectExceptionObject(new HypernodeApiServerException($badRequestResponse));
206+
207+
$this->client->brancherApp->update('johndoe', ['labels' => []]);
208+
}
161209
}

0 commit comments

Comments
 (0)