From 9c8142cdacf38c8f4e41378897cf8bf7171fed7c Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 29 Sep 2021 10:56:26 +0200 Subject: [PATCH] Format, require ETag, fix imports Signed-off-by: Johan Haals --- .../software-catalog/external-integrations.md | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/features/software-catalog/external-integrations.md b/docs/features/software-catalog/external-integrations.md index 508b04d77a..4d15371a6b 100644 --- a/docs/features/software-catalog/external-integrations.md +++ b/docs/features/software-catalog/external-integrations.md @@ -174,16 +174,16 @@ registered location and you'll see your entities start to appear in Backstage. ## Caching processing results -The catalog periodically refreshes entities in the catalog, and in doing so it calls -out to external systems to fetch changes. This can be taxing for upstream +The catalog periodically refreshes entities in the catalog, and in doing so it +calls out to external systems to fetch changes. This can be taxing for upstream services and large deployments may get rate limited if too many requests are sent. Luckily many external systems provide ETag support to check for changes -which usually doesn't count towards the quota and saves resources both internally -and externally. +which usually doesn't count towards the quota and saves resources both +internally and externally. -The catalog has built in support for leveraging ETags when refreshing external locations -in GitHub. This example aims to demonstrate how to add the same behavior for `system-x` -that we implemented earlier. +The catalog has built in support for leveraging ETags when refreshing external +locations in GitHub. This example aims to demonstrate how to add the same +behavior for `system-x` that we implemented earlier. ```ts import { UrlReader } from '@backstage/backend-common'; @@ -193,8 +193,8 @@ import { CatalogProcessor, CatalogProcessorEmit, CatalogProcessorCache, + CatalogProcessorParser, } from '@backstage/plugin-catalog-backend'; -import { CatalogProcessorParser } from './CatalogProcessorParser'; // It's recommended to always bump the CACHE_KEY version if you make // changes to the processor implementation or CacheItem. @@ -204,7 +204,7 @@ const CACHE_KEY = 'v1'; // as well as the processing result used when the Etag matches. // Bump the CACHE_KEY version if you make any changes to this type. type CacheItem = { - etag?: string; + etag: string; entity: Entity; }; @@ -244,6 +244,13 @@ export class SystemXReaderProcessor implements CatalogProcessor { ); } + // ETag is optional in the response but we need it to cache the result. + if (!response.etag) { + throw new Error( + 'No ETag returned from system-x, cannot use response for caching', + ); + } + // For this example the JSON payload is a single entity. const entity: Entity = JSON.parse(response.buffer.toString()); emit(results.entity(location, entity));