feat(catalog-backend): move filters into their own query key (#3066)

This commit is contained in:
Fredrik Adelöw
2020-10-26 10:10:18 +01:00
committed by GitHub
parent 31ba976ea8
commit 002860e7ac
13 changed files with 324 additions and 157 deletions
@@ -14,13 +14,13 @@
* limitations under the License.
*/
import fetch from 'cross-fetch';
import { UserEntity } from '@backstage/catalog-model';
import {
ConflictError,
NotFoundError,
PluginEndpointDiscovery,
} from '@backstage/backend-common';
import { UserEntity } from '@backstage/catalog-model';
import fetch from 'cross-fetch';
type UserQuery = {
annotations: Record<string, string>;
@@ -42,15 +42,17 @@ export class CatalogIdentityClient {
* Throws a NotFoundError or ConflictError if 0 or multiple users are found.
*/
async findUser(query: UserQuery): Promise<UserEntity> {
const params = new URLSearchParams();
params.append('kind', 'User');
const conditions = ['kind=user'];
for (const [key, value] of Object.entries(query.annotations)) {
params.append(`metadata.annotations.${key}`, value);
const uk = encodeURIComponent(key);
const uv = encodeURIComponent(value);
conditions.push(`metadata.annotations.${uk}=${uv}`);
}
const baseUrl = await this.discovery.getBaseUrl('catalog');
const response = await fetch(`${baseUrl}/entities?${params}`);
const response = await fetch(
`${baseUrl}/entities?filter=${conditions.join(',')}`,
);
if (!response.ok) {
const text = await response.text();