1
- import { Codegen } from './internal/codegen'
1
+ import { Codegen , Context } from './internal/codegen'
2
2
import { Program , ObjectValueBuilder } from '../sourcegen'
3
3
import { AlertEscalationResource , valueForAlertEscalation } from './alert-escalation-policy-codegen'
4
4
import { ApiCheckCodegen , ApiCheckResource } from './api-check-codegen'
@@ -9,11 +9,11 @@ import { FrequencyResource, valueForFrequency } from './frequency-codegen'
9
9
import { HeartbeatCheckCodegen , HeartbeatCheckResource } from './heartbeat-check-codegen'
10
10
import { valueForKeyValuePair } from './key-value-pair-codegen'
11
11
import { MultiStepCheckCodegen , MultiStepCheckResource } from './multi-step-check-codegen'
12
- import { valueForRef } from './ref-codegen'
13
12
import { RetryStrategyResource , valueForRetryStrategy } from './retry-strategy-codegen'
14
13
import { TcpCheckCodegen , TcpCheckResource } from './tcp-check-codegen'
15
14
16
15
export interface CheckResource {
16
+ id : string
17
17
checkType : string
18
18
name : string
19
19
activated ?: boolean
@@ -22,7 +22,6 @@ export interface CheckResource {
22
22
shouldFail ?: boolean
23
23
runtimeId ?: string
24
24
locations ?: string [ ]
25
- // TODO: privateLocations
26
25
tags ?: string [ ]
27
26
frequency ?: FrequencyResource
28
27
environmentVariables ?: EnvironmentVariable [ ]
@@ -37,6 +36,7 @@ export function buildCheckProps (
37
36
program : Program ,
38
37
builder : ObjectValueBuilder ,
39
38
resource : CheckResource ,
39
+ context : Context ,
40
40
) : void {
41
41
builder . string ( 'name' , resource . name )
42
42
@@ -65,9 +65,21 @@ export function buildCheckProps (
65
65
} )
66
66
}
67
67
68
- // if (resource.privateLocations) {
69
- // // TODO: privateLocations - live variables
70
- // }
68
+ const privateLocationIds = ( ( ) => {
69
+ try {
70
+ return context . lookupCheckPrivateLocations ( resource . id )
71
+ } catch ( err ) {
72
+ }
73
+ } ) ( )
74
+
75
+ if ( privateLocationIds !== undefined ) {
76
+ builder . array ( 'privateLocations' , builder => {
77
+ for ( const privateLocationId of privateLocationIds ) {
78
+ const privateLocationVariable = context . lookupPrivateLocation ( privateLocationId )
79
+ builder . value ( privateLocationVariable )
80
+ }
81
+ } )
82
+ }
71
83
72
84
if ( resource . tags ) {
73
85
const tags = resource . tags
@@ -91,17 +103,30 @@ export function buildCheckProps (
91
103
} )
92
104
}
93
105
94
- // if (resource.groupId) {
95
- // builder.value('groupId', valueForRef(program, resource.groupId))
96
- // }
106
+ if ( resource . groupId ) {
107
+ try {
108
+ const groupVariable = context . lookupCheckGroup ( resource . groupId )
109
+ builder . value ( 'group' , groupVariable )
110
+ } catch ( err ) {
111
+ throw new Error ( 'Check belongs to a group that is not being imported.' )
112
+ }
113
+ }
97
114
98
- // if (resource.group) {
99
- // // TODO: group - live variables
100
- // }
115
+ const alertChannelIds = ( ( ) => {
116
+ try {
117
+ return context . lookupCheckAlertChannels ( resource . id )
118
+ } catch ( err ) {
119
+ }
120
+ } ) ( )
101
121
102
- // if (resource.alertChannels) {
103
- // // TODO: alertChannels - live variables
104
- // }
122
+ if ( alertChannelIds !== undefined ) {
123
+ builder . array ( 'alertChannels' , builder => {
124
+ for ( const alertChannelId of alertChannelIds ) {
125
+ const alertChannelVariable = context . lookupAlertChannel ( alertChannelId )
126
+ builder . value ( alertChannelVariable )
127
+ }
128
+ } )
129
+ }
105
130
106
131
if ( resource . alertSettings ) {
107
132
builder . value ( 'alertEscalationPolicy' , valueForAlertEscalation ( program , resource . alertSettings ) )
@@ -136,20 +161,20 @@ export class CheckCodegen extends Codegen<CheckResource> {
136
161
this . tcpCheckCodegen = new TcpCheckCodegen ( program )
137
162
}
138
163
139
- gencode ( logicalId : string , resource : CheckResource ) : void {
164
+ gencode ( logicalId : string , resource : CheckResource , context : Context ) : void {
140
165
const { checkType } = resource
141
166
142
167
switch ( checkType ) {
143
168
case 'BROWSER' :
144
- return this . browserCheckCodegen . gencode ( logicalId , resource as BrowserCheckResource )
169
+ return this . browserCheckCodegen . gencode ( logicalId , resource as BrowserCheckResource , context )
145
170
case 'API' :
146
- return this . apiCheckCodegen . gencode ( logicalId , resource as ApiCheckResource )
171
+ return this . apiCheckCodegen . gencode ( logicalId , resource as ApiCheckResource , context )
147
172
case 'TCP' :
148
- return this . tcpCheckCodegen . gencode ( logicalId , resource as TcpCheckResource )
173
+ return this . tcpCheckCodegen . gencode ( logicalId , resource as TcpCheckResource , context )
149
174
case 'MULTI_STEP' :
150
- return this . multiStepCheckCodegen . gencode ( logicalId , resource as MultiStepCheckResource )
175
+ return this . multiStepCheckCodegen . gencode ( logicalId , resource as MultiStepCheckResource , context )
151
176
case 'HEARTBEAT' :
152
- return this . heartbeatCheckCodegen . gencode ( logicalId , resource as HeartbeatCheckResource )
177
+ return this . heartbeatCheckCodegen . gencode ( logicalId , resource as HeartbeatCheckResource , context )
153
178
default :
154
179
throw new Error ( `Unable to generate code for unsupported check type '${ checkType } '.` )
155
180
}
0 commit comments