66import CloudGraph from '@cloudgraph/sdk'
77import { Config } from 'aws-sdk'
88import { ApplicationDescription } from 'aws-sdk/clients/elasticbeanstalk'
9+ import { groupBy } from 'lodash'
910import isEmpty from 'lodash/isEmpty'
1011import awsLoggerText from '../../properties/logger'
1112import { AwsTag , TagMap } from '../../types'
@@ -19,6 +20,7 @@ const serviceName = 'ElasticBeanstalkApp'
1920const errorLog = new AwsErrorLog ( serviceName )
2021
2122export interface RawAwsElasticBeanstalkApp extends ApplicationDescription {
23+ region : string
2224 Tags ?: TagMap
2325}
2426
@@ -64,12 +66,13 @@ export const getResourceTags = async (
6466 }
6567 resolveTags ( convertAwsTagsToTagMap ( tags as AwsTag [ ] ) )
6668 } )
67- . catch ( err =>
69+ . catch ( err => {
6870 errorLog . generateAwsErrorLog ( {
6971 functionName : 'elasticBeanstalk:listTagsForResource' ,
7072 err,
7173 } )
72- )
74+ resolveTags ( { } )
75+ } )
7376 } )
7477
7578const getApplications = async (
@@ -97,25 +100,25 @@ export default async ({
97100 new Promise ( async resolve => {
98101 const { credentials } = config
99102 let numberOfApps = 0
100- const output : { [ property : string ] : RawAwsElasticBeanstalkApp [ ] } = { }
103+ const appsData : RawAwsElasticBeanstalkApp [ ] = [ ]
101104
102- // First we get all applications for all regions
103- await Promise . all (
104- regions . split ( ',' ) . map ( region => {
105- const eb = new ElasticBeanstalkClient ( {
106- credentials,
107- region,
108- } )
109- output [ region ] = [ ]
110- return new Promise < void > ( async resolveRegion => {
111- const apps = ( await getApplications ( eb ) ) || [ ]
112- output [ region ] = apps
105+ const regionPromises = regions . split ( ',' ) . map ( region => {
106+ const eb = new ElasticBeanstalkClient ( {
107+ credentials,
108+ region,
109+ } )
110+ return new Promise < void > ( async resolveRegion => {
111+ const apps = ( await getApplications ( eb ) ) || [ ]
112+ if ( ! isEmpty ( apps ) ) {
113+ appsData . push ( ...apps . map ( val => ( { ...val , region } ) ) )
113114 numberOfApps += apps . length
114- resolveRegion ( )
115- } )
115+ }
116+ resolveRegion ( )
116117 } )
117- )
118+ } )
119+
120+ await Promise . all ( regionPromises )
118121 errorLog . reset ( )
119122 logger . debug ( lt . fetchedElasticBeanstalkApps ( numberOfApps ) )
120- resolve ( output )
123+ resolve ( groupBy ( appsData , 'region' ) )
121124 } )
0 commit comments