device-api: Add v2/device/name endpoint

This endpoint returns the last known device name from the API. This
differs from the BALENA_DEVICE_NAME_AT_INIT env var because this will
not change throughout the runtime of the container.

Closes: #908
Change-type: minor
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2019-03-12 13:56:31 +00:00
parent 2855295432
commit 3f231e8ff3
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
2 changed files with 39 additions and 0 deletions

View File

@ -1102,3 +1102,27 @@ Response:
"serviceName": "main"
}
```
### V2 Device Information
#### Device name
Added in supervisor version v9.11.0
Get the last returned device name from the balena API. Note that this differs from the
`BALENA_DEVICE_NAME_AT_INIT` environment variable provided to containers, as this will
not change throughout the runtime of the container, but the endpoint will always return
the latest known device name.
From an application container:
```
$ curl "$BALENA_SUPERVISOR_ADDRESS/v2/device/name?apikey=$BALENA_SUPERVISOR_API_KEY"
```
Response:
```json
{
"status": "success",
"deviceName": "holy-wildflower"
}
```

View File

@ -436,4 +436,19 @@ export function createV2Api(router: Router, applications: ApplicationManager) {
release: currentRelease,
});
});
router.get('/v2/device/name', async (_req, res) => {
try {
const deviceName = await applications.config.get('name');
res.json({
status: 'success',
deviceName,
});
} catch (e) {
res.status(503).json({
status: 'failed',
message: messageFromError(e),
});
}
});
}