Add example of a token with a policy to read allocs

Signed-off-by: josh <josh.timmons@hashicorp.com>
This commit is contained in:
josh
2023-06-05 21:00:29 -04:00
parent c0f49cec77
commit 2de03da519
3 changed files with 78 additions and 18 deletions
+1 -1
View File
@@ -47,4 +47,4 @@ export default async function createPlugin(
+ apiRouter.use('/nomad', await nomad(nomadEnv));
```
Note: for this backend to work, the `nomad` configuration described in the README of `@backstage/plugin-nomad` must be configured.
Note: for this backend to work, the `nomad` configuration described in the README of `@backstage/plugin-nomad` must be implemented.
+31 -15
View File
@@ -41,7 +41,7 @@ export async function createRouter(
resp.json({ status: 'ok' });
});
router.get('/v1/allocations', async (req, resp) => {
router.get('/v1/allocations', async (req, res) => {
// Check namespace argument
const namespace = (req.query.namespace as string) ?? '';
if (!namespace || namespace === '') {
@@ -53,23 +53,29 @@ export async function createRouter(
logger.debug(`request headers: namespace=${namespace} filter=${filter}`);
// Issue the request
const allocationsResp = await fetch(
`${addr}/v1/allocations?namespace=${encodeURIComponent(
namespace,
)}&filter=${encodeURIComponent(filter)}`,
{
method: 'GET',
headers: {
Accept: 'application/json',
'X-Nomad-Token': token || '',
},
const endpoint = `${addr}/v1/allocations?namespace=${encodeURIComponent(
namespace,
)}&filter=${encodeURIComponent(filter)}`;
const allocationsResp = await fetch(endpoint, {
method: 'GET',
headers: {
Accept: 'application/json',
'X-Nomad-Token': token || '',
},
);
});
// Check response
if (allocationsResp.status !== 200) {
const body = await allocationsResp.text();
logger.error(`failed to call /v1/allocations endpoint: ${body}`);
res.status(allocationsResp.status).send(body);
return;
}
// Deserialize and return
const allocationsBody = await allocationsResp.json();
logger.debug(`/v1/allocations response: ${allocationsBody}`);
resp.json(allocationsBody);
res.json(allocationsBody);
});
router.get('/v1/job/:job_id/versions', async (req, resp) => {
@@ -82,8 +88,10 @@ export async function createRouter(
// Get job ID
const jobID = (req.params.job_id as string) ?? '';
logger.info(`token: ${token}`);
// Issue the request
const versionsResp = await fetch(
const apiResp = await fetch(
`${addr}/v1/job/${jobID}/versions?namespace=${encodeURIComponent(
namespace,
)}`,
@@ -96,8 +104,16 @@ export async function createRouter(
},
);
// Check response
if (apiResp.status !== 200) {
const body = await apiResp.text();
logger.error(`failed to call /v1/job/:job_id/versions endpoint: ${body}`);
resp.status(apiResp.status).send(body);
return;
}
// Deserialize and return
const versionsBody = await versionsResp.json();
const versionsBody = await apiResp.json();
logger.debug(`/v1/job/:job_id/versions response: ${versionsBody}`);
resp.json(versionsBody);
});