Skip to content

service/brancher: Add update method #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' => []]);
}
}