1
- import { DeployOptions } from '@aws-cdk/cloud-assembly-schema ' ;
1
+ import { format } from 'util ' ;
2
2
import * as cfnDiff from '@aws-cdk/cloudformation-diff' ;
3
3
import { ResourceDifference } from '@aws-cdk/cloudformation-diff' ;
4
4
import * as cxapi from '@aws-cdk/cx-api' ;
5
5
import * as chalk from 'chalk' ;
6
6
import * as fs from 'fs-extra' ;
7
7
import * as promptly from 'promptly' ;
8
- import { assertIsSuccessfulDeployStackResult , Deployments , DeploymentMethod , ResourceIdentifierProperties , ResourcesToImport } from './api/deployments' ;
9
- import { Tag } from './api/tags' ;
10
- import { StackActivityProgress } from './api/util/cloudformation/stack-activity-monitor' ;
11
- import { error , info , success , warning } from './logging' ;
12
- import { ToolkitError } from './toolkit/error' ;
13
-
14
- export interface ImportDeploymentOptions extends DeployOptions {
15
- deploymentMethod ?: DeploymentMethod ;
16
- progress ?: StackActivityProgress ;
17
- tags ?: Tag [ ] ;
8
+ import { error , info , warn } from '../../cli/messages' ;
9
+ import { IIoHost , ToolkitAction } from '../../toolkit/cli-io-host' ;
10
+ import { ToolkitError } from '../../toolkit/error' ;
11
+ import { assertIsSuccessfulDeployStackResult , Deployments , DeploymentMethod , ResourceIdentifierProperties , ResourcesToImport } from '../deployments' ;
12
+ import { Tag } from '../tags' ;
13
+ import { StackActivityProgress } from '../util/cloudformation/stack-activity-monitor' ;
14
+
15
+ export interface ResourceImporterProps {
16
+ deployments : Deployments ;
17
+ ioHost : IIoHost ;
18
+ action : ToolkitAction ;
19
+ }
20
+
21
+ export interface ImportDeploymentOptions {
22
+ /**
23
+ * Role to pass to CloudFormation for deployment
24
+ *
25
+ * @default - Default stack role
26
+ */
27
+ readonly roleArn ?: string ;
28
+
29
+ /**
30
+ * Deployment method
31
+ *
32
+ * @default - Change set with default options
33
+ */
34
+ readonly deploymentMethod ?: DeploymentMethod ;
35
+
36
+ /**
37
+ * Stack tags (pass through to CloudFormation)
38
+ *
39
+ * @default - No tags
40
+ */
41
+ readonly tags ?: Tag [ ] ;
42
+
43
+ /**
44
+ * Use previous values for unspecified parameters
45
+ *
46
+ * If not set, all parameters must be specified for every deployment.
47
+ *
48
+ * @default true
49
+ */
50
+ readonly usePreviousParameters ?: boolean ;
51
+
52
+ /**
53
+ * Display mode for stack deployment progress.
54
+ *
55
+ * @default - StackActivityProgress.Bar - stack events will be displayed for
56
+ * the resource currently being deployed.
57
+ */
58
+ readonly progress ?: StackActivityProgress ;
59
+
60
+ /**
61
+ * Rollback failed deployments
62
+ *
63
+ * @default true
64
+ */
65
+ readonly rollback ?: boolean ;
18
66
}
19
67
20
68
/**
@@ -61,9 +109,20 @@ export type ResourceMap = { [logicalResource: string]: ResourceIdentifierPropert
61
109
export class ResourceImporter {
62
110
private _currentTemplate : any ;
63
111
112
+ private readonly stack : cxapi . CloudFormationStackArtifact ;
113
+ private readonly cfn : Deployments ;
114
+ private readonly ioHost : IIoHost ;
115
+ private readonly action : ToolkitAction ;
116
+
64
117
constructor (
65
- private readonly stack : cxapi . CloudFormationStackArtifact ,
66
- private readonly cfn : Deployments ) { }
118
+ stack : cxapi . CloudFormationStackArtifact ,
119
+ props : ResourceImporterProps ,
120
+ ) {
121
+ this . stack = stack ;
122
+ this . cfn = props . deployments ;
123
+ this . ioHost = props . ioHost ;
124
+ this . action = props . action ;
125
+ }
67
126
68
127
/**
69
128
* Ask the user for resources to import
@@ -96,19 +155,19 @@ export class ResourceImporter {
96
155
const descr = this . describeResource ( resource . logicalId ) ;
97
156
const idProps = contents [ resource . logicalId ] ;
98
157
if ( idProps ) {
99
- info ( '%s: importing using %s' , chalk . blue ( descr ) , chalk . blue ( fmtdict ( idProps ) ) ) ;
158
+ await this . ioHost . notify ( info ( this . action , format ( '%s: importing using %s' , chalk . blue ( descr ) , chalk . blue ( fmtdict ( idProps ) ) ) ) ) ;
100
159
101
160
ret . importResources . push ( resource ) ;
102
161
ret . resourceMap [ resource . logicalId ] = idProps ;
103
162
delete contents [ resource . logicalId ] ;
104
163
} else {
105
- info ( '%s: skipping' , chalk . blue ( descr ) ) ;
164
+ await this . ioHost . notify ( info ( this . action , format ( '%s: skipping' , chalk . blue ( descr ) ) ) ) ;
106
165
}
107
166
}
108
167
109
168
const unknown = Object . keys ( contents ) ;
110
169
if ( unknown . length > 0 ) {
111
- warning ( `Unrecognized resource identifiers in mapping file: ${ unknown . join ( ', ' ) } ` ) ;
170
+ await this . ioHost . notify ( warn ( this . action , `Unrecognized resource identifiers in mapping file: ${ unknown . join ( ', ' ) } ` ) ) ;
112
171
}
113
172
114
173
return ret ;
@@ -121,7 +180,7 @@ export class ResourceImporter {
121
180
* @param importMap Mapping from CDK construct tree path to physical resource import identifiers
122
181
* @param options Options to pass to CloudFormation deploy operation
123
182
*/
124
- public async importResourcesFromMap ( importMap : ImportMap , options : ImportDeploymentOptions ) {
183
+ public async importResourcesFromMap ( importMap : ImportMap , options : ImportDeploymentOptions = { } ) {
125
184
const resourcesToImport : ResourcesToImport = await this . makeResourcesToImport ( importMap ) ;
126
185
const updatedTemplate = await this . currentTemplateWithAdditions ( importMap . importResources ) ;
127
186
@@ -136,7 +195,7 @@ export class ResourceImporter {
136
195
* @param resourcesToImport The mapping created by cdk migrate
137
196
* @param options Options to pass to CloudFormation deploy operation
138
197
*/
139
- public async importResourcesFromMigrate ( resourcesToImport : ResourcesToImport , options : ImportDeploymentOptions ) {
198
+ public async importResourcesFromMigrate ( resourcesToImport : ResourcesToImport , options : ImportDeploymentOptions = { } ) {
140
199
const updatedTemplate = this . removeNonImportResources ( ) ;
141
200
142
201
await this . importResources ( updatedTemplate , resourcesToImport , options ) ;
@@ -158,9 +217,9 @@ export class ResourceImporter {
158
217
? ' ✅ %s (no changes)'
159
218
: ' ✅ %s' ;
160
219
161
- success ( '\n' + message , this . stack . displayName ) ;
220
+ await this . ioHost . notify ( info ( this . action , '\n' + chalk . green ( format ( message , this . stack . displayName ) ) ) ) ;
162
221
} catch ( e ) {
163
- error ( '\n ❌ %s failed: %s' , chalk . bold ( this . stack . displayName ) , e ) ;
222
+ await this . ioHost . notify ( error ( this . action , format ( '\n ❌ %s failed: %s' , chalk . bold ( this . stack . displayName ) , e ) , 'CDK_TOOLKIT_E3900' ) ) ;
164
223
throw e ;
165
224
}
166
225
}
@@ -189,7 +248,7 @@ export class ResourceImporter {
189
248
const offendingResources = nonAdditions . map ( ( [ logId , _ ] ) => this . describeResource ( logId ) ) ;
190
249
191
250
if ( allowNonAdditions ) {
192
- warning ( `Ignoring updated/deleted resources (--force): ${ offendingResources . join ( ', ' ) } ` ) ;
251
+ await this . ioHost . notify ( warn ( this . action , `Ignoring updated/deleted resources (--force): ${ offendingResources . join ( ', ' ) } ` ) ) ;
193
252
} else {
194
253
throw new ToolkitError ( 'No resource updates or deletes are allowed on import operation. Make sure to resolve pending changes ' +
195
254
`to existing resources, before attempting an import. Updated/deleted resources: ${ offendingResources . join ( ', ' ) } (--force to override)` ) ;
@@ -277,7 +336,7 @@ export class ResourceImporter {
277
336
// Skip resources that do not support importing
278
337
const resourceType = chg . resourceDiff . newResourceType ;
279
338
if ( resourceType === undefined || ! ( resourceType in resourceIdentifiers ) ) {
280
- warning ( `${ resourceName } : unsupported resource type ${ resourceType } , skipping import.` ) ;
339
+ await this . ioHost . notify ( warn ( this . action , `${ resourceName } : unsupported resource type ${ resourceType } , skipping import.` ) ) ;
281
340
return undefined ;
282
341
}
283
342
@@ -303,7 +362,7 @@ export class ResourceImporter {
303
362
304
363
// If we got here and the user rejected any available identifiers, then apparently they don't want the resource at all
305
364
if ( satisfiedPropSets . length > 0 ) {
306
- info ( chalk . grey ( `Skipping import of ${ resourceName } ` ) ) ;
365
+ await this . ioHost . notify ( info ( this . action , chalk . grey ( `Skipping import of ${ resourceName } ` ) ) ) ;
307
366
return undefined ;
308
367
}
309
368
@@ -321,7 +380,7 @@ export class ResourceImporter {
321
380
322
381
// Do the input loop here
323
382
if ( preamble ) {
324
- info ( preamble ) ;
383
+ await this . ioHost . notify ( info ( this . action , preamble ) ) ;
325
384
}
326
385
for ( const idProps of idPropSets ) {
327
386
const input : Record < string , string > = { } ;
@@ -356,7 +415,7 @@ export class ResourceImporter {
356
415
}
357
416
}
358
417
359
- info ( chalk . grey ( `Skipping import of ${ resourceName } ` ) ) ;
418
+ await this . ioHost . notify ( info ( this . action , chalk . grey ( `Skipping import of ${ resourceName } ` ) ) ) ;
360
419
return undefined ;
361
420
}
362
421
0 commit comments