1- import { DeployOptions } from '@aws-cdk/cloud-assembly-schema ' ;
1+ import { format } from 'util ' ;
22import * as cfnDiff from '@aws-cdk/cloudformation-diff' ;
33import { ResourceDifference } from '@aws-cdk/cloudformation-diff' ;
44import * as cxapi from '@aws-cdk/cx-api' ;
55import * as chalk from 'chalk' ;
66import * as fs from 'fs-extra' ;
77import * 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+ * Name of the toolkit stack to use/deploy
24+ *
25+ * @default CDKToolkit
26+ */
27+ readonly toolkitStackName : string ;
28+
29+ /**
30+ * Role to pass to CloudFormation for deployment
31+ *
32+ * @default - Default stack role
33+ */
34+ readonly roleArn ?: string ;
35+
36+ /**
37+ * Deployment method
38+ */
39+ readonly deploymentMethod ?: DeploymentMethod ;
40+
41+ /**
42+ * Stack tags (pass through to CloudFormation)
43+ */
44+ readonly tags ?: Tag [ ] ;
45+
46+ /**
47+ * Use previous values for unspecified parameters
48+ *
49+ * If not set, all parameters must be specified for every deployment.
50+ *
51+ * @default true
52+ */
53+ readonly usePreviousParameters ?: boolean ;
54+
55+ /**
56+ * Display mode for stack deployment progress.
57+ *
58+ * @default - StackActivityProgress.Bar - stack events will be displayed for
59+ * the resource currently being deployed.
60+ */
61+ readonly progress ?: StackActivityProgress ;
62+
63+ /**
64+ * Rollback failed deployments
65+ *
66+ * @default true
67+ */
68+ readonly rollback ?: boolean ;
1869}
1970
2071/**
@@ -61,9 +112,20 @@ export type ResourceMap = { [logicalResource: string]: ResourceIdentifierPropert
61112export class ResourceImporter {
62113 private _currentTemplate : any ;
63114
115+ private readonly stack : cxapi . CloudFormationStackArtifact ;
116+ private readonly cfn : Deployments ;
117+ private readonly ioHost : IIoHost ;
118+ private readonly action : ToolkitAction ;
119+
64120 constructor (
65- private readonly stack : cxapi . CloudFormationStackArtifact ,
66- private readonly cfn : Deployments ) { }
121+ stack : cxapi . CloudFormationStackArtifact ,
122+ props : ResourceImporterProps ,
123+ ) {
124+ this . stack = stack ;
125+ this . cfn = props . deployments ;
126+ this . ioHost = props . ioHost ;
127+ this . action = props . action ;
128+ }
67129
68130 /**
69131 * Ask the user for resources to import
@@ -96,19 +158,19 @@ export class ResourceImporter {
96158 const descr = this . describeResource ( resource . logicalId ) ;
97159 const idProps = contents [ resource . logicalId ] ;
98160 if ( idProps ) {
99- info ( '%s: importing using %s' , chalk . blue ( descr ) , chalk . blue ( fmtdict ( idProps ) ) ) ;
161+ await this . ioHost . notify ( info ( this . action , format ( '%s: importing using %s' , chalk . blue ( descr ) , chalk . blue ( fmtdict ( idProps ) ) ) ) ) ;
100162
101163 ret . importResources . push ( resource ) ;
102164 ret . resourceMap [ resource . logicalId ] = idProps ;
103165 delete contents [ resource . logicalId ] ;
104166 } else {
105- info ( '%s: skipping' , chalk . blue ( descr ) ) ;
167+ await this . ioHost . notify ( info ( this . action , format ( '%s: skipping' , chalk . blue ( descr ) ) ) ) ;
106168 }
107169 }
108170
109171 const unknown = Object . keys ( contents ) ;
110172 if ( unknown . length > 0 ) {
111- warning ( `Unrecognized resource identifiers in mapping file: ${ unknown . join ( ', ' ) } ` ) ;
173+ await this . ioHost . notify ( warn ( this . action , `Unrecognized resource identifiers in mapping file: ${ unknown . join ( ', ' ) } ` ) ) ;
112174 }
113175
114176 return ret ;
@@ -158,9 +220,9 @@ export class ResourceImporter {
158220 ? ' ✅ %s (no changes)'
159221 : ' ✅ %s' ;
160222
161- success ( '\n' + message , this . stack . displayName ) ;
223+ await this . ioHost . notify ( info ( this . action , '\n' + chalk . green ( format ( message , this . stack . displayName ) ) ) ) ;
162224 } catch ( e ) {
163- error ( '\n ❌ %s failed: %s' , chalk . bold ( this . stack . displayName ) , e ) ;
225+ await this . ioHost . notify ( error ( this . action , format ( '\n ❌ %s failed: %s' , chalk . bold ( this . stack . displayName ) , e ) , 'CDK_TOOLKIT_E3900' ) ) ;
164226 throw e ;
165227 }
166228 }
@@ -189,7 +251,7 @@ export class ResourceImporter {
189251 const offendingResources = nonAdditions . map ( ( [ logId , _ ] ) => this . describeResource ( logId ) ) ;
190252
191253 if ( allowNonAdditions ) {
192- warning ( `Ignoring updated/deleted resources (--force): ${ offendingResources . join ( ', ' ) } ` ) ;
254+ await this . ioHost . notify ( warn ( this . action , `Ignoring updated/deleted resources (--force): ${ offendingResources . join ( ', ' ) } ` ) ) ;
193255 } else {
194256 throw new ToolkitError ( 'No resource updates or deletes are allowed on import operation. Make sure to resolve pending changes ' +
195257 `to existing resources, before attempting an import. Updated/deleted resources: ${ offendingResources . join ( ', ' ) } (--force to override)` ) ;
@@ -277,7 +339,7 @@ export class ResourceImporter {
277339 // Skip resources that do not support importing
278340 const resourceType = chg . resourceDiff . newResourceType ;
279341 if ( resourceType === undefined || ! ( resourceType in resourceIdentifiers ) ) {
280- warning ( `${ resourceName } : unsupported resource type ${ resourceType } , skipping import.` ) ;
342+ await this . ioHost . notify ( warn ( this . action , `${ resourceName } : unsupported resource type ${ resourceType } , skipping import.` ) ) ;
281343 return undefined ;
282344 }
283345
@@ -303,7 +365,7 @@ export class ResourceImporter {
303365
304366 // If we got here and the user rejected any available identifiers, then apparently they don't want the resource at all
305367 if ( satisfiedPropSets . length > 0 ) {
306- info ( chalk . grey ( `Skipping import of ${ resourceName } ` ) ) ;
368+ await this . ioHost . notify ( info ( this . action , chalk . grey ( `Skipping import of ${ resourceName } ` ) ) ) ;
307369 return undefined ;
308370 }
309371
@@ -321,7 +383,7 @@ export class ResourceImporter {
321383
322384 // Do the input loop here
323385 if ( preamble ) {
324- info ( preamble ) ;
386+ await this . ioHost . notify ( info ( this . action , preamble ) ) ;
325387 }
326388 for ( const idProps of idPropSets ) {
327389 const input : Record < string , string > = { } ;
@@ -356,7 +418,7 @@ export class ResourceImporter {
356418 }
357419 }
358420
359- info ( chalk . grey ( `Skipping import of ${ resourceName } ` ) ) ;
421+ await this . ioHost . notify ( info ( this . action , chalk . grey ( `Skipping import of ${ resourceName } ` ) ) ) ;
360422 return undefined ;
361423 }
362424
0 commit comments