|
| 1 | +/*! |
| 2 | + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { Credentials, Schemas } from 'aws-sdk' |
| 7 | +import { SchemaClient } from '../../shared/clients/schemaClient' |
| 8 | +import { getLogger, Logger } from '../../shared/logger' |
| 9 | +import { toArrayAsync } from '../../shared/utilities/collectionUtils' |
| 10 | + |
| 11 | +export class Cache { |
| 12 | + public constructor(public readonly credentialsRegionDataList: credentialsRegionDataListMap[]) {} |
| 13 | +} |
| 14 | + |
| 15 | +export interface credentialsRegionDataListMap { |
| 16 | + credentials: Credentials |
| 17 | + regionDataList: regionRegistryMap[] |
| 18 | +} |
| 19 | + |
| 20 | +export interface regionRegistryMap { |
| 21 | + region: string |
| 22 | + registryNames: string[] |
| 23 | + registrySchemasMapList: registrySchemasMap[] |
| 24 | +} |
| 25 | + |
| 26 | +export interface registrySchemasMap { |
| 27 | + registryName: string |
| 28 | + schemaList: Schemas.SchemaSummary[] |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Responsible for retaining registry && schema list per region for Create-New-SAM-Application wizard |
| 33 | + */ |
| 34 | +export class SchemasDataProvider { |
| 35 | + private static INSTANCE: SchemasDataProvider | undefined |
| 36 | + private readonly logger: Logger = getLogger() |
| 37 | + |
| 38 | + public constructor(private readonly cache: Cache) {} |
| 39 | + |
| 40 | + public async getRegistries(region: string, client: SchemaClient, credentials: Credentials) { |
| 41 | + const cachedRegion = this.cache.credentialsRegionDataList |
| 42 | + .filter(x => x.credentials === credentials) |
| 43 | + .shift() |
| 44 | + ?.regionDataList.filter(x => x.region === region) |
| 45 | + .shift() |
| 46 | + |
| 47 | + try { |
| 48 | + // if region is not cached, make api query and retain results |
| 49 | + if (!cachedRegion || cachedRegion.registryNames.length === 0) { |
| 50 | + const registrySummary = await toArrayAsync(client.listRegistries()) |
| 51 | + const registryNames = registrySummary.map(x => x.RegistryName!) |
| 52 | + this.pushRegionDataIntoCache(region, registryNames, [], credentials) |
| 53 | + |
| 54 | + return registryNames |
| 55 | + } |
| 56 | + } catch (err) { |
| 57 | + const error = err as Error |
| 58 | + this.logger.error('Error retrieving registries', error) |
| 59 | + |
| 60 | + return undefined |
| 61 | + } |
| 62 | + |
| 63 | + return cachedRegion!.registryNames |
| 64 | + } |
| 65 | + |
| 66 | + public async getSchemas(region: string, registryName: string, client: SchemaClient, credentials: Credentials) { |
| 67 | + const registrySchemasMapList = this.cache.credentialsRegionDataList |
| 68 | + .filter(x => x.credentials === credentials) |
| 69 | + .shift() |
| 70 | + ?.regionDataList.filter(x => x.region === region) |
| 71 | + .shift()?.registrySchemasMapList |
| 72 | + let schemas = registrySchemasMapList?.filter(x => x.registryName === registryName).shift()?.schemaList |
| 73 | + try { |
| 74 | + // if no schemas found, make api query and retain results given that registryName && region already cached |
| 75 | + if (!schemas || schemas.length === 0) { |
| 76 | + schemas = await toArrayAsync(client.listSchemas(registryName)) |
| 77 | + const singleItem: registrySchemasMap = { registryName: registryName, schemaList: schemas } |
| 78 | + //wizard setup always calls getRegistries method prior to getSchemas, so this shouldn't be undefined |
| 79 | + if (!registrySchemasMapList) { |
| 80 | + this.pushRegionDataIntoCache(region, [], [singleItem], credentials) |
| 81 | + } |
| 82 | + |
| 83 | + if (registrySchemasMapList) { |
| 84 | + registrySchemasMapList.push(singleItem) |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (err) { |
| 88 | + const error = err as Error |
| 89 | + this.logger.error('Error retrieving schemas', error) |
| 90 | + |
| 91 | + return undefined |
| 92 | + } |
| 93 | + |
| 94 | + return schemas |
| 95 | + } |
| 96 | + |
| 97 | + private pushRegionDataIntoCache( |
| 98 | + region: string, |
| 99 | + registryNames: string[], |
| 100 | + registrySchemasMapList: registrySchemasMap[], |
| 101 | + credentials?: Credentials |
| 102 | + ): void { |
| 103 | + const regionData: regionRegistryMap = { |
| 104 | + region: region, |
| 105 | + registryNames: registryNames, |
| 106 | + registrySchemasMapList: registrySchemasMapList |
| 107 | + } |
| 108 | + |
| 109 | + const cachedCredential = this.cache.credentialsRegionDataList.filter(x => x.credentials === credentials).shift() |
| 110 | + cachedCredential?.regionDataList.push(regionData) |
| 111 | + |
| 112 | + if (!cachedCredential) { |
| 113 | + const regionDataWithCredentials: credentialsRegionDataListMap = { |
| 114 | + credentials: credentials!, |
| 115 | + regionDataList: [regionData] |
| 116 | + } |
| 117 | + this.cache.credentialsRegionDataList.push(regionDataWithCredentials) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static getInstance(): SchemasDataProvider { |
| 122 | + if (!SchemasDataProvider.INSTANCE) { |
| 123 | + SchemasDataProvider.INSTANCE = new SchemasDataProvider(new Cache([])) |
| 124 | + } |
| 125 | + |
| 126 | + return SchemasDataProvider.INSTANCE |
| 127 | + } |
| 128 | +} |
0 commit comments