Add Nomad Backend plugin

Signed-off-by: josh <josh.timmons@hashicorp.com>
This commit is contained in:
josh
2023-06-04 15:33:41 -04:00
parent 90913537ff
commit b373ade344
18 changed files with 402 additions and 46 deletions
+1 -23
View File
@@ -51,27 +51,5 @@
},
"files": [
"dist"
],
"configSchema": {
"$schema": "https://backstage.io/schema/config-v1",
"title": "@backstage/nomad",
"type": "object",
"properties": {
"nomad": {
"type": "object",
"properties": {
"addr": {
"type": "string",
"description": "The address of the Nomad API. See: https://developer.hashicorp.com/nomad/api-docs#addressing-and-ports",
"visibility": "frontend"
},
"token": {
"type": "string",
"description": "The token to call the Nomad API with. See: https://developer.hashicorp.com/nomad/api-docs#authentication",
"visibility": "secret"
}
}
}
}
}
]
}
+12 -19
View File
@@ -14,8 +14,11 @@
* limitations under the License.
*/
import { Config } from '@backstage/config';
import { createApiRef } from '@backstage/core-plugin-api';
import {
DiscoveryApi,
FetchApi,
createApiRef,
} from '@backstage/core-plugin-api';
/** @public */
export const nomadApiRef = createApiRef<NomadApi>({
@@ -81,32 +84,22 @@ export class FetchError extends Error {
/** @public */
export class NomadHttpApi implements NomadApi {
static fromConfig(config: Config) {
return new NomadHttpApi(
config.getOptionalString('nomad.addr'),
config.getOptionalString('nomad.token'),
);
static new(discoveryApi: DiscoveryApi, fetchApi: FetchApi) {
return new NomadHttpApi(discoveryApi, fetchApi);
}
constructor(
private addr: string = 'http://127.0.0.1:4646',
private token?: string,
) {}
constructor(private discoveryApi: DiscoveryApi, private fetchApi: FetchApi) {}
// TODO: pagination
async listAllocations(
options: ListAllocationsRequest,
): Promise<ListAllocationsResponse> {
const resp = await fetch(
`${this.addr}/v1/allocations?namespace=${encodeURIComponent(
const apiUrl = await this.discoveryApi.getBaseUrl('nomad');
const resp = await this.fetchApi.fetch(
`${apiUrl}/v1/allocations?namespace=${encodeURIComponent(
options.namespace,
)}&filter=${encodeURIComponent(options.filter)}`,
{
method: 'GET',
headers: {
'X-Nomad-Token': this.token || '',
},
},
);
if (!resp.ok) throw await FetchError.forResponse(resp);
@@ -44,6 +44,7 @@ export const GroupListForEntity = () => {
return;
}
// Issue call to nomad-backend
try {
const resp = await nomadApi.listAllocations({
namespace,
@@ -58,7 +59,7 @@ export const GroupListForEntity = () => {
if (response.loading) {
return <Progress />;
}
if (response.error || !response.value) {
if (response.error) {
return null;
}
+5 -3
View File
@@ -14,7 +14,8 @@
* limitations under the License.
*/
import {
configApiRef,
discoveryApiRef,
fetchApiRef,
createApiFactory,
createPlugin,
createRoutableExtension,
@@ -33,8 +34,9 @@ export const nomadPlugin = createPlugin({
apis: [
createApiFactory({
api: nomadApiRef,
deps: { configApi: configApiRef },
factory: ({ configApi }) => NomadHttpApi.fromConfig(configApi),
deps: { discoveryApi: discoveryApiRef, fetchApi: fetchApiRef },
factory: ({ discoveryApi, fetchApi }) =>
NomadHttpApi.new(discoveryApi, fetchApi),
}),
],
routes: {