Skip to content

Commit

Permalink
Merge pull request #17 from ByteInternet/brancher_update
Browse files Browse the repository at this point in the history
  • Loading branch information
tdgroot authored Apr 4, 2023
2 parents 93d8d97 + 8a6e0d0 commit 1726fea
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/Service/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Service/BrancherApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/Service/BrancherAppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => []]);
}
}

0 comments on commit 1726fea

Please sign in to comment.