|
| 1 | +import CloudGraph from '@cloudgraph/sdk' |
| 2 | +import groupBy from 'lodash/groupBy' |
| 3 | +import { Datastore } from '@google-cloud/datastore' |
| 4 | +import gcpLoggerText from '../../properties/logger' |
| 5 | +import { GcpServiceInput } from '../../types' |
| 6 | +import { initTestEndpoint, generateGcpErrorLog } from '../../utils' |
| 7 | +import { GLOBAL_REGION } from '../../config/constants' |
| 8 | +import { listData } from '../../utils/fetchUtils' |
| 9 | + |
| 10 | +const lt = { ...gcpLoggerText } |
| 11 | +const { logger } = CloudGraph |
| 12 | +const serviceName = 'Firestore Database' |
| 13 | +const apiEndpoint = initTestEndpoint(serviceName) |
| 14 | + |
| 15 | +export interface RawGcpFirestoreDatabase { |
| 16 | + id: string |
| 17 | + projectId: string |
| 18 | + region: string |
| 19 | + name: string |
| 20 | + createTime: string |
| 21 | + updateTime: string |
| 22 | + locationId: string |
| 23 | + type: string |
| 24 | + concurrencyMode: string |
| 25 | + appEngineIntegrationMode: string |
| 26 | + keyPrefix: string |
| 27 | +} |
| 28 | + |
| 29 | +export default async ({ |
| 30 | + config, |
| 31 | +}: GcpServiceInput): Promise<{ |
| 32 | + [region: string]: RawGcpFirestoreDatabase[] |
| 33 | +}> => |
| 34 | + new Promise(async resolve => { |
| 35 | + const firestoreDatabaseList: RawGcpFirestoreDatabase[] = [] |
| 36 | + const { projectId } = config |
| 37 | + |
| 38 | + try { |
| 39 | + const service = new Datastore({ ...config, apiEndpoint }) |
| 40 | + const data = await listData({ |
| 41 | + accessToken: await service.auth.getAccessToken(), |
| 42 | + apiUri: `https://firestore.googleapis.com/v1/projects/${projectId}/databases`, |
| 43 | + dataFieldName: 'databases', |
| 44 | + }) |
| 45 | + |
| 46 | + for (const {name, createTime, updateTime, locationId, type, concurrencyMode, appEngineIntegrationMode, keyPrefix} of data) { |
| 47 | + firestoreDatabaseList.push({ |
| 48 | + id: name, |
| 49 | + projectId, |
| 50 | + region: GLOBAL_REGION, |
| 51 | + name, |
| 52 | + createTime, |
| 53 | + updateTime, |
| 54 | + locationId, |
| 55 | + type, |
| 56 | + concurrencyMode, |
| 57 | + appEngineIntegrationMode, |
| 58 | + keyPrefix, |
| 59 | + }) |
| 60 | + } |
| 61 | + } catch (error) { |
| 62 | + generateGcpErrorLog(serviceName, 'firestore:listDatabase', error) |
| 63 | + } |
| 64 | + |
| 65 | + logger.debug(lt.foundResources(serviceName, firestoreDatabaseList.length)) |
| 66 | + resolve(groupBy(firestoreDatabaseList, 'region')) |
| 67 | + }) |
0 commit comments