Skip to content

Commit ebfa63f

Browse files
authored
Merge pull request #140 from cloudgraphdev/feature/EP-3181-support-elasticBeanstalk-service
feat(aws): updated elastic beanstalk client to aws-sdk-v3 to fix empty crawl data issue
2 parents 403fcf5 + 3f3e6b0 commit ebfa63f

4 files changed

Lines changed: 818 additions & 127 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"terraform:cleanup": "rimraf ./tests/terraform/{.terraform,.terraform.lock.hcl,tfplan} ./tests/terraform/*.{tfstate,tfplan,backup}"
3232
},
3333
"dependencies": {
34+
"@aws-sdk/client-elastic-beanstalk": "^3.338.0",
3435
"@aws-sdk/credential-providers": "^3.256.0",
3536
"@aws-sdk/shared-ini-file-loader": "^3.254.0",
3637
"@cloudgraph/sdk": "^0.22.1",

src/services/elasticBeanstalkApplication/data.ts

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
1+
import {
2+
DescribeApplicationsCommand,
3+
ElasticBeanstalkClient,
4+
ListTagsForResourceCommand,
5+
} from '@aws-sdk/client-elastic-beanstalk'
16
import CloudGraph from '@cloudgraph/sdk'
2-
import { AWSError } from 'aws-sdk'
3-
import ElasticBeanstalk, {
4-
ApplicationDescription,
5-
ApplicationDescriptionsMessage,
6-
ResourceTagsDescriptionMessage,
7-
} from 'aws-sdk/clients/elasticbeanstalk'
7+
import { Config } from 'aws-sdk'
8+
import { ApplicationDescription } from 'aws-sdk/clients/elasticbeanstalk'
89
import isEmpty from 'lodash/isEmpty'
9-
10-
import { AwsTag, Credentials, TagMap } from '../../types'
11-
import { convertAwsTagsToTagMap } from '../../utils/format'
12-
import { settleAllPromises } from '../../utils/index'
1310
import awsLoggerText from '../../properties/logger'
14-
import { initTestEndpoint } from '../../utils'
11+
import { AwsTag, TagMap } from '../../types'
1512
import AwsErrorLog from '../../utils/errorLog'
13+
import { convertAwsTagsToTagMap } from '../../utils/format'
14+
import { settleAllPromises } from '../../utils/index'
1615

1716
const lt = { ...awsLoggerText }
1817
const { logger } = CloudGraph
1918
const serviceName = 'ElasticBeanstalkApp'
2019
const errorLog = new AwsErrorLog(serviceName)
21-
const endpoint = initTestEndpoint(serviceName)
2220

2321
export interface RawAwsElasticBeanstalkApp extends ApplicationDescription {
2422
Tags?: TagMap
2523
}
2624

2725
const listApplications = async (
28-
eb: ElasticBeanstalk
26+
eb: ElasticBeanstalkClient
2927
): Promise<ApplicationDescription[]> =>
3028
new Promise(async resolve => {
31-
eb.describeApplications(
32-
{},
33-
(err: AWSError, data: ApplicationDescriptionsMessage) => {
34-
if (err) {
35-
errorLog.generateAwsErrorLog({
36-
functionName: 'elasticBeanstalk:describeApplications',
37-
err,
38-
})
39-
}
40-
/**
41-
* No EB Applications for this region
42-
*/
29+
const command = new DescribeApplicationsCommand({})
30+
eb.send(command)
31+
.then(data => {
4332
if (isEmpty(data)) {
4433
return resolve([])
4534
}
@@ -48,29 +37,24 @@ const listApplications = async (
4837
return resolve([])
4938
}
5039
resolve(Applications)
51-
}
52-
)
40+
})
41+
.catch(err => {
42+
errorLog.generateAwsErrorLog({
43+
functionName: 'elasticBeanstalk:describeApplications',
44+
err,
45+
})
46+
resolve([])
47+
})
5348
})
5449

5550
export const getResourceTags = async (
56-
eb: ElasticBeanstalk,
51+
eb: ElasticBeanstalkClient,
5752
resourceArn: string
5853
): Promise<TagMap> =>
5954
new Promise(resolveTags => {
60-
eb.listTagsForResource(
61-
{
62-
ResourceArn: resourceArn,
63-
},
64-
(err: AWSError, data: ResourceTagsDescriptionMessage) => {
65-
if (err) {
66-
errorLog.generateAwsErrorLog({
67-
functionName: 'elasticBeanstalk:listTagsForResource',
68-
err,
69-
})
70-
}
71-
/**
72-
* No EB Applications for this region
73-
*/
55+
const command = new ListTagsForResourceCommand({ ResourceArn: resourceArn })
56+
eb.send(command)
57+
.then(data => {
7458
if (isEmpty(data)) {
7559
return resolveTags({})
7660
}
@@ -79,12 +63,17 @@ export const getResourceTags = async (
7963
return resolveTags({})
8064
}
8165
resolveTags(convertAwsTagsToTagMap(tags as AwsTag[]))
82-
}
83-
)
66+
})
67+
.catch(err =>
68+
errorLog.generateAwsErrorLog({
69+
functionName: 'elasticBeanstalk:listTagsForResource',
70+
err,
71+
})
72+
)
8473
})
8574

8675
const getApplications = async (
87-
eb: ElasticBeanstalk
76+
eb: ElasticBeanstalkClient
8877
): Promise<RawAwsElasticBeanstalkApp[]> => {
8978
const apps = await listApplications(eb)
9079
if (!isEmpty(apps)) {
@@ -100,19 +89,23 @@ const getApplications = async (
10089

10190
export default async ({
10291
regions,
103-
credentials,
92+
config,
10493
}: {
10594
regions: string
106-
credentials: Credentials
95+
config: Config
10796
}): Promise<{ [property: string]: RawAwsElasticBeanstalkApp[] }> =>
10897
new Promise(async resolve => {
98+
const { credentials } = config
10999
let numberOfApps = 0
110100
const output: { [property: string]: RawAwsElasticBeanstalkApp[] } = {}
111101

112102
// First we get all applications for all regions
113103
await Promise.all(
114104
regions.split(',').map(region => {
115-
const eb = new ElasticBeanstalk({ region, credentials, endpoint })
105+
const eb = new ElasticBeanstalkClient({
106+
credentials,
107+
region,
108+
})
116109
output[region] = []
117110
return new Promise<void>(async resolveRegion => {
118111
const apps = (await getApplications(eb)) || []

src/services/elasticBeanstalkEnvironment/data.ts

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
import {
2+
DescribeConfigurationSettingsCommand,
3+
DescribeConfigurationSettingsMessage,
4+
DescribeEnvironmentResourcesCommand,
5+
DescribeEnvironmentResourcesMessage,
6+
DescribeEnvironmentsCommand,
7+
ElasticBeanstalkClient,
8+
} from '@aws-sdk/client-elastic-beanstalk'
19
import CloudGraph from '@cloudgraph/sdk'
2-
import { AWSError } from 'aws-sdk'
3-
import ElasticBeanstalk, {
10+
import { Config } from 'aws-sdk'
11+
import {
412
ConfigurationSettingsDescription,
5-
ConfigurationSettingsDescriptions,
613
DescribeEnvironmentsMessage,
714
EnvironmentDescription,
8-
EnvironmentDescriptionsMessage,
915
EnvironmentResourceDescription,
10-
EnvironmentResourceDescriptionsMessage,
1116
} from 'aws-sdk/clients/elasticbeanstalk'
1217
import isEmpty from 'lodash/isEmpty'
13-
14-
import { Credentials, TagMap } from '../../types'
15-
import { settleAllPromises } from '../../utils/index'
1618
import awsLoggerText from '../../properties/logger'
17-
import { initTestEndpoint } from '../../utils'
19+
import { TagMap } from '../../types'
1820
import AwsErrorLog from '../../utils/errorLog'
21+
import { settleAllPromises } from '../../utils/index'
1922
import { getResourceTags } from '../elasticBeanstalkApplication/data'
2023

2124
const lt = { ...awsLoggerText }
2225
const { logger } = CloudGraph
2326
const serviceName = 'ElasticBeanstalkEnv'
2427
const errorLog = new AwsErrorLog(serviceName)
25-
const endpoint = initTestEndpoint(serviceName)
2628
const MAX_ITEMS = 1000
2729

2830
export interface RawAwsElasticBeanstalkEnv extends EnvironmentDescription {
@@ -32,95 +34,106 @@ export interface RawAwsElasticBeanstalkEnv extends EnvironmentDescription {
3234
}
3335

3436
const listEnvironments = async (
35-
eb: ElasticBeanstalk
37+
eb: ElasticBeanstalkClient
3638
): Promise<EnvironmentDescription[]> =>
3739
new Promise(async resolve => {
3840
const environments: EnvironmentDescription[] = []
3941

40-
const listAllEnvironmentsOpts: DescribeEnvironmentsMessage = {
42+
const input: DescribeEnvironmentsMessage = {
4143
MaxRecords: MAX_ITEMS,
4244
}
45+
4346
const listAllEnvironments = (token?: string): void => {
4447
if (token) {
45-
listAllEnvironmentsOpts.NextToken = token
48+
input.NextToken = token
4649
}
47-
try {
48-
eb.describeEnvironments(
49-
listAllEnvironmentsOpts,
50-
(err: AWSError, data: EnvironmentDescriptionsMessage) => {
51-
const { Environments = [], NextToken: nextToken } = data || {}
52-
if (err) {
53-
errorLog.generateAwsErrorLog({
54-
functionName: 'elasticBeanstalk:describeEnvironments',
55-
err,
56-
})
57-
}
50+
const command = new DescribeEnvironmentsCommand(input)
51+
eb.send(command)
52+
.then(data => {
53+
if (isEmpty(data)) {
54+
return resolve([])
55+
}
5856

59-
environments.push(...Environments)
57+
const { Environments = [], NextToken: nextToken } = data || {}
6058

61-
if (nextToken) {
62-
logger.debug(lt.foundAnotherThousand)
63-
listAllEnvironments(nextToken)
64-
} else {
65-
resolve(environments)
66-
}
59+
environments.push(...Environments)
60+
61+
if (nextToken) {
62+
logger.debug(lt.foundAnotherThousand)
63+
listAllEnvironments(nextToken)
64+
} else {
65+
resolve(environments)
6766
}
68-
)
69-
} catch (error) {
70-
resolve([])
71-
}
67+
})
68+
.catch(err => {
69+
errorLog.generateAwsErrorLog({
70+
functionName: 'elasticBeanstalk:describeEnvironments',
71+
err,
72+
})
73+
resolve([])
74+
})
7275
}
7376
listAllEnvironments()
7477
})
7578

7679
const getConfigSettingsForEnv = async (
77-
eb: ElasticBeanstalk,
80+
eb: ElasticBeanstalkClient,
7881
ApplicationName: string,
7982
EnvironmentName: string
8083
): Promise<ConfigurationSettingsDescription[]> =>
8184
new Promise(resolve => {
82-
eb.describeConfigurationSettings(
83-
{
84-
ApplicationName,
85-
EnvironmentName,
86-
},
87-
(err: AWSError, data: ConfigurationSettingsDescriptions) => {
88-
if (err) {
89-
errorLog.generateAwsErrorLog({
90-
functionName: 'elasticBeanstalk:describeConfigurationSettings',
91-
err,
92-
})
85+
const input: DescribeConfigurationSettingsMessage = {
86+
ApplicationName,
87+
EnvironmentName,
88+
}
89+
const command = new DescribeConfigurationSettingsCommand(input)
90+
eb.send(command)
91+
.then(data => {
92+
if (isEmpty(data)) {
93+
return resolve([])
9394
}
95+
9496
const { ConfigurationSettings: settings = [] } = data || {}
9597
resolve(settings)
96-
}
97-
)
98+
})
99+
.catch(err => {
100+
errorLog.generateAwsErrorLog({
101+
functionName: 'elasticBeanstalk:describeConfigurationSettings',
102+
err,
103+
})
104+
resolve([])
105+
})
98106
})
99107

100108
const getResourcesForEnv = async (
101-
eb: ElasticBeanstalk,
109+
eb: ElasticBeanstalkClient,
102110
EnvironmentName: string
103111
): Promise<EnvironmentResourceDescription> =>
104112
new Promise(resolve => {
105-
eb.describeEnvironmentResources(
106-
{
107-
EnvironmentName,
108-
},
109-
(err: AWSError, data: EnvironmentResourceDescriptionsMessage) => {
110-
if (err) {
111-
errorLog.generateAwsErrorLog({
112-
functionName: 'elasticBeanstalk:describeEnvironmentResources',
113-
err,
114-
})
113+
const input: DescribeEnvironmentResourcesMessage = {
114+
EnvironmentName,
115+
}
116+
const command = new DescribeEnvironmentResourcesCommand(input)
117+
eb.send(command)
118+
.then(data => {
119+
if (isEmpty(data)) {
120+
return resolve({})
115121
}
122+
116123
const { EnvironmentResources: resources = {} } = data || {}
117124
resolve(resources)
118-
}
119-
)
125+
})
126+
.catch(err => {
127+
errorLog.generateAwsErrorLog({
128+
functionName: 'elasticBeanstalk:describeEnvironmentResources',
129+
err,
130+
})
131+
resolve({})
132+
})
120133
})
121134

122135
const getEnvironments = async (
123-
eb: ElasticBeanstalk
136+
eb: ElasticBeanstalkClient
124137
): Promise<RawAwsElasticBeanstalkEnv[]> => {
125138
const envs = await listEnvironments(eb)
126139
if (!isEmpty(envs)) {
@@ -152,14 +165,15 @@ const getEnvironments = async (
152165

153166
export default async ({
154167
regions,
155-
credentials,
168+
config,
156169
}: {
157170
regions: string
158-
credentials: Credentials
171+
config: Config
159172
}): Promise<{
160173
[property: string]: RawAwsElasticBeanstalkEnv[]
161174
}> =>
162175
new Promise(async resolve => {
176+
const { credentials } = config
163177
let numberOfEnvs = 0
164178
const output: {
165179
[property: string]: RawAwsElasticBeanstalkEnv[]
@@ -168,7 +182,10 @@ export default async ({
168182
// First we get all applications for all regions
169183
await Promise.all(
170184
regions.split(',').map(region => {
171-
const eb = new ElasticBeanstalk({ region, credentials, endpoint })
185+
const eb = new ElasticBeanstalkClient({
186+
credentials,
187+
region,
188+
})
172189
output[region] = []
173190
return new Promise<void>(async resolveRegion => {
174191
const envs = (await getEnvironments(eb)) || []

0 commit comments

Comments
 (0)