Skip to content

Commit aab3b84

Browse files
committed
fix: refactor listenerCertificates method
1 parent ef6f342 commit aab3b84

File tree

1 file changed

+65
-85
lines changed

1 file changed

+65
-85
lines changed

src/services/alb/data.ts

Lines changed: 65 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import ELBV2, {
1010
DescribeTargetGroupsOutput,
1111
DescribeTargetHealthOutput,
1212
DescribeListenerCertificatesInput,
13-
DescribeListenerCertificatesOutput,
1413
Listeners,
1514
LoadBalancer,
1615
LoadBalancerAttributeValue,
@@ -55,90 +54,64 @@ export type RawAwsAlb = LoadBalancer & {
5554
}
5655

5756
const describeListenerCertificatesForAlb = async ({
58-
alb,
5957
elbv2,
60-
marker: Marker = '',
6158
ListenerArn,
62-
resolveListenerCertificates,
6359
}: {
64-
alb: RawAwsAlb
6560
elbv2: ELBV2
66-
marker?: string
6761
ListenerArn: string
68-
resolveListenerCertificates: () => void
69-
}): Promise<Request<DescribeListenerCertificatesOutput, AWSError>> => {
70-
let args: DescribeListenerCertificatesInput = { ListenerArn }
71-
72-
if (Marker) {
73-
args = {
74-
...args,
75-
Marker,
76-
}
77-
}
78-
79-
return elbv2.describeListenerCertificates(args, async (err, data) => {
80-
if (err) {
81-
errorLog.generateAwsErrorLog({
82-
functionName: 'elbv2:describeListenerCertificates',
83-
err,
84-
})
85-
}
86-
87-
/**
88-
* No certificates
89-
*/
90-
91-
if (isEmpty(data)) {
92-
return resolveListenerCertificates()
93-
}
94-
95-
const { Certificates: certificates = [], NextMarker: marker } = data || {}
96-
97-
logger.debug(
98-
lt.fetchedAlbListenerCertificates(certificates.length, ListenerArn)
99-
)
100-
101-
/**
102-
* No certificates found
103-
*/
62+
}): Promise<Certificate[]> =>
63+
new Promise(resolve => {
64+
const certificateList: Certificate[] = []
65+
const listAllCertificates = (token?: string): void => {
66+
const args: DescribeListenerCertificatesInput = { ListenerArn }
67+
if (token) {
68+
args.Marker = token
69+
}
10470

105-
if (isEmpty(certificates)) {
106-
return resolveListenerCertificates()
107-
}
71+
try {
72+
elbv2.describeListenerCertificates(args, (err, data) => {
73+
if (err) {
74+
errorLog.generateAwsErrorLog({
75+
functionName: 'elbv2:describeListenerCertificates',
76+
err,
77+
})
78+
resolve(certificateList)
79+
return
80+
}
10881

109-
/**
110-
* Check to see if there are more
111-
*/
82+
const { Certificates: certificates = [], NextMarker: marker } =
83+
data || {}
11284

113-
if (marker) {
114-
describeListenerCertificatesForAlb({
115-
alb,
116-
elbv2,
117-
marker,
118-
ListenerArn,
119-
resolveListenerCertificates,
120-
})
121-
}
85+
logger.debug(
86+
lt.fetchedAlbListenerCertificates(certificates.length, ListenerArn)
87+
)
12288

123-
/**
124-
* If there are not, then add the targetGroups to the alb's targetGroups
125-
*/
126-
alb.listenerCertificates.push(
127-
...certificates.map(certificate => ({
128-
listenerArn: ListenerArn,
129-
...certificate,
130-
}))
131-
)
89+
if (isEmpty(certificates)) {
90+
resolve(certificateList)
91+
return
92+
}
13293

133-
/**
134-
* If this is the last page of data then return
135-
*/
94+
certificateList.push(...certificates)
13695

137-
if (!marker) {
138-
resolveListenerCertificates()
96+
/**
97+
* Check to see if there are more
98+
*/
99+
if (marker) {
100+
listAllCertificates(marker)
101+
} else {
102+
resolve(certificateList)
103+
}
104+
})
105+
} catch (err) {
106+
errorLog.generateAwsErrorLog({
107+
functionName: 'elbv2:describeListenerCertificates',
108+
err,
109+
})
110+
resolve([])
111+
}
139112
}
113+
listAllCertificates()
140114
})
141-
}
142115

143116
export default async ({
144117
regions,
@@ -735,22 +708,29 @@ export default async ({
735708
endpoint,
736709
...customRetrySettings,
737710
})
738-
const listenerCertificatesPromise = new Promise<void>(
739-
resolveListenerCertificates => {
740-
listeners.forEach(({ ListenerArn }) => {
741-
describeListenerCertificatesForAlb({
742-
alb,
743-
elbv2,
744-
ListenerArn,
745-
resolveListenerCertificates,
746-
})
711+
listeners.forEach(({ ListenerArn }) => {
712+
const certificatePromise = new Promise<void>(async resolveListener => {
713+
const certificates = await describeListenerCertificatesForAlb({
714+
elbv2,
715+
ListenerArn,
747716
})
748-
}
749-
)
750-
listenerCertificatesPromises.push(listenerCertificatesPromise)
717+
if (!isEmpty(certificates)) {
718+
alb.listenerCertificates.push(
719+
...certificates.map(cerficate => ({
720+
...cerficate,
721+
listenerArn: ListenerArn,
722+
}))
723+
)
724+
}
725+
resolveListener()
726+
})
727+
listenerCertificatesPromises.push(certificatePromise)
728+
})
751729
})
752730

753-
await Promise.all(listenerCertificatesPromises)
731+
if (!isEmpty(listenerCertificatesPromises)) {
732+
await Promise.all(listenerCertificatesPromises)
733+
}
754734
errorLog.reset()
755735

756736
resolve(groupBy(albData, 'region'))

0 commit comments

Comments
 (0)