Skip to content

Commit 6249ef2

Browse files
authored
fix: create a separate file for each imported alert channel (#1070)
Earlier, all alert channels of the same type were bundled into the same file. Unfortunately when you did a per-resource import of the same type later, it would clear everything else in the file and only include the newly generated construct. Now, each alert channels is in a separate file.
1 parent ad35209 commit 6249ef2

10 files changed

+75
-20
lines changed

packages/cli/src/constructs/email-alert-channel-codegen.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ export class EmailAlertChannelCodegen extends Codegen<EmailAlertChannelResource>
1919
prepare (logicalId: string, resource: EmailAlertChannelResource, context: Context): void {
2020
const { address } = resource.config
2121

22+
const prefix = address.split('@')[0]
23+
24+
const filename = context.filePath('resources/alert-channels/email', prefix, {
25+
unique: true,
26+
})
27+
2228
context.registerAlertChannel(
2329
resource.id,
24-
`${address.split('@')[0]} email`,
25-
this.program.generatedConstructFile('resources/alert-channels/email'),
30+
`${prefix} email`,
31+
this.program.generatedConstructFile(filename.fullPath),
2632
)
2733
}
2834

packages/cli/src/constructs/incidentio-alert-channel-codegen.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ export class IncidentioAlertChannelCodegen extends Codegen<IncidentioAlertChanne
6767

6868
const { name } = resource.config
6969

70+
const filename = context.filePath('resources/alert-channels/incident-io', name, {
71+
unique: true,
72+
})
73+
7074
context.registerAlertChannel(
7175
resource.id,
7276
`${name} incidentio`,
73-
this.program.generatedConstructFile('resources/alert-channels/incident-io'),
77+
this.program.generatedConstructFile(filename.fullPath),
7478
)
7579
}
7680

packages/cli/src/constructs/msteams-alert-channel-codegen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ export class MSTeamsAlertChannelCodegen extends Codegen<MSTeamsAlertChannelResou
4444

4545
const { name } = resource.config
4646

47+
const filename = context.filePath('resources/alert-channels/ms-teams', name, {
48+
unique: true,
49+
})
50+
4751
context.registerAlertChannel(
4852
resource.id,
49-
name ? `${name} teams` : 'teams',
50-
this.program.generatedConstructFile('resources/alert-channels/ms-teams'),
53+
`${name} teams`,
54+
this.program.generatedConstructFile(filename.fullPath),
5155
)
5256
}
5357

packages/cli/src/constructs/opsgenie-alert-channel-codegen.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ export class OpsgenieAlertChannelCodegen extends Codegen<OpsgenieAlertChannelRes
2222
prepare (logicalId: string, resource: OpsgenieAlertChannelResource, context: Context): void {
2323
const { name } = resource.config
2424

25+
const filename = context.filePath('resources/alert-channels/opsgenie', name, {
26+
unique: true,
27+
})
28+
2529
context.registerAlertChannel(
2630
resource.id,
2731
`${name} opsgenie`,
28-
this.program.generatedConstructFile('resources/alert-channels/opsgenie'),
32+
this.program.generatedConstructFile(filename.fullPath),
2933
)
3034
}
3135

packages/cli/src/constructs/pagerduty-alert-channel-codegen.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ export class PagerdutyAlertChannelCodegen extends Codegen<PagerdutyAlertChannelR
2727
}
2828

2929
prepare (logicalId: string, resource: PagerdutyAlertChannelResource, context: Context): void {
30-
const { serviceName, account } = resource.config
30+
const { serviceName, account, serviceKey } = resource.config
31+
32+
const prefix = serviceName ?? account
33+
const last4Chars = serviceKey.slice(-4)
34+
const fallbackName = `pagerduty-${last4Chars}`
35+
36+
const filename = context.filePath('resources/alert-channels/pagerduty', prefix || fallbackName, {
37+
unique: true,
38+
})
3139

3240
context.registerAlertChannel(
3341
resource.id,
34-
serviceName ? `${serviceName} pagerduty` : (account ? `${account} pagerduty` : `pagerduty`),
35-
this.program.generatedConstructFile('resources/alert-channels/pagerduty'),
42+
prefix ? `${prefix} pagerduty` : fallbackName,
43+
this.program.generatedConstructFile(filename.fullPath),
3644
)
3745
}
3846

packages/cli/src/constructs/phone-call-alert-channel-codegen.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ export class PhoneCallAlertChannelCodegen extends Codegen<PhoneCallAlertChannelR
2222
}
2323

2424
prepare (logicalId: string, resource: PhoneCallAlertChannelResource, context: Context): void {
25-
const { name } = resource.config
25+
const { name, number } = resource.config
26+
27+
const last4Digits = number.slice(-4)
28+
const fallbackName = `phone-call-${last4Digits}`
29+
30+
const filename = context.filePath('resources/alert-channels/phone-call', name || fallbackName, {
31+
unique: true,
32+
})
2633

2734
context.registerAlertChannel(
2835
resource.id,
29-
name ? `${name} phone call` : 'phone call',
30-
this.program.generatedConstructFile('resources/alert-channels/phone-call'),
36+
name ? `${name} phone call` : fallbackName,
37+
this.program.generatedConstructFile(filename.fullPath),
3138
)
3239
}
3340

packages/cli/src/constructs/slack-alert-channel-codegen.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ export class SlackAlertChannelCodegen extends Codegen<SlackAlertChannelResource>
2222
}
2323

2424
prepare (logicalId: string, resource: SlackAlertChannelResource, context: Context): void {
25-
const { channel } = resource.config
25+
const { channel, url } = resource.config
26+
27+
const last4Chars = url.slice(-4)
28+
const fallbackName = `slack-${last4Chars}`
29+
30+
const filename = context.filePath('resources/alert-channels/slack', channel || fallbackName, {
31+
unique: true,
32+
})
2633

2734
context.registerAlertChannel(
2835
resource.id,
29-
channel ? `${channel} slack` : 'slack',
30-
this.program.generatedConstructFile('resources/alert-channels/slack'),
36+
channel ? `${channel} slack` : fallbackName,
37+
this.program.generatedConstructFile(filename.fullPath),
3138
)
3239
}
3340

packages/cli/src/constructs/sms-alert-channel-codegen.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ export class SmsAlertChannelCodegen extends Codegen<SmsAlertChannelResource> {
2222
}
2323

2424
prepare (logicalId: string, resource: SmsAlertChannelResource, context: Context): void {
25-
const { name } = resource.config
25+
const { name, number } = resource.config
26+
27+
const last4Digits = number.slice(-4)
28+
const fallbackName = `sms-${last4Digits}`
29+
30+
const filename = context.filePath('resources/alert-channels/sms', name || fallbackName, {
31+
unique: true,
32+
})
2633

2734
context.registerAlertChannel(
2835
resource.id,
29-
name ? `${name} sms` : 'sms',
30-
this.program.generatedConstructFile('resources/alert-channels/sms'),
36+
name ? `${name} sms` : fallbackName,
37+
this.program.generatedConstructFile(filename.fullPath),
3138
)
3239
}
3340

packages/cli/src/constructs/telegram-alert-channel-codegen.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ export class TelegramAlertChannelCodegen extends Codegen<TelegramAlertChannelRes
7575

7676
const { name } = resource.config
7777

78+
const filename = context.filePath('resources/alert-channels/telegram', name, {
79+
unique: true,
80+
})
81+
7882
context.registerAlertChannel(
7983
resource.id,
8084
`${name} telegram`,
81-
this.program.generatedConstructFile('resources/alert-channels/telegram'),
85+
this.program.generatedConstructFile(filename.fullPath),
8286
)
8387
}
8488

packages/cli/src/constructs/webhook-alert-channel-codegen.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,14 @@ export class WebhookAlertChannelCodegen extends Codegen<WebhookAlertChannelResou
145145

146146
const { name } = resource.config
147147

148+
const filename = context.filePath('resources/alert-channels/webhook', name, {
149+
unique: true,
150+
})
151+
148152
context.registerAlertChannel(
149153
resource.id,
150154
`${name} webhook`,
151-
this.program.generatedConstructFile('resources/alert-channels/webhook'),
155+
this.program.generatedConstructFile(filename.fullPath),
152156
)
153157
}
154158

0 commit comments

Comments
 (0)