Merge pull request #5043 from backstage/freben/unregister

Update the entity unregister dialog behavior, to support both unregistration as well as plain deletion
This commit is contained in:
Fredrik Adelöw
2021-03-30 09:40:53 +02:00
committed by GitHub
24 changed files with 1026 additions and 202 deletions
+69 -31
View File
@@ -19,6 +19,7 @@ import {
EntityName,
Location,
LOCATION_ANNOTATION,
ORIGIN_LOCATION_ANNOTATION,
stringifyLocationReference,
} from '@backstage/catalog-model';
import { ResponseError } from '@backstage/errors';
@@ -44,7 +45,7 @@ export class CatalogClient implements CatalogApi {
id: String,
options?: CatalogRequestOptions,
): Promise<Location | undefined> {
return await this.getOptional(`/locations/${id}`, options);
return await this.requestOptional('GET', `/locations/${id}`, options);
}
async getEntities(
@@ -60,6 +61,7 @@ export class CatalogClient implements CatalogApi {
filterParts.push(`${encodeURIComponent(key)}=${encodeURIComponent(v)}`);
}
}
if (filterParts.length) {
params.push(`filter=${filterParts.join(',')}`);
}
@@ -69,7 +71,8 @@ export class CatalogClient implements CatalogApi {
}
const query = params.length ? `?${params.join('&')}` : '';
const entities: Entity[] = await this.getRequired(
const entities: Entity[] = await this.requestRequired(
'GET',
`/entities${query}`,
options,
);
@@ -81,7 +84,8 @@ export class CatalogClient implements CatalogApi {
options?: CatalogRequestOptions,
): Promise<Entity | undefined> {
const { kind, namespace = 'default', name } = compoundName;
return this.getOptional(
return this.requestOptional(
'GET',
`/entities/by-name/${kind}/${namespace}/${name}`,
options,
);
@@ -126,12 +130,17 @@ export class CatalogClient implements CatalogApi {
};
}
async getLocationByEntity(
async getOriginLocationByEntity(
entity: Entity,
options?: CatalogRequestOptions,
): Promise<Location | undefined> {
const locationCompound = entity.metadata.annotations?.[LOCATION_ANNOTATION];
const all: { data: Location }[] = await this.getRequired(
const locationCompound =
entity.metadata.annotations?.[ORIGIN_LOCATION_ANNOTATION];
if (!locationCompound) {
return undefined;
}
const all: { data: Location }[] = await this.requestRequired(
'GET',
'/locations',
options,
);
@@ -140,39 +149,68 @@ export class CatalogClient implements CatalogApi {
.find(l => locationCompound === stringifyLocationReference(l));
}
async getLocationByEntity(
entity: Entity,
options?: CatalogRequestOptions,
): Promise<Location | undefined> {
const locationCompound = entity.metadata.annotations?.[LOCATION_ANNOTATION];
if (!locationCompound) {
return undefined;
}
const all: { data: Location }[] = await this.requestRequired(
'GET',
'/locations',
options,
);
return all
.map(r => r.data)
.find(l => locationCompound === stringifyLocationReference(l));
}
async removeLocationById(
id: string,
options?: CatalogRequestOptions,
): Promise<void> {
await this.requestIgnored('DELETE', `/locations/${id}`, options);
}
async removeEntityByUid(
uid: string,
options?: CatalogRequestOptions,
): Promise<void> {
const response = await fetch(
`${await this.discoveryApi.getBaseUrl('catalog')}/entities/by-uid/${uid}`,
{
headers: options?.token
? { Authorization: `Bearer ${options.token}` }
: {},
method: 'DELETE',
},
);
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
return undefined;
await this.requestIgnored('DELETE', `/entities/by-uid/${uid}`, options);
}
//
// Private methods
//
private async getRequired(
private async requestIgnored(
method: string,
path: string,
options?: CatalogRequestOptions,
): Promise<void> {
const url = `${await this.discoveryApi.getBaseUrl('catalog')}${path}`;
const headers = new Headers(
options?.token ? { Authorization: `Bearer ${options.token}` } : {},
);
const response = await fetch(url, { method, headers });
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
}
private async requestRequired(
method: string,
path: string,
options?: CatalogRequestOptions,
): Promise<any> {
const url = `${await this.discoveryApi.getBaseUrl('catalog')}${path}`;
const response = await fetch(url, {
headers: options?.token
? { Authorization: `Bearer ${options.token}` }
: {},
});
const headers = new Headers(
options?.token ? { Authorization: `Bearer ${options.token}` } : {},
);
const response = await fetch(url, { method, headers });
if (!response.ok) {
throw await ResponseError.fromResponse(response);
@@ -181,16 +219,16 @@ export class CatalogClient implements CatalogApi {
return await response.json();
}
private async getOptional(
private async requestOptional(
method: string,
path: string,
options?: CatalogRequestOptions,
): Promise<any | undefined> {
const url = `${await this.discoveryApi.getBaseUrl('catalog')}${path}`;
const response = await fetch(url, {
headers: options?.token
? { Authorization: `Bearer ${options.token}` }
: {},
});
const headers = new Headers(
options?.token ? { Authorization: `Bearer ${options.token}` } : {},
);
const response = await fetch(url, { method, headers });
if (!response.ok) {
if (response.status === 404) {
+24 -13
View File
@@ -30,28 +30,39 @@ export type CatalogRequestOptions = {
};
export interface CatalogApi {
getLocationById(
id: String,
options?: CatalogRequestOptions,
): Promise<Location | undefined>;
getEntityByName(
name: EntityName,
options?: CatalogRequestOptions,
): Promise<Entity | undefined>;
// Entities
getEntities(
request?: CatalogEntitiesRequest,
options?: CatalogRequestOptions,
): Promise<CatalogListResponse<Entity>>;
addLocation(
location: AddLocationRequest,
getEntityByName(
name: EntityName,
options?: CatalogRequestOptions,
): Promise<AddLocationResponse>;
): Promise<Entity | undefined>;
removeEntityByUid(
uid: string,
options?: CatalogRequestOptions,
): Promise<void>;
// Locations
getLocationById(
id: String,
options?: CatalogRequestOptions,
): Promise<Location | undefined>;
getOriginLocationByEntity(
entity: Entity,
options?: CatalogRequestOptions,
): Promise<Location | undefined>;
getLocationByEntity(
entity: Entity,
options?: CatalogRequestOptions,
): Promise<Location | undefined>;
removeEntityByUid(
uid: string,
addLocation(
location: AddLocationRequest,
options?: CatalogRequestOptions,
): Promise<AddLocationResponse>;
removeLocationById(
id: string,
options?: CatalogRequestOptions,
): Promise<void>;
}